Skip to content

Commit bb8d326

Browse files
committed
Document parsing in README.md
1 parent 2f309f2 commit bb8d326

File tree

1 file changed

+100
-3
lines changed

1 file changed

+100
-3
lines changed

README.md

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,112 @@ in the `init` of your args object after declaring all of your parsed
202202
properties.
203203

204204

205-
<!--
206205
## Parsing
207206

208-
TODO: write a brief explanation of how parsing works
207+
Parsing of command-line arguments is performed sequentially. So long as
208+
option-processing is enabled, each not-yet-processed command-line argument that
209+
starts with a hyphen (`-`) is treated as an option.
210+
211+
### Option Syntaxes
212+
213+
- Short options start with a single hyphen. If the option takes an argument, the
214+
argument can either be appended:
215+
216+
```bash
217+
# "-o" with argument "ARGUMENT"
218+
my_program -oARGUMENT
219+
```
220+
221+
or can be the following command-line argument:
222+
223+
```bash
224+
# "-o" with argument "ARGUMENT"
225+
my_program -o ARGUMENT
226+
```
227+
228+
Zero argument short options can also be appended to each other without
229+
intermediate hyphens:
230+
231+
```bash
232+
# "-x", "-y" and "-z" options
233+
my_program -xyz
234+
```
235+
236+
An option that accepts arguments is also allowed at the end of such a chain:
237+
238+
```bash
239+
# "-x", "-y" and "-z" options, with argument for "-z"
240+
my_program -xyzARGUMENT
241+
```
242+
243+
- Long options start with a double hyphen (`--`). An argument to a long option
244+
can
245+
either be delimited with an equal sign:
246+
247+
```bash
248+
# "--foo" with argument "ARGUMENT"
249+
my_program --foo=ARGUMENT
250+
```
251+
252+
or can be the following command-line argument:
209253

254+
```bash
255+
# "--foo" with argument "ARGUMENT"
256+
my_program --foo ARGUMENT
257+
```
258+
259+
Multi-argument options are supported, though not by any of the convenience
260+
methods. Option-arguments after the first must be separate command-line
261+
arguments, for both an long and short forms of an option.
262+
263+
### Positional Arguments
264+
265+
In GNU mode (the default), options can be interspersed with positional
266+
arguments, but in POSIX mode the first positional argument that is encountered
267+
disables option processing for the remaining arguments. In either mode, if the
268+
argument "--" is encountered while option processing is enabled, then option
269+
processing is for the rest of the command-line. Once the options and
270+
option-arguments have been eliminated, what remains are considered to be
271+
positional arguments.
272+
273+
Each positional argument delegate can specify a minimum and maximum number of
274+
arguments it is willing to collect.
275+
276+
The positional arguments are distributed to the delegates by allocating each
277+
positional delegate at least as many arguments as it requires. If more than the
278+
minimum number of positional arguments have been supplied then additional arguments
279+
will be allocated to the first delegate up to its maximum, then the second, and so
280+
on, until all arguments have been allocated to a delegate.
281+
282+
This makes it easy to create a program that behaves like `grep`:
283+
284+
```kotlin
285+
class Args(parser: ArgParser) {
286+
// accept 1 regex followed by n filenames
287+
val regex by parser.positional("REGEX",
288+
help = "regular expression to search for")
289+
val files by parser.positionalList("FILE",
290+
help = "file to search in")
291+
}
292+
```
293+
294+
And equally easy to create a program that behaves like `cp`:
210295

296+
```kotlin
297+
class Args(parser: ArgParser) {
298+
// accept n source files followed by 1 destination
299+
val sources by parser.positionalList("SOURCE",
300+
help = "source file")
301+
val destination by parser.positional("DEST",
302+
help = "destination file")
303+
}
304+
```
305+
306+
307+
<!--
211308
## Help Formatting
212309
213-
TODO: write an explanation of help formatting once implemented
310+
TODO: write an explanation of help formatting
214311
-->
215312

216313

0 commit comments

Comments
 (0)