Skip to content

Commit 26744de

Browse files
authored
Fix zsh dashes in command names (#284)
1 parent 69ddee8 commit 26744de

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

Sources/ArgumentParser/Completions/ZshCompletionsGenerator.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct ZshCompletionsGenerator {
1717
return """
1818
#compdef \(type._commandName)
1919
local context state state_descr line
20-
_\(type._commandName)_commandname=$words[1]
20+
_\(type._commandName.zshEscapingCommandName())_commandname=$words[1]
2121
typeset -A opt_args
2222
2323
\(generateCompletionFunction([type]))
@@ -122,6 +122,10 @@ extension String {
122122
fileprivate func zshEscaped() -> String {
123123
self.zshEscapingSingleQuotes().zshEscapingMetacharacters()
124124
}
125+
126+
fileprivate func zshEscapingCommandName() -> String {
127+
self.replacingOccurrences(of: "-", with: "_")
128+
}
125129
}
126130

127131
extension ArgumentDefinition {

Tests/ArgumentParserUnitTests/CompletionScriptTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ extension CompletionScriptTests {
125125
}
126126

127127
extension CompletionScriptTests {
128-
struct Escaped: ParsableCommand {
128+
struct EscapedCommand: ParsableCommand {
129129
@Option(help: #"Escaped chars: '[]\."#)
130130
var one: String
131131
}
132132

133133
func testEscaped_Zsh() throws {
134-
XCTAssertEqual(zshEscapedCompletion, Escaped.completionScript(for: .zsh))
134+
XCTAssertEqual(zshEscapedCompletion, EscapedCommand.completionScript(for: .zsh))
135135
}
136136
}
137137

@@ -213,12 +213,12 @@ complete -F _base base
213213
"""
214214

215215
private let zshEscapedCompletion = """
216-
#compdef escaped
216+
#compdef escaped-command
217217
local context state state_descr line
218-
_escaped_commandname=$words[1]
218+
_escaped_command_commandname=$words[1]
219219
typeset -A opt_args
220220
221-
_escaped() {
221+
_escaped-command() {
222222
integer ret=1
223223
local -a args
224224
args+=(
@@ -236,7 +236,7 @@ _custom_completion() {
236236
_describe '' completions
237237
}
238238
239-
_escaped
239+
_escaped-command
240240
"""
241241

242242
private let fishBaseCompletions = """

0 commit comments

Comments
 (0)