-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopt
153 lines (139 loc) · 4.08 KB
/
copt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#! .desc:
# Match `-`-delimited short/long option with a mandatory option argument
#! .params (<1+>):
# ["$1"] - -* (short)
# ["$2"] - --* (long)
#! .uses.var:
# <opt> $ - string;
# first positional parameter
# <opt_arg> $ - string;
# second positional parameter
#! .gives:
# (0) <"$opt_arg"> - string;
# option argument
# (0) <"$opt_match"> - string;
# literal option matched
# (0) <$opt_shift> - integer;
# shift count for option
#! .sets:
# (0) <opt> $ - '';
#! .rc:
# (0) success
# (1) no match
#! .ec:
# (2) bad argument specification
# (255) bad input
#! .caveats:
# > `INT_MAX`.
#.
copt() {
# Assert the usage of the function is appropriate (else `return 1`) or
# correct (else `exit 255`) and set the appropriate short/long option
# variant as $1 to simplify further operations.
case "$opt" in
'--'*)
[ "${#opt}" -gt 2 ] || return 1
case "$2" in
'--'*)
[ "${#2}" -gt 2 ] || exit 255
set -- "$2"
;;
*)
[ ! "$2" ] || exit 255
case "$1" in
'--'*)
[ "${#1}" -gt 2 ] || exit 255
;;
'-'*)
return 1
;;
*)
exit 255
;;
esac
;;
esac
;;
'-'*)
[ "${#opt}" -gt 1 ] || return 1
case "$1" in
'--'*)
return 1
;;
'-'*)
[ "${#1}" = 2 ] || exit 255
;;
*)
exit 255
;;
esac
;;
*)
return 1
;;
esac
# Try to match a short/long option. The second positional parameter is
# presumably its argument.
case "$opt" in
"$1")
# Assert the second positional parameter is present.
if [ ! "$opt_arg" ]; then
printf "%s: Option '%s' requires an argument.\n" \
"${0##*/}" "$1" >&2
printf "%s: Try '%s --help' for more information.\n" \
"${0##*/}" "${0##*/}" >&2
exit 2
fi
opt_match="$1"
opt_shift=2
opt=
return 0
;;
esac
# Try to match a long option with an argument.
case "$1" in
'--'*)
# Assert any potential argument specification is valid. A long
# option cannot end with `=` after being matched, otherwise it's
# invalid; example: `--foo=`.
case "$opt" in
"$1"'=')
printf "%s: Invalid argument specification for: '%s'\n" \
"${0##*/}" "$1" >&2
printf "%s: Try '%s --help' for more information.\n" \
"${0##*/}" "${0##*/}" >&2
exit 2
;;
esac
# Try to match a long option with an argument separated by `=` on
# the same positional parameter; example: `--foo=bar`.
case "$opt" in
"$1"'='*)
opt_arg="${opt#"$1"=}"
opt_match="$1"
opt_shift=1
opt=
return 0
;;
esac
return 1
;;
esac
# Try to match a short option with an argument.
case "$1" in
'-'*)
# Try to match a short option together with its argument; example
# with short option `-a` and argument `foo`: `-afoo`.
case "$opt" in
"$1"*)
opt_arg="${opt#"$1"}"
opt_match="$1"
opt_shift=1
opt=
return 0
;;
esac
;;
esac
return 1
}