v.io/jiri@v0.0.0-20160715023856-abfb8b131290/scripts/jiri-bash-completion.sh (about) 1 #!/bin/bash 2 # Copyright 2015 The Vanadium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style 4 # license that can be found in the LICENSE file. 5 6 # Check for bash 7 [[ -z "$BASH_VERSION" ]] && return 8 9 # Extracts subcommands from the help output of a given command ($1). 10 # An example help output from "jiri help": 11 # ... 12 # The jiri commands are: 13 # build Tool for managing vanadium builds 14 # contributors List vanadium project contributors 15 # ... 16 # Run "jiri help [command]" for command usage. 17 # ... 18 _jiri_extract_subcommands() { 19 local -r CMD="$1" 20 local -r START_LINE="The .* commands are" 21 local -r END_LINE="Run \"jiri" 22 ${CMD} -h > /dev/null 2>&1 23 if [[ "$?" = 0 ]]; then 24 ${CMD} -h \ 25 | sed -n "/${START_LINE}/,/${END_LINE}/p" | grep '^ '| awk '{print $1}' | sort 26 fi 27 } 28 29 # Extracts flags from the help output of a given command ($1). 30 # Note that we ignore all "global" flags. 31 # An example help output from "jiri help update": 32 # ... 33 # The jiri update flags are: 34 # -gc=false 35 # Garbage collect obsolete repositories. 36 # -n=false 37 # ... 38 _jiri_extract_flags() { 39 local -r CMD="$1" 40 local -r START_LINE="The .* flags are" 41 local -r END_LINE="^$" 42 ${CMD} -h > /dev/null 2>&1 43 if [[ "$?" = 0 ]]; then 44 ${CMD} -h | sed -n "/${START_LINE}/,/${END_LINE}/p" | grep '^ -'| cut -d= -f1 | tr -d " " | sort 45 fi 46 } 47 48 # Gets the command line before the word where the current cursor is. 49 _jiri_get_cmdline_before_cursor() { 50 local i 51 local CMD="" 52 for(( i=0; i<COMP_CWORD; i++ )); do 53 CMD="${CMD} ${COMP_WORDS[i]}" 54 done 55 echo "${CMD}" 56 } 57 58 # Gets the names of available projects. 59 _jiri_get_project_names() { 60 jiri project list | sed "s/^.*project=\"\(.*\)\" path.*/\1/" 61 } 62 63 64 # Main bash completion function for the "jiri" command. 65 # 66 # Completion-related internal Bash vars: 67 # - COMP_WORDS: An array variable consisting of the individual words in the 68 # current command line 69 # - COMP_CWORD: An index into ${COMP_WORDS} of the word containing the current cursor 70 # position. 71 # - COMPREPLY An array variable from which bash reads the possible completions 72 # generated by a completion function. 73 _jiri_complete() { 74 local -r CUR="${COMP_WORDS[COMP_CWORD]}" 75 local -r CMD="$(_jiri_get_cmdline_before_cursor)" 76 77 case "${CUR}" in 78 -*) 79 # Complete flags. 80 COMPREPLY=($(compgen -W "$(_jiri_extract_flags "${CMD}")" -- ${CUR})) 81 ;; 82 *) 83 # Complete commands. 84 if [[ "${COMP_LINE}" =~ .*test(\ )+project.* ]]; then 85 # Special handling for completing "jiri test project". 86 COMPREPLY=($(compgen -W "$(_jiri_get_project_names)" -- ${CUR})) 87 elif [[ "${COMP_LINE}" =~ .*test(\ )+run.* ]]; then 88 # Special handling for completing "jiri test run". 89 COMPREPLY=($(compgen -W "$(jiri test list)" -- ${CUR})) 90 else 91 COMPREPLY=($(compgen -W "$(_jiri_extract_subcommands "${CMD}")" -- ${CUR})) 92 fi 93 ;; 94 esac 95 96 return 0 97 } 98 99 # Main bash completion function for the "vcd" command. 100 _jiri_vcd_complete() { 101 local -r CUR="${COMP_WORDS[COMP_CWORD]}" 102 103 COMPREPLY=($(compgen -W "$(_jiri_get_project_names)" -- ${CUR})) 104 } 105 106 complete -F _jiri_complete jiri 107 complete -F _jiri_vcd_complete vcd