@@ -525,7 +525,11 @@ var argv = require('yargs')
525
525
.command(module)
526
526
----------------
527
527
528
- Document the commands exposed by your application.
528
+ Define the commands exposed by your application.
529
+
530
+ ` cmd ` should be a string representing the command or an array of strings
531
+ representing the command and its aliases. Read more about command aliases in the
532
+ subsection below.
529
533
530
534
Use ` desc ` to provide a description for each command your application accepts (the
531
535
values stored in ` argv._ ` ). Set ` desc ` to ` false ` to create a hidden command.
@@ -536,7 +540,8 @@ Optionally, you can provide a `builder` object to give hints about the
536
540
options that your command accepts:
537
541
538
542
``` js
539
- yargs .command (' get' , ' make a get HTTP request' , {
543
+ yargs
544
+ .command (' get' , ' make a get HTTP request' , {
540
545
url: {
541
546
alias: ' u' ,
542
547
default: ' http://yargs.js.org/'
@@ -558,7 +563,8 @@ options (if used) **always** apply globally, just like the
558
563
with a ` yargs ` instance, and can be used to provide _ advanced_ command specific help:
559
564
560
565
``` js
561
- yargs .command (' get' , ' make a get HTTP request' , function (yargs ) {
566
+ yargs
567
+ .command (' get' , ' make a get HTTP request' , function (yargs ) {
562
568
return yargs .option (' url' , {
563
569
alias: ' u' ,
564
570
default: ' http://yargs.js.org/'
@@ -614,12 +620,78 @@ yargs.command('download <url> [files..]', 'download several files')
614
620
.argv
615
621
```
616
622
623
+ ### Command Execution
624
+
625
+ When a command is given on the command line, yargs will execute the following:
626
+
627
+ 1 . push the command into the current context
628
+ 2 . reset non-global configuration
629
+ 3 . apply command configuration via the ` builder ` , if given
630
+ 4 . parse and validate args from the command line, including positional args
631
+ 5 . if validation succeeds, run the ` handler ` function, if given
632
+ 6 . pop the command from the current context
633
+
634
+ ### Command Aliases
635
+
636
+ You can define aliases for a command by putting the command and all of its
637
+ aliases into an array.
638
+
639
+ Alternatively, a command module may specify an ` aliases ` property, which may be
640
+ a string or an array of strings. All aliases defined via the ` command ` property
641
+ and the ` aliases ` property will be concatenated together.
642
+
643
+ The first element in the array is considered the canonical command, which may
644
+ define positional arguments, and the remaining elements in the array are
645
+ considered aliases. Aliases inherit positional args from the canonical command,
646
+ and thus any positional args defined in the aliases themselves are ignored.
647
+
648
+ If either the canonical command or any of its aliases are given on the command
649
+ line, the command will be executed.
650
+
651
+ ``` js
652
+ #! / usr/ bin/ env node
653
+ require (' yargs' )
654
+ .command ([' start [app]' , ' run' , ' up' ], ' Start up an app' , {}, (argv ) => {
655
+ console .log (' starting up the' , argv .app || ' default' , ' app' )
656
+ })
657
+ .command ({
658
+ command: ' configure <key> [value]' ,
659
+ aliases: [' config' , ' cfg' ],
660
+ desc: ' Set a config variable' ,
661
+ builder : (yargs ) => yargs .default (' value' , ' true' ),
662
+ handler : (argv ) => {
663
+ console .log (` setting ${ argv .key } to ${ argv .value } ` )
664
+ }
665
+ })
666
+ .demand (1 )
667
+ .help ()
668
+ .wrap (72 )
669
+ .argv
670
+ ```
671
+
672
+ ```
673
+ $ ./svc.js help
674
+ Commands:
675
+ start [app] Start up an app [aliases: run, up]
676
+ configure <key> [value] Set a config variable [aliases: config, cfg]
677
+
678
+ Options:
679
+ --help Show help [boolean]
680
+
681
+ $ ./svc.js cfg concurrency 4
682
+ setting concurrency to 4
683
+
684
+ $ ./svc.js run web
685
+ starting up the web app
686
+ ```
687
+
617
688
### Providing a Command Module
618
689
619
690
For complicated commands you can pull the logic into a module. A module
620
691
simply needs to export:
621
692
622
- * ` exports.command ` : string that executes this command when given on the command line, may contain positional args
693
+ * ` exports.command ` : string (or array of strings) that executes this command when given on the command line, first string may contain positional args
694
+ * ` exports.aliases ` : array of strings (or a single string) representing aliases of ` exports.command ` , positional args defined in an alias are ignored
623
695
* ` exports.describe ` : string used as the description for the command in help text, use ` false ` for a hidden command
624
696
* ` exports.builder ` : object declaring the options the command accepts, or a function accepting and returning a yargs instance
625
697
* ` exports.handler ` : a function which will be passed the parsed argv.
0 commit comments