@bemoje/cli 4.0.1 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,10 +30,10 @@ npm install @bemoje/cli
30
30
  - **Declarative, chainable API** - Build commands fluently with method chaining; each call returns a narrowed type.
31
31
  - **Subcommand hierarchies** - Nest commands arbitrarily deep. Options are inherited by subcommands. Aliases are auto-generated.
32
32
  - **Auto-generated help** - Colorized, grouped, and wrapped help output from your command definitions. Fully customizable via the `Help` class.
33
- - **Built-in `--help`, `--debug`, and `--version` flags** - Automatically added to root commands.
33
+ - **Built-in `--help`, `--debug`, and `--version` flags** - Automatically added to root commands. An option of your own with the same long name replaces one, and one of your own may take its short flag.
34
34
  - **Option hooks** - Register side-effect actions that trigger when specific options are set (e.g., `--help` prints help and sets `process.exitCode`).
35
35
  - **Parse-only design** - `parseArgv()` returns a result object with `args`, `opts`, `errors`, `hooks`, and an `execute()` method. You control when and whether execution happens.
36
- - **Validation** - Missing required arguments, unknown options, and invalid choices are reported as errors on the result.
36
+ - **Validation** - Missing required arguments and options, options given without their value, unknown options and subcommands, arguments no definition takes, and invalid choices are reported as errors on the result.
37
37
  - **Variadic arguments and options** - Both arguments and options support variadic (`...`) syntax to collect multiple values into arrays.
38
38
  - **Environment variable defaults** - Options can fall back to environment variables via the `env` option.
39
39
  - **Argument and option choices** - Restrict allowed values with `choices` arrays; violations are reported as validation errors.
@@ -105,6 +105,8 @@ const cmd = new Command('deploy').addArgument('<env>', { choices: ['dev', 'stagi
105
105
 
106
106
  Options are named flags parsed from `argv`. The format is always `-<short>, --<long> [value-syntax]`.
107
107
 
108
+ An option written with `<value>` is required: when it is not given and has no `defaultValue` and no `env` value, `parseArgv()` reports `Missing option ...` in `errors`. Write it `[value]` when it may be left out. Either way, an option that takes a value must be given one: `--output` at the end of argv, or followed by another option, is the error `Option --output requires a value`. A value is given as `--output dist`, `--output=dist`, `-o dist`, `-odist` or `-o=dist`; a negative number or a lone `-` is taken as a value, and any value that starts with `-` can be given with `=` (`--output=-x`).
109
+
108
110
  ```ts
109
111
  const cmd = new Command('build')
110
112
  .addOption('-o, --output <dir>', { description: 'Output directory' }) // required string
@@ -117,7 +119,7 @@ const cmd = new Command('build')
117
119
  })
118
120
  ```
119
121
 
120
- **Variadic options** collect multiple values into an array:
122
+ **Variadic options** collect multiple values into an array, up to the next option or `--`. Given more than once, they keep the values of every time. An optional one (`[patterns...]`) given with no value is `[]`:
121
123
 
122
124
  ```ts
123
125
  const cmd = new Command('lint')
@@ -166,7 +168,7 @@ const cmd = new Command('server')
166
168
  .addOption('-v, --verbose', { description: 'Verbose logging', group: 'Logging:' })
167
169
  ```
168
170
 
169
- **Negatable flags** - any boolean option can be negated with `--no-<name>`:
171
+ **Negatable flags** - any boolean option can be negated with `--no-<name>` (an option that takes a value cannot, and a boolean flag takes no value: `--color=false` is an error):
170
172
 
171
173
  ```ts
172
174
  const cmd = new Command('build').addOption('-c, --color', { description: 'Colorize output' })
@@ -188,7 +190,7 @@ cmd.parseArgv(['--output-dir', '/tmp'])
188
190
 
189
191
  Use `.command()` to create a subcommand and get it back, or `.addCommand()` to add one via callback and get the parent back for chaining.
190
192
 
191
- Options defined on the parent are automatically inherited by subcommands. Aliases are auto-generated from subcommand name initials (e.g., `build-project` gets alias `bp`).
193
+ Options defined on the parent are automatically inherited by subcommands, and may be given before the subcommand's name (`git -v clone url`). An unknown subcommand name is reported as ``Unknown command `name` `` in `errors`. Aliases are auto-generated from subcommand name initials (e.g., `build-project` gets alias `bp`).
192
194
 
193
195
  ```ts
194
196
  const cli = new Command('git')
@@ -284,6 +286,14 @@ const cli = new Command('tool')
284
286
 
285
287
  Hooks are evaluated in registration order. If a hook sets `process.exitCode`, subsequent hooks and the main action are skipped.
286
288
 
289
+ **Replacing a built-in** - an option of your own with the long name of a built-in (`--help`, `--debug`, `--version`) replaces it, its hook included, and its type replaces the built-in's. An option of your own may also take a built-in's short flag; the built-in then keeps only its long name:
290
+
291
+ ```ts
292
+ const cli = new Command('server')
293
+ .addOption('-d, --debug', { description: 'Run in debug mode' }) // replaces -D, --debug
294
+ .addOption('-h, --host <addr>', { defaultValue: 'localhost' }) // --help keeps working, without -h
295
+ ```
296
+
287
297
  ### Help Output
288
298
 
289
299
  Help is auto-generated with ANSI color support, word wrapping, and section grouping.