From 2b94e3ff6e3f116256ed74b0552dd2a19eb91f48 Mon Sep 17 00:00:00 2001 From: Yves Blusseau Date: Wed, 15 Sep 2010 11:46:16 +0200 Subject: [PATCH] Add function to get completions from usage. * util/bash-completion.d/grub-completion.bash.in: Add function to get completions from usage. Use LC_ALL=C to get options properly. --- ChangeLog | 7 +++++ ChangeLog.completion-usage | 4 +++ .../bash-completion.d/grub-completion.bash.in | 30 ++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 ChangeLog.completion-usage diff --git a/ChangeLog b/ChangeLog index c525fe369..1113b8157 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2010-09-15 Yves Blusseau + + Add function to get completions from usage. + + * util/bash-completion.d/grub-completion.bash.in: Add function to get + completions from usage. Use LC_ALL=C to get options properly. + 2010-09-15 Vladimir Serbinenko * grub-core/gnulib/basename-lgpl.c: Imported. diff --git a/ChangeLog.completion-usage b/ChangeLog.completion-usage new file mode 100644 index 000000000..eee7b018f --- /dev/null +++ b/ChangeLog.completion-usage @@ -0,0 +1,4 @@ +2010-09-15 Yves Blusseau + + * util/bash-completion.d/grub-completion.bash.in: Add function to get + completions from usage. diff --git a/util/bash-completion.d/grub-completion.bash.in b/util/bash-completion.d/grub-completion.bash.in index abba0df78..65cbb80ee 100644 --- a/util/bash-completion.d/grub-completion.bash.in +++ b/util/bash-completion.d/grub-completion.bash.in @@ -58,7 +58,7 @@ __grubcomp () { esac } -# Function that return long options from the help +# Function that return long options from the help of the command # - arg: $1 (optional) command to get the long options from __grub_get_options_from_help () { local prog @@ -70,7 +70,7 @@ __grub_get_options_from_help () { fi local i IFS=" "$'\t'$'\n' - for i in $($prog --help) + for i in $(LC_ALL=C $prog --help) do case $i in --*) echo "${i%=*}";; @@ -78,6 +78,28 @@ __grub_get_options_from_help () { done } +# Function that return long options from the usage of the command +# - arg: $1 (optional) command to get the long options from +__grub_get_options_from_usage () { + local prog + + if [ $# -ge 1 ]; then + prog="$1" + else + prog="${COMP_WORDS[0]}" + fi + + local i IFS=" "$'\t'$'\n' + for i in $(LC_ALL=C $prog --usage) + do + case $i in + \[--*\]) i=${i#[} # Remove leading [ + echo ${i%%?(=*)]} # Remove optional value and trailing ] + ;; + esac + done +} + __grub_get_last_option () { local i for (( i=$COMP_CWORD-1; i > 0; i-- )); do @@ -355,7 +377,7 @@ _grub_mkimage () { -O|--format) # Get available format from help local prog=${COMP_WORDS[0]} - __grubcomp "$($prog --help | \ + __grubcomp "$(LC_ALL=C $prog --help | \ awk -F ":" '/available formats/ { print $2 }' | \ sed 's/, / /g')" return @@ -415,7 +437,7 @@ _grub_probe () { -t|--target) # Get target type from help local prog=${COMP_WORDS[0]} - __grubcomp "$($prog --help | \ + __grubcomp "$(LC_ALL=C $prog --help | \ awk -F "[()]" '/--target=/ { print $2 }' | \ sed 's/|/ /g')" return