You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* First cut at parse rework
- skip asterisk tests
- other tests runnning
- nested commands untested
- lots of details to check
* Add check for requiredOption when calling executable subcommand
* Set program name using supported approach
* Add .addCommand, easy after previous work
* Add support for default command using action handler
- and remove stale _execs
* Add implicitHelpCommand and change help flags description
* Add implicit help command to help
* Turn off implicit help command for most help tests
* .addHelpCommand
* Remove addHelpCommand from tests and make match more narrow
* Use test of complete default help output
* Add tests for whether implicit help appears in help
* Add tests that help command dispatched to correct command
* Add simple nested subcommand test
* Add default command tests for action based subcommand
* Remove mainModule, out of scope for current PR
* Add legacy asterisk handling and tests
* Add more initialisation so object in known state
* Tests for addCommand
* Add first cut at enhanced default error detection
* Add test that addCommand requires name
* Add block on automatic name generation for deeply nested executables
* Add block on automatic name generation for deeply nested executables
* Fix describe name for tests
* Refine unknownCommand handling and add tests
* Add suggestion to try help, when appropriate
* Fix typo
* Move common command configuration options in README, and add isDefault example program
* Add isDefault and example to README
* Add nested commands
* Document .addHelpCommand, and tweaks
* Remove old default command, and rework command:* example
* Document .addCommand
* Remove comment referring to removed code.
* Revert the error tip "try --help", not happy with the wording
* Say "unknown command", like "unknown option"
* Set properties to null rather than undefined in constructor
error: required option '-c, --cheese <type>' not specified
275
276
```
@@ -296,7 +297,11 @@ program.version('0.0.1', '-v, --vers', 'output the current version');
296
297
297
298
## Commands
298
299
299
-
You can specify (sub)commands foryour top-level command using `.command`. There are two ways these can be implemented: using an action handler attached to the command, or as a separate executable file (describedin more detail later). In the first parameter to `.command` you specify the command name and any command arguments. The arguments may be `<required>` or `[optional]`, and the last argument may also be `variadic...`.
300
+
You can specify (sub)commands foryour top-level command using `.command()` or `.addCommand()`. There are two ways these can be implemented: using an action handler attached to the command, or as a separate executable file (describedin more detail later). The subcommands may be nested ([example](./examples/nestedCommands.js)).
301
+
302
+
In the first parameter to `.command()` you specify the command name and any command arguments. The arguments may be `<required>` or `[optional]`, and the last argument may also be `variadic...`.
303
+
304
+
You can use `.addCommand()` to add an already configured subcommand to the program.
300
305
301
306
For example:
302
307
@@ -315,8 +320,15 @@ program
315
320
program
316
321
.command('start <service>', 'start named service')
317
322
.command('stop [service]', 'stop named service, or all if no name supplied');
323
+
324
+
// Command prepared separately.
325
+
// Returns top-level command for adding more commands.
326
+
program
327
+
.addCommand(build.makeBuildCommand());
318
328
```
319
329
330
+
Configuration options can be passed with the call to `.command()`. Specifying `true`for`opts.noHelp` will remove the command from the generated help output. Specifying `true`for`opts.isDefault` will run the subcommand if no other subcommand is specified ([example](./examples/defaultCommand.js)).
331
+
320
332
### Specify the argument syntax
321
333
322
334
You use `.arguments` to specify the arguments forthe top-level command, and for subcommands they are includedin the `.command` call. Angled brackets (e.g. `<required>`) indicate required input. Square brackets (e.g. `[optional]`) indicate optional input.
@@ -397,13 +409,11 @@ async function main() {
397
409
}
398
410
```
399
411
400
-
A command's options on the command line are validated when the command is used. Any unknown options will be reported as an error. However, if an action-based command does not define an action, then the options are not validated.
401
-
402
-
Configuration options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the command from the generated help output.
412
+
A command's options on the command line are validated when the command is used. Any unknown options will be reported as an error.
403
413
404
414
### Git-style executable (sub)commands
405
415
406
-
When `.command()` is invoked with a description argument, this tells commander that you're going to use separate executables for sub-commands, much like `git(1)` and other popular tools.
416
+
When `.command()` is invoked with a description argument, this tells commander that you're going to use separate executables for sub-commands, much like `git` and other popular tools.
407
417
Commander will search the executables in the directory of the entry script (like `./examples/pm`) with the name `program-subcommand`, like `pm-install`, `pm-search`.
408
418
You can specify a custom name with the `executableFile` configuration option.
409
419
@@ -422,14 +432,12 @@ program
422
432
.parse(process.argv);
423
433
```
424
434
425
-
Configuration options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the command from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified.
426
-
Specifying a name with `executableFile` will override the default constructed name.
427
-
428
435
If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.
429
436
430
-
## Automated --help
437
+
## Automated help
431
438
432
-
The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:
439
+
The help information is auto-generated based on the information commander already knows about your program. The default
440
+
help option is `-h,--help`.
433
441
434
442
```bash
435
443
$ ./examples/pizza --help
@@ -444,17 +452,25 @@ Options:
444
452
-b, --bbq Add bbq sauce
445
453
-c, --cheese <type> Add the specified type of cheese (default: "marble")
446
454
-C, --no-cheese You do not want any cheese
447
-
-h, --help output usage information
455
+
-h, --help display help for command
456
+
```
457
+
458
+
A `help` command is added by default if your command has subcommands. It can be used alone, or with a subcommand name to show
459
+
further help for the subcommand. These are effectively the same if the `shell` program has implicit help:
460
+
461
+
```bash
462
+
shell help
463
+
shell --help
464
+
465
+
shell help spawn
466
+
shell spawn --help
448
467
```
449
468
450
469
### Custom help
451
470
452
-
You can display arbitrary `-h, --help` information
471
+
You can display extra `-h, --help` information
453
472
by listening for "--help". Commander will automatically
454
-
exit once you are done so that the remainder of your program
455
-
does not execute causing undesired behaviors, for example
456
-
in the following executable "stuff" will not output when
457
-
`--help` is used.
473
+
exit after displaying the help.
458
474
459
475
```js
460
476
#!/usr/bin/env node
@@ -467,9 +483,7 @@ program
467
483
.option('-b, --bar', 'enable some bar')
468
484
.option('-B, --baz', 'enable some baz');
469
485
470
-
// must be before .parse() since
471
-
// node's emit() is immediate
472
-
486
+
// must be before .parse()
473
487
program.on('--help', function(){
474
488
console.log('')
475
489
console.log('Examples:');
@@ -488,11 +502,11 @@ Yields the following help output when `node script-name.js -h` or `node script-n
488
502
Usage: custom-help [options]
489
503
490
504
Options:
491
-
-h, --help output usage information
492
505
-V, --version output the version number
493
506
-f, --foo enable some foo
494
507
-b, --bar enable some bar
495
508
-B, --baz enable some baz
509
+
-h, --help display help for command
496
510
497
511
Examples:
498
512
$ custom-help --help
@@ -550,6 +564,16 @@ program
550
564
.helpOption('-e, --HELP', 'read more information');
551
565
```
552
566
567
+
### .addHelpCommand()
568
+
569
+
You can explicitly turn on or off the implicit help command with `.addHelpCommand()` and `.addHelpCommand(false)`.
570
+
571
+
You can both turn on and customise the help command by supplying the name and description:
0 commit comments