Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 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 | |
Denys Vlasenko | 007ce9f | 2017-08-13 02:59:00 +0200 | [diff] [blame] | 13 | ( |
| 14 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 15 | echo "*** no OPTIND, optstring:'ab' args:-a -b c" |
| 16 | var=QWERTY |
| 17 | while getopts "ab" var -a -b c; do |
| 18 | echo "var:'$var' OPTIND:$OPTIND" |
| 19 | done |
| 20 | # unfortunately, "rc:0" is shown since while's overall exitcode is "success" |
| 21 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 22 | |
| 23 | # Resetting behavior =1 |
| 24 | echo "*** OPTIND=1, optstring:'ab' args:-a -b c" |
| 25 | OPTIND=1 |
| 26 | while getopts "ab" var -a -b c; do |
| 27 | echo "var:'$var' OPTIND:$OPTIND" |
| 28 | done |
| 29 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 30 | |
| 31 | # Resetting behavior =0 |
| 32 | echo "*** OPTIND=0, optstring:'ab' args:-a -b c" |
| 33 | OPTIND=0 |
| 34 | while getopts "ab" var -a -b c; do |
| 35 | echo "var:'$var' OPTIND:$OPTIND" |
| 36 | done |
| 37 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 38 | |
| 39 | # Resetting behavior "unset" |
| 40 | echo "*** unset OPTIND, optstring:'ab' args:-a -b c" |
| 41 | unset OPTIND |
| 42 | while getopts "ab" var -a -b c; do |
| 43 | echo "var:'$var' OPTIND:$OPTIND" |
| 44 | done |
| 45 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 46 | |
| 47 | # What is the final exitcode? |
| 48 | echo "*** optstring:'ab' args:-a -b c" |
| 49 | unset OPTIND |
| 50 | getopts "ab" var -a -b c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND" |
| 51 | getopts "ab" var -a -b c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND" |
| 52 | getopts "ab" var -a -b c; echo "3 rc:$? var:'$var' OPTIND:$OPTIND" |
| 53 | |
| 54 | # Where would it stop? c or -c? |
| 55 | echo "*** unset OPTIND, optstring:'ab' args:-a c -c -b d" |
| 56 | unset OPTIND |
| 57 | while getopts "ab" var -a c -c -b d; do |
| 58 | echo "var:'$var' OPTIND:$OPTIND" |
| 59 | done |
| 60 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 61 | |
| 62 | # What happens on unknown option? |
| 63 | echo "*** unset OPTIND, optstring:'ab' args:-a -c -b d" |
| 64 | unset OPTIND |
| 65 | while getopts "ab" var -a -c -b d; do |
| 66 | echo "var:'$var' OPTIND:$OPTIND" |
| 67 | done |
| 68 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 69 | |
| 70 | # ORTERR=0 suppresses error message? |
| 71 | echo "*** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d" |
| 72 | unset OPTIND |
| 73 | OPTERR=0 |
| 74 | while getopts "ab" var -a -c -b d; do |
| 75 | echo "var:'$var' OPTIND:$OPTIND" |
| 76 | done |
| 77 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
Denys Vlasenko | 007ce9f | 2017-08-13 02:59:00 +0200 | [diff] [blame] | 78 | |
| 79 | ) 2>&1 \ |
| 80 | | sed -e 's/ unrecognized option: / invalid option -- /' \ |
| 81 | -e 's/ illegal option -- / invalid option -- /' \ |