Comparing version 6.5.4 to 6.5.5
@@ -223,2 +223,24 @@ 'use strict'; | ||
}; | ||
const camelcaseOptionName = (name) => { | ||
// Camelcase the option name | ||
// Don't camelcase anything after the dot `.` | ||
return name | ||
.split('.') | ||
.map((v, i) => { | ||
return i === 0 ? camelcase(v) : v; | ||
}) | ||
.join('.'); | ||
}; | ||
class CACError extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
if (typeof Error.captureStackTrace === 'function') { | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
else { | ||
this.stack = new Error(message).stack; | ||
} | ||
} | ||
} | ||
@@ -241,11 +263,3 @@ class Option { | ||
} | ||
// Camelcase the option name | ||
// Don't camelcase anything after the dot `.` | ||
name = name | ||
.split('.') | ||
.map((v, i) => { | ||
return i === 0 ? camelcase(v) : v; | ||
}) | ||
.join('.'); | ||
return name; | ||
return camelcaseOptionName(name); | ||
}) | ||
@@ -272,5 +286,2 @@ .sort((a, b) => (a.length > b.length ? 1 : -1)); // Sort names | ||
const deno = typeof window !== 'undefined' && window.Deno; | ||
const exit = (code) => { | ||
return deno ? Deno.exit(code) : process.exit(code); | ||
}; | ||
const processArgs = deno ? ['deno'].concat(Deno.args) : process.argv; | ||
@@ -425,3 +436,2 @@ const platformInfo = deno | ||
.join('\n\n')); | ||
exit(0); | ||
} | ||
@@ -434,3 +444,2 @@ outputVersion() { | ||
} | ||
exit(0); | ||
} | ||
@@ -440,4 +449,3 @@ checkRequiredArgs() { | ||
if (this.cli.args.length < minimalArgsCount) { | ||
console.error(`error: missing required args for command \`${this.rawName}\``); | ||
exit(1); | ||
throw new CACError(`missing required args for command \`${this.rawName}\``); | ||
} | ||
@@ -451,10 +459,9 @@ } | ||
checkUnknownOptions() { | ||
const { rawOptions, globalCommand } = this.cli; | ||
const { options, globalCommand } = this.cli; | ||
if (!this.config.allowUnknownOptions) { | ||
for (const name of Object.keys(rawOptions)) { | ||
for (const name of Object.keys(options)) { | ||
if (name !== '--' && | ||
!this.hasOption(name) && | ||
!globalCommand.hasOption(name)) { | ||
console.error(`error: Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``); | ||
exit(1); | ||
throw new CACError(`Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``); | ||
} | ||
@@ -468,6 +475,6 @@ } | ||
checkOptionValue() { | ||
const { rawOptions, globalCommand } = this.cli; | ||
const { options: parsedOptions, globalCommand } = this.cli; | ||
const options = [...globalCommand.options, ...this.options]; | ||
for (const option of options) { | ||
const value = rawOptions[option.name.split('.')[0]]; | ||
const value = parsedOptions[option.name.split('.')[0]]; | ||
// Check required option value | ||
@@ -477,4 +484,3 @@ if (option.required) { | ||
if (value === true || (value === false && !hasNegated)) { | ||
console.error(`error: option \`${option.rawName}\` value is missing`); | ||
exit(1); | ||
throw new CACError(`option \`${option.rawName}\` value is missing`); | ||
} | ||
@@ -562,3 +568,2 @@ } | ||
* | ||
* This will also call `process.exit(0)` to quit the process. | ||
*/ | ||
@@ -576,3 +581,2 @@ outputHelp() { | ||
* | ||
* This will also call `process.exit(0)` to quit the process. | ||
*/ | ||
@@ -582,6 +586,5 @@ outputVersion() { | ||
} | ||
setParsedInfo({ args, options, rawOptions }, matchedCommand, matchedCommandName) { | ||
setParsedInfo({ args, options }, matchedCommand, matchedCommandName) { | ||
this.args = args; | ||
this.options = options; | ||
this.rawOptions = rawOptions; | ||
if (matchedCommand) { | ||
@@ -608,7 +611,7 @@ this.matchedCommand = matchedCommand; | ||
for (const command of this.commands) { | ||
const mriResult = this.mri(argv.slice(2), command); | ||
const commandName = mriResult.args[0]; | ||
const parsed = this.mri(argv.slice(2), command); | ||
const commandName = parsed.args[0]; | ||
if (command.isMatched(commandName)) { | ||
shouldParse = false; | ||
const parsedInfo = Object.assign({}, mriResult, { args: mriResult.args.slice(1) }); | ||
const parsedInfo = Object.assign({}, parsed, { args: parsed.args.slice(1) }); | ||
this.setParsedInfo(parsedInfo, command, commandName); | ||
@@ -623,4 +626,4 @@ this.emit(`command:${commandName}`, command); | ||
shouldParse = false; | ||
const mriResult = this.mri(argv.slice(2), command); | ||
this.setParsedInfo(mriResult, command); | ||
const parsed = this.mri(argv.slice(2), command); | ||
this.setParsedInfo(parsed, command); | ||
this.emit(`command:!`, command); | ||
@@ -631,4 +634,4 @@ } | ||
if (shouldParse) { | ||
const mriResult = this.mri(argv.slice(2)); | ||
this.setParsedInfo(mriResult); | ||
const parsed = this.mri(argv.slice(2)); | ||
this.setParsedInfo(parsed); | ||
} | ||
@@ -665,3 +668,6 @@ if (this.options.help && this.showHelpOnExit) { | ||
} | ||
const parsed = lib(argv, mriOptions); | ||
let parsed = lib(argv, mriOptions); | ||
parsed = Object.keys(parsed).reduce((res, name) => { | ||
return Object.assign({}, res, { [camelcaseOptionName(name)]: parsed[name] }); | ||
}, { _: [] }); | ||
const args = parsed._; | ||
@@ -701,4 +707,3 @@ delete parsed._; | ||
args, | ||
options, | ||
rawOptions: parsed | ||
options | ||
}; | ||
@@ -705,0 +710,0 @@ } |
81
mod.js
@@ -219,2 +219,24 @@ import { EventEmitter } from 'events'; | ||
}; | ||
const camelcaseOptionName = (name) => { | ||
// Camelcase the option name | ||
// Don't camelcase anything after the dot `.` | ||
return name | ||
.split('.') | ||
.map((v, i) => { | ||
return i === 0 ? camelcase(v) : v; | ||
}) | ||
.join('.'); | ||
}; | ||
class CACError extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
if (typeof Error.captureStackTrace === 'function') { | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
else { | ||
this.stack = new Error(message).stack; | ||
} | ||
} | ||
} | ||
@@ -237,11 +259,3 @@ class Option { | ||
} | ||
// Camelcase the option name | ||
// Don't camelcase anything after the dot `.` | ||
name = name | ||
.split('.') | ||
.map((v, i) => { | ||
return i === 0 ? camelcase(v) : v; | ||
}) | ||
.join('.'); | ||
return name; | ||
return camelcaseOptionName(name); | ||
}) | ||
@@ -268,5 +282,2 @@ .sort((a, b) => (a.length > b.length ? 1 : -1)); // Sort names | ||
const deno = typeof window !== 'undefined' && window.Deno; | ||
const exit = (code) => { | ||
return deno ? Deno.exit(code) : process.exit(code); | ||
}; | ||
const processArgs = deno ? ['deno'].concat(Deno.args) : process.argv; | ||
@@ -421,3 +432,2 @@ const platformInfo = deno | ||
.join('\n\n')); | ||
exit(0); | ||
} | ||
@@ -430,3 +440,2 @@ outputVersion() { | ||
} | ||
exit(0); | ||
} | ||
@@ -436,4 +445,3 @@ checkRequiredArgs() { | ||
if (this.cli.args.length < minimalArgsCount) { | ||
console.error(`error: missing required args for command \`${this.rawName}\``); | ||
exit(1); | ||
throw new CACError(`missing required args for command \`${this.rawName}\``); | ||
} | ||
@@ -447,10 +455,9 @@ } | ||
checkUnknownOptions() { | ||
const { rawOptions, globalCommand } = this.cli; | ||
const { options, globalCommand } = this.cli; | ||
if (!this.config.allowUnknownOptions) { | ||
for (const name of Object.keys(rawOptions)) { | ||
for (const name of Object.keys(options)) { | ||
if (name !== '--' && | ||
!this.hasOption(name) && | ||
!globalCommand.hasOption(name)) { | ||
console.error(`error: Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``); | ||
exit(1); | ||
throw new CACError(`Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``); | ||
} | ||
@@ -464,6 +471,6 @@ } | ||
checkOptionValue() { | ||
const { rawOptions, globalCommand } = this.cli; | ||
const { options: parsedOptions, globalCommand } = this.cli; | ||
const options = [...globalCommand.options, ...this.options]; | ||
for (const option of options) { | ||
const value = rawOptions[option.name.split('.')[0]]; | ||
const value = parsedOptions[option.name.split('.')[0]]; | ||
// Check required option value | ||
@@ -473,4 +480,3 @@ if (option.required) { | ||
if (value === true || (value === false && !hasNegated)) { | ||
console.error(`error: option \`${option.rawName}\` value is missing`); | ||
exit(1); | ||
throw new CACError(`option \`${option.rawName}\` value is missing`); | ||
} | ||
@@ -558,3 +564,2 @@ } | ||
* | ||
* This will also call `process.exit(0)` to quit the process. | ||
*/ | ||
@@ -572,3 +577,2 @@ outputHelp() { | ||
* | ||
* This will also call `process.exit(0)` to quit the process. | ||
*/ | ||
@@ -578,6 +582,5 @@ outputVersion() { | ||
} | ||
setParsedInfo({ args, options, rawOptions }, matchedCommand, matchedCommandName) { | ||
setParsedInfo({ args, options }, matchedCommand, matchedCommandName) { | ||
this.args = args; | ||
this.options = options; | ||
this.rawOptions = rawOptions; | ||
if (matchedCommand) { | ||
@@ -604,7 +607,7 @@ this.matchedCommand = matchedCommand; | ||
for (const command of this.commands) { | ||
const mriResult = this.mri(argv.slice(2), command); | ||
const commandName = mriResult.args[0]; | ||
const parsed = this.mri(argv.slice(2), command); | ||
const commandName = parsed.args[0]; | ||
if (command.isMatched(commandName)) { | ||
shouldParse = false; | ||
const parsedInfo = Object.assign({}, mriResult, { args: mriResult.args.slice(1) }); | ||
const parsedInfo = Object.assign({}, parsed, { args: parsed.args.slice(1) }); | ||
this.setParsedInfo(parsedInfo, command, commandName); | ||
@@ -619,4 +622,4 @@ this.emit(`command:${commandName}`, command); | ||
shouldParse = false; | ||
const mriResult = this.mri(argv.slice(2), command); | ||
this.setParsedInfo(mriResult, command); | ||
const parsed = this.mri(argv.slice(2), command); | ||
this.setParsedInfo(parsed, command); | ||
this.emit(`command:!`, command); | ||
@@ -627,4 +630,4 @@ } | ||
if (shouldParse) { | ||
const mriResult = this.mri(argv.slice(2)); | ||
this.setParsedInfo(mriResult); | ||
const parsed = this.mri(argv.slice(2)); | ||
this.setParsedInfo(parsed); | ||
} | ||
@@ -661,3 +664,6 @@ if (this.options.help && this.showHelpOnExit) { | ||
} | ||
const parsed = lib(argv, mriOptions); | ||
let parsed = lib(argv, mriOptions); | ||
parsed = Object.keys(parsed).reduce((res, name) => { | ||
return Object.assign({}, res, { [camelcaseOptionName(name)]: parsed[name] }); | ||
}, { _: [] }); | ||
const args = parsed._; | ||
@@ -697,4 +703,3 @@ delete parsed._; | ||
args, | ||
options, | ||
rawOptions: parsed | ||
options | ||
}; | ||
@@ -701,0 +706,0 @@ } |
{ | ||
"name": "cac", | ||
"version": "6.5.4", | ||
"version": "6.5.5", | ||
"description": "Simple yet powerful framework for building command-line apps.", | ||
@@ -36,3 +36,3 @@ "repository": { | ||
"husky": "^1.2.0", | ||
"jest": "^23.6.0", | ||
"jest": "^24.9.0", | ||
"lint-staged": "^8.1.0", | ||
@@ -39,0 +39,0 @@ "markdown-toc": "^1.2.0", |
@@ -11,7 +11,2 @@ /// <reference types="node" /> | ||
} | ||
interface MriResult extends ParsedArgv { | ||
rawOptions: { | ||
[k: string]: any; | ||
}; | ||
} | ||
declare class CAC extends EventEmitter { | ||
@@ -31,11 +26,7 @@ /** The program name to display in help and version message */ | ||
*/ | ||
args: MriResult['args']; | ||
args: ParsedArgv['args']; | ||
/** | ||
* Parsed CLI options, camelCased | ||
*/ | ||
options: MriResult['options']; | ||
/** | ||
* Raw CLI options, i.e. not camelcased | ||
*/ | ||
rawOptions: MriResult['rawOptions']; | ||
options: ParsedArgv['options']; | ||
private showHelpOnExit; | ||
@@ -84,3 +75,2 @@ private showVersionOnExit; | ||
* | ||
* This will also call `process.exit(0)` to quit the process. | ||
*/ | ||
@@ -91,3 +81,2 @@ outputHelp(): void; | ||
* | ||
* This will also call `process.exit(0)` to quit the process. | ||
*/ | ||
@@ -94,0 +83,0 @@ outputVersion(): void; |
@@ -1,3 +0,2 @@ | ||
export declare const exit: (code: number) => any; | ||
export declare const processArgs: string[]; | ||
export declare const platformInfo: string; |
@@ -27,2 +27,6 @@ import Option from './Option'; | ||
export declare const getFileName: (input: string) => string; | ||
export declare const camelcaseOptionName: (name: string) => string; | ||
export declare class CACError extends Error { | ||
constructor(message: string); | ||
} | ||
export {}; |
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
1620
70063