@@ -2,6 +2,8 @@ const path = require('path')
22const inspect = require ( 'util' ) . inspect
33const camelCase = require ( 'camelcase' )
44
5+ const DEFAULT_MARKER = '*'
6+
57// handles parsing positional arguments,
68// and populating argv with said positional
79// arguments.
@@ -10,6 +12,7 @@ module.exports = function (yargs, usage, validation) {
1012
1113 var handlers = { }
1214 var aliasMap = { }
15+ var defaultCommand
1316 self . addHandler = function ( cmd , description , builder , handler ) {
1417 var aliases = [ ]
1518 if ( Array . isArray ( cmd ) ) {
@@ -28,15 +31,50 @@ module.exports = function (yargs, usage, validation) {
2831 return
2932 }
3033
34+ // parse positionals out of cmd string
3135 var parsedCommand = self . parseCommand ( cmd )
36+
37+ // remove positional args from aliases only
3238 aliases = aliases . map ( function ( alias ) {
33- alias = self . parseCommand ( alias ) . cmd // remove positional args
39+ return self . parseCommand ( alias ) . cmd
40+ } )
41+
42+ // check for default and filter out '*''
43+ var isDefault = false
44+ var parsedAliases = [ parsedCommand . cmd ] . concat ( aliases ) . filter ( function ( c ) {
45+ if ( c === DEFAULT_MARKER ) {
46+ isDefault = true
47+ return false
48+ }
49+ return true
50+ } )
51+
52+ // short-circuit if default with no aliases
53+ if ( isDefault && parsedAliases . length === 0 ) {
54+ defaultCommand = {
55+ original : cmd . replace ( DEFAULT_MARKER , '' ) . trim ( ) ,
56+ handler : handler ,
57+ builder : builder || { } ,
58+ demanded : parsedCommand . demanded ,
59+ optional : parsedCommand . optional
60+ }
61+ return
62+ }
63+
64+ // shift cmd and aliases after filtering out '*'
65+ if ( isDefault ) {
66+ parsedCommand . cmd = parsedAliases [ 0 ]
67+ aliases = parsedAliases . slice ( 1 )
68+ cmd = cmd . replace ( DEFAULT_MARKER , parsedCommand . cmd )
69+ }
70+
71+ // populate aliasMap
72+ aliases . forEach ( function ( alias ) {
3473 aliasMap [ alias ] = parsedCommand . cmd
35- return alias
3674 } )
3775
3876 if ( description !== false ) {
39- usage . command ( cmd , description , aliases )
77+ usage . command ( cmd , description , isDefault , aliases )
4078 }
4179
4280 handlers [ parsedCommand . cmd ] = {
@@ -46,6 +84,8 @@ module.exports = function (yargs, usage, validation) {
4684 demanded : parsedCommand . demanded ,
4785 optional : parsedCommand . optional
4886 }
87+
88+ if ( isDefault ) defaultCommand = handlers [ parsedCommand . cmd ]
4989 }
5090
5191 self . addDirectory = function ( dir , context , req , callerFile , opts ) {
@@ -130,9 +170,13 @@ module.exports = function (yargs, usage, validation) {
130170 return handlers
131171 }
132172
173+ self . hasDefaultCommand = function ( ) {
174+ return ! ! defaultCommand
175+ }
176+
133177 self . runCommand = function ( command , yargs , parsed ) {
134178 var aliases = parsed . aliases
135- var commandHandler = handlers [ command ] || handlers [ aliasMap [ command ] ]
179+ var commandHandler = handlers [ command ] || handlers [ aliasMap [ command ] ] || defaultCommand
136180 var currentContext = yargs . getContext ( )
137181 var numFiles = currentContext . files . length
138182 var parentCommands = currentContext . commands . slice ( )
@@ -142,7 +186,7 @@ module.exports = function (yargs, usage, validation) {
142186 var innerYargs = null
143187 var positionalMap = { }
144188
145- currentContext . commands . push ( command )
189+ if ( command ) currentContext . commands . push ( command )
146190 if ( typeof commandHandler . builder === 'function' ) {
147191 // a function can be provided, which builds
148192 // up a yargs chain and possibly returns it.
@@ -186,7 +230,7 @@ module.exports = function (yargs, usage, validation) {
186230 commandHandler . handler ( innerArgv )
187231 }
188232
189- currentContext . commands . pop ( )
233+ if ( command ) currentContext . commands . pop ( )
190234 numFiles = currentContext . files . length - numFiles
191235 if ( numFiles > 0 ) currentContext . files . splice ( numFiles * - 1 , numFiles )
192236
@@ -263,6 +307,7 @@ module.exports = function (yargs, usage, validation) {
263307 self . reset = function ( ) {
264308 handlers = { }
265309 aliasMap = { }
310+ defaultCommand = undefined
266311 return self
267312 }
268313
@@ -275,10 +320,12 @@ module.exports = function (yargs, usage, validation) {
275320 frozen = { }
276321 frozen . handlers = handlers
277322 frozen . aliasMap = aliasMap
323+ frozen . defaultCommand = defaultCommand
278324 }
279325 self . unfreeze = function ( ) {
280326 handlers = frozen . handlers
281327 aliasMap = frozen . aliasMap
328+ defaultCommand = frozen . defaultCommand
282329 frozen = undefined
283330 }
284331
0 commit comments