argument.js
This file
Path
lib/argument.js
147 lines · 3.1 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
The walk stopped after 200 commits: lines older than 471ec40 point at it.
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
line 1why, one step back1import { InvalidArgumentError } from './error.js';Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 2why, one step back2Switching to esm-only (#2464)John Geebefore adoptionf4bd470
line 3why, one step back3export class Argument {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 4–35why, one step back4 /**5 * Initialize a new command argument with the given name and description.6 * The default is that the argument is required, and you can explicitly7 * indicate this with <> around the name. Put [] around the name for an optional argument.8 *9 * @param {string} name10 * @param {string} [description]11 */1213 constructor(name, description) {14 this.description = description || '';15 this.variadic = false;16 this.parseArg = undefined;17 this.defaultValue = undefined;18 this.defaultValueDescription = undefined;19 this.argChoices = undefined;2021 switch (name[0]) {22 case '<': // e.g. <required>23 this.required = true;24 this._name = name.slice(1, -1);25 break;26 case '[': // e.g. [optional]27 this.required = false;28 this._name = name.slice(1, -1);29 break;30 default:31 this.required = true;32 this._name = name;33 break;34 }35Merge from develop (#2482)John Geebefore adoptionb51c0ea
line 36why, one step back36 if (this._name.endsWith('...')) {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 37–52why, one step back37 this.variadic = true;38 this._name = this._name.slice(0, -3);39 }40 }4142 /**43 * Return argument name.44 *45 * @return {string}46 */4748 name() {49 return this._name;50 }5152 /**Fix some JSDoc lint issues (#2181)John Geebefore adoptionb95ea44
line 53why, one step back53 * @packageAdd copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 54–55why, one step back54 */55Merge from develop (#2482)John Geebefore adoptionb51c0ea
line 56why, one step back56 _collectValue(value, previous) {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 57–60why, one step back57 if (previous === this.defaultValue || !Array.isArray(previous)) {58 return [value];59 }60Merge from develop (#2482)John Geebefore adoptionb51c0ea
lines 61–62why, one step back61 previous.push(value);62 return previous;Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 63–67why, one step back63 }6465 /**66 * Set the default value, and optionally supply the description to be displayed in the help.67 *Replace {any} with {*} as native JSDoc (#2015)John Geebefore adoption26a34e6
line 68why, one step back68 * @param {*} valueAdd copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 69–101why, one step back69 * @param {string} [description]70 * @return {Argument}71 */7273 default(value, description) {74 this.defaultValue = value;75 this.defaultValueDescription = description;76 return this;77 }7879 /**80 * Set the custom handler for processing CLI command arguments into argument values.81 *82 * @param {Function} [fn]83 * @return {Argument}84 */8586 argParser(fn) {87 this.parseArg = fn;88 return this;89 }9091 /**92 * Only allow argument value to be one of choices.93 *94 * @param {string[]} values95 * @return {Argument}96 */9798 choices(values) {99 this.argChoices = values.slice();100 this.parseArg = (arg, previous) => {101 if (!this.argChoices.includes(arg)) {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 102–104why, one step back102 throw new InvalidArgumentError(103 `Allowed choices are ${this.argChoices.join(', ')}.`,104 );Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 105–106why, one step back105 }106 if (this.variadic) {Merge from develop (#2482)John Geebefore adoptionb51c0ea
line 107why, one step back107 return this._collectValue(arg, previous);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 108–115why, one step back108 }109 return arg;110 };111 return this;112 }113114 /**115 * Make argument required.Fix some JSDoc lint issues (#2181)John Geebefore adoptionb95ea44
lines 116–117why, one step back116 *117 * @returns {Argument}Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 118–125why, one step back118 */119 argRequired() {120 this.required = true;121 return this;122 }123124 /**125 * Make argument optional.Fix some JSDoc lint issues (#2181)John Geebefore adoptionb95ea44
lines 126–127why, one step back126 *127 * @returns {Argument}Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 128–139why, one step back128 */129 argOptional() {130 this.required = false;131 return this;132 }133}134135/**136 * Takes an argument and returns its human readable equivalent for help usage.137 *138 * @param {Argument} arg139 * @return {string}Merge pull request #2140 from tj/release/12.xJohn Geebefore adoptione3192a1
line 140why, one step back140 * @privateAdd copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 141–142why, one step back141 */142Switching to esm-only (#2464)John Geebefore adoptionf4bd470
line 143why, one step back143export function humanReadableArgName(arg) {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 144–145why, one step back144 const nameOutput = arg.name() + (arg.variadic === true ? '...' : '');145Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 146why, one step back146 return arg.required ? '<' + nameOutput + '>' : '[' + nameOutput + ']';Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 147why, one step back147}