Comparing version 4.3.7 to 4.4.0
@@ -67,8 +67,9 @@ 'use strict'; | ||
} | ||
const option = Object.assign({}, opt, | ||
{name, | ||
const option = Object.assign({}, opt, { | ||
name, | ||
alias: opt.alias || [], | ||
desc: opt.desc, | ||
default: opt.default, | ||
type: parseType(opt.type)}); | ||
type: parseType(opt.type) | ||
}); | ||
option.names = orderNames([option.name].concat(option.alias)); | ||
@@ -132,6 +133,7 @@ this.options.push(option); | ||
const command = Object.assign({}, option, | ||
{name, | ||
const command = Object.assign({}, option, { | ||
name, | ||
alias: option.alias || [], | ||
desc: option.desc}); | ||
desc: option.desc | ||
}); | ||
@@ -199,5 +201,3 @@ command.names = orderNames([command.name].concat(command.alias)); | ||
for (var i = 0, list = this.root.extraHelps; i < list.length; i += 1) { | ||
const h = list[i]; | ||
for (const h of this.root.extraHelps) { | ||
help += formatSection(h); | ||
@@ -248,5 +248,3 @@ } | ||
if (failedOptions.length > 0) { | ||
for (var i = 0, list = failedOptions; i < list.length; i += 1) { | ||
const option = list[i]; | ||
for (const option of failedOptions) { | ||
console.log(`The value of flag "${option.name}" should be one of: ${option.choices.map(choice => `"${choice}"`)}`); | ||
@@ -336,32 +334,20 @@ } | ||
findCommandByNameOrAlias(name) { | ||
return this.commands.filter(({ command }) => { | ||
return command.names.indexOf(name) > -1; | ||
})[0]; | ||
/** | ||
* Find command by command name, alias or addtionalMatch | ||
*/ | ||
findCommand(name) { | ||
for (const command of this.commands) { | ||
const { names, match } = command.command; | ||
if (names.includes(name)) { | ||
return { command, sliceFirstArg: name && name !== '*' }; | ||
} | ||
if (match && match(name)) { | ||
return { command, sliceFirstArg: false }; | ||
} | ||
} | ||
return null; | ||
} | ||
getCommand(name) { | ||
// No command name, use default command | ||
if (!isExplictCommand(name)) { | ||
return { | ||
command: this.findCommandByNameOrAlias('*'), | ||
sliceFirstArg: false | ||
}; | ||
} | ||
const command = this.findCommandByNameOrAlias(name); | ||
// Found sub command | ||
if (command) { | ||
return { | ||
command, | ||
sliceFirstArg: true | ||
}; | ||
} | ||
// Fallback to default command | ||
return { | ||
command: this.findCommandByNameOrAlias('*'), | ||
sliceFirstArg: false | ||
}; | ||
return this.findCommand(name) || this.findCommand('*') || {}; | ||
} | ||
@@ -399,3 +385,5 @@ | ||
argv = argv || process.argv.slice(2); | ||
this.firstArg = argv[0]; | ||
this.firstArg = argv[0] || ''; | ||
// Ensure that first arg is not a flag | ||
this.firstArg = this.firstArgs.startsWith('-') ? null : this.firstArgs; | ||
const { command, sliceFirstArg } = this.getCommand(this.firstArg); | ||
@@ -407,6 +395,4 @@ this.matchedCommand = command; | ||
string: [...this.options.getOptionNamesByType('string'), ...(command ? command.options.getOptionNamesByType('string') : [])], | ||
default: Object.assign({}, this.options.getDefaultsMapping(), | ||
(command ? command.options.getDefaultsMapping() : [])), | ||
alias: Object.assign({}, this.options.getAliasMap(), | ||
(command ? command.options.getAliasMap() : {})) | ||
default: Object.assign({}, this.options.getDefaultsMapping(), command ? command.options.getDefaultsMapping() : []), | ||
alias: Object.assign({}, this.options.getAliasMap(), command ? command.options.getAliasMap() : {}) | ||
}); | ||
@@ -413,0 +399,0 @@ |
{ | ||
"name": "cac", | ||
"version": "4.3.7", | ||
"version": "4.4.0", | ||
"description": "Command-line queen.", | ||
@@ -48,3 +48,3 @@ "repository": { | ||
"babel-register": "^6.24.1", | ||
"bili": "^1.1.0", | ||
"bili": "^2.2.6", | ||
"eslint-config-rem": "^3.0.0", | ||
@@ -51,0 +51,0 @@ "markdown-toc": "^1.1.0", |
@@ -119,2 +119,3 @@ <img width="945" alt="2017-07-26 9 27 05" src="https://user-images.githubusercontent.com/8784712/28623641-373450f4-7249-11e7-854d-1b076dab274d.png"> | ||
- examples: `Array<string>` command examples | ||
- match: `(name: string) => boolean` A custom command matcher | ||
- handler: `function` command handler | ||
@@ -121,0 +122,0 @@ - input: `Array<string>` cli arguments |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
263
21535
364