Skip to content

Commit 6a64a8b

Browse files
authored
Merge pull request #6167 from nitsky/feat/nushell-completion-arguments-index
fix(clap_complete_nushell): order positional arguments by index
2 parents 33d24d8 + 83cbb5b commit 6a64a8b

File tree

10 files changed

+75
-15
lines changed

10 files changed

+75
-15
lines changed

clap_complete_nushell/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,16 @@ fn generate_completion(completions: &mut String, cmd: &Command, is_subcommand: b
219219
completions.push_str(format!(" export extern {name} [\n").as_str());
220220
}
221221

222-
for arg in cmd.get_arguments() {
222+
let flags: Vec<_> = cmd.get_arguments().filter(|a| !a.is_positional()).collect();
223+
let mut positionals: Vec<_> = cmd.get_positionals().collect();
224+
225+
positionals.sort_by_key(|arg| arg.get_index());
226+
227+
for arg in flags {
228+
append_argument(arg, name, completions);
229+
}
230+
231+
for arg in positionals {
223232
append_argument(arg, name, completions);
224233
}
225234

clap_complete_nushell/tests/common.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ pub(crate) fn sub_subcommands_command(name: &'static str) -> Command {
168168
)
169169
}
170170

171+
pub(crate) fn positional_index_command(name: &'static str) -> Command {
172+
Command::new(name)
173+
.version("3.0")
174+
.about("Tests positional argument index ordering")
175+
.arg(
176+
Arg::new("flag")
177+
.short('f')
178+
.long("flag")
179+
.action(ArgAction::SetTrue)
180+
.help("some flag"),
181+
)
182+
.arg(Arg::new("third").index(3).help("third positional"))
183+
.arg(Arg::new("first").index(1).help("first positional"))
184+
.arg(
185+
Arg::new("option")
186+
.short('o')
187+
.long("option")
188+
.action(ArgAction::Set)
189+
.help("some option"),
190+
)
191+
.arg(Arg::new("second").index(2).help("second positional"))
192+
}
193+
171194
pub(crate) fn value_hint_command(name: &'static str) -> Command {
172195
Command::new(name)
173196
.arg(

clap_complete_nushell/tests/nushell.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ fn value_hint() {
8383
name,
8484
);
8585
}
86+
87+
#[test]
88+
fn positional_index() {
89+
let name = "my-app";
90+
let cmd = common::positional_index_command(name);
91+
common::assert_matches(
92+
snapbox::file!["snapshots/positional_index.nu"],
93+
clap_complete_nushell::Nushell,
94+
cmd,
95+
name,
96+
);
97+
}

clap_complete_nushell/tests/snapshots/aliases.nu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module completions {
88
--option(-o): string # cmd option
99
--opt: string # cmd option
1010
-O: string # cmd option
11-
positional?: string
1211
--help(-h) # Print help
1312
--version(-V) # Print version
13+
positional?: string
1414
]
1515

1616
}

clap_complete_nushell/tests/snapshots/feature_sample.nu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ module completions {
66

77
# Tests completions
88
export extern my-app [
9-
file?: path # some input file
109
--config(-c) # some config file with another line
1110
--conf # some config file with another line
1211
-C # some config file with another line
13-
choice?: string@"nu-complete my-app choice"
1412
--help(-h) # Print help
1513
--version(-V) # Print version
14+
file?: path # some input file
15+
choice?: string@"nu-complete my-app choice"
1616
]
1717

1818
# tests things

clap_complete_nushell/tests/snapshots/home/static/test/nu/.config/nushell/completions/test.nu

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ module completions {
111111
--delim: string
112112
--tuple: string
113113
--require-eq: string
114-
...term: string
115114
--global # everywhere
116115
--help(-h) # Print help
117116
--version(-V) # Print version
117+
...term: string
118118
]
119119

120120
export extern "test pacman" [
@@ -150,11 +150,11 @@ module completions {
150150
]
151151

152152
export extern "test last" [
153-
first?: string
154-
free?: string
155153
--global # everywhere
156154
--help(-h) # Print help
157155
--version(-V) # Print version
156+
first?: string
157+
free?: string
158158
]
159159

160160
export extern "test alias" [
@@ -164,10 +164,10 @@ module completions {
164164
--option(-o): string # cmd option
165165
--opt: string # cmd option
166166
-O: string # cmd option
167-
positional?: string
168167
--global # everywhere
169168
--help(-h) # Print help
170169
--version(-V) # Print version
170+
positional?: string
171171
]
172172

173173
def "nu-complete test hint choice" [] {
@@ -184,14 +184,14 @@ module completions {
184184
--exe(-e): path
185185
--cmd-name: string
186186
--cmd(-c): string
187-
command_with_args?: string
188187
--user(-u): string
189188
--host(-H): string
190189
--url: string
191190
--email: string
192191
--global # everywhere
193192
--help(-h) # Print help
194193
--version(-V) # Print version
194+
command_with_args?: string
195195
]
196196

197197
# Print this message or the help of the given subcommand(s)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module completions {
2+
3+
# Tests positional argument index ordering
4+
export extern my-app [
5+
--flag(-f) # some flag
6+
--option(-o): string # some option
7+
--help(-h) # Print help
8+
--version(-V) # Print version
9+
first?: string # first positional
10+
second?: string # second positional
11+
third?: string # third positional
12+
]
13+
14+
}
15+
16+
export use completions *

clap_complete_nushell/tests/snapshots/special_commands.nu

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ module completions {
66

77
# Tests completions
88
export extern my-app [
9-
file?: path # some input file
109
--config(-c) # some config file with another line
1110
--conf # some config file with another line
1211
-C # some config file with another line
13-
choice?: string@"nu-complete my-app choice"
1412
--help(-h) # Print help
1513
--version(-V) # Print version
14+
file?: path # some input file
15+
choice?: string@"nu-complete my-app choice"
1616
]
1717

1818
# tests things
@@ -25,9 +25,9 @@ module completions {
2525
# tests other things
2626
export extern "my-app some_cmd" [
2727
--config: string # the other case to test
28-
...path: string
2928
--help(-h) # Print help
3029
--version(-V) # Print version
30+
...path: string
3131
]
3232

3333
export extern "my-app some-cmd-with-hyphens" [

clap_complete_nushell/tests/snapshots/sub_subcommands.nu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ module completions {
66

77
# Tests completions
88
export extern my-app [
9-
file?: path # some input file
109
--config(-c) # some config file with another line
1110
--conf # some config file with another line
1211
-C # some config file with another line
13-
choice?: string@"nu-complete my-app choice"
1412
--help(-h) # Print help
1513
--version(-V) # Print version
14+
file?: path # some input file
15+
choice?: string@"nu-complete my-app choice"
1616
]
1717

1818
# tests things

clap_complete_nushell/tests/snapshots/value_hint.nu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ module completions {
1414
--exe(-e): path
1515
--cmd-name: string
1616
--cmd(-c): string
17-
command_with_args?: string
1817
--user(-u): string
1918
--host(-H): string
2019
--url: string
2120
--email: string
2221
--help(-h) # Print help
22+
command_with_args?: string
2323
]
2424

2525
}

0 commit comments

Comments
 (0)