Skip to content

Dashes in command names break completion scripts #277

@natecook1000

Description

@natecook1000

Dashes aren't valid variable identifier characters for Zsh (and maybe Bash just Zsh). When building a completion script for a command that includes one or more dashes in the command name, the completion script prints errors instead of providing completions.

ArgumentParser version: main branch
Swift version: Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)

Checklist

  • If possible, I've reproduced the issue using the main branch of this package
  • I've searched for existing GitHub issues

Steps to Reproduce

Create a CLI tool with a dash in the command name, then generate and install a Zsh completion script for the command.

Command:

struct ExampleCommand: ParsableCommand {
  @Option var name: String
  @Flag var verbose = false
}

Script:

#compdef example-command
local context state state_descr line
_example-command_commandname=$words[1]
typeset -A opt_args

_example-command() {
    integer ret=1
    local -a args
    args+=(
        '--name:name:'
        '--verbose'
        '(-h --help)'{-h,--help}'[Print help information.]'
    )
    _arguments -w -s -S $args[@] && ret=0

    return ret
}


_custom_completion() {
    local completions=("${(@f)$($*)}")
    _describe '' completions
}

_example-command

Expected behavior

Typing example-command and then TAB should offer completions.

Actual behavior

The shell prints _example-command:3: command not found: _example-command_commandname=example-command.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions