Skip to content

plugins: Bash completion fails when command names contain a hyphen #2836

Open

Description

@jhermann

beets version 1.4.6
Python version 3.5.2

For commands like foo-bar, you create invalid bash code. The following diff is part of a solution, and at least prevents ugly bash error messages when you press TAB.

--- a/beets/ui/commands.py
+++ b/beets/ui/commands.py
@@ -1758,7 +1758,7 @@ def completion_script(commands):
     # Command aliases
     yield u"  local aliases='%s'\n" % ' '.join(aliases.keys())
     for alias, cmd in aliases.items():
-        yield u"  local alias__%s=%s\n" % (alias, cmd)
+        yield u"  local alias__%s=%s\n" % (alias.replace('-', '_'), cmd)
     yield u'\n'
 
     # Fields
@@ -1775,7 +1775,7 @@ def completion_script(commands):
             if option_list:
                 option_list = u' '.join(option_list)
                 yield u"  local %s__%s='%s'\n" % (
-                    option_type, cmd, option_list)
+                    option_type, cmd.replace('-', '_'), option_list)
 
     yield u'  _beet_dispatch\n'
     yield u'}\n'
--- a/beets/ui/completion_base.sh
+++ b/beets/ui/completion_base.sh
@@ -70,7 +70,7 @@ _beet_dispatch() {
 
   # Replace command shortcuts
   if [[ -n $cmd ]] && _list_include_item "$aliases" "$cmd"; then
-    eval "cmd=\$alias__$cmd"
+    eval "cmd=\$alias__${cmd//-/_}"
   fi
 
   case $cmd in
@@ -94,8 +94,8 @@ _beet_dispatch() {
 _beet_complete() {
   if [[ $cur == -* ]]; then
     local opts flags completions
-    eval "opts=\$opts__$cmd"
-    eval "flags=\$flags__$cmd"
+    eval "opts=\$opts__${cmd//-/_}"
+    eval "flags=\$flags__${cmd//-/_}"
     completions="${flags___common} ${opts} ${flags}"
     COMPREPLY+=( $(compgen -W "$completions"  -- $cur) )
   else
@@ -129,7 +129,7 @@ _beet_complete_global() {
     COMPREPLY+=( $(compgen -W "$completions" -- $cur) )
   elif [[ -n $cur ]] && _list_include_item "$aliases" "$cur"; then
     local cmd
-    eval "cmd=\$alias__$cur"
+    eval "cmd=\$alias__${cur//-/_}"
     COMPREPLY+=( "$cmd" )
   else
     COMPREPLY+=( $(compgen -W "$commands" -- $cur) )
@@ -138,7 +138,7 @@ _beet_complete_global() {
 
 _beet_complete_query() {
   local opts
-  eval "opts=\$opts__$cmd"
+  eval "opts=\$opts__${cmd//-/_}"
 
   if [[ $cur == -* ]] || _list_include_item "$opts" "$prev"; then
     _beet_complete

But then option completion still doesn't work correctly.

Activity

  1. added
    bug
    on Mar 11, 2018
  2. sampsyo commented on Mar 11, 2018

    @sampsyo
    Member

    Interesting! Thanks for investigating. Do you think you might be able to submit this change as a pull request?

  3. added a commit that references this issue on Mar 12, 2018
  4. snejus commented on Oct 5, 2026

    @snejus
    Member

    This is fixed but needs a regression test to be closed. Adding the good first issue label.

  5. Samy-Le-S commented on Nov 27, 2026

    @Samy-Le-S
    Contributor

    Hi! I'd like to work on this. Since the fix is already in, I plan to add a regression test that checks the generated completion script uses _ instead of - in variable names for hyphenated commands. Is that what you had in mind?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions