libk  Diff

Differences From Artifact [0390b41ce1]:

To Artifact [5e7f6f38dd]:


     1      1   # kcli
     2      2   **kcli** is a module that implements common routines used by command-line utilities, such as option parsing, usage display, and more.
     3      3   
     4      4   # syntax
     5         -kcli implements the unspoken standard for command line parsing that is adhered to by many UNIX programs. it operates on an array of tokens, whose role depends on their content and on preceeding flags. note that in flag names, dashes and underscores are equivalent. this is to prevent obnoxious behavior and enable the macros to define multi-word long flags.
            5  +kcli implements the unwritten standard for command line parsing that is adhered to by many UNIX programs. it operates on an array of tokens, whose role depends on their content and on preceeding flags. note that in flag names, dashes and underscores are equivalent. this is to prevent obnoxious behavior and enable the macros to define multi-word long flags.
     6      6   
     7      7   1. if the argument `--` preceded the current argument, it is interpreted as a parameter.
     8      8   2. if an argument begins with `-` and is followed by any symbol besides `-`, the remainder of the its characters are interpreted as short flags.
     9      9   3. if an argument consists solely of `--`, it is ignored, and all further arguments are treated as parameters regardless of their form.
    10         -4. if an argument begins with `--` and is longer than two characters, it is interpreted as a long flag.
           10  +4. if an argument begins with `--` is longer than two characters, and does not begin with `--@`, it is interpreted as a long flag.
    11     11   5. if a flag takes a parameter, the cursor is incremented and the field it points to is interpreted as its parameter. if there are not enough arguments, parse fails.
    12     12   6. if an argument consists solely of `@`, it is treated as in step 7, except flags and parameters are read from standard in.
    13     13   7. if an argument begins with `@`, the remainder is interpreted as a filename. this file is loaded and interpreted according to this same set of rules, where spaces separate each parameter, and double or single quote marks can be used to escape strings. newlines are ignored.
    14         -8. if an argument consists solely of `=@`, it is treated as in step 9, except flags and parameters are read from standard in.
    15         -9. if an argument begins with `=@`, the remainder is interpreted as a filename. this file is loaded and interpreted according to this same set of rules, where newlines separate each parameter.
    16         -8. if an argument consists solely of `:@`, lines will be read from stdin and interpreted as individual parameters, not as flags.
    17         -8. if an argument begins with `:@`, the remainder is interpreted as a filename. all of the file's lines will be interpreted as individual parameters, not as flags.
    18         -10. if no other rules apply, the argument is added to the parameter list.
           14  +8. if an argument consists solely of `-@`, it is treated as in step 11, except flags and parameters are read from standard in.
           15  +9. if an argument consists solely of `--@`, lines will be read from stdin and interpreted as individual parameters, not as flags.
           16  +10. if an argument begins with `--@`, the remainder is interpreted as a filename. all of the file's lines will be interpreted as individual parameters, not as flags.
           17  +11. if an argument begins with `-@`, the remainder is interpreted as a filename. this file is loaded and interpreted under the assumption that newlines separate each parameter.
           18  +12. if no other rules apply, the argument is added to the parameter list.
    19     19   
    20     20   **note:** in the future, `kcore` will parse arguments at startup before passing them on to the application. all libk control arguments are prefixed with `-:`. these will not be passed onto the application, and so will not be available for kcli to parse.
    21     21   
    22     22   consider the following examples:
    23     23   1. `--no-arg-flag --arg-flag --other-flag` will set the flag `no-arg-flag`, and set the field `arg-flag` to `"--other-flag"`.
    24     24   2. if `-a` names a flag and `-b` and `-c` name string fields, `-abc b-field-value c-field-value parameter` sets the short flag `a`, set the field `b` to `"b-field-value"`, set `c` to `"c-field-value"`, and add the parameter `parameter`
    25     25   3. `-s --long @args -- -s --long @args` sets the flags `s`, `long`, and parses the file `args` for further parameters, then adds the parameters `"-s"``"--long"``"@args"` to the parameter list.