|
const { InvalidArgumentError } = require('./error.js'); |
|
|
|
|
|
|
|
class Argument { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(name, description) { |
|
this.description = description || ''; |
|
this.variadic = false; |
|
this.parseArg = undefined; |
|
this.defaultValue = undefined; |
|
this.defaultValueDescription = undefined; |
|
this.argChoices = undefined; |
|
|
|
switch (name[0]) { |
|
case '<': |
|
this.required = true; |
|
this._name = name.slice(1, -1); |
|
break; |
|
case '[': |
|
this.required = false; |
|
this._name = name.slice(1, -1); |
|
break; |
|
default: |
|
this.required = true; |
|
this._name = name; |
|
break; |
|
} |
|
|
|
if (this._name.length > 3 && this._name.slice(-3) === '...') { |
|
this.variadic = true; |
|
this._name = this._name.slice(0, -3); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
name() { |
|
return this._name; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
_concatValue(value, previous) { |
|
if (previous === this.defaultValue || !Array.isArray(previous)) { |
|
return [value]; |
|
} |
|
|
|
return previous.concat(value); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default(value, description) { |
|
this.defaultValue = value; |
|
this.defaultValueDescription = description; |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
argParser(fn) { |
|
this.parseArg = fn; |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
choices(values) { |
|
this.argChoices = values.slice(); |
|
this.parseArg = (arg, previous) => { |
|
if (!this.argChoices.includes(arg)) { |
|
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(', ')}.`); |
|
} |
|
if (this.variadic) { |
|
return this._concatValue(arg, previous); |
|
} |
|
return arg; |
|
}; |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
argRequired() { |
|
this.required = true; |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
argOptional() { |
|
this.required = false; |
|
return this; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function humanReadableArgName(arg) { |
|
const nameOutput = arg.name() + (arg.variadic === true ? '...' : ''); |
|
|
|
return arg.required |
|
? '<' + nameOutput + '>' |
|
: '[' + nameOutput + ']'; |
|
} |
|
|
|
exports.Argument = Argument; |
|
exports.humanReadableArgName = humanReadableArgName; |
|
|