libk  Diff

Differences From Artifact [09b7990027]:

To Artifact [fb0395874f]:


1
2
3























4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# kcli
**kcli** is a module that implements common routines used by command-line utilities, such as option parsing, usage display, and more.
























## functions

### kcli_usage(kcli_set, kiochan)
kcli_usage() takes a `kcli_set` and prints a succinct usage summary to a [kiochan](../kio/kio.md).

## types

### struct kcli_set
`kcli_set` is a struct containing information about your program, such as its name, a synopsis of its function, a pointer to `argv`, and a list of `kcli_opt`s.

 * `const char* name` - program name (if null, will be determined from argv instead)
 * `size_t argc` - the number of arguments in the `argv` array.
 * `const char** argv` - the `argv` pointer passed to the `entry` function, representing the command-line arguments passed to the program.
 * `const char* desc` - program description
 * `const kcli_param* params` - a list of options expected by the program.



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

|


|

|







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
# kcli
**kcli** is a module that implements common routines used by command-line utilities, such as option parsing, usage display, and more.

# syntax
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.

1. if the argument `--` preceded the current argument, it is interpreted as a parameter.
2. if an argument begins with `-` and is followed by any symbol besides `-`, the remainder of the its characters are interpreted as short flags.
3. if an argument consists solely of `--`, it is ignored, and all further arguments are treated as parameters regardless of their form.
4. if an argument begins with `--` and is longer than two characters, it is interpreted as a long flag.
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.
6. if an argument consists solely of `@`, it is treated as in step 7, except flags and parameters are read from standard in.
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.
8. if an argument consists solely of `=@`, it is treated as in step 9, except flags and parameters are read from standard in.
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.
8. if an argument consists solely of `:@`, lines will be read from stdin and interpreted as individual parameters, not as flags.
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.
10. if no other rules apply, the argument is added to the parameter list.

**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.

consider the following examples:
1. `--no-arg-flag --arg-flag --other-flag` will set the flag `no-arg-flag`, and set the field `arg-flag` to `"--other-flag"`.
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`
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.

# functions

## kcli_usage(kcli_set, kiochan)
kcli_usage() takes a `kcli_set` and prints a succinct usage summary to a [kiochan](../kio/kio.md).

# types

## struct kcli_set
`kcli_set` is a struct containing information about your program, such as its name, a synopsis of its function, a pointer to `argv`, and a list of `kcli_opt`s.

 * `const char* name` - program name (if null, will be determined from argv instead)
 * `size_t argc` - the number of arguments in the `argv` array.
 * `const char** argv` - the `argv` pointer passed to the `entry` function, representing the command-line arguments passed to the program.
 * `const char* desc` - program description
 * `const kcli_param* params` - a list of options expected by the program.