Mercurial > hgrepos > FreeBSD > ports > sysutils > local-bsdtools
comparison tests/builtin_getopts/testsetup.sh @ 707:cc06815e5504
Add some tests about the behaviour of FreeBSD's /bin/sh builtin getopts().
It also has the "silent" option -- but it is undocumented.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Wed, 02 Oct 2024 19:50:37 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 706:45051412d0b8 | 707:cc06815e5504 |
|---|---|
| 1 #!/bin/sh | |
| 2 # | |
| 3 # Test the behaviour of the shell's builtin `getopts` | |
| 4 # | |
| 5 | |
| 6 #: | |
| 7 #: Reset all the global variables thar are used by `getopts` | |
| 8 #: | |
| 9 #: Output (Globals): | |
| 10 #: OPTIND (int): | |
| 11 #: OPTARG: | |
| 12 #: | |
| 13 reset_getopts() { | |
| 14 OPTIND=1 | |
| 15 OPTARG= | |
| 16 # XXX FIXME: Is this used in FreeBSD's /bin/sh | |
| 17 # OPTERR= | |
| 18 | |
| 19 # The test variable | |
| 20 opt='' | |
| 21 | |
| 22 return 0 | |
| 23 } | |
| 24 | |
| 25 | |
| 26 test_standard_1() { | |
| 27 while getopts 'ab:' opt; do | |
| 28 case "$opt" in | |
| 29 a) | |
| 30 echo "OPTION: a with arg: \`${OPTARG+SET}'" | |
| 31 ;; | |
| 32 b) | |
| 33 echo "OPTION: b with arg: \`${OPTARG}'" | |
| 34 ;; | |
| 35 \?) | |
| 36 # All errors are reported here | |
| 37 if [ -n "${OPTARG+SET}" ] ; then | |
| 38 echo "ERROR: unknown option: ${OPTARG}" >&2 | |
| 39 else | |
| 40 echo "ERROR: unknown option" >&2 | |
| 41 fi | |
| 42 ;; | |
| 43 :) | |
| 44 # Never landing here | |
| 45 if [ -n "${OPTARG+SET}" ] ; then | |
| 46 echo "ERROR: unknown option (colon): ${OPTARG}" >&2 | |
| 47 else | |
| 48 echo "ERROR: unknown option (colon)" >&2 | |
| 49 fi | |
| 50 ;; | |
| 51 *) | |
| 52 echo "ERROR: option handling" >&2 | |
| 53 ;; | |
| 54 esac | |
| 55 done | |
| 56 echo "OPTIND: ${OPTIND}" | |
| 57 } | |
| 58 | |
| 59 | |
| 60 #: | |
| 61 #: Silent mode with a colon ``:`` as the first character in optstring. | |
| 62 #: | |
| 63 #: This is undocumented in current FreeBSD's :command:`/bin/sh`. | |
| 64 #: But it works as in :command:`/usr/local/bin/ksh93`. | |
| 65 #: | |
| 66 test_noreport_1() { | |
| 67 # Note: The first char in optstring is a colon! | |
| 68 while getopts ':ab:' opt; do | |
| 69 case "$opt" in | |
| 70 a) | |
| 71 echo "OPTION: a with arg: \`${OPTARG+SET}'" | |
| 72 ;; | |
| 73 b) | |
| 74 echo "OPTION: b with arg: \`${OPTARG}'" | |
| 75 ;; | |
| 76 \?) | |
| 77 # An invalid option is reported here | |
| 78 if [ -n "${OPTARG+SET}" ] ; then | |
| 79 echo "ERROR: unknown option: ${OPTARG}" >&2 | |
| 80 else | |
| 81 echo "ERROR: unknown option" >&2 | |
| 82 fi | |
| 83 ;; | |
| 84 :) | |
| 85 # A missing required parameter is reported here | |
| 86 if [ -n "${OPTARG+SET}" ] ; then | |
| 87 echo "ERROR: unknown option (colon): ${OPTARG}" >&2 | |
| 88 else | |
| 89 echo "ERROR: unknown option (colon)" >&2 | |
| 90 fi | |
| 91 ;; | |
| 92 *) | |
| 93 echo "ERROR: option handling" >&2 | |
| 94 ;; | |
| 95 esac | |
| 96 done | |
| 97 echo "OPTIND: ${OPTIND}" | |
| 98 } |
