diff options
author | Roman Rakus <rrakus@redhat.com> | 2013-09-10 15:40:02 +0200 |
---|---|---|
committer | Roman Rakus <rrakus@redhat.com> | 2013-09-10 16:05:23 +0200 |
commit | b3df3eac1358ef68125d56b0ae479f680e2424ac (patch) | |
tree | c23785054a7b12f514b612ce54db4ac5626cc73a /completion | |
parent | b2faf179799ebaee8e746f703568e7d7c5a8780b (diff) | |
download | openlmi-scripts-b3df3eac1358ef68125d56b0ae479f680e2424ac.tar.gz openlmi-scripts-b3df3eac1358ef68125d56b0ae479f680e2424ac.tar.xz openlmi-scripts-b3df3eac1358ef68125d56b0ae479f680e2424ac.zip |
completion: Initial completion for LMI commands
Allow to complete:
- lmi options
- lmi commands
- lmi help command
Signed-off-by: Roman Rakus <rrakus@redhat.com>
Diffstat (limited to 'completion')
-rwxr-xr-x | completion/commands/_help | 5 | ||||
-rwxr-xr-x | completion/helpers/print_possible_commands.sh | 11 | ||||
-rw-r--r-- | completion/lmi.bash | 57 |
3 files changed, 73 insertions, 0 deletions
diff --git a/completion/commands/_help b/completion/commands/_help new file mode 100755 index 0000000..a56259f --- /dev/null +++ b/completion/commands/_help @@ -0,0 +1,5 @@ +#!/bin/bash +if (( $# > 1 )); then + exit +fi +compgen "-W $(helpers/print_possible_commands.sh)" -- "$1" diff --git a/completion/helpers/print_possible_commands.sh b/completion/helpers/print_possible_commands.sh new file mode 100755 index 0000000..ba29a4a --- /dev/null +++ b/completion/helpers/print_possible_commands.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# prints possible commands +# one per line +# parse lmi help +re_command="^[[:blank:]]*([[:alnum:]]+)[[:blank:]]*-" +while IFS= read line; do + if [[ $line =~ $re_command ]]; then + printf '%s\n' "${BASH_REMATCH[1]}" + fi +done < <(lmi help 2>/dev/null) diff --git a/completion/lmi.bash b/completion/lmi.bash new file mode 100644 index 0000000..568a7ae --- /dev/null +++ b/completion/lmi.bash @@ -0,0 +1,57 @@ +# +# Copyright (C) 2013 Red Hat, Inc. All rights reserved. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# Authors: Roman Rakus <rrakus@redhat.com> +# +# Bash completion for LMI commands + +_lmi() { +# echo $COMP_CWORD +# printf '<%s>\n' "${COMP_WORDS[@]}" + local options=(-c --config-file -h --host --hosts-file --user -v --trace -q --quiet -n --noverify --same-credentials --help --version) + local current="${COMP_WORDS[$COMP_CWORD]}" + local previous="${COMP_WORDS[COMP_CWORD-1]}" + local commands=( $(helpers/print_possible_commands.sh) ) + local used_command= + + for (( i=1; i < ${#COMP_WORDS[@]} - 1 && i < $COMP_CWORD; i++ )); do + for (( ci=0; ci < ${#commands[@]}; ci++ )); do + if [[ ${COMP_WORDS[$i]} == ${commands[$ci]} ]]; then + used_command=${commands[$ci]} + break 2; + fi + done + done + + if [[ $used_command ]]; then + # Check if we have completion executable for command + if [[ -x commands/"_$used_command" ]] ; then + # pass the rest of words typed as parameters + COMPREPLY=( $(commands/_$used_command "${COMP_WORDS[@]:$((i+1))}") ) + else + # commands without completion - filename completion + COMPREPLY=( $(compgen -f -- "$current") ) + fi + else + case $current in + -*) COMPREPLY=( $(compgen "-W ${options[*]}" -- "$current" ) );; + *) COMPREPLY=( $(compgen "-W ${commands[*]}" -- "$current" ) );; + esac + fi +} + +complete -F _lmi lmi |