commandpost
Version:
commandline option parser
98 lines • 3.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const error_1 = require("./error");
// jsdoc, see constructor.
class Argument {
/**
* class of argument.
* ```
* cmd --path foo/bar buzz.txt
* ↑ this one!
* ```
* @param arg pass '<foo>'(required) or '[foo]'(optional) or '<foo...>'(required & variadic) or '[foo...]'(optional & variadic)
* @class
*/
constructor(arg) {
switch (arg.charAt(0)) {
case "<":
this.required = true;
this.name = arg.slice(1, -1);
break;
case "[":
this.required = false;
this.name = arg.slice(1, -1);
break;
default:
throw new error_1.CommandpostError({
message: `unsupported format: ${arg}`,
parts: [arg],
reason: error_1.ErrorReason.UnsupportedFormatArgument,
params: {
origin: this,
arg,
},
});
}
if (/\.\.\.$/.test(this.name)) {
this.name = this.name.slice(0, -3);
this.variadic = true;
}
else {
this.variadic = false;
}
}
/**
* parse args.
* build to opts.
*
* e.g. #1
* instance member: name=foo, required=true, variadic=false
* method arguments: opts={}, args=["foo!", "bar!"].
* opts are modified to { foo: "foo!" } and return ["bar!"].
*
* e.g. #2
* instance member: name=foo, required=false, variadic=true
* method arguments: opts={}, args=["foo!", "bar!"].
* opts are modified to { foo: ["foo!", "bar!"] } and return [].
*
* @param opts build target object
* @param args
* @returns {string[]} rest args
*/
parse(opts, args) {
if (this.required && this.variadic && args.length === 0) {
throw new error_1.CommandpostError({
message: `${this.name} requires more than one argument`,
parts: [this.name],
reason: error_1.ErrorReason.ArgumentsRequired,
params: {
origin: this,
opts,
args,
},
});
}
if (this.variadic) {
opts[this.name] = args;
args = [];
return args;
}
let arg = args.shift();
if (this.required && !arg) {
throw new error_1.CommandpostError({
message: `${this.name} is required`,
reason: error_1.ErrorReason.ArgumentRequired,
parts: [this.name],
params: {
origin: this,
opts,
args,
},
});
}
opts[this.name] = arg;
return args;
}
}
exports.default = Argument;
//# sourceMappingURL=argument.js.map