From 2239419e98cc435b4fe81ea427fbfdcd604ebf30 Mon Sep 17 00:00:00 2001 From: Alfredo Delli Bovi Date: Sat, 6 Mar 2021 18:39:36 +0100 Subject: [PATCH] Fix zsh dashes in command names --- .../Completions/ZshCompletionsGenerator.swift | 6 +++++- .../CompletionScriptTests.swift | 12 ++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Sources/ArgumentParser/Completions/ZshCompletionsGenerator.swift b/Sources/ArgumentParser/Completions/ZshCompletionsGenerator.swift index aee012edd..e623df06b 100644 --- a/Sources/ArgumentParser/Completions/ZshCompletionsGenerator.swift +++ b/Sources/ArgumentParser/Completions/ZshCompletionsGenerator.swift @@ -17,7 +17,7 @@ struct ZshCompletionsGenerator { return """ #compdef \(type._commandName) local context state state_descr line - _\(type._commandName)_commandname=$words[1] + _\(type._commandName.zshEscapingCommandName())_commandname=$words[1] typeset -A opt_args \(generateCompletionFunction([type])) @@ -122,6 +122,10 @@ extension String { fileprivate func zshEscaped() -> String { self.zshEscapingSingleQuotes().zshEscapingMetacharacters() } + + fileprivate func zshEscapingCommandName() -> String { + self.replacingOccurrences(of: "-", with: "_") + } } extension ArgumentDefinition { diff --git a/Tests/ArgumentParserUnitTests/CompletionScriptTests.swift b/Tests/ArgumentParserUnitTests/CompletionScriptTests.swift index da327d00f..9e53639ef 100644 --- a/Tests/ArgumentParserUnitTests/CompletionScriptTests.swift +++ b/Tests/ArgumentParserUnitTests/CompletionScriptTests.swift @@ -125,13 +125,13 @@ extension CompletionScriptTests { } extension CompletionScriptTests { - struct Escaped: ParsableCommand { + struct EscapedCommand: ParsableCommand { @Option(help: #"Escaped chars: '[]\."#) var one: String } func testEscaped_Zsh() throws { - XCTAssertEqual(zshEscapedCompletion, Escaped.completionScript(for: .zsh)) + XCTAssertEqual(zshEscapedCompletion, EscapedCommand.completionScript(for: .zsh)) } } @@ -213,12 +213,12 @@ complete -F _base base """ private let zshEscapedCompletion = """ -#compdef escaped +#compdef escaped-command local context state state_descr line -_escaped_commandname=$words[1] +_escaped_command_commandname=$words[1] typeset -A opt_args -_escaped() { +_escaped-command() { integer ret=1 local -a args args+=( @@ -236,7 +236,7 @@ _custom_completion() { _describe '' completions } -_escaped +_escaped-command """ private let fishBaseCompletions = """