argument.js
This file
Path
lib/argument.js
147 lines · 3.1 KiB · JavaScript
Last changed by
Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
History of this fileTake it
Choices for arguments (#1525)John Geebefore adoption708527e
lines 1–2why, one step back1const { InvalidArgumentError } = require('./error.js');2Refactor classes into files (#1522)John Geebefore adoption08245cc
lines 3–20why, one step back3// @ts-check45class Argument {6 /**7 * Initialize a new command argument with the given name and description.8 * The default is that the argument is required, and you can explicitly9 * indicate this with <> around the name. Put [] around the name for an optional argument.10 *11 * @param {string} name12 * @param {string} [description]13 */1415 constructor(name, description) {16 this.description = description || '';17 this.variadic = false;18 this.parseArg = undefined;19 this.defaultValue = undefined;20 this.defaultValueDescription = undefined;Choices for arguments (#1525)John Geebefore adoption708527e
line 21why, one step back21 this.argChoices = undefined;Refactor classes into files (#1522)John Geebefore adoption08245cc
lines 22–51why, one step back2223 switch (name[0]) {24 case '<': // e.g. <required>25 this.required = true;26 this._name = name.slice(1, -1);27 break;28 case '[': // e.g. [optional]29 this.required = false;30 this._name = name.slice(1, -1);31 break;32 default:33 this.required = true;34 this._name = name;35 break;36 }3738 if (this._name.length > 3 && this._name.slice(-3) === '...') {39 this.variadic = true;40 this._name = this._name.slice(0, -3);41 }42 }4344 /**45 * Return argument name.46 *47 * @return {string}48 */4950 name() {51 return this._name;Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 52why, one step back52 }Refactor classes into files (#1522)John Geebefore adoption08245cc
lines 53–54why, one step back5354 /**Choices for arguments (#1525)John Geebefore adoption708527e
lines 55–66why, one step back55 * @api private56 */5758 _concatValue(value, previous) {59 if (previous === this.defaultValue || !Array.isArray(previous)) {60 return [value];61 }6263 return previous.concat(value);64 }6566 /**Refactor classes into files (#1522)John Geebefore adoption08245cc
lines 67–77why, one step back67 * Set the default value, and optionally supply the description to be displayed in the help.68 *69 * @param {any} value70 * @param {string} [description]71 * @return {Argument}72 */7374 default(value, description) {75 this.defaultValue = value;76 this.defaultValueDescription = description;77 return this;Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 78why, one step back78 }Refactor classes into files (#1522)John Geebefore adoption08245cc
lines 79–88why, one step back7980 /**81 * Set the custom handler for processing CLI command arguments into argument values.82 *83 * @param {Function} [fn]84 * @return {Argument}85 */8687 argParser(fn) {88 this.parseArg = fn;Choices for arguments (#1525)John Geebefore adoption708527e
line 89why, one step back89 return this;Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 90why, one step back90 }Choices for arguments (#1525)John Geebefore adoption708527e
lines 91–92why, one step back9192 /**Fix comments to refer to argument rather than option (#1630)John Geebefore adoptionc3b419a
line 93why, one step back93 * Only allow argument value to be one of choices.Choices for arguments (#1525)John Geebefore adoption708527e
lines 94–99why, one step back94 *95 * @param {string[]} values96 * @return {Argument}97 */9899 choices(values) {Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 100why, one step back100 this.argChoices = values.slice();Choices for arguments (#1525)John Geebefore adoption708527e
line 101why, one step back101 this.parseArg = (arg, previous) => {Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 102–103why, one step back102 if (!this.argChoices.includes(arg)) {103 throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(', ')}.`);Choices for arguments (#1525)John Geebefore adoption708527e
lines 104–109why, one step back104 }105 if (this.variadic) {106 return this._concatValue(arg, previous);107 }108 return arg;109 };Refactor classes into files (#1522)John Geebefore adoption08245cc
line 110why, one step back110 return this;Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 111why, one step back111 }Feature/argument arg explicit (#1567)John Geebefore adoption6f51e4a
lines 112–113why, one step back112113 /**Fix comments to refer to argument rather than option (#1630)John Geebefore adoptionc3b419a
line 114why, one step back114 * Make argument required.Feature/argument arg explicit (#1567)John Geebefore adoption6f51e4a
lines 115–121why, one step back115 */116 argRequired() {117 this.required = true;118 return this;119 }120121 /**Fix comments to refer to argument rather than option (#1630)John Geebefore adoptionc3b419a
line 122why, one step back122 * Make argument optional.Feature/argument arg explicit (#1567)John Geebefore adoption6f51e4a
lines 123–127why, one step back123 */124 argOptional() {125 this.required = false;126 return this;127 }Refactor classes into files (#1522)John Geebefore adoption08245cc
lines 128–147why, one step back128}129130/**131 * Takes an argument and returns its human readable equivalent for help usage.132 *133 * @param {Argument} arg134 * @return {string}135 * @api private136 */137138function humanReadableArgName(arg) {139 const nameOutput = arg.name() + (arg.variadic === true ? '...' : '');140141 return arg.required142 ? '<' + nameOutput + '>'143 : '[' + nameOutput + ']';144}145146exports.Argument = Argument;147exports.humanReadableArgName = humanReadableArgName;