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 | |
parent | 4628945cd8d4679912f126d5f18f954210abb7d0 (diff) | |
download | busybox-w32-74d40589288890fffea437ed2f38ad1f2dc740e5.tar.gz busybox-w32-74d40589288890fffea437ed2f38ad1f2dc740e5.tar.bz2 busybox-w32-74d40589288890fffea437ed2f38ad1f2dc740e5.zip |
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')
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 @@ | |||
1 | *** no OPTIND, optstring:'we' args:-q -w -e r -t -y | ||
2 | ./getopt_positional.tests: invalid option -- q | ||
3 | var:'?' OPTIND:2 | ||
4 | var:'w' OPTIND:3 | ||
5 | var:'e' OPTIND:4 | ||
6 | 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 @@ | |||
1 | set -- -q -w -e r -t -y | ||
2 | echo "*** no OPTIND, optstring:'we' args:$*" | ||
3 | var=QWERTY | ||
4 | while getopts "we" var; do | ||
5 | echo "var:'$var' OPTIND:$OPTIND" | ||
6 | done | ||
7 | # unfortunately, "rc:0" is shown since while's overall exitcode is "success" | ||
8 | 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 @@ | |||
1 | *** no OPTIND, optstring:'ab' args:-a -b c | ||
2 | var:'a' OPTIND:2 | ||
3 | var:'b' OPTIND:3 | ||
4 | exited: rc:0 var:'?' OPTIND:3 | ||
5 | *** OPTIND=1, optstring:'ab' args:-a -b c | ||
6 | var:'a' OPTIND:2 | ||
7 | var:'b' OPTIND:3 | ||
8 | exited: rc:0 var:'?' OPTIND:3 | ||
9 | *** OPTIND=0, optstring:'ab' args:-a -b c | ||
10 | var:'a' OPTIND:2 | ||
11 | var:'b' OPTIND:3 | ||
12 | exited: rc:0 var:'?' OPTIND:3 | ||
13 | *** unset OPTIND, optstring:'ab' args:-a -b c | ||
14 | var:'a' OPTIND:2 | ||
15 | var:'b' OPTIND:3 | ||
16 | exited: rc:0 var:'?' OPTIND:3 | ||
17 | *** optstring:'ab' args:-a -b c | ||
18 | 1 rc:0 var:'a' OPTIND:2 | ||
19 | 2 rc:0 var:'b' OPTIND:3 | ||
20 | 3 rc:1 var:'?' OPTIND:3 | ||
21 | *** unset OPTIND, optstring:'ab' args:-a c -c -b d | ||
22 | var:'a' OPTIND:2 | ||
23 | exited: rc:0 var:'?' OPTIND:2 | ||
24 | *** unset OPTIND, optstring:'ab' args:-a -c -b d | ||
25 | var:'a' OPTIND:2 | ||
26 | ./getopt_simple.tests: invalid option -- c | ||
27 | var:'?' OPTIND:3 | ||
28 | var:'b' OPTIND:4 | ||
29 | exited: rc:0 var:'?' OPTIND:4 | ||
30 | *** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d | ||
31 | var:'a' OPTIND:2 | ||
32 | var:'?' OPTIND:3 | ||
33 | var:'b' OPTIND:4 | ||
34 | 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 @@ | |||
1 | # Simple usage cases for getopts. | ||
2 | # | ||
3 | # OPTIND is either not touched at all (first loop with getopts, | ||
4 | # relying on shell startup init), or getopts state is reset | ||
5 | # before new loop with "unset OPTIND", "OPTIND=1" or "OPTIND=0". | ||
6 | # | ||
7 | # Each option is a separate argument (no "-abc"). This conceptually | ||
8 | # needs only $OPTIND to hold getopts state. | ||
9 | # | ||
10 | # We check that loop does not stop on unknown option (sets "?"), | ||
11 | # stops on _first_ non-option argument. | ||
12 | |||
13 | echo "*** no OPTIND, optstring:'ab' args:-a -b c" | ||
14 | var=QWERTY | ||
15 | while getopts "ab" var -a -b c; do | ||
16 | echo "var:'$var' OPTIND:$OPTIND" | ||
17 | done | ||
18 | # unfortunately, "rc:0" is shown since while's overall exitcode is "success" | ||
19 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
20 | |||
21 | # Resetting behavior =1 | ||
22 | echo "*** OPTIND=1, optstring:'ab' args:-a -b c" | ||
23 | OPTIND=1 | ||
24 | while getopts "ab" var -a -b c; do | ||
25 | echo "var:'$var' OPTIND:$OPTIND" | ||
26 | done | ||
27 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
28 | |||
29 | # Resetting behavior =0 | ||
30 | echo "*** OPTIND=0, optstring:'ab' args:-a -b c" | ||
31 | OPTIND=0 | ||
32 | while getopts "ab" var -a -b c; do | ||
33 | echo "var:'$var' OPTIND:$OPTIND" | ||
34 | done | ||
35 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
36 | |||
37 | # Resetting behavior "unset" | ||
38 | echo "*** unset OPTIND, optstring:'ab' args:-a -b c" | ||
39 | unset OPTIND | ||
40 | while getopts "ab" var -a -b c; do | ||
41 | echo "var:'$var' OPTIND:$OPTIND" | ||
42 | done | ||
43 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
44 | |||
45 | # What is the final exitcode? | ||
46 | echo "*** optstring:'ab' args:-a -b c" | ||
47 | unset OPTIND | ||
48 | getopts "ab" var -a -b c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND" | ||
49 | getopts "ab" var -a -b c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND" | ||
50 | getopts "ab" var -a -b c; echo "3 rc:$? var:'$var' OPTIND:$OPTIND" | ||
51 | |||
52 | # Where would it stop? c or -c? | ||
53 | echo "*** unset OPTIND, optstring:'ab' args:-a c -c -b d" | ||
54 | unset OPTIND | ||
55 | while getopts "ab" var -a c -c -b d; do | ||
56 | echo "var:'$var' OPTIND:$OPTIND" | ||
57 | done | ||
58 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
59 | |||
60 | # What happens on unknown option? | ||
61 | echo "*** unset OPTIND, optstring:'ab' args:-a -c -b d" | ||
62 | unset OPTIND | ||
63 | while getopts "ab" var -a -c -b d; do | ||
64 | echo "var:'$var' OPTIND:$OPTIND" | ||
65 | done | ||
66 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
67 | |||
68 | # ORTERR=0 suppresses error message? | ||
69 | echo "*** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d" | ||
70 | unset OPTIND | ||
71 | OPTERR=0 | ||
72 | while getopts "ab" var -a -c -b d; do | ||
73 | echo "var:'$var' OPTIND:$OPTIND" | ||
74 | done | ||
75 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||