diff options
author | Ville Skyttä <ville.skytta@iki.fi> | 2011-11-24 23:26:52 +0200 |
---|---|---|
committer | Ville Skyttä <ville.skytta@iki.fi> | 2011-11-25 08:45:59 +0200 |
commit | 3af9222e964739c3ca954bc1b966dc5628160457 (patch) | |
tree | 63a8f1fbec78606633d329f7e775fce9ba388ae3 | |
parent | f271d7e8db836f126ec20747095086ca01c744bb (diff) | |
download | bash-completion-3af9222e964739c3ca954bc1b966dc5628160457.tar.gz |
_parse_help, _parse_usage: If first arg is "-", read from stdin.
-rw-r--r-- | bash_completion | 10 | ||||
-rw-r--r-- | test/unit/_parse_help.exp | 5 | ||||
-rw-r--r-- | test/unit/_parse_usage.exp | 5 |
3 files changed, 16 insertions, 4 deletions
diff --git a/bash_completion b/bash_completion index ac5842a1..9bd43bdd 100644 --- a/bash_completion +++ b/bash_completion @@ -735,14 +735,15 @@ __parse_options() } # Parse GNU style help output of the given command. -# @param $1 command +# @param $1 command; if "-", read from stdin and ignore rest of args # @param $2 command options (default: --help) # _parse_help() { eval local cmd=$1 local line - "$cmd" ${2:---help} 2>&1 | while read -r line; do + { case $cmd in -) cat ;; *) "$cmd" ${2:---help} 2>&1 ;; esac } \ + | while read -r line; do [[ $line == *([ $'\t'])-* ]] || continue # transform "-f FOO, --foo=FOO" to "-f , --foo=FOO" etc @@ -756,14 +757,15 @@ _parse_help() } # Parse BSD style usage output (options in brackets) of the given command. -# @param $1 command +# @param $1 command; if "-", read from stdin and ignore rest of args # @param $2 command options (default: --usage) # _parse_usage() { eval local cmd=$1 local line match option i char - "$cmd" ${2:---usage} 2>&1 | while read -r line; do + { case $cmd in -) cat ;; *) "$cmd" ${2:---usage} 2>&1 ;; esac } \ + | while read -r line; do while [[ $line =~ \[[[:space:]]*(-[^]]+)[[:space:]]*\] ]]; do match=${BASH_REMATCH[0]} diff --git a/test/unit/_parse_help.exp b/test/unit/_parse_help.exp index a6d58d5a..43c5dc29 100644 --- a/test/unit/_parse_help.exp +++ b/test/unit/_parse_help.exp @@ -7,6 +7,7 @@ proc setup {} { proc teardown {} { assert_env_unmodified { /declare -f fn/d + /PIPESTATUS=/d } } @@ -129,5 +130,9 @@ set cmd {fn() { printf '%s\n' "-f or --foo"; }; _parse_help fn} assert_bash_list "--foo" $cmd "-f or --foo" sync_after_int +set cmd { printf '%s\n' "-f or --foo" | _parse_help - } +assert_bash_list "--foo" $cmd "from stdin" +sync_after_int + teardown diff --git a/test/unit/_parse_usage.exp b/test/unit/_parse_usage.exp index 0069848d..5a40b1fe 100644 --- a/test/unit/_parse_usage.exp +++ b/test/unit/_parse_usage.exp @@ -5,6 +5,7 @@ proc setup {} { proc teardown {} { assert_env_unmodified { /declare -f fn/d + /PIPESTATUS=/d } } @@ -55,5 +56,9 @@ set cmd {fn() { printf '%s\n' "----\n---foo\n----- bar"; }; _parse_usage fn} assert_bash_list "" $cmd "many dashes" sync_after_int +set cmd { printf '%s\n' "[-duh]" | _parse_usage - } +assert_bash_list "-d -u -h" $cmd "from stdin" +sync_after_int + teardown |