diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-11 01:32:46 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-11 01:32:46 +0200 |
commit | 74d40589288890fffea437ed2f38ad1f2dc740e5 (patch) | |
tree | fafc1724b3b2c11280c067eaa4eaabb57e5f34cd /shell/hush_test/hush-getopts | |
parent | 4628945cd8d4679912f126d5f18f954210abb7d0 (diff) | |
download | busybox-74d40589288890fffea437ed2f38ad1f2dc740e5.tar.gz |
hush: getopts builtin
function old new delta
builtin_getopts - 271 +271
bltins1 372 384 +12
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/0 up/down: 283/0) Total: 283 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell/hush_test/hush-getopts')
4 files changed, 123 insertions, 0 deletions
diff --git a/shell/hush_test/hush-getopts/getopt_positional.right b/shell/hush_test/hush-getopts/getopt_positional.right new file mode 100644 index 000000000..f1c942476 --- /dev/null +++ b/shell/hush_test/hush-getopts/getopt_positional.right @@ -0,0 +1,6 @@ +*** no OPTIND, optstring:'we' args:-q -w -e r -t -y +./getopt_positional.tests: invalid option -- q +var:'?' OPTIND:2 +var:'w' OPTIND:3 +var:'e' OPTIND:4 +exited: var:'?' OPTIND:4 diff --git a/shell/hush_test/hush-getopts/getopt_positional.tests b/shell/hush_test/hush-getopts/getopt_positional.tests new file mode 100755 index 000000000..a5404a2a0 --- /dev/null +++ b/shell/hush_test/hush-getopts/getopt_positional.tests @@ -0,0 +1,8 @@ +set -- -q -w -e r -t -y +echo "*** no OPTIND, optstring:'we' args:$*" +var=QWERTY +while getopts "we" var; do + echo "var:'$var' OPTIND:$OPTIND" +done +# unfortunately, "rc:0" is shown since while's overall exitcode is "success" +echo "exited: var:'$var' OPTIND:$OPTIND" diff --git a/shell/hush_test/hush-getopts/getopt_simple.right b/shell/hush_test/hush-getopts/getopt_simple.right new file mode 100644 index 000000000..b4855fa1a --- /dev/null +++ b/shell/hush_test/hush-getopts/getopt_simple.right @@ -0,0 +1,34 @@ +*** no OPTIND, optstring:'ab' args:-a -b c +var:'a' OPTIND:2 +var:'b' OPTIND:3 +exited: rc:0 var:'?' OPTIND:3 +*** OPTIND=1, optstring:'ab' args:-a -b c +var:'a' OPTIND:2 +var:'b' OPTIND:3 +exited: rc:0 var:'?' OPTIND:3 +*** OPTIND=0, optstring:'ab' args:-a -b c +var:'a' OPTIND:2 +var:'b' OPTIND:3 +exited: rc:0 var:'?' OPTIND:3 +*** unset OPTIND, optstring:'ab' args:-a -b c +var:'a' OPTIND:2 +var:'b' OPTIND:3 +exited: rc:0 var:'?' OPTIND:3 +*** optstring:'ab' args:-a -b c +1 rc:0 var:'a' OPTIND:2 +2 rc:0 var:'b' OPTIND:3 +3 rc:1 var:'?' OPTIND:3 +*** unset OPTIND, optstring:'ab' args:-a c -c -b d +var:'a' OPTIND:2 +exited: rc:0 var:'?' OPTIND:2 +*** unset OPTIND, optstring:'ab' args:-a -c -b d +var:'a' OPTIND:2 +./getopt_simple.tests: invalid option -- c +var:'?' OPTIND:3 +var:'b' OPTIND:4 +exited: rc:0 var:'?' OPTIND:4 +*** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d +var:'a' OPTIND:2 +var:'?' OPTIND:3 +var:'b' OPTIND:4 +exited: rc:0 var:'?' OPTIND:4 diff --git a/shell/hush_test/hush-getopts/getopt_simple.tests b/shell/hush_test/hush-getopts/getopt_simple.tests new file mode 100755 index 000000000..8615ae366 --- /dev/null +++ b/shell/hush_test/hush-getopts/getopt_simple.tests @@ -0,0 +1,75 @@ +# Simple usage cases for getopts. +# +# OPTIND is either not touched at all (first loop with getopts, +# relying on shell startup init), or getopts state is reset +# before new loop with "unset OPTIND", "OPTIND=1" or "OPTIND=0". +# +# Each option is a separate argument (no "-abc"). This conceptually +# needs only $OPTIND to hold getopts state. +# +# We check that loop does not stop on unknown option (sets "?"), +# stops on _first_ non-option argument. + +echo "*** no OPTIND, optstring:'ab' args:-a -b c" +var=QWERTY +while getopts "ab" var -a -b c; do + echo "var:'$var' OPTIND:$OPTIND" +done +# unfortunately, "rc:0" is shown since while's overall exitcode is "success" +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# Resetting behavior =1 +echo "*** OPTIND=1, optstring:'ab' args:-a -b c" +OPTIND=1 +while getopts "ab" var -a -b c; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# Resetting behavior =0 +echo "*** OPTIND=0, optstring:'ab' args:-a -b c" +OPTIND=0 +while getopts "ab" var -a -b c; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# Resetting behavior "unset" +echo "*** unset OPTIND, optstring:'ab' args:-a -b c" +unset OPTIND +while getopts "ab" var -a -b c; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# What is the final exitcode? +echo "*** optstring:'ab' args:-a -b c" +unset OPTIND +getopts "ab" var -a -b c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND" +getopts "ab" var -a -b c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND" +getopts "ab" var -a -b c; echo "3 rc:$? var:'$var' OPTIND:$OPTIND" + +# Where would it stop? c or -c? +echo "*** unset OPTIND, optstring:'ab' args:-a c -c -b d" +unset OPTIND +while getopts "ab" var -a c -c -b d; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# What happens on unknown option? +echo "*** unset OPTIND, optstring:'ab' args:-a -c -b d" +unset OPTIND +while getopts "ab" var -a -c -b d; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# ORTERR=0 suppresses error message? +echo "*** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d" +unset OPTIND +OPTERR=0 +while getopts "ab" var -a -c -b d; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |