Mercurial > hgrepos > FreeBSD > ports > sysutils > local-bsdtools
comparison tests/builtin_getopts/builtin_getopts.t @ 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 Tests for the behaviour of the Shell's `getopts` | |
| 2 | |
| 3 Shell is /bin/sh. | |
| 4 | |
| 5 Setup | |
| 6 ===== | |
| 7 | |
| 8 $ set -u | |
| 9 $ . "${TESTDIR}/testsetup.sh" | |
| 10 | |
| 11 | |
| 12 Standard | |
| 13 ======== | |
| 14 | |
| 15 $ test_standard_1 -a -b bbb1 | |
| 16 OPTION: a with arg: `' | |
| 17 OPTION: b with arg: `bbb1' | |
| 18 OPTIND: 4 | |
| 19 | |
| 20 The variable will be set to ``?`` after the last option when getopts returns 1: | |
| 21 | |
| 22 $ reset_getopts | |
| 23 $ test_standard_1 -a -b bbb1 | |
| 24 OPTION: a with arg: `' | |
| 25 OPTION: b with arg: `bbb1' | |
| 26 OPTIND: 4 | |
| 27 $ test "$opt" = '?' | |
| 28 | |
| 29 No option: | |
| 30 | |
| 31 $ reset_getopts | |
| 32 $ test_standard_1 arg1 arg2 args | |
| 33 OPTIND: 1 | |
| 34 $ test "$opt" = '?' | |
| 35 | |
| 36 OPTARG is **unset** at every invocation of `getopts` first: | |
| 37 | |
| 38 $ reset_getopts | |
| 39 $ test_standard_1 -a -b bbb1 -a | |
| 40 OPTION: a with arg: `' | |
| 41 OPTION: b with arg: `bbb1' | |
| 42 OPTION: a with arg: `' | |
| 43 OPTIND: 5 | |
| 44 | |
| 45 Option arguments are not checked for another option syntax: | |
| 46 | |
| 47 $ reset_getopts | |
| 48 $ test_standard_1 -a -b -a | |
| 49 OPTION: a with arg: `' | |
| 50 OPTION: b with arg: `-a' | |
| 51 OPTIND: 4 | |
| 52 | |
| 53 | |
| 54 Missing arguments: | |
| 55 | |
| 56 $ reset_getopts | |
| 57 $ test_standard_1 -b | |
| 58 No arg for -b option | |
| 59 ERROR: unknown option | |
| 60 OPTIND: 2 | |
| 61 $ test "$opt" = '?' | |
| 62 | |
| 63 No abort on errors: | |
| 64 | |
| 65 $ reset_getopts | |
| 66 $ test_standard_1 -u -a | |
| 67 Illegal option -u | |
| 68 ERROR: unknown option | |
| 69 OPTION: a with arg: `' | |
| 70 OPTIND: 3 | |
| 71 $ test "$opt" = '?' | |
| 72 | |
| 73 Abort processing with '--' | |
| 74 | |
| 75 $ reset_getopts | |
| 76 $ test_standard_1 -a -b xxx -- -a -b yyy | |
| 77 OPTION: a with arg: `' | |
| 78 OPTION: b with arg: `xxx' | |
| 79 OPTIND: 5 | |
| 80 $ test "$opt" = '?' | |
| 81 | |
| 82 '--' does not stop option processing when a required arg just before is missing | |
| 83 | |
| 84 $ reset_getopts | |
| 85 $ test_standard_1 -a -b -- -a -b yyy | |
| 86 OPTION: a with arg: `' | |
| 87 OPTION: b with arg: `--' | |
| 88 OPTION: a with arg: `' | |
| 89 OPTION: b with arg: `yyy' | |
| 90 OPTIND: 7 | |
| 91 | |
| 92 | |
| 93 No Error Reports | |
| 94 ================ | |
| 95 | |
| 96 This behaviour with a colon as the very first character in optstring is not | |
| 97 documented. But it works similar to ksh93. | |
| 98 | |
| 99 $ reset_getopts | |
| 100 $ test_noreport_1 -a -b bbb1 | |
| 101 OPTION: a with arg: `' | |
| 102 OPTION: b with arg: `bbb1' | |
| 103 OPTIND: 4 | |
| 104 | |
| 105 The variable will be set to ``?`` after the last option when getopts returns 1: | |
| 106 | |
| 107 $ reset_getopts | |
| 108 $ test_standard_1 -a -b bbb1 | |
| 109 OPTION: a with arg: `' | |
| 110 OPTION: b with arg: `bbb1' | |
| 111 OPTIND: 4 | |
| 112 $ test "$opt" = '?' | |
| 113 | |
| 114 No option: | |
| 115 | |
| 116 $ reset_getopts | |
| 117 $ test_noreport_1 arg1 arg2 args | |
| 118 OPTIND: 1 | |
| 119 $ test "$opt" = '?' | |
| 120 | |
| 121 OPTARG is **unset** at every invocation of `getopts` first: | |
| 122 | |
| 123 $ reset_getopts | |
| 124 $ test_noreport_1 -a -b bbb1 -a | |
| 125 OPTION: a with arg: `' | |
| 126 OPTION: b with arg: `bbb1' | |
| 127 OPTION: a with arg: `' | |
| 128 OPTIND: 5 | |
| 129 | |
| 130 Option arguments are not checked for another option syntax: | |
| 131 | |
| 132 $ reset_getopts | |
| 133 $ test_noreport_1 -a -b -a | |
| 134 OPTION: a with arg: `' | |
| 135 OPTION: b with arg: `-a' | |
| 136 OPTIND: 4 | |
| 137 | |
| 138 Here FreeBSD's `/bin/sh` behaves like `ksh93` | |
| 139 | |
| 140 Missing arguments are reported via ':' here: | |
| 141 | |
| 142 $ reset_getopts | |
| 143 $ test_noreport_1 -b | |
| 144 ERROR: unknown option (colon): b | |
| 145 OPTIND: 2 | |
| 146 $ test "$opt" = '?' | |
| 147 | |
| 148 No abort on errors -- unknown options are reported via '?' here: | |
| 149 | |
| 150 $ reset_getopts | |
| 151 $ test_noreport_1 -u -a | |
| 152 ERROR: unknown option: u | |
| 153 OPTION: a with arg: `' | |
| 154 OPTIND: 3 | |
| 155 $ test "$opt" = '?' | |
| 156 | |
| 157 Abort processing with '--' | |
| 158 | |
| 159 $ reset_getopts | |
| 160 $ test_noreport_1 -a -b xxx -- -a -b yyy | |
| 161 OPTION: a with arg: `' | |
| 162 OPTION: b with arg: `xxx' | |
| 163 OPTIND: 5 | |
| 164 | |
| 165 '--' does not stop option processing when a required arg just before is missing | |
| 166 | |
| 167 $ reset_getopts | |
| 168 $ test_noreport_1 -a -b -- -a -b yyy | |
| 169 OPTION: a with arg: `' | |
| 170 OPTION: b with arg: `--' | |
| 171 OPTION: a with arg: `' | |
| 172 OPTION: b with arg: `yyy' | |
| 173 OPTIND: 7 |
