Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion clap_complete_nushell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
23 changes: 23 additions & 0 deletions clap_complete_nushell/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions clap_complete_nushell/tests/nushell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
2 changes: 1 addition & 1 deletion clap_complete_nushell/tests/snapshots/aliases.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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
]

}
Expand Down
4 changes: 2 additions & 2 deletions clap_complete_nushell/tests/snapshots/feature_sample.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" [
Expand Down Expand Up @@ -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" [
Expand All @@ -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" [] {
Expand All @@ -184,14 +184,14 @@ 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
--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)
Expand Down
16 changes: 16 additions & 0 deletions clap_complete_nushell/tests/snapshots/positional_index.nu
Original file line number Diff line number Diff line change
@@ -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 *
6 changes: 3 additions & 3 deletions clap_complete_nushell/tests/snapshots/special_commands.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" [
Expand Down
4 changes: 2 additions & 2 deletions clap_complete_nushell/tests/snapshots/sub_subcommands.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion clap_complete_nushell/tests/snapshots/value_hint.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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
]

}
Expand Down
Loading