@@ -183,9 +183,9 @@ spelling _means_:
183183stringOption({ alias: "p" })` steals ` --path`'s shorthand. Restating an
184184 option's own shorthand (` path: stringOption({ alias: "p" }) ` ) is fine.
185185
186- The merge replaces the CLI-wide entry rather than patching it, so a
187- redeclaration inherits nothing: restate the ` alias ` and ` hasSensitiveValue ` the
188- global declaration carries if the command still wants them .
186+ A redeclaration that leaves ` alias ` , ` default ` or ` hasSensitiveValue ` unset
187+ keeps what the CLI-wide declaration carries for them, so ` path: stringOption() `
188+ still answers to ` -p ` and stays out of the logs; set one only to change it .
189189
190190### How validation behaves
191191
@@ -349,15 +349,22 @@ The run context
349349- ` ctx.injector ` — this invocation's injector, a child of the one the command
350350 was registered against; see
351351 [ Injection, and the first ` await ` ] ( #injection-and-the-first-await ) .
352- - ` ctx.fail(message) ` — fails the command with ` message ` and a usage help
353- suggestion.
352+ - ` ctx.fail(message, options? ) ` — fails the command with ` message ` , followed
353+ by a usage help suggestion unless ` options.help ` is ` false ` .
354354
355355` run ` may be synchronous or ` async ` ; the CLI awaits the result and treats a
356356rejection as a command failure.
357357
358358### Failing a command
359359
360- ` ctx.fail(message) ` is the idiomatic way to stop a command:
360+ ` ctx.fail(message) ` is the idiomatic way to stop a command. By default it
361+ follows the message with the usage help suggestion — the "Run `ns widget add
362+ --help`" line — which is what the user needs when they got the command line
363+ wrong: a missing argument, an unknown value, an invalid combination of options.
364+
365+ When the command line was fine and something else is not — the environment,
366+ the project, a file on disk — the help suggestion only gets in the way. Pass
367+ ` { help: false } ` to print the message alone:
361368
362369``` ts
363370defineCommand ({
@@ -369,17 +376,22 @@ defineCommand({
369376 ctx .fail (" --output is required." );
370377 }
371378
379+ if (fs .existsSync (ctx .options .output )) {
380+ ctx .fail (` ${ctx .options .output } already exists. ` , { help: false });
381+ }
382+
372383 /* ... */
373384 },
374385});
375386```
376387
377- It is available on the ` canExecute ` context as well, and it returns ` never ` , so
378- it can end a branch without a ` return ` . The message must be a non-empty string.
388+ It is available on the ` setup ` and ` canExecute ` contexts as well, and it
389+ returns ` never ` , so it can end a branch without a ` return ` . The message must be
390+ a non-empty string, and ` options ` , when given, a plain object. The two forms
391+ map onto the ` errors ` service's ` failWithHelp ` and ` fail ` .
379392
380- Throwing is equivalent and keeps working — ` ctx.fail ` is sugar over the
381- ` errors ` service's ` failWithHelp ` , which is what adds the "Run `ns widget add
382- --help` " line. Throw when you already have an ` Error` to propagate; call
393+ Throwing keeps working too: an error thrown from a handler propagates
394+ unchanged. Throw when you already have an ` Error ` to propagate; call
383395` ctx.fail ` when you are writing the message.
384396
385397Injection, and the first ` await `
0 commit comments