Skip to content

ROADMAP - Bash completion #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iandstanley opened this issue Jun 11, 2021 · 3 comments
Closed

ROADMAP - Bash completion #4

iandstanley opened this issue Jun 11, 2021 · 3 comments
Assignees
Labels
enhancement New feature or request ROADMAP Added to ROADMAP

Comments

@iandstanley
Copy link
Contributor

ROADMAP feature:

Create bash completion script

@iandstanley iandstanley added the enhancement New feature or request label Jun 11, 2021
@iandstanley iandstanley self-assigned this Jun 11, 2021
@iandstanley iandstanley changed the title [FEATURE REQUEST] - Bash completion ROADMAP - Bash completion Jun 11, 2021
@iandstanley iandstanley added the ROADMAP Added to ROADMAP label Jun 11, 2021
@iandstanley
Copy link
Contributor Author

Never done a bash completion before so I am going to borrow from the pass project and just rewrite the internals.

Apparently we need a script in the folder

/usr/share/bash-completion/completions

It would appear that all you need to do is supply a bash script that supplies a number of functions

@iandstanley
Copy link
Contributor Author

iandstanley commented Jun 17, 2021

You'll have to create a new file:

/etc/bash_completion.d/foo

For a static autocompletion (--help / --verbose for instance) add this:

_foo() 
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--help --verbose --version"

    if [[ ${cur} == -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}
complete -F _foo foo

  • COMP_WORDS is an array containing all individual words in the current command line.
  • COMP_CWORD is an index of the word containing the current cursor position.
  • COMPREPLY is an array variable from which Bash reads the possible completions.

And the compgen command returns the array of elements from --help, --verbose and --version matching the current word "${cur}":

compgen -W "--help --verbose --version" -- "<userinput>"

@iandstanley
Copy link
Contributor Author

Here is a complete tutorial.

Let's have an example of script called admin.sh to which you would like to have autocomplete working.

#!/bin/bash

while [ $# -gt 0 ]; do
  arg=$1
  case $arg in
    option_1)
     # do_option_1
    ;;
    option_2)
     # do_option_2
    ;;
    shortlist)
      echo option_1 option_2 shortlist
    ;;
    *)
     echo Wrong option
    ;;
  esac
  shift
done

Note the option shortlist. Calling the script with this option will print out all possible options for this script.

And here you have the autocomplete script:

_script()
{
  _script_commands=$(/path/to/your/script.sh shortlist)

  local cur
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  COMPREPLY=( $(compgen -W "${_script_commands}" -- ${cur}) )

  return 0
}
complete -o nospace -F _script ./admin.sh

Note that the last argument to complete is the name of the script you want to add autocompletion to. All you need to do is to add your autocomplete script to bashrc as

source /path/to/your/autocomplete.sh

or copy it to /etc/bash.completion.d

iandstanley added a commit that referenced this issue Jun 20, 2021

Unverified

This user has not yet uploaded their public signing key.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ROADMAP Added to ROADMAP
Projects
None yet
Development

No branches or pull requests

1 participant