Skip to content

Commit

Permalink
Bash completion for subcommand flags (#87)
Browse files Browse the repository at this point in the history
Signed-off-by: Mabel Zhang <[email protected]>
  • Loading branch information
mabelzhang authored Jun 14, 2022
1 parent f266973 commit 9881cd0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
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)
18 changes: 18 additions & 0 deletions etc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Tack version onto and install the bash completion scripts

# Install script to source all bash completion scripts, of all library versions.
configure_file(
"gz.completion"
"${CMAKE_CURRENT_BINARY_DIR}/gz${PROJECT_MAJOR_VERSION}.completion" @ONLY)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/gz${PROJECT_MAJOR_VERSION}.completion
DESTINATION
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz)

configure_file(
"ign.bash_completion.sh"
"${CMAKE_CURRENT_BINARY_DIR}/ign${PROJECT_MAJOR_VERSION}.bash_completion.sh" @ONLY)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/ign${PROJECT_MAJOR_VERSION}.bash_completion.sh
DESTINATION
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${PROJECT_MAJOR_VERSION}.completion.d)
29 changes: 29 additions & 0 deletions etc/gz.completion
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
#
# Copyright (C) 2022 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Source scripts from different libraries for subcommand bash completions

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

# Source the bash completion script for each subcommand
# Files with greater version numbers will be sourced after files with lesser
# version numbers, which means functions from newer versions will overwrite
# those from older versions.
for f in $SCRIPT_DIR/gz*.completion.d/*.bash_completion.sh ; do
source $f
done
43 changes: 39 additions & 4 deletions etc/ign.bash_completion.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,66 @@
#!/usr/bin/env bash
#
# Copyright (C) 2022 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# 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"
# Complete subcommands
# If subcommand completion function exists (defined by corresponding
# library), call it.
func="_gz_$cmd"
if [[ "$(type -t $func)" == 'function' ]]; then
$func
else
# Use bash default auto-complete
COMPREPLY=($(compgen -o default -- "${COMP_WORDS[COMP_CWORD]}"))
fi
return
fi

# on first word, top-level command (ign)
else
opts="$(ign --commands) help"
fi
Expand Down

0 comments on commit 9881cd0

Please sign in to comment.