commander
Advanced tools
Comparing version 8.2.0 to 8.3.0
@@ -39,3 +39,3 @@ const EventEmitter = require('events').EventEmitter; | ||
this._optionValues = {}; | ||
this._optionValueSources = {}; // default < env < cli | ||
this._optionValueSources = {}; // default < config < env < cli | ||
this._storeOptionsAsProperties = false; | ||
@@ -467,6 +467,6 @@ this._actionHandler = null; | ||
* program | ||
* .command('help') | ||
* .description('display verbose help') | ||
* .command('serve') | ||
* .description('start service') | ||
* .action(function() { | ||
* // output help here | ||
* // do work here | ||
* }); | ||
@@ -532,3 +532,3 @@ * | ||
if (defaultValue !== undefined) { | ||
this._setOptionValueWithSource(name, defaultValue, 'default'); | ||
this.setOptionValueWithSource(name, defaultValue, 'default'); | ||
} | ||
@@ -564,9 +564,9 @@ } | ||
if (val == null) { | ||
this._setOptionValueWithSource(name, option.negate ? false : defaultValue || true, valueSource); | ||
this.setOptionValueWithSource(name, option.negate ? false : defaultValue || true, valueSource); | ||
} else { | ||
this._setOptionValueWithSource(name, val, valueSource); | ||
this.setOptionValueWithSource(name, val, valueSource); | ||
} | ||
} else if (val !== null) { | ||
// reassign | ||
this._setOptionValueWithSource(name, option.negate ? false : val, valueSource); | ||
this.setOptionValueWithSource(name, option.negate ? false : val, valueSource); | ||
} | ||
@@ -800,10 +800,29 @@ }; | ||
/** | ||
* @api private | ||
*/ | ||
_setOptionValueWithSource(key, value, source) { | ||
* Store option value and where the value came from. | ||
* | ||
* @param {string} key | ||
* @param {Object} value | ||
* @param {string} source - expected values are default/config/env/cli | ||
* @return {Command} `this` command for chaining | ||
*/ | ||
setOptionValueWithSource(key, value, source) { | ||
this.setOptionValue(key, value); | ||
this._optionValueSources[key] = source; | ||
return this; | ||
} | ||
/** | ||
* Get source of option value. | ||
* Expected values are default | config | env | cli | ||
* | ||
* @param {string} key | ||
* @return {string} | ||
*/ | ||
getOptionValueSource(key) { | ||
return this._optionValueSources[key]; | ||
}; | ||
/** | ||
* Get user arguments implied or explicit arguments. | ||
@@ -1120,2 +1139,3 @@ * Side-effects: set _scriptPath if args included application, and use that to set implicit command name. | ||
* @return {Promise|undefined} | ||
* @api private | ||
*/ | ||
@@ -1465,4 +1485,4 @@ | ||
const optionKey = option.attributeName(); | ||
// env is second lowest priority source, above default | ||
if (this.getOptionValue(optionKey) === undefined || this._optionValueSources[optionKey] === 'default') { | ||
// Priority check. Do not overwrite cli or options from unknown source (client-code). | ||
if (this.getOptionValue(optionKey) === undefined || ['default', 'config', 'env'].includes(this.getOptionValueSource(optionKey))) { | ||
if (option.required || option.optional) { // option can take a value | ||
@@ -1469,0 +1489,0 @@ // keep very simple, optional always takes value |
{ | ||
"name": "commander", | ||
"version": "8.2.0", | ||
"version": "8.3.0", | ||
"description": "the complete solution for node.js command-line programs", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -101,3 +101,4 @@ # Commander.js | ||
The parsed options can be accessed by calling `.opts()` on a `Command` object, and are passed to the action handler. | ||
You can also use `.getOptionValue()` and `.setOptionValue()` to work with a single option value. | ||
(You can also use `.getOptionValue()` and `.setOptionValue()` to work with a single option value, | ||
and `.getOptionValueSource()` and `.setOptionValueWithSource()` when it matters where the option value came from.) | ||
@@ -782,9 +783,2 @@ Multi-word options such as "--template-engine" are camel-cased, becoming `program.opts().templateEngine` etc. | ||
}); | ||
program.on('command:*', function (operands) { | ||
console.error(`error: unknown command '${operands[0]}'`); | ||
const availableCommands = program.commands.map(cmd => cmd.name()); | ||
mySuggestBestMatch(operands[0], availableCommands); | ||
process.exitCode = 1; | ||
}); | ||
``` | ||
@@ -791,0 +785,0 @@ |
@@ -217,4 +217,5 @@ // Type definitions for commander | ||
type AddHelpTextPosition = 'beforeAll' | 'before' | 'after' | 'afterAll'; | ||
type HookEvent = 'preAction' | 'postAction'; | ||
export type AddHelpTextPosition = 'beforeAll' | 'before' | 'after' | 'afterAll'; | ||
export type HookEvent = 'preAction' | 'postAction'; | ||
export type OptionValueSource = 'default' | 'env' | 'config' | 'cli'; | ||
@@ -428,6 +429,6 @@ export interface OptionValues { | ||
* program | ||
* .command('help') | ||
* .description('display verbose help') | ||
* .command('serve') | ||
* .description('start service') | ||
* .action(function() { | ||
* // output help here | ||
* // do work here | ||
* }); | ||
@@ -531,3 +532,3 @@ * ``` | ||
/** | ||
/** | ||
* Store option value. | ||
@@ -538,2 +539,12 @@ */ | ||
/** | ||
* Store option value and where the value came from. | ||
*/ | ||
setOptionValueWithSource(key: string, value: unknown, source: OptionValueSource): this; | ||
/** | ||
* Retrieve option value source. | ||
*/ | ||
getOptionValueSource(key: string): OptionValueSource; | ||
/** | ||
* Alter parsing of short flags with optional values. | ||
@@ -540,0 +551,0 @@ * |
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
151267
3248
1016