From 5cd5993211818e4c46367abd78c70a02b04458ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 27 Apr 2011 15:29:00 +0300 Subject: [PATCH] _parse_help: Change to output first long option, or first short if not found. Makes things more consistent with the rest of our option offerings. --- CHANGES | 2 +- bash_completion | 16 ++++++++++++++-- test/unit/_parse_help.exp | 14 +++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGES b/CHANGES index 0016003c..ff7fb741 100644 --- a/CHANGES +++ b/CHANGES @@ -3,7 +3,7 @@ bash-completion (2.x) [ Ville Skyttä ] * Add pxz and reptyr completions. * Improve aspell, gendiff, rsync, screen, smartctl, tar, xz, xzdec, and - generic long option completions. + generic parsed and long option completions. * Try harder to find the correct perl executable to run the perl helper with. * Drop rpm query support for rpm < 4.1. * Split rpm and rpmbuild completions and improve them both. diff --git a/bash_completion b/bash_completion index fe01ac2a..feb520b9 100644 --- a/bash_completion +++ b/bash_completion @@ -747,8 +747,20 @@ _init_completion() # _parse_help() { - $1 ${2:---help} 2>&1 | sed -e '/^[[:space:]]*-/!d' -e 's|[,/]| |g' | \ - awk '{ print $1; if ($2 ~ /^-/) { print $2 } }' | sed -e 's|[<=[].*||' + # Print first found long option, or first short if not found. + $1 ${2:---help} 2>&1 | awk \ + '{ + if ($0 !~ /^[ \t]*-/) { next } + gsub("[,/]", " "); + x = -1; + for (i = 1; i <= NF; i++) { + if (sub("^(--.[^<[]*)", "&", $i)) { x = i ; break } + if ($i !~ /^-/) { break } + } + if (x == -1) { x = 1 } + sub("=.*", "", $x); + print $x + }' } # This function completes on signal names diff --git a/test/unit/_parse_help.exp b/test/unit/_parse_help.exp index 4beb28d2..2c95032f 100644 --- a/test/unit/_parse_help.exp +++ b/test/unit/_parse_help.exp @@ -38,11 +38,11 @@ assert_bash_list "-space" $cmd "one dash after space" sync_after_int set cmd {fn() { printf '%s\n' "-one -two dashes"; }; _parse_help fn} -assert_bash_list "-one\n-two" $cmd "two dashes, space-separated" +assert_bash_list "-one" $cmd "two dashes, space-separated" sync_after_int set cmd {fn() { printf '%s\n' "-one,-t dashes"; }; _parse_help fn} -assert_bash_list "-one\n-t" $cmd "two dashes, comma-separated" +assert_bash_list "-one" $cmd "two dashes, comma-separated" sync_after_int set cmd {fn() { printf '%s\n' "-one dash-inside"; }; _parse_help fn} @@ -58,15 +58,19 @@ assert_bash_list "--long-arg" $cmd "value not seen as option" sync_after_int set cmd {fn() { printf '%s\n' "--long-arg=-value,--opt2=val"; }; _parse_help fn} -assert_bash_list "--long-arg\n--opt2" $cmd "two options with values" +assert_bash_list "--long-arg" $cmd "two options with values" sync_after_int set cmd {fn() { printf '%s\n' "-m,--mirror"; }; _parse_help fn} -assert_bash_list "-m\n--mirror" $cmd "short + long" +assert_bash_list "--mirror" $cmd "short + long" sync_after_int set cmd {fn() { printf '%s\n' "-T/--upload-file"; }; _parse_help fn} -assert_bash_list "-T\n--upload-file" $cmd "short + long, slash separated" +assert_bash_list "--upload-file" $cmd "short + long, slash separated" +sync_after_int + +set cmd {fn() { printf '%s\n' "-f, -F, --foo"; }; _parse_help fn} +assert_bash_list "--foo" $cmd "short + short + long" sync_after_int