mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 09:25:38 +02:00
Rename main
to script name when running scripts (#9948)
# Description This PR does three related changes: * Keeps the originally declared name in help outputs. * Updates the name of the commands called `main` in the user script to the name of the script. * Fixes the source of signature information in multiple places. This allows scripts to have more complete help output. Combined, the above allow the user to see the script name in the help output of scripts, like so:  NOTE: You still declare and call the definition `main`, so from inside the script `main` is still the correct name. But multiple folks agreed that seeing `main` in the script help was confusing, so this PR changes that. # User-Facing Changes One potential minor breaking change is that module renames will be shown as their originally defined name rather than the renamed name. I believe this to be a better default. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
This commit is contained in:
@ -783,16 +783,22 @@ impl EngineState {
|
||||
decls.into_iter()
|
||||
}
|
||||
|
||||
#[allow(clippy::borrowed_box)]
|
||||
pub fn get_signature(&self, decl: &Box<dyn Command>) -> Signature {
|
||||
if let Some(block_id) = decl.get_block_id() {
|
||||
*self.blocks[block_id].signature.clone()
|
||||
} else {
|
||||
decl.signature()
|
||||
}
|
||||
}
|
||||
|
||||
/// Get signatures of all commands within scope.
|
||||
pub fn get_signatures(&self, include_hidden: bool) -> Vec<Signature> {
|
||||
self.get_decls_sorted(include_hidden)
|
||||
.map(|(name_bytes, id)| {
|
||||
.map(|(_, id)| {
|
||||
let decl = self.get_decl(id);
|
||||
// the reason to create the name this way is because the command could be renamed
|
||||
// during module imports but the signature still contains the old name
|
||||
let name = String::from_utf8_lossy(&name_bytes).to_string();
|
||||
|
||||
(*decl).signature().update_from_command(name, decl.borrow())
|
||||
self.get_signature(decl).update_from_command(decl.borrow())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@ -807,13 +813,10 @@ impl EngineState {
|
||||
include_hidden: bool,
|
||||
) -> Vec<(Signature, Vec<Example>, bool, bool, bool)> {
|
||||
self.get_decls_sorted(include_hidden)
|
||||
.map(|(name_bytes, id)| {
|
||||
.map(|(_, id)| {
|
||||
let decl = self.get_decl(id);
|
||||
// the reason to create the name this way is because the command could be renamed
|
||||
// during module imports but the signature still contains the old name
|
||||
let name = String::from_utf8_lossy(&name_bytes).to_string();
|
||||
|
||||
let signature = (*decl).signature().update_from_command(name, decl.borrow());
|
||||
let signature = self.get_signature(decl).update_from_command(decl.borrow());
|
||||
|
||||
(
|
||||
signature,
|
||||
|
@ -48,8 +48,8 @@ impl PluginSignature {
|
||||
}
|
||||
|
||||
/// Update signature's fields from a Command trait implementation
|
||||
pub fn update_from_command(mut self, name: String, command: &dyn Command) -> PluginSignature {
|
||||
self.sig = self.sig.update_from_command(name, command);
|
||||
pub fn update_from_command(mut self, command: &dyn Command) -> PluginSignature {
|
||||
self.sig = self.sig.update_from_command(command);
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -237,8 +237,7 @@ impl Signature {
|
||||
}
|
||||
|
||||
/// Update signature's fields from a Command trait implementation
|
||||
pub fn update_from_command(mut self, name: String, command: &dyn Command) -> Signature {
|
||||
self.name = name;
|
||||
pub fn update_from_command(mut self, command: &dyn Command) -> Signature {
|
||||
self.search_terms = command
|
||||
.search_terms()
|
||||
.into_iter()
|
||||
|
Reference in New Issue
Block a user