From 74d40589288890fffea437ed2f38ad1f2dc740e5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 11 Aug 2017 01:32:46 +0200 Subject: 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 --- shell/hush_test/hush-getopts/getopt_simple.tests | 75 ++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 shell/hush_test/hush-getopts/getopt_simple.tests (limited to 'shell/hush_test/hush-getopts/getopt_simple.tests') 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 0000000..8615ae3 --- /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" -- cgit v1.1