Skip to content
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

Bash completion for subcommand flags #87

Merged
merged 14 commits into from
Jun 14, 2022
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,4 @@ endif(build_errors)
include(CTest)

add_subdirectory(src)
add_subdirectory(etc)
3 changes: 3 additions & 0 deletions etc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Install the bash completion script in an unversioned location.
chapulina marked this conversation as resolved.
Show resolved Hide resolved
install(FILES ign.bash_completion.sh DESTINATION share/ignition/ignition.completion.d)
install(FILES ignition.completion DESTINATION share/ignition)
mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 25 additions & 5 deletions etc/ign.bash_completion.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
#!/usr/bin/env bash
mabelzhang marked this conversation as resolved.
Show resolved Hide resolved

# ign bash completion

function _ign
{
local prev cur cmd opts
local ign="$1"
COMPREPLY=()
cur="$2"
prev="$3"

# searching for the command
# Return value
COMPREPLY=()

# Look for the first command that is not an option (-*)
# COMP_WORDS: array of words in current command line COMP_LINE
# COMP_CWORD: index of word containing current cursur location
# COMP_LINE: line entered so far
for ((i=1; $i<=$COMP_CWORD; i++)); do
if [[ ${COMP_WORDS[i]} != -* ]]; then
cmd="${COMP_WORDS[i]}"
break
fi
done

# On a word after top-level command ign. It may be an option (-*)
if [[ "$cur" == -* ]] || [[ "$prev" != "ign" ]]; then

# Subcommand is help
if [[ "$cmd" == "help" ]]; then
opts=$(ign --commands)

# Subcommand is a library name or an option (-*)
else
COMPREPLY=($(compgen -f -- "${COMP_WORDS[${COMP_CWORD}]}" ))
complete -o filenames -o nospace -F "_ign" "ign"
return
# Complete subcommands
case "$cmd" in
gui)
_gz_gui
return
;;
plugin)
_gz_plugin
mabelzhang marked this conversation as resolved.
Show resolved Hide resolved
return
;;
esac
fi

# on first word, top-level command (ign)
else
opts="$(ign --commands) help"
fi
Expand Down
9 changes: 9 additions & 0 deletions etc/ignition.completion
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

# Directory of current script
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

# Source the bash completion script for each subcommand
for f in $SCRIPT_DIR/ignition.completion.d/*.bash_completion.sh ; do
source $f
done