@@ -57,9 +57,7 @@ program
57
57
. option ( '-x, --proxy <proxyUrl>' , 'Use specified proxy when creating project' )
58
58
. option ( '-b, --bare' , 'Scaffold project without beginner instructions' )
59
59
. option ( '--skipGetStarted' , 'Skip displaying "Get started" instructions' )
60
- . action ( ( name , cmd ) => {
61
- const options = cleanArgs ( cmd )
62
-
60
+ . action ( ( name , options ) => {
63
61
if ( minimist ( process . argv . slice ( 3 ) ) . _ . length > 1 ) {
64
62
console . log ( chalk . yellow ( '\n Info: You provided more than one argument. The first one will be used as the app\'s name, the rest are ignored.' ) )
65
63
}
@@ -97,8 +95,8 @@ program
97
95
. option ( '--rules' , 'list all module rule names' )
98
96
. option ( '--plugins' , 'list all plugin names' )
99
97
. option ( '-v --verbose' , 'Show full function definitions in output' )
100
- . action ( ( paths , cmd ) => {
101
- require ( '../lib/inspect' ) ( paths , cleanArgs ( cmd ) )
98
+ . action ( ( paths , options ) => {
99
+ require ( '../lib/inspect' ) ( paths , options )
102
100
} )
103
101
104
102
program
@@ -124,9 +122,9 @@ program
124
122
. option ( '-D, --dev' , 'Run in dev mode' )
125
123
. option ( '--quiet' , `Don't output starting messages` )
126
124
. option ( '--headless' , `Don't open browser on start and output port` )
127
- . action ( ( cmd ) => {
125
+ . action ( ( options ) => {
128
126
checkNodeVersion ( '>=8.6' , 'vue ui' )
129
- require ( '../lib/ui' ) ( cleanArgs ( cmd ) )
127
+ require ( '../lib/ui' ) ( options )
130
128
} )
131
129
132
130
program
@@ -146,16 +144,16 @@ program
146
144
. option ( '-d, --delete <path>' , 'delete option from config' )
147
145
. option ( '-e, --edit' , 'open config with default editor' )
148
146
. option ( '--json' , 'outputs JSON result only' )
149
- . action ( ( value , cmd ) => {
150
- require ( '../lib/config' ) ( value , cleanArgs ( cmd ) )
147
+ . action ( ( value , options ) => {
148
+ require ( '../lib/config' ) ( value , options )
151
149
} )
152
150
153
151
program
154
152
. command ( 'outdated' )
155
153
. description ( '(experimental) check for outdated vue cli service / plugins' )
156
154
. option ( '--next' , 'Also check for alpha / beta / rc versions when upgrading' )
157
- . action ( ( cmd ) => {
158
- require ( '../lib/outdated' ) ( cleanArgs ( cmd ) )
155
+ . action ( ( options ) => {
156
+ require ( '../lib/outdated' ) ( options )
159
157
} )
160
158
161
159
program
@@ -166,17 +164,16 @@ program
166
164
. option ( '-r, --registry <url>' , 'Use specified npm registry when installing dependencies' )
167
165
. option ( '--all' , 'Upgrade all plugins' )
168
166
. option ( '--next' , 'Also check for alpha / beta / rc versions when upgrading' )
169
- . action ( ( packageName , cmd ) => {
170
- require ( '../lib/upgrade' ) ( packageName , cleanArgs ( cmd ) )
167
+ . action ( ( packageName , options ) => {
168
+ require ( '../lib/upgrade' ) ( packageName , options )
171
169
} )
172
170
173
171
program
174
172
. command ( 'migrate [plugin-name]' )
175
173
. description ( '(experimental) run migrator for an already-installed cli plugin' )
176
- // TODO: use `requiredOption` after upgrading to commander 4.x
177
- . option ( '-f, --from <version>' , 'The base version for the migrator to migrate from' )
178
- . action ( ( packageName , cmd ) => {
179
- require ( '../lib/migrate' ) ( packageName , cleanArgs ( cmd ) )
174
+ . requiredOption ( '-f, --from <version>' , 'The base version for the migrator to migrate from' )
175
+ . action ( ( packageName , options ) => {
176
+ require ( '../lib/migrate' ) ( packageName , options )
180
177
} )
181
178
182
179
program
@@ -201,15 +198,13 @@ program
201
198
} )
202
199
203
200
// output help information on unknown commands
204
- program
205
- . arguments ( '<command>' )
206
- . action ( ( cmd ) => {
207
- program . outputHelp ( )
208
- console . log ( ` ` + chalk . red ( `Unknown command ${ chalk . yellow ( cmd ) } .` ) )
209
- console . log ( )
210
- suggestCommands ( cmd )
211
- process . exitCode = 1
212
- } )
201
+ program . on ( 'command:*' , ( [ cmd ] ) => {
202
+ program . outputHelp ( )
203
+ console . log ( ` ` + chalk . red ( `Unknown command ${ chalk . yellow ( cmd ) } .` ) )
204
+ console . log ( )
205
+ suggestCommands ( cmd )
206
+ process . exitCode = 1
207
+ } )
213
208
214
209
// add some useful info on help
215
210
program . on ( '--help' , ( ) => {
@@ -259,22 +254,3 @@ function suggestCommands (unknownCommand) {
259
254
console . log ( ` ` + chalk . red ( `Did you mean ${ chalk . yellow ( suggestion ) } ?` ) )
260
255
}
261
256
}
262
-
263
- function camelize ( str ) {
264
- return str . replace ( / - ( \w ) / g, ( _ , c ) => c ? c . toUpperCase ( ) : '' )
265
- }
266
-
267
- // commander passes the Command object itself as options,
268
- // extract only actual options into a fresh object.
269
- function cleanArgs ( cmd ) {
270
- const args = { }
271
- cmd . options . forEach ( o => {
272
- const key = camelize ( o . long . replace ( / ^ - - / , '' ) )
273
- // if an option is not present and Command has a method with the same name
274
- // it should not be copied
275
- if ( typeof cmd [ key ] !== 'function' && typeof cmd [ key ] !== 'undefined' ) {
276
- args [ key ] = cmd [ key ]
277
- }
278
- } )
279
- return args
280
- }
0 commit comments