|
| 1 | +### Allocation |
| 2 | + |
| 3 | +Allocation is what I call the first stage of parsing. It's where the various |
| 4 | +text values on the command line are allocated/assigned to collections |
| 5 | +keyed by option keys (and to one unkeyed collection). For details on how |
| 6 | +that works please refer to [this article](allocator.md). |
| 7 | + |
| 8 | +This article focuses on what a command line could look like an be |
| 9 | +allocated, and then parsed, successfully. To do so it helps to distinguish |
| 10 | +between options -- what the framework refers to as keyed options -- and |
| 11 | +plain old command line parameters. |
| 12 | + |
| 13 | +For a piece of text to be considered a (keyed) option it must be preceded by |
| 14 | +an **option key**, which is composed of two parts: an allowed prefix (e.g., -, -- |
| 15 | +or /) and some text, terminated by a terminator character. |
| 16 | + |
| 17 | +Spaces are always terminator characters unless they occur inside a piece of |
| 18 | +quoted text. So the space in `-x 1` terminates the option key while the |
| 19 | +second and third spaces in `-x "some quoted text"` are part of the text |
| 20 | +element's value and don't terminate anything. |
| 21 | + |
| 22 | +Every option must be preceded by an option key. Conversely, any text element |
| 23 | +not preceded by an option key is considered merely a value to be assigned |
| 24 | +to either a keyed option collection or the overall unkeyed option collection. |
| 25 | +There's only one unkeyed option collection for each parsing action. |
| 26 | + |
| 27 | +During the allocation phase all the parameters associated with the same |
| 28 | +option key are stored in the same collection. Multiple values do not need |
| 29 | +to be specified sequentially but each must be preceded by the same option |
| 30 | +key. |
| 31 | + |
| 32 | +For example, the allocator converts `-x abc -y -x def` into two collections, |
| 33 | +one keyed by **x** containing two values *abc* and *def* and one keyed by |
| 34 | +**y** containing no values (**y** is presumably a switch option). |
| 35 | + |
| 36 | +However, while the allocator converts `-x abc def -y` into the same two |
| 37 | +collections, the **x** collection in the second example *only* contains |
| 38 | +*abc*. The *def* parameter value is stored in the **unkeyed option collection**. |
| 39 | +That latter collection is where what are traditionally thought |
| 40 | +of as plain old command line parameters end up. They can be converted |
| 41 | +in the parsing phase to a collection of any type for which a text converter |
| 42 | +is defined. |
0 commit comments