2 # Verify that all options mentioned in usage are recognized by getopt.
4 # Copyright (C) 2017-2025 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 .
"${srcdir=.}/tests/init.sh"; path_prepend_ .
/src
24 # Get stderr output for unrecognized options for later use as a pattern.
25 # Also consider the expected exit status of each program.
28 dir |
ls |
printenv |
sort | tty |
vdir ) rcexp
=2 ;;
29 env | chroot | nice | nohup | runcon | stdbuf | timeout
) rcexp
=125 ;;
31 # Write the pattern for a long, unknown option into a pattern file.
32 o
='thisoptiondoesnotexist'
33 returns_
$rcexp $prg --$o >/dev
/null
2> err || fail
=1
34 grep -F "$o" err || framework_failure_
35 sed -n "1s/--$o/OPT/p" < err
> pat || framework_failure_
37 # Append the pattern for a short unknown option.
38 returns_
$rcexp $prg -/ >/dev
/null
2> err || fail
=1
39 grep " '*/'*" err || framework_failure_
40 # The following only handles the common case that has single quotes,
41 # as it simplifies to identify missing options only on common systems.
42 sed -n "1s/'\/'/'OPT'/p" < err
>> pat || framework_failure_
44 # Get output for --help.
45 $prg --help > help || fail
=1
47 # Extract all options mention in the above --help output.
50 sed -n -e '/--version/q' \
51 -e 's/^ \{2,6\}-/-/; s/ .*//; s/[=[].*//; s/, /\'"$nl"'/g
; s
/^
-/-/p
' help \
52 > opts || framework_failure_
53 cat opts # for debugging purposes
55 # Test all options mentioned in usage (but --version).
57 test "x$opt" = 'x--help
' \
59 # Append --help to be on the safe side: the option under test either
60 # requires a further argument, or --help triggers usage(); so $prg should
61 # exit without performing its regular operation.
62 # Add a 2nd --help for the 'env
-u --help' case.
63 $prg "$opt" --help --help </dev/null >out 2>err1
65 # In the --help case, i.e., when the option under test has been accepted,
66 # the exit code should be Zero.
68 compare help out || fail=1
70 # Else $prg should have complained about a missing argument.
71 # Catch false positives.
73 'pr
/-COLUMN') continue;;
75 # Replace $opt in stderr output by the neutral placeholder.
76 # Handle both long and short option error messages.
77 sed -e "s/$opt/OPT/" -e "s/'.
'/'OPT
'/" < err1 > err || framework_failure_
78 # Fail if the stderr output matches the above provoked error.
79 grep -Ff pat err && { fail=1; cat err1; }
84 for prg in $built_programs; do
86 # Skip utilities entirely which have special option parsing.
89 # Wrap some utilities known by the shell by env.
90 echo | false | kill | printf | pwd | sleep | test | true )