0

I'm attempting to do bash completion for a command, and everything is working as normal with the full word commands, but when I try to complete the "--" flags, with the following code (following by a complete -F _keybase keybase) using any letter after the two tacks (i.e. keybase --h<TAB>) I get a compgen usage error. (keybase --<TAB> works as expected however). This is on Ubuntu 18.04. I'm sourcing this file in the current shell session and not as part of a logon script (yet).

_keybase() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    #...code removed for brevity

    if [[ ${cur} == -* ]]; then
# complete the "--" parameters
        opts=$(keybase help advanced | grep -Po '\-\-[a-z\-]+' | tr "\n" " ")
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    fi
}

This is what prints out when I type keybase --h<TAB>

keybase --hbash: compgen: --: invalid option
compgen: usage: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist]  [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]

I tweaked the escaping of the quotes, and added/removed the newlines in the defintion of $opt and added/removed the "--" as the second argument to compgen (I added these at the behest of a Debian article explaing bash completion keybase help advanced | grep -Po '\-\-[a-z\-]+' | tr "\n" " " outputs:

--api-dump-unsafe --api-timeout --api-uri-path-prefix --app-start-mode --bg-identifier-disabled --chat-db --code-signing-kids --config-file --use-root-config-file --db --debug --display-raw-untrusted-output --features --gpg --gpg-options --home --leveldb-num-files --local-rpc-debug-unsafe --log-file --ek-log-file --log-format --log-prefix --merkle-kids --no-debug --debug --pgpdir --gpgdir --pid-file --pinentry --proof-cache-size --proxy --push-disabled --push-save-interval --push-server-uri --pvl-kit --paramproof-kit --prove-bypass --remember-passphrase --run-mode --scraper-timeout --secret-keyring --server --session-file --slow-gregor-conn --read-deleted-sigchain --socket-file --standalone --timers --tor-hidden-address --tor-mode --tor-proxy --updater-config-file --upgrade-per-user-key --use-default-log-file --user-cache-size --vdebug --disable-team-auditor --disable-merkle-auditor --disable-search-indexer --disable-bg-conv-loader --enable-bot-lite-mode --auto-fork --no-auto-fork --help --generate-bash-completion --version 

and when I echo the command that compgen is expected to run with echo "compgen -W \"${opts}\" -- ${cur}" I get the command I expect and indeed one that runs just fine at the terminal:

compgen -W "--api-dump-unsafe --api-timeout --api-uri-path-prefix --app-start-mode --bg-identifier-disabled --chat-db --code-signing-kids --config-file --use-root-config-file --db --debug --display-raw-untrusted-output --features --gpg --gpg-options --home --leveldb-num-files --local-rpc-debug-unsafe --log-file --ek-log-file --log-format --log-prefix --merkle-kids --no-debug --debug --pgpdir --gpgdir --pid-file --pinentry --proof-cache-size --proxy --push-disabled --push-save-interval --push-server-uri --pvl-kit --paramproof-kit --prove-bypass --remember-passphrase --run-mode --scraper-timeout --secret-keyring --server --session-file --slow-gregor-conn --read-deleted-sigchain --socket-file --standalone --timers --tor-hidden-address --tor-mode --tor-proxy --updater-config-file --upgrade-per-user-key --use-default-log-file --user-cache-size --vdebug --disable-team-auditor --disable-merkle-auditor --disable-search-indexer --disable-bg-conv-loader --enable-bot-lite-mode --auto-fork --no-auto-fork --help --generate-bash-completion --version " -- --h
--home
--help
  • Curiously, this same code works fine on Fedora. Perhaps this is an Ubuntu bug. – Christopher Morrow Mar 4 at 3:04
0

Turns out the code works find on Fedora 29, leading me to believe this is a bug in Ubuntu and not in the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.