Skip to content

Commit 04695ec

Browse files
authored
Escape single quotes in shellCommand completion for fish. (#811)
Signed-off-by: Ross Goldberg <[email protected]>
1 parent e905148 commit 04695ec

File tree

7 files changed

+9
-7
lines changed

7 files changed

+9
-7
lines changed

Examples/math/Math.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ extension Math.Statistics {
229229
var directory: String?
230230

231231
@Option(
232-
completion: .shellCommand("head -100 /usr/share/dict/words | tail -50")
232+
completion: .shellCommand("head -100 '/usr/share/dict/words' | tail -50")
233233
)
234234
var shell: String?
235235

Sources/ArgumentParser/Completions/FishCompletionsGenerator.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ extension CommandInfoV0 {
217217
case .directory:
218218
results += ["-\(r)fa '(\(completeDirectoriesFunctionName))'"]
219219
case .shellCommand(let shellCommand):
220-
results += ["-\(r)fka '(\(shellCommand))'"]
220+
results += [
221+
"-\(r)fka '(\(shellCommand.fishEscapeForSingleQuotedString()))'"
222+
]
221223
case .custom, .customAsync:
222224
results += [
223225
"""

Tests/ArgumentParserExampleTests/Snapshots/testMathBashCompletionScript().bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ _math_stats_quantiles() {
247247
return
248248
;;
249249
'--shell')
250-
__math_add_completions -W "$(eval 'head -100 /usr/share/dict/words | tail -50')"
250+
__math_add_completions -W "$(eval 'head -100 '\''/usr/share/dict/words'\'' | tail -50')"
251251
return
252252
;;
253253
'--custom')

Tests/ArgumentParserExampleTests/Snapshots/testMathFishCompletionScript().fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ complete -c 'math' -n '__math_should_offer_completions_for "math stats quantiles
105105
complete -c 'math' -n '__math_should_offer_completions_for "math stats quantiles" 3' -fka '(__math_custom_completion ---completion stats quantiles -- positional@2)'
106106
complete -c 'math' -n '__math_should_offer_completions_for "math stats quantiles"' -l 'file' -rfa '(set -l exts \'txt\' \'md\';for p in (string match -e -- \'*/\' (commandline -t);or printf \n)*.{$exts};printf %s\n $p;end;__fish_complete_directories (commandline -t) \'\')'
107107
complete -c 'math' -n '__math_should_offer_completions_for "math stats quantiles"' -l 'directory' -rfa '(__math_complete_directories)'
108-
complete -c 'math' -n '__math_should_offer_completions_for "math stats quantiles"' -l 'shell' -rfka '(head -100 /usr/share/dict/words | tail -50)'
108+
complete -c 'math' -n '__math_should_offer_completions_for "math stats quantiles"' -l 'shell' -rfka '(head -100 \'/usr/share/dict/words\' | tail -50)'
109109
complete -c 'math' -n '__math_should_offer_completions_for "math stats quantiles"' -l 'custom' -rfka '(__math_custom_completion ---completion stats quantiles -- --custom (count (__math_tokens -pc)) (__math_tokens -tC))'
110110
complete -c 'math' -n '__math_should_offer_completions_for "math stats quantiles"' -l 'custom-deprecated' -rfka '(__math_custom_completion ---completion stats quantiles -- --custom-deprecated)'
111111
complete -c 'math' -n '__math_should_offer_completions_for "math stats quantiles"' -l 'version' -d 'Show the version.'

Tests/ArgumentParserExampleTests/Snapshots/testMathZshCompletionScript().zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ _math_stats_quantiles() {
161161
'*:values:'
162162
'--file:file:_files -g '\''*.txt *.md'\'''
163163
'--directory:directory:_files -/'
164-
'--shell:shell:{local -a list;list=(${(f)"$(head -100 /usr/share/dict/words | tail -50)"});_describe -V "" list}'
164+
'--shell:shell:{local -a list;list=(${(f)"$(head -100 '\''/usr/share/dict/words'\'' | tail -50)"});_describe -V "" list}'
165165
'--custom:custom:{__math_custom_complete ---completion stats quantiles -- --custom "${current_word_index}" "$(__math_cursor_index_in_current_word)"}'
166166
'--custom-deprecated:custom-deprecated:{__math_custom_complete ---completion stats quantiles -- --custom-deprecated}'
167167
'--version[Show the version.]'

Tests/ArgumentParserUnitTests/Snapshots/testMathDumpHelp().json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@
603603
{
604604
"completionKind" : {
605605
"shellCommand" : {
606-
"command" : "head -100 \/usr\/share\/dict\/words | tail -50"
606+
"command" : "head -100 '\/usr\/share\/dict\/words' | tail -50"
607607
}
608608
},
609609
"isOptional" : true,

Tests/ArgumentParserUnitTests/Snapshots/testMathStatsDumpHelp().json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@
386386
{
387387
"completionKind" : {
388388
"shellCommand" : {
389-
"command" : "head -100 \/usr\/share\/dict\/words | tail -50"
389+
"command" : "head -100 '\/usr\/share\/dict\/words' | tail -50"
390390
}
391391
},
392392
"isOptional" : true,

0 commit comments

Comments
 (0)