Skip to content

Commit f47a00f

Browse files
committed
Fix zsh dashes in command names
1 parent 69ddee8 commit f47a00f

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

Sources/ArgumentParser/Completions/ZshCompletionsGenerator.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
struct ZshCompletionsGenerator {
1313
/// Generates a Zsh completion script for the given command.
1414
static func generateCompletionScript(_ type: ParsableCommand.Type) -> String {
15-
let initialFunctionName = [type].completionFunctionName()
15+
let initialFunctionName = [type].completionFunctionName().zshEscapingCommandName()
16+
let commandName = type._commandName.zshEscapingCommandName()
1617

1718
return """
18-
#compdef \(type._commandName)
19+
#compdef \(commandName)
1920
local context state state_descr line
20-
_\(type._commandName)_commandname=$words[1]
21+
_\(commandName)_commandname=$words[1]
2122
typeset -A opt_args
2223
2324
\(generateCompletionFunction([type]))
@@ -32,7 +33,7 @@ struct ZshCompletionsGenerator {
3233

3334
static func generateCompletionFunction(_ commands: [ParsableCommand.Type]) -> String {
3435
let type = commands.last!
35-
let functionName = commands.completionFunctionName()
36+
let functionName = commands.completionFunctionName().zshEscapingCommandName()
3637
let isRootCommand = commands.count == 1
3738

3839
var args = generateCompletionArguments(commands)
@@ -50,14 +51,14 @@ struct ZshCompletionsGenerator {
5051

5152
let subcommandModes = subcommands.map {
5253
"""
53-
'\($0._commandName):\($0.configuration.abstract.zshEscaped())'
54+
'\($0._commandName.zshEscapingCommandName()):\($0.configuration.abstract.zshEscaped())'
5455
"""
5556
.indentingEachLine(by: 12)
5657
}
5758
let subcommandArgs = subcommands.map {
5859
"""
59-
(\($0._commandName))
60-
\(functionName)_\($0._commandName)
60+
(\($0._commandName.zshEscapingCommandName()))
61+
\(functionName)_\($0._commandName.zshEscaped())
6162
;;
6263
"""
6364
.indentingEachLine(by: 12)
@@ -122,6 +123,10 @@ extension String {
122123
fileprivate func zshEscaped() -> String {
123124
self.zshEscapingSingleQuotes().zshEscapingMetacharacters()
124125
}
126+
127+
fileprivate func zshEscapingCommandName() -> String {
128+
self.replacingOccurrences(of: "-", with: "_")
129+
}
125130
}
126131

127132
extension ArgumentDefinition {
@@ -187,7 +192,7 @@ extension ArgumentDefinition {
187192

188193
case .custom:
189194
// Generate a call back into the command to retrieve a completions list
190-
let commandName = commands.first!._commandName
195+
let commandName = commands.first!._commandName.zshEscapingCommandName()
191196
return "{_custom_completion $_\(commandName)_commandname \(customCompletionCall(commands)) $words}"
192197
}
193198
}

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)