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