@@ -2,6 +2,8 @@ const path = require('path')
2
2
const inspect = require ( 'util' ) . inspect
3
3
const camelCase = require ( 'camelcase' )
4
4
5
+ const DEFAULT_MARKER = '*'
6
+
5
7
// handles parsing positional arguments,
6
8
// and populating argv with said positional
7
9
// arguments.
@@ -10,6 +12,7 @@ module.exports = function (yargs, usage, validation) {
10
12
11
13
var handlers = { }
12
14
var aliasMap = { }
15
+ var defaultCommand
13
16
self . addHandler = function ( cmd , description , builder , handler ) {
14
17
var aliases = [ ]
15
18
if ( Array . isArray ( cmd ) ) {
@@ -28,15 +31,50 @@ module.exports = function (yargs, usage, validation) {
28
31
return
29
32
}
30
33
34
+ // parse positionals out of cmd string
31
35
var parsedCommand = self . parseCommand ( cmd )
36
+
37
+ // remove positional args from aliases only
32
38
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 ) {
34
73
aliasMap [ alias ] = parsedCommand . cmd
35
- return alias
36
74
} )
37
75
38
76
if ( description !== false ) {
39
- usage . command ( cmd , description , aliases )
77
+ usage . command ( cmd , description , isDefault , aliases )
40
78
}
41
79
42
80
handlers [ parsedCommand . cmd ] = {
@@ -46,6 +84,8 @@ module.exports = function (yargs, usage, validation) {
46
84
demanded : parsedCommand . demanded ,
47
85
optional : parsedCommand . optional
48
86
}
87
+
88
+ if ( isDefault ) defaultCommand = handlers [ parsedCommand . cmd ]
49
89
}
50
90
51
91
self . addDirectory = function ( dir , context , req , callerFile , opts ) {
@@ -130,9 +170,13 @@ module.exports = function (yargs, usage, validation) {
130
170
return handlers
131
171
}
132
172
173
+ self . hasDefaultCommand = function ( ) {
174
+ return ! ! defaultCommand
175
+ }
176
+
133
177
self . runCommand = function ( command , yargs , parsed ) {
134
178
var aliases = parsed . aliases
135
- var commandHandler = handlers [ command ] || handlers [ aliasMap [ command ] ]
179
+ var commandHandler = handlers [ command ] || handlers [ aliasMap [ command ] ] || defaultCommand
136
180
var currentContext = yargs . getContext ( )
137
181
var numFiles = currentContext . files . length
138
182
var parentCommands = currentContext . commands . slice ( )
@@ -142,7 +186,7 @@ module.exports = function (yargs, usage, validation) {
142
186
var innerYargs = null
143
187
var positionalMap = { }
144
188
145
- currentContext . commands . push ( command )
189
+ if ( command ) currentContext . commands . push ( command )
146
190
if ( typeof commandHandler . builder === 'function' ) {
147
191
// a function can be provided, which builds
148
192
// up a yargs chain and possibly returns it.
@@ -186,7 +230,7 @@ module.exports = function (yargs, usage, validation) {
186
230
commandHandler . handler ( innerArgv )
187
231
}
188
232
189
- currentContext . commands . pop ( )
233
+ if ( command ) currentContext . commands . pop ( )
190
234
numFiles = currentContext . files . length - numFiles
191
235
if ( numFiles > 0 ) currentContext . files . splice ( numFiles * - 1 , numFiles )
192
236
@@ -263,6 +307,7 @@ module.exports = function (yargs, usage, validation) {
263
307
self . reset = function ( ) {
264
308
handlers = { }
265
309
aliasMap = { }
310
+ defaultCommand = undefined
266
311
return self
267
312
}
268
313
@@ -275,10 +320,12 @@ module.exports = function (yargs, usage, validation) {
275
320
frozen = { }
276
321
frozen . handlers = handlers
277
322
frozen . aliasMap = aliasMap
323
+ frozen . defaultCommand = defaultCommand
278
324
}
279
325
self . unfreeze = function ( ) {
280
326
handlers = frozen . handlers
281
327
aliasMap = frozen . aliasMap
328
+ defaultCommand = frozen . defaultCommand
282
329
frozen = undefined
283
330
}
284
331
0 commit comments