Skip to content

Commit 2074d31

Browse files
committed
Complete repeatable positional instances after the first in bash.
Signed-off-by: Ross Goldberg <[email protected]>
1 parent a3957de commit 2074d31

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

Sources/ArgumentParser/Completions/BashCompletionsGenerator.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ extension CommandInfoV0 {
217217
result += """
218218
\(declareTopLevelArray)flags=(\(flagCompletions.joined(separator: " ")))
219219
\(declareTopLevelArray)options=(\(optionCompletions.joined(separator: " ")))
220-
\(offerFlagsOptionsFunctionName) \(positionalArguments.count)
220+
\(offerFlagsOptionsFunctionName) \
221+
\(positionalArguments.contains { $0.isRepeating } ? 9_223_372_036_854_775_807 : positionalArguments.count)
221222
222223
"""
223224
}
@@ -248,14 +249,23 @@ extension CommandInfoV0 {
248249
"""
249250
}
250251

252+
var encounteredRepeatingPositional = false
251253
let positionalCases =
252254
zip(1..., positionalArguments)
253255
.compactMap { position, arg in
256+
guard !encounteredRepeatingPositional else {
257+
return nil as String?
258+
}
259+
260+
if arg.isRepeating {
261+
encounteredRepeatingPositional = true
262+
}
263+
254264
let completion = valueCompletion(arg)
255265
return completion.isEmpty
256266
? nil
257267
: """
258-
\(position))
268+
\(encounteredRepeatingPositional ? "*" : position.description))
259269
\(completion.indentingEachLine(by: 8))\
260270
return
261271
;;
@@ -375,7 +385,6 @@ extension CommandInfoV0 {
375385
"""
376386

377387
case .custom, .customAsync:
378-
// Generate a call back into the command to retrieve a completions list
379388
return """
380389
\(addCompletionsFunctionName) -W\
381390
"$(\(customCompleteFunctionName) \(arg.commonCustomCompletionCall(command: self))\
@@ -385,7 +394,6 @@ extension CommandInfoV0 {
385394
"""
386395

387396
case .customDeprecated:
388-
// Generate a call back into the command to retrieve a completions list
389397
return """
390398
\(addCompletionsFunctionName) -W\
391399
"$(\(customCompleteFunctionName) \(arg.commonCustomCompletionCall(command: self)))"

Tests/ArgumentParserExampleTests/Snapshots/testMathBashCompletionScript().bash

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ _math() {
181181
_math_add() {
182182
flags=(--hex-output -x --version -h --help)
183183
options=()
184-
__math_offer_flags_options 1
184+
__math_offer_flags_options 9223372036854775807
185185
}
186186

187187
_math_multiply() {
188188
flags=(--hex-output -x --version -h --help)
189189
options=()
190-
__math_offer_flags_options 1
190+
__math_offer_flags_options 9223372036854775807
191191
}
192192

193193
_math_stats() {
@@ -214,7 +214,7 @@ _math_stats() {
214214
_math_stats_average() {
215215
flags=(--version -h --help)
216216
options=(--kind)
217-
__math_offer_flags_options 1
217+
__math_offer_flags_options 9223372036854775807
218218

219219
# Offer option value completions
220220
case "${prev}" in
@@ -228,13 +228,13 @@ _math_stats_average() {
228228
_math_stats_stdev() {
229229
flags=(--version -h --help)
230230
options=()
231-
__math_offer_flags_options 1
231+
__math_offer_flags_options 9223372036854775807
232232
}
233233

234234
_math_stats_quantiles() {
235235
flags=(--version -h --help)
236236
options=(--file --directory --shell --custom --custom-deprecated)
237-
__math_offer_flags_options 4
237+
__math_offer_flags_options 9223372036854775807
238238

239239
# Offer option value completions
240240
case "${prev}" in
@@ -280,7 +280,7 @@ _math_stats_quantiles() {
280280
_math_help() {
281281
flags=(--version)
282282
options=()
283-
__math_offer_flags_options 1
283+
__math_offer_flags_options 9223372036854775807
284284
}
285285

286286
complete -o filenames -F _math math

0 commit comments

Comments
 (0)