| 1 |
# Design of RedCLAP |
| 2 |
|
| 3 |
RedCLAP means Redesigned CLAP and is direct descendant of the CLAP library. |
| 4 |
Its development started after numerous flaws, bugs and shortcomings in the original CLAP have been |
| 5 |
exposed and the design proved to be too complex to be fixed reasonably. |
| 6 |
|
| 7 |
From now on, both CLAP and RedCLAP refer the new version, unless otherwise specified (e.g. by saying old-CLAP). |
| 8 |
|
| 9 |
---- |
| 10 |
|
| 11 |
## Structure of input |
| 12 |
|
| 13 |
Input is made of modes, options and operands. Modes can be nested. |
| 14 |
|
| 15 |
Example: |
| 16 |
|
| 17 |
``` |
| 18 |
program --verbose modea --foo modeb --bar modec --baz 2 4 8 |
| 19 |
``` |
| 20 |
|
| 21 |
Here, program `program` has one top-level mode and two nested modes. |
| 22 |
Each mode has one argument-less option passed, and the last one has three operands. |
| 23 |
|
| 24 |
|
| 25 |
### Modes |
| 26 |
|
| 27 |
Modes are strings beginning with a letter and followed by a combination of letters, numbers, hyphens and underscores. |
| 28 |
Modes can be nested, and can have options and operands attached. |
| 29 |
|
| 30 |
All specified operands for modes are required to be passed, unless a nested mode appears, then they become optional. |
| 31 |
|
| 32 |
If both operands and nested modes are accepted by a mode, last non-option string is checked for its being a name of a |
| 33 |
nested mode. |
| 34 |
In case the match returns true, the string is assumed to be child-mode and rest of the args is passed to it for parsing. |
| 35 |
This can be switched off by passing `--` terminator before list of operans. |
| 36 |
|
| 37 |
``` |
| 38 |
program modea --spam foo bar modeb --with eggs |
| 39 |
``` |
| 40 |
|
| 41 |
In this example, `--spam` is option for `modea`, `foo bar` are its operands, and `modeb` is its child-mode which has its |
| 42 |
own option `--with` and operand `eggs`. |
| 43 |
|
| 44 |
Modes have: |
| 45 |
|
| 46 |
- local options, |
| 47 |
- global options, |
| 48 |
- child modes, |
| 49 |
- operands, |
| 50 |
|
| 51 |
|
| 52 |
### Options |
| 53 |
|
| 54 |
Standard options are local to their mode. |
| 55 |
Global options found in parent modes are passed to their child-modes. |
| 56 |
If a child-mode defines a global option of parent mode as *local* its propagation is stopped. |
| 57 |
|
| 58 |
Options have: |
| 59 |
|
| 60 |
- short name (single character preceded by single hyphen), |
| 61 |
- long name (two or more character preceded by two hyphens), |
| 62 |
- help message, |
| 63 |
- list of other options the current one *requires* to be passed with it (all must be present), |
| 64 |
- list of other options the current one *wants* to be passed with it (at least one must be present), |
| 65 |
- list of other options the current one *conflicts with* (no one of them must be present), |
| 66 |
- list of types of parameters for this option (may be empty), it should be a list of two-tuples: `(type, descriptive_name_for_help)`, |
| 67 |
- boolean flag telling whether this option is required to be passed or not, |
| 68 |
- list of options the current one is not required to be passed (only one of them may be present), |
| •69 |
- boolean flag specifying whether this option is singular or plural (plural option means that each occurrence has semantic meaning), |
| 70 |
|
| 71 |
|
| 72 |
### Operands |
| 73 |
|
| 74 |
Operands are whatever non-option-looking-strings are found after options and child-modes. |
| 75 |
|
| 76 |
List of operands begins with: |
| 77 |
|
| 78 |
- first non-option-looking string, or |
| 79 |
- first non-child-mode string, or |
| 80 |
- `--` terminator, |
| 81 |
|
| 82 |
List of operands ends when: |
| 83 |
|
| 84 |
- `---` terminator is encountered (everything after it is discarded if no child modes are set), |
| 85 |
- one of the algorithms detecing nested mode's presence returns true, |
| 86 |
|
| 87 |
If a mode has a defined list of types for its operands they are required. |
| 88 |
If a mode has an empty list of operands it accepts whatever operands are given to it (and may freely discard them). |
| 89 |
If a mode has boolean false in the place of list of operands it accepts no operands. |
| 90 |
|
| 91 |
**Examples:** |
| 92 |
|
| •93 |
Program in the examples has three options: |
| 94 |
|
| 95 |
- `-f` / `--foo`, |
| 96 |
- `-b` / `--bar`, |
| 97 |
- `-B` / `--baz`, |
| 98 |
|
| 99 |
``` |
| 100 |
program --foo --bar --baz spam with ham answer is 42 |
| 101 |
# Options: --foo --bar --baz |
| 102 |
# Operands: spam with ham answer is 42 |
| 103 |
|
| 104 |
program --foo --bar -- --baz spam with ham answer is 42 |
| 105 |
# Options: --foo --bar |
| 106 |
# Operands: --baz spam with ham answer is 42 |
| 107 |
|
| 108 |
program --foo --bar -- --baz spam with ham --- answer is 42 |
| 109 |
# Options: --foo --bar |
| 110 |
# Operands: --baz spam with ham |
| 111 |
# Discarded (after operands-close sign): answer is 42 |
| 112 |
``` |
| 113 |
|
| 114 |
In the last example, CLAP can act in three different ways depending on how the `programs`'s UI is created: |
| 115 |
|
| 116 |
- if the main mode has some child modes and `answer` is one of them, CLAP will continue parsing, |
| 117 |
- if the main mode has some child modes and `answer` is not one of them, CLAP will raise an exception (unknown mode), |
| 118 |
- if the main mode has no child modes, CLAP will raise an exception (unknown mode?, unused operands? - that's not yet decided), |
| 119 |
|
| 120 |
**NOTE: TODO: third point on the list above** |
| 121 |
|
| 122 |
> What to do with discarded operands? |
| 123 |
|
| 124 |
|
| 125 |
---- |
| 126 |
|
| 127 |
## Interface building |
| 128 |
|
| 129 |
Information about how CLAP UIs are designed to be built. |
| 130 |
|
| 131 |
### Operands |
| 132 |
|
| 133 |
UIs can take various numbers and types of operands. |
| •134 |
This is specified with `__operands__` directive in mode's JSON representation in UI description file. |
| 135 |
|
| 136 |
|
| 137 |
#### Operand types |
| 138 |
|
| 139 |
> NOTICE: this may be removed from design |
| 140 |
|
| 141 |
On the command line, all operands are strings. |
| 142 |
CLAP lets programmers define types into which these operands should be converted, same as for options. |
| 143 |
|
| 144 |
Converters for most basic data types - `str`, `int` and `float` - are always present. |
| 145 |
|
| 146 |
Converter function for `bool` type should be programmer-specified if needed. |
| 147 |
This is because `'False'` string will result in `True` boolean value if simply passed to `bool()` function; |
| 148 |
as such it requires a bit more sematic analysis, e.g. whether both `False` and `false` should be accepted, |
| 149 |
should `no` also mean false etc. |
| 150 |
|
| 151 |
Programmers can define their own, custom converter functions and use them to convert operands to any data-type they wish. |
| 152 |
Such functions MUST: |
| 153 |
|
| 154 |
- require exactly one parameter, |
| 155 |
- accept string as this parameter, |
| 156 |
- raise `ValueError` if the string has invalid form and the function is unable to convert it, |
| 157 |
|
| 158 |
These functions are attached to builder objects, and are referred to in the UI descriptions by the name |
| 159 |
under which the functions were attached, a custom string, not by the function name. |
| 160 |
|
| 161 |
|
| 162 |
#### Operand schemes |
| 163 |
|
| 164 |
The `operands` directive may include a *scheme* of operands. |
| 165 |
If no scheme is set, CLAP will accept any number and any type of operands. |
| 166 |
Otherwise, the set of operands given will be matched against the scheme present. |
| 167 |
|
| 168 |
|
| 169 |
##### Scheme layout |
| 170 |
|
| 171 |
``` |
| 172 |
{ |
| 173 |
... |
| 174 |
"operands": { |
| 175 |
"no": [<int>, <int>], |
| 176 |
"types": [<str>, <str>...] |
| 177 |
} |
| 178 |
} |
| 179 |
``` |
| 180 |
|
| 181 |
The `"no"` rule specifies number of operands accepted by the mode. |
| 182 |
The `"types"` rule specifies expected types of operands. |
| 183 |
|
| 184 |
##### Omission |
| 185 |
|
| 186 |
Both rules can be omitted. |
| 187 |
|
| 188 |
If *no* rule is omitted and *types* is not, number of operands must be divisible by the length of types list. |
| 189 |
List of operands will be divided into groups, and each group will have its members converted according to the |
| 190 |
specified types. |
| 191 |
|
| 192 |
If *no* rule is not omitted and *types* is, CLAP will accept any type of operands, and will only try to match against |
| 193 |
number rules. |
| 194 |
|
| 195 |
If *no* rule is omitted and *types* group is omitted, CLAP will accept any number and type of operands. |
| 196 |
Shorthand for this behaviour is specifying no scheme at all. |
| 197 |
|
| 198 |
|
| 199 |
##### `types` rule: defining expected types of operands |
| 200 |
|
| 201 |
Types of operands are defined by *types* rule. |
| 202 |
It is a list of converter-function names (i.e. list of strings). |
| 203 |
|
| 204 |
|
| 205 |
##### `no` rule: specifying accepted numbers of operands** |
| 206 |
|
| 207 |
Accepted numbers of operands are defined by *no* rule. |
| 208 |
It is a list of integers. |
| 209 |
CLAP will interpret this list's contents and, according to them, form matching rules. |
| 210 |
|
| 211 |
**`[]`** |
| 212 |
|
| 213 |
If list is empty, CLAP will accept any arguments. |
| 214 |
|
| 215 |
**`[<int>]`** |
| 216 |
|
| 217 |
If list contains one integer and it is not negative, CLAP will accept *at least* `<int>` operands. |
| 218 |
|
| 219 |
**`[-<int>]`** |
| 220 |
|
| 221 |
If list contains one integer and it is negative, CLAP will accept *at most* `<int>` operands. |
| 222 |
|
| 223 |
**`[<int>, <int>]`** |
| 224 |
|
| 225 |
If list contains two integers and both are not negative, CLAP will accept any number of operands between these two integers (inclusive). |
| 226 |
This means that `[0, 2]` sequence will cause CLAP to accept 0, 1 or 2 operands. |
| 227 |
The *no* rule can be set to `[0, 0]` to make CLAP accept no operands. |
| 228 |
If the first item is `None` it is converted to `0`. |
| 229 |
|
| 230 |
**`[-<int>, -<int>]` and other sequences** |
| 231 |
|
| 232 |
Sequences containing: |
| 233 |
|
| 234 |
- two integers that are not both positive, |
| 235 |
- three or more integers, |
| 236 |
|
| 237 |
are invalid. |
| 238 |
|
| 239 |
|
| 240 |
---- |
| 241 |
|
| 242 |
### Nested modes |
| 243 |
|
| 244 |
Modes can be nested. |
| 245 |
|
| 246 |
However, there is a problem due to the fact that nested modes appear *after* operands of their parent mode and |
| 247 |
sometimes it may be hard to distinguish what is an operand and what is nested node. |
| 248 |
Another problem that is immediately encountered is error reporting - when to report invalid number of operands and |
| 249 |
when an unknown node. |
| 250 |
|
| 251 |
These problem has two possible solutions: |
| 252 |
|
| 253 |
- to disallow operands in modes that are not the final leafs of a mode-tree, |
| 254 |
- to define rules specifying when, and when not, to check for child modes, |
| 255 |
|
| 256 |
#### Algorithm detecting nested modes |
| 257 |
|
| 258 |
Detection of nested modes is **not** performed when: |
| 259 |
|
| 260 |
- current mode has no child modes, |
| 261 |
- the `--` sybmol has appeared in the input but the `---` has not, |
| 262 |
- current mode has no upper range of operands, |
| 263 |
|
| 264 |
**Open problems, dilemmas with the algorithm:** |
| 265 |
|
| 266 |
- how to define when to stop iterating when range is not-fixed, |
| 267 |
- on first string above minimal number of accepted operands that can be accepted as child mode (*first safe match counts* strategy)? |
| 268 |
- on the very first string that can be accepted as child mode (*first match counts* strategy)? |
| 269 |
|
| 270 |
|
| 271 |
##### Rules and algorithm |
| 272 |
|
| 273 |
**NOTE:TODO** |
| 274 |
|
| 275 |
> These are just rules, algorithm is still being designed (first, in code) and |
| 276 |
> covered by unit tests. |
| 277 |
> When it's finished, it will be documented here. |
| 278 |
|
| 279 |
- if the first out-of-range or any above lower margin operand is valid child mode, then |
| 280 |
parsing continues with rules taken from this mode and it becomes nested mode (no error to report), |
| 281 |
- otherwise, if an operand looks like an option *and* the operand before it is a valid child mode, then |
| 282 |
the operand before is considered a nested mode (if it's below the lower margin this would cause an error about insufficient number of operands to |
| 283 |
be reported), |
| 284 |
- otherwise, if the first out-of-range operand is not a valid child mode *and* second out-of-range operand looks like an option, then |
| 285 |
the first out-of-range operand is considered nested mode (which will cause an error about unknown mode to be reported), |
| 286 |
- else, every out-of-range operand is considered an operand given to current mode, |
| 287 |
|
| 288 |
---- |
| 289 |
|
| 290 |
### Types |
| 291 |
|
| 292 |
Options can take arguments. |
| 293 |
These arguments must have a defined type as the number of arguments taken is length of the list of argument types. |
| 294 |
|
| 295 |
#### Built-in types |
| 296 |
|
| 297 |
Just as the original CLAP, RedCLAP supportsd these types by default: |
| 298 |
|
| 299 |
- `str`: string arguments, |
| 300 |
- `int`: decimal integers, |
| 301 |
- `float`: decimal floating point numbers, |
| 302 |
|
| 303 |
|
| 304 |
#### Custom types |
| 305 |
|
| 306 |
RedCLAP - just as original CLAP - supports custom types to be used for operands and options. |
| 307 |
Type converters MUST BE functions taking single string as their parameter and: |
| 308 |
|
| 309 |
- returning desired type upon successful conversion, |
| 310 |
- raising `ValueError` upon unsuccessful conversion, |
| 311 |
|
| 312 |
|
| 313 |
##### Adding custom type handlers |
| 314 |
|
| 315 |
Type handlers have to be added to every parser indivdually, via API of the parser object. |
| 316 |
|
| 317 |
---- |
| 318 |
|
| 319 |
## JSON representations of UIs |
| 320 |
|
| 321 |
RedCLAP UIs can be saved as JSON encoded files and |
| 322 |
built dynamically. |
| 323 |
This provides for easier interface building as a developer can create the UI structire in a declarative way and |
| 324 |
let the code do the heavy lifting. |
| 325 |
|
| 326 |
|
| 327 |
### Modes |
| 328 |
|
| 329 |
Example bare-bones (taking no options and having no sub-modes) UI written as JSON: |
| 330 |
|
| 331 |
``` |
| 332 |
{ |
| 333 |
"modes": [], |
| 334 |
"options": { |
| 335 |
"local": [], |
| 336 |
"global": [] |
| 337 |
}, |
| 338 |
"help": "" |
| 339 |
} |
| 340 |
``` |
| 341 |
|
| 342 |
Explanations: |
| 343 |
|
| 344 |
- `"mode"`: is a list of child modes (modes can be nested to any level of depth), |
| 345 |
- `"options"`: is a dictionary with two possible keys `local` and `global` (every other key is discarded), |
| 346 |
- `"local"`: is a list of local options (that *will not* be propagated to child modes), |
| 347 |
- `"global"`: is a list of global options (that *will* be propagated to child modes), |
| 348 |
- `"help"`: is a string containing help message for this mode, |
| 349 |
|
| 350 |
|
| 351 |
### Options |
| 352 |
|
| 353 |
Options are described in form of JSON dictionaries. |
| 354 |
|
| 355 |
All available keys are listed here: |
| 356 |
|
| 357 |
- `short` (*string*): short name of the option, |
| 358 |
- `long` (*string*): long name of the option, |
| 359 |
- `arguments` (*list of strings*): list of types of arguments the option takes, every argument is required, |
| 360 |
- `requires` (*list of strings*): list of options this option requires to be passed alongside it (input is valid only if all of them are found), |
| 361 |
- `wants` (*list of strings*): list of options this option wants to be passed alongside it (input is valid even if only one of them is found), |
| 362 |
- `conflicts` (*list of strings*): list of options this option has conflict with (input is invalid even if only one of them is found), |
| 363 |
- `required` (*Boolean*): specifies wheter the option is required or not, |
| 364 |
- `not_with` (*list of strings*): list of options that (if passed) render this option not required, |
| 365 |
- `plural` (*Boolean*): if true, each use of the option is counted or acumulated (check code of parser for exact behaviour), |
| 366 |
- `help` (*string*): help message for this option, |
| 367 |
|
| 368 |
**Note regarding plural options:** plural options are tricky beasts and RedCLAP does some magic to support them in a reasonable way. |
| 369 |
It is advisable to check the code of `.get()` method in the final object given after the input is parsed to get understanding of the exact behaviour of them. |
| 370 |
|
| 371 |
The only required keys are `short` or `long`, and if one of them is present the other one is optional. |
| 372 |
If a key not present on this list will be found in the dictionary it will cause an exception to be raised or be discarded, |
| 373 |
check the code of builder for exact behaviour. |
| 374 |
|
| 375 |
Examples of options described in JSON: |
| 376 |
|
| 377 |
*Most basic; specifing only short name* |
| 378 |
|
| 379 |
``` |
| 380 |
{"short": "f"} |
| 381 |
``` |
| 382 |
|
| 383 |
*Slightly more advanced; specifing short and long names, list of arguments and a help string* |
| 384 |
|
| 385 |
``` |
| 386 |
{ |
| 387 |
"short": "o", |
| 388 |
"long": "output", |
| 389 |
"arguments": ["str"], |
| 390 |
"help": "specifies output path" |
| 391 |
} |
| 392 |
``` |