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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2023 WATANABE Yuki
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Getopts built-in
//!
//! The **`getopts`** built-in is used to parse options in shell scripts.
//!
//! # Synopsis
//!
//! ```sh
//! getopts option_spec variable_name [argument…]
//! ```
//!
//! # Description
//!
//! The getopts built-in parses single-character options in the specified
//! arguments according to the specified option specification, and assigns the
//! parsed options to the specified variable. This built-in is meant to be used
//! in the condition of a `while` loop to iterate over the options in the
//! arguments. Every invocation of the built-in parses the next option in the
//! arguments. The built-in returns a non-zero exit status when there are no more
//! options to parse.
//!
//! The shell uses the `$OPTIND` variable to keep track of the current position
//! in the arguments. When the shell starts, the variable is initialized to `1`.
//! The built-in updates the variable to the index of the next argument to parse.
//! When all arguments are parsed, the built-in sets the variable to the index of
//! the first operand after the options, or to the number of arguments plus one
//! if there are no operands.
//!
//! When the built-in parses an option, it sets the specified variable to the
//! option name. If the option takes an argument, the built-in also sets the
//! `$OPTARG` variable to the argument.
//!
//! If the built-in encounters an option that is not listed in the option
//! specification, the specified variable is set to `?`. Additionally, if the
//! option specification starts with a colon (`:`), the built-in sets the
//! `$OPTARG` variable to the encountered option character. Otherwise, the
//! built-in unsets the `$OPTARG` variable and prints an error message to the
//! standard error describing the invalid option.
//!
//! If the built-in encounters an option that takes an argument but the argument
//! is missing, the error handling is similar to the case of an invalid option.
//! If the option specification starts with a colon, the built-in sets the
//! the specified variable to `:` (not `?`) and sets the `$OPTARG` variable to
//! the option character. Otherwise, the built-in sets the specified variable to
//! `?`, unsets the `$OPTARG` variable, and prints an error message to the
//! standard error describing the missing argument.
//!
//! When there are no more options to parse, the specified variable is set to
//! `?` and the `$OPTARG` variable is unset.
//!
//! In repeated invocations of the built-in, you must pass the same arguments to
//! the built-in. You must not modify the `$OPTIND` variable between
//! invocations, either. Otherwise, the built-in may not be able to parse the
//! options correctly.
//!
//! To start parsing a new set of options, you must reset the `$OPTIND` variable
//! to `1` before invoking the built-in.
//!
//! # Options
//!
//! None.
//!
//! # Operands
//!
//! The first operand (***option_spec***) is the option specification. It is a
//! string that contains the option characters the built-in parses. If a
//! character is followed by a colon (`:`), the option takes an argument. If the
//! option specification starts with a colon, the built-in does not print an
//! error message when it encounters an invalid option or an option that is
//! missing an argument.
//!
//! The second operand (***variable_name***) is the name of the variable to
//! which the built-in assigns the parsed option. In case of an invalid option
//! or an option that is missing an argument, the built-in assigns `?` or `:` to
//! the variable (see above).
//!
//! The remaining operands (***argument…***) are the arguments to parse.
//! If there are no operands, the built-in parses the positional parameters.
//!
//! # Errors
//!
//! The built-in may print an error message to the standard error when it
//! encounters an invalid option or an option that is missing an argument (see
//! the description above). However, this is not considered an error of the
//! built-in itself.
//!
//! The following conditions are considered errors of the built-in:
//!
//! - The built-in is invoked with less than two operands.
//! - `$OPTIND`, `$OPTARG`, or the specified variable is read-only.
//! - The built-in is re-invoked with different arguments or a different value
//! of `$OPTIND` than the previous invocation (except when `$OPTIND` is reset
//! to `1`).
//! - The value of `$OPTIND` is not `1` on the first invocation.
//!
//! # Exit status
//!
//! The built-in returns an exit status of zero if it parses an option,
//! regardless of whether the option is valid or not. When there are no more
//! options to parse, the built-in returns a non-zero exit status.
//!
//! The exit status is non-zero on error.
//!
//! # Examples
//!
//! In the following example, the getopts built-in parses three kinds of options
//! (`-a`, `-b`, and `-c`), of which only `-b` takes an argument. In case of an
//! error, the built-in prints an error message to the standard error, so the
//! script just exits with a non-zero exit status when `$opt` is set to `?`.
//!
//! ```sh
//! a=false c=false
//! while getopts ab:c opt; do
//! case "$opt" in
//! a) a=true ;;
//! b) b="$OPTARG" ;;
//! c) c=true ;;
//! '?') exit 1 ;;
//! esac
//! done
//! shift "$((OPTIND - 1))"
//!
//! if "$a"; then printf 'The -a option was specified\n'; fi
//! if [ "${b+set}" ]; then printf 'The -b option was specified with argument %s\n' "$b"; fi
//! if "$c"; then printf 'The -c option was specified\n'; fi
//! printf 'The remaining operands are: %s\n' "$*"
//! ```
//!
//! If you prefer to print an error message yourself, put a colon at the
//! beginning of the option specification like this:
//!
//! ```sh
//! while getopts :ab:c opt; do
//! case "$opt" in
//! a) a=true ;;
//! b) b="$OPTARG" ;;
//! c) c=true ;;
//! '?') printf 'Invalid option: -%s\n' "$OPTARG" >&2; exit 1 ;;
//! :) printf 'Option -%s requires an argument\n' "$OPTARG" >&2; exit 1 ;;
//! esac
//! done
//! ```
//!
//! # Portability
//!
//! The getopts built-in is specified by POSIX. Only ASCII alphanumeric
//! characters are allowed for option names, though this implementation allows
//! any characters but `:`.
//!
//! Although POSIX requires the built-in to support the Utility Syntax
//! Guidelines 3 to 10, some implementations do not support the `--` separator
//! placed before operands to the built-in itself, that is, between the built-in
//! name `getopts` and the first operand *option_spec*.
//!
//! The value of the `$OPTIND` variable is not portable until the built-in
//! finishes parsing all options. In this implementation, the value may
//! temporarily contain two integers separated by a colon. The first integer is
//! the index of the next argument to parse, and the second is the index of the
//! character in the argument to parse. Other implementations may use a
//! different scheme. Some sets `$OPTIND` to the index of the just-parsed
//! argument and uses a hidden variable to keep track of the character index.
//!
//! The behavior is unspecified if you modify the `$OPTIND` variable between
//! invocations of the built-in or to a value other than `1`.
//!
//! # Implementation notes
//!
//! This implementation uses the `getopts_state` field in the [`Env`] to check
//! if the built-in is invoked with the same arguments and `$OPTIND` as the
//! previous invocation.
use cratereport_error;
use cratereport_simple_error;
use crateparse_arguments;
use crateMode;
use ;
use NonZeroUsize;
use Origin;
use ExitStatus;
use Field;
use OPTIND;
use Env;
/// Computes the `arg_index` and `char_index` parameters of the
/// [`next`](self::model::next) function from the `$OPTIND` value.
/// Computes the `$OPTIND` value from the `arg_index` and `char_index`.
/// Entry point of the getopts built-in
pub async