blob: 92e7e27cc9e0b063602c6086d723e2fce0c98995 [file] [log] [blame]
#!/bin/bash
# Copyright 2015 The Vanadium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# Check for bash
[[ -z "$BASH_VERSION" ]] && return
# Extracts subcommands from the help output of a given command ($1).
# An example help output from "jiri help":
# ...
# The jiri commands are:
# build Tool for managing vanadium builds
# contributors List vanadium project contributors
# ...
# Run "jiri help [command]" for command usage.
# ...
_jiri_extract_subcommands() {
local -r CMD="$1"
local -r START_LINE="The .* commands are"
local -r END_LINE="Run \"jiri"
${CMD} -h > /dev/null 2>&1
if [[ "$?" = 0 ]]; then
${CMD} -h \
| sed -n "/${START_LINE}/,/${END_LINE}/p" | grep '^ '| awk '{print $1}' | sort
fi
}
# Extracts flags from the help output of a given command ($1).
# Note that we ignore all "global" flags.
# An example help output from "jiri help update":
# ...
# The jiri update flags are:
# -gc=false
# Garbage collect obsolete repositories.
# -n=false
# ...
_jiri_extract_flags() {
local -r CMD="$1"
local -r START_LINE="The .* flags are"
local -r END_LINE="^$"
${CMD} -h > /dev/null 2>&1
if [[ "$?" = 0 ]]; then
${CMD} -h | sed -n "/${START_LINE}/,/${END_LINE}/p" | grep '^ -'| cut -d= -f1 | tr -d " " | sort
fi
}
# Gets the command line before the word where the current cursor is.
_jiri_get_cmdline_before_cursor() {
local i
local CMD=""
for(( i=0; i<COMP_CWORD; i++ )); do
CMD="${CMD} ${COMP_WORDS[i]}"
done
echo "${CMD}"
}
# Gets the names of available projects.
_jiri_get_project_names() {
jiri project list | sed "s/^.*project=\"\(.*\)\" path.*/\1/"
}
# Main bash completion function for the "jiri" command.
#
# Completion-related internal Bash vars:
# - COMP_WORDS: An array variable consisting of the individual words in the
# current command line
# - COMP_CWORD: An index into ${COMP_WORDS} of the word containing the current cursor
# position.
# - COMPREPLY An array variable from which bash reads the possible completions
# generated by a completion function.
_jiri_complete() {
local -r CUR="${COMP_WORDS[COMP_CWORD]}"
local -r CMD="$(_jiri_get_cmdline_before_cursor)"
case "${CUR}" in
-*)
# Complete flags.
COMPREPLY=($(compgen -W "$(_jiri_extract_flags "${CMD}")" -- ${CUR}))
;;
*)
# Complete commands.
if [[ "${COMP_LINE}" =~ .*test(\ )+project.* ]]; then
# Special handling for completing "jiri test project".
COMPREPLY=($(compgen -W "$(_jiri_get_project_names)" -- ${CUR}))
elif [[ "${COMP_LINE}" =~ .*test(\ )+run.* ]]; then
# Special handling for completing "jiri test run".
COMPREPLY=($(compgen -W "$(jiri test list)" -- ${CUR}))
else
COMPREPLY=($(compgen -W "$(_jiri_extract_subcommands "${CMD}")" -- ${CUR}))
fi
;;
esac
return 0
}
# Main bash completion function for the "vcd" command.
_jiri_vcd_complete() {
local -r CUR="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=($(compgen -W "$(_jiri_get_project_names)" -- ${CUR}))
}
complete -F _jiri_complete jiri
complete -F _jiri_vcd_complete vcd