Skip to content

Commit fb0f246

Browse files
committed
Improve documentation
1 parent 06cf8f1 commit fb0f246

1 file changed

Lines changed: 52 additions & 5 deletions

File tree

doc/readme.md

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ await commandApp.RunAsync(args);
9898

9999
An option is composed of a prototype that defines the option syntax (e.g. `"o|output="`).
100100

101+
### Quick Reference
102+
103+
The prototype is what you pass to `Add(...)` (or use in the collection initializer). It controls how the option can be written on the command line.
104+
105+
| Prototype (declaration) | What it declares | How it’s passed | Notes |
106+
|---|---|---|---|
107+
| `"v|verbose"` | Flag / boolean option | `-v`, `--verbose`, `/v` | Use `-v+` / `-v-` to explicitly enable/disable when parsed as a bool option. |
108+
| `"n|name="` | Required value | `--name John`, `--name=John`, `-nJohn`, `-n:John`, `/name:John` | If you omit `:`/`=`, the next token is consumed as the value (e.g. `--name John`). |
109+
| `"o:"` | Optional value | `-o`, `-oVALUE`, `-o:VALUE`, `--o=VALUE` | Optional values must be inline; `-o VALUE` does **not** attach `VALUE` to `-o`. |
110+
| `"D:"` with 2 values | Key/value pair (2 values) | `-DKEY`, `-DKEY=VALUE`, `-DKEY:VALUE` | Typically used for “macro” options. With `:` (optional), the second value can be omitted. |
111+
| `"P={->}"` with 2 values | Key/value pair with custom separator | `-PKEY->VALUE` | Custom separator is declared between `{...}`. |
112+
| `"i"` in a bundle | Bundled short options | `-abc`, `-txc` | Only works with `-` and single-letter options; at most one option in the bundle can take a value. |
113+
| `--` | Stop option parsing | `myexe -- --not-an-option -x /mnt/home` | Everything after `--` is treated as positional arguments. |
114+
115+
Value placeholders in descriptions:
116+
- For `MaxValueCount == 1`, `"{NAME}"` sets the displayed value name.
117+
- For multiple values, use `"{0:KEY} {1:VALUE}"` to name each value.
118+
119+
Strictness note:
120+
- Unknown `-` / `--` options fail by default (`CommandConfig.StrictOptionParsing = true`).
121+
- This does not apply to `/...` tokens so POSIX paths like `/mnt/home` can be passed as positional arguments.
122+
101123
```
102124
Regex-like BNF Grammar:
103125
name: .+
@@ -294,11 +316,13 @@ In addition to options (prefixed with `-`, `--`, `/`), you can declare positiona
294316

295317
An argument prototype uses angle brackets:
296318

297-
- `"<input>"`: a required positional argument
298-
- `"<output>?"`: an optional positional argument (only allowed for the last argument)
299-
- `"<files>*"`: an optional list argument (0 or more values, only allowed for the last argument)
300-
- `"<files>+"`: a required list argument (1 or more values, only allowed for the last argument)
301-
- `"<>"`: a remainder argument (0 or more values, only allowed for the last argument) forwarded to the command action
319+
| Prototype | Cardinality | Meaning |
320+
|---|---:|---|
321+
| `"<input>"` | 1 | Required positional argument |
322+
| `"<output>?"` | 0..1 | Optional positional argument (only allowed for the last argument) |
323+
| `"<files>*"` | 0..N | Optional list argument (only allowed for the last argument) |
324+
| `"<files>+"` | 1..N | Required list argument (only allowed for the last argument) |
325+
| `"<>"` | 0..N | Remainder argument forwarded to the command action (only allowed for the last argument) |
302326

303327
Collection initializer forms:
304328
- Bind a single value: `{ "<input>", "Input file", v => input = v }`
@@ -449,6 +473,19 @@ You can also complete from a pre-tokenized command line (useful for shells that
449473
var candidates = commandApp.GetCompletionsForTokens(["myexe", "hello", "--na"], tokenIndex: 2); // -> ["--name"]
450474
```
451475

476+
Value completions (optional):
477+
478+
```csharp
479+
var app = new CommandApp("myexe")
480+
{
481+
{ "c|color=", "Console {COLOR}", v => {} },
482+
{ "<file>", "Input {FILE}", v => {} },
483+
};
484+
485+
app.Options["color"].ValueCompleter = static (_, prefix) => ["red", "green", "blue"];
486+
app.Arguments[0].ValueCompleter = static (_, prefix) => ["README.md", "src/"];
487+
```
488+
452489
To expose completions from a CLI, add `CompletionCommands` (it adds `completion <shell>` and a hidden `__complete` command):
453490

454491
```csharp
@@ -553,6 +590,16 @@ Response file parsing supports:
553590
- `#` comments (when `#` is the first non-whitespace character on a line, or after a completed token)
554591
- Basic `\` escaping on non-Windows platforms (e.g. `c\ d` -> `c d`), while keeping `\` as a literal character on Windows (so paths like `C:\Temp\file.txt` are preserved).
555592

593+
Quick examples:
594+
595+
| Response file line | Produces tokens |
596+
|---|---|
597+
| `--name John` | `--name`, `John` |
598+
| `"hello world"` | `hello world` |
599+
| `# comment` | *(no tokens)* |
600+
| `c\\ d` (non-Windows) | `c d` |
601+
| `C:\Temp\file.txt` (Windows) | `C:\Temp\file.txt` |
602+
556603
## CommandGroup
557604

558605
`CommandGroup` are a special kind of nodes that can contain any other nodes (commands, options, text, actions...). They are used to group commands/options together, but more importantly, they can be used to declare when they are active based on a function callback.

0 commit comments

Comments
 (0)