diff --git a/clap_complete_nushell/src/lib.rs b/clap_complete_nushell/src/lib.rs index d0c08540460..9939bf417a9 100644 --- a/clap_complete_nushell/src/lib.rs +++ b/clap_complete_nushell/src/lib.rs @@ -219,7 +219,16 @@ fn generate_completion(completions: &mut String, cmd: &Command, is_subcommand: b completions.push_str(format!(" export extern {name} [\n").as_str()); } - for arg in cmd.get_arguments() { + let flags: Vec<_> = cmd.get_arguments().filter(|a| !a.is_positional()).collect(); + let mut positionals: Vec<_> = cmd.get_positionals().collect(); + + positionals.sort_by_key(|arg| arg.get_index()); + + for arg in flags { + append_argument(arg, name, completions); + } + + for arg in positionals { append_argument(arg, name, completions); } diff --git a/clap_complete_nushell/tests/common.rs b/clap_complete_nushell/tests/common.rs index 0c38f0d0c4a..4d5bd02f215 100644 --- a/clap_complete_nushell/tests/common.rs +++ b/clap_complete_nushell/tests/common.rs @@ -168,6 +168,29 @@ pub(crate) fn sub_subcommands_command(name: &'static str) -> Command { ) } +pub(crate) fn positional_index_command(name: &'static str) -> Command { + Command::new(name) + .version("3.0") + .about("Tests positional argument index ordering") + .arg( + Arg::new("flag") + .short('f') + .long("flag") + .action(ArgAction::SetTrue) + .help("some flag"), + ) + .arg(Arg::new("third").index(3).help("third positional")) + .arg(Arg::new("first").index(1).help("first positional")) + .arg( + Arg::new("option") + .short('o') + .long("option") + .action(ArgAction::Set) + .help("some option"), + ) + .arg(Arg::new("second").index(2).help("second positional")) +} + pub(crate) fn value_hint_command(name: &'static str) -> Command { Command::new(name) .arg( diff --git a/clap_complete_nushell/tests/nushell.rs b/clap_complete_nushell/tests/nushell.rs index f3eacd85c0e..a05d2d482d6 100644 --- a/clap_complete_nushell/tests/nushell.rs +++ b/clap_complete_nushell/tests/nushell.rs @@ -83,3 +83,15 @@ fn value_hint() { name, ); } + +#[test] +fn positional_index() { + let name = "my-app"; + let cmd = common::positional_index_command(name); + common::assert_matches( + snapbox::file!["snapshots/positional_index.nu"], + clap_complete_nushell::Nushell, + cmd, + name, + ); +} diff --git a/clap_complete_nushell/tests/snapshots/aliases.nu b/clap_complete_nushell/tests/snapshots/aliases.nu index 8263e60315e..67a70d6ff85 100644 --- a/clap_complete_nushell/tests/snapshots/aliases.nu +++ b/clap_complete_nushell/tests/snapshots/aliases.nu @@ -8,9 +8,9 @@ module completions { --option(-o): string # cmd option --opt: string # cmd option -O: string # cmd option - positional?: string --help(-h) # Print help --version(-V) # Print version + positional?: string ] } diff --git a/clap_complete_nushell/tests/snapshots/feature_sample.nu b/clap_complete_nushell/tests/snapshots/feature_sample.nu index eb081b9b4de..912ac28aa78 100644 --- a/clap_complete_nushell/tests/snapshots/feature_sample.nu +++ b/clap_complete_nushell/tests/snapshots/feature_sample.nu @@ -6,13 +6,13 @@ module completions { # Tests completions export extern my-app [ - file?: path # some input file --config(-c) # some config file with another line --conf # some config file with another line -C # some config file with another line - choice?: string@"nu-complete my-app choice" --help(-h) # Print help --version(-V) # Print version + file?: path # some input file + choice?: string@"nu-complete my-app choice" ] # tests things diff --git a/clap_complete_nushell/tests/snapshots/home/static/test/nu/.config/nushell/completions/test.nu b/clap_complete_nushell/tests/snapshots/home/static/test/nu/.config/nushell/completions/test.nu index ac1e050f667..98e5762e4d8 100644 --- a/clap_complete_nushell/tests/snapshots/home/static/test/nu/.config/nushell/completions/test.nu +++ b/clap_complete_nushell/tests/snapshots/home/static/test/nu/.config/nushell/completions/test.nu @@ -111,10 +111,10 @@ module completions { --delim: string --tuple: string --require-eq: string - ...term: string --global # everywhere --help(-h) # Print help --version(-V) # Print version + ...term: string ] export extern "test pacman" [ @@ -150,11 +150,11 @@ module completions { ] export extern "test last" [ - first?: string - free?: string --global # everywhere --help(-h) # Print help --version(-V) # Print version + first?: string + free?: string ] export extern "test alias" [ @@ -164,10 +164,10 @@ module completions { --option(-o): string # cmd option --opt: string # cmd option -O: string # cmd option - positional?: string --global # everywhere --help(-h) # Print help --version(-V) # Print version + positional?: string ] def "nu-complete test hint choice" [] { @@ -184,7 +184,6 @@ module completions { --exe(-e): path --cmd-name: string --cmd(-c): string - command_with_args?: string --user(-u): string --host(-H): string --url: string @@ -192,6 +191,7 @@ module completions { --global # everywhere --help(-h) # Print help --version(-V) # Print version + command_with_args?: string ] # Print this message or the help of the given subcommand(s) diff --git a/clap_complete_nushell/tests/snapshots/positional_index.nu b/clap_complete_nushell/tests/snapshots/positional_index.nu new file mode 100644 index 00000000000..126a92fae99 --- /dev/null +++ b/clap_complete_nushell/tests/snapshots/positional_index.nu @@ -0,0 +1,16 @@ +module completions { + + # Tests positional argument index ordering + export extern my-app [ + --flag(-f) # some flag + --option(-o): string # some option + --help(-h) # Print help + --version(-V) # Print version + first?: string # first positional + second?: string # second positional + third?: string # third positional + ] + +} + +export use completions * diff --git a/clap_complete_nushell/tests/snapshots/special_commands.nu b/clap_complete_nushell/tests/snapshots/special_commands.nu index 0551531b3f7..6b6541d0786 100644 --- a/clap_complete_nushell/tests/snapshots/special_commands.nu +++ b/clap_complete_nushell/tests/snapshots/special_commands.nu @@ -6,13 +6,13 @@ module completions { # Tests completions export extern my-app [ - file?: path # some input file --config(-c) # some config file with another line --conf # some config file with another line -C # some config file with another line - choice?: string@"nu-complete my-app choice" --help(-h) # Print help --version(-V) # Print version + file?: path # some input file + choice?: string@"nu-complete my-app choice" ] # tests things @@ -25,9 +25,9 @@ module completions { # tests other things export extern "my-app some_cmd" [ --config: string # the other case to test - ...path: string --help(-h) # Print help --version(-V) # Print version + ...path: string ] export extern "my-app some-cmd-with-hyphens" [ diff --git a/clap_complete_nushell/tests/snapshots/sub_subcommands.nu b/clap_complete_nushell/tests/snapshots/sub_subcommands.nu index 2f56ca6d46c..a17626202c9 100644 --- a/clap_complete_nushell/tests/snapshots/sub_subcommands.nu +++ b/clap_complete_nushell/tests/snapshots/sub_subcommands.nu @@ -6,13 +6,13 @@ module completions { # Tests completions export extern my-app [ - file?: path # some input file --config(-c) # some config file with another line --conf # some config file with another line -C # some config file with another line - choice?: string@"nu-complete my-app choice" --help(-h) # Print help --version(-V) # Print version + file?: path # some input file + choice?: string@"nu-complete my-app choice" ] # tests things diff --git a/clap_complete_nushell/tests/snapshots/value_hint.nu b/clap_complete_nushell/tests/snapshots/value_hint.nu index f27849a5463..f3d2a470596 100644 --- a/clap_complete_nushell/tests/snapshots/value_hint.nu +++ b/clap_complete_nushell/tests/snapshots/value_hint.nu @@ -14,12 +14,12 @@ module completions { --exe(-e): path --cmd-name: string --cmd(-c): string - command_with_args?: string --user(-u): string --host(-H): string --url: string --email: string --help(-h) # Print help + command_with_args?: string ] }