|
21 | 21 | - [Install](#install) |
22 | 22 | - [Use](#use) |
23 | 23 | - [Creating a *program*](#creating-a-program) |
| 24 | + - [Options](#options) |
24 | 25 | - [API](#api) |
25 | 26 | - [`Argument(info)`](#argumentinfo) |
26 | 27 | - [`Argument#choices([choices])`](#argumentchoiceschoices) |
@@ -240,6 +241,48 @@ import { Command } from '@flex-development/kronk' |
240 | 241 | const program: Command = new Command() |
241 | 242 | ``` |
242 | 243 |
|
| 244 | +### Options |
| 245 | + |
| 246 | +Options are defined with the [`.option()`](#commandoptioninfo-data) and [`.options()`](#commandoptionsinfos) methods, |
| 247 | +which also serve as documentation for the options. Each option can have at most 2 flags, typically one long flag and one |
| 248 | +short or shortish (e.g. `--ws`) flag. Flags can be separated by commas (`,`), pipes (`|`), or spaces (` `). |
| 249 | + |
| 250 | +An option and its option-argument can be separated by an equal sign (`=`) or spaces (` `). |
| 251 | +A short option (i.e. `-p`) and its option-argument can also be combined. |
| 252 | + |
| 253 | +```sh |
| 254 | +serve --port 80 |
| 255 | +serve --port=80 |
| 256 | +serve -p 80 |
| 257 | +serve -p80 |
| 258 | +serve -p=80 |
| 259 | +``` |
| 260 | + |
| 261 | +Options on the command line are not positional, and can be specified before or after command-arguments. |
| 262 | + |
| 263 | +```sh |
| 264 | +serve --port=80 ./server.mts |
| 265 | +serve ./src/main.mts -p8080 |
| 266 | +``` |
| 267 | + |
| 268 | +If a non-option (i.e. `-13`) looks like an option because it starts with a hyphen (`-`), a delimiter (`--`) can be used |
| 269 | +to demarcate the command-argument from an option. |
| 270 | + |
| 271 | +```sh |
| 272 | +clamp -M3 -m-1 -- -13 |
| 273 | +``` |
| 274 | + |
| 275 | +Parsed options can be accessed by calling [`.opts<T>()`](#commandoptst) on a [`Command`](#commandinfo) object, and are |
| 276 | +passed to the command's [`action`](#commandactionaction) handler. |
| 277 | +Multi-word options such as `--template-engine` are camelCased, becoming `program.opts().templateEngine`, or can be |
| 278 | +configured to be converted using snake\_case, becoming `program.opts().template_engine`. |
| 279 | + |
| 280 | +There are additional, related routines for when `.opts<T>()` is not enough: |
| 281 | + |
| 282 | +- [`.optionValue(key[, value][, source])`](#commandoptionvaluekey-value-source) |
| 283 | +- [`.optionValueSource(key[, source])`](#commandoptionvaluesourcekey-source) |
| 284 | +- [`.optsWithGlobals<T>()`](#commandoptswithglobalst) |
| 285 | + |
243 | 286 | ## API |
244 | 287 |
|
245 | 288 | ### `Argument(info)` |
|
0 commit comments