Skip to content

Commit d82fee4

Browse files
committed
feat: add return values for _parse_help and _parse_usage
1 parent fcaf3d4 commit d82fee4

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

bash_completion

+29-7
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ _init_completion()
869869
}
870870

871871
# Helper function for _parse_help and _parse_usage.
872+
# @return True (0) if an option was found, False (> 0) otherwise
872873
__parse_options()
873874
{
874875
local option option2 i IFS=$' \t\n,/|'
@@ -887,7 +888,7 @@ __parse_options()
887888
*) break ;;
888889
esac
889890
done
890-
[[ $option ]] || return 0
891+
[[ $option ]] || return 1
891892

892893
IFS=$' \t\n' # affects parsing of the regexps below...
893894

@@ -900,7 +901,10 @@ __parse_options()
900901
fi
901902
902903
option=${option%%[<{().[]*}
903-
printf '%s\n' "${option/=*/=}"
904+
option=${option/=*/=}
905+
[[ $option ]] || return 1
906+
907+
printf '%s\n' "$option"
904908
}
905909
906910
# Parse GNU style help output of the given command.
@@ -909,8 +913,13 @@ __parse_options()
909913
#
910914
_parse_help()
911915
{
916+
local IFS=$' \t\n'
917+
local reset_monitor=$(shopt -po monitor) reset_lastpipe=$(shopt -p lastpipe)
918+
set +o monitor
919+
shopt -s lastpipe
920+
912921
eval local cmd="$(quote "$1")"
913-
local line
922+
local line rc=1
914923
{
915924
case $cmd in
916925
-) cat ;;
@@ -925,9 +934,13 @@ _parse_help()
925934
((^|[^-])-[A-Za-z0-9?][[:space:]]+)\[?[A-Z0-9]+([,_-]+[A-Z0-9]+)?(\.\.+)?\]? ]]; do
926935
line=${line/"${BASH_REMATCH[0]}"/"${BASH_REMATCH[1]}"}
927936
done
928-
__parse_options "${line// or /, }"
937+
__parse_options "${line// or /, }" && rc=0
929938
930939
done
940+
941+
$reset_monitor
942+
$reset_lastpipe
943+
return $rc
931944
}
932945
933946
# Parse BSD style usage output (options in brackets) of the given command.
@@ -936,8 +949,13 @@ _parse_help()
936949
#
937950
_parse_usage()
938951
{
952+
local IFS=$' \t\n'
953+
local reset_monitor=$(shopt -po monitor) reset_lastpipe=$(shopt -p lastpipe)
954+
set +o monitor
955+
shopt -s lastpipe
956+
939957
eval local cmd="$(quote "$1")"
940-
local line match option i char
958+
local line match option i char rc=1
941959
{
942960
case $cmd in
943961
-) cat ;;
@@ -954,17 +972,21 @@ _parse_usage()
954972
# Treat as bundled short options
955973
for ((i = 1; i < ${#option}; i++)); do
956974
char=${option:i:1}
957-
[[ $char != '[' ]] && printf '%s\n' -$char
975+
[[ $char != '[' ]] && printf '%s\n' -$char && rc=0
958976
done
959977
;;
960978
*)
961-
__parse_options "$option"
979+
__parse_options "$option" && rc=0
962980
;;
963981
esac
964982
line=${line#*"$match"}
965983
done
966984

967985
done
986+
987+
$reset_monitor
988+
$reset_lastpipe
989+
return $rc
968990
}
969991

970992
# This function completes on signal names (minus the SIG prefix)

test/t/unit/test_unit_parse_help.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
class TestUnitParseHelp:
1010
def test_1(self, bash):
1111
assert_bash_exec(bash, "fn() { echo; }")
12-
output = assert_bash_exec(bash, "_parse_help fn")
12+
output = assert_bash_exec(bash, "_parse_help fn; (($? == 1))")
1313
assert not output
1414

1515
def test_2(self, bash):
1616
assert_bash_exec(bash, "fn() { echo 'no dashes here'; }")
17-
output = assert_bash_exec(bash, "_parse_help fn")
17+
output = assert_bash_exec(bash, "_parse_help fn; (($? == 1))")
1818
assert not output
1919

2020
def test_3(self, bash):
2121
assert_bash_exec(bash, "fn() { echo 'internal-dash'; }")
22-
output = assert_bash_exec(bash, "_parse_help fn")
22+
output = assert_bash_exec(bash, "_parse_help fn; (($? == 1))")
2323
assert not output
2424

2525
def test_4(self, bash):
2626
assert_bash_exec(bash, "fn() { echo 'no -leading-dashes'; }")
27-
output = assert_bash_exec(bash, "_parse_help fn")
27+
output = assert_bash_exec(bash, "_parse_help fn; (($? == 1))")
2828
assert not output
2929

3030
def test_5(self, bash):
@@ -161,7 +161,7 @@ def test_30(self, bash):
161161
assert_bash_exec(
162162
bash, r"fn() { printf '%s\n' $'----\n---foo\n----- bar'; }"
163163
)
164-
output = assert_bash_exec(bash, "_parse_help fn")
164+
output = assert_bash_exec(bash, "_parse_help fn; (($? == 1))")
165165
assert not output
166166

167167
def test_31(self, bash):

test/t/unit/test_unit_parse_usage.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
class TestUnitParseUsage:
88
def test_1(self, bash):
99
assert_bash_exec(bash, "fn() { echo; }")
10-
output = assert_bash_exec(bash, "_parse_usage fn")
10+
output = assert_bash_exec(bash, "_parse_usage fn; (($? == 1))")
1111
assert not output
1212

1313
def test_2(self, bash):
1414
assert_bash_exec(bash, "fn() { echo 'no dashes here'; }")
15-
output = assert_bash_exec(bash, "_parse_usage fn")
15+
output = assert_bash_exec(bash, "_parse_usage fn; (($? == 1))")
1616
assert not output
1717

1818
def test_3(self, bash):
@@ -59,7 +59,7 @@ def test_11(self, bash):
5959
assert_bash_exec(
6060
bash, "fn() { echo ----; echo ---foo; echo '----- bar'; }"
6161
)
62-
output = assert_bash_exec(bash, "_parse_usage fn")
62+
output = assert_bash_exec(bash, "_parse_usage fn; (($? == 1))")
6363
assert not output
6464

6565
def test_12(self, bash):

0 commit comments

Comments
 (0)