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CAAC,MAAM,KAAK,CAAC,EAAE;YACrD,MAAM,IAAI,wBAAgB,CAAC;gBACvB,OAAO,EAAE,GAAG,IAAI,CAAC,IAAI,kCAAkC;gBACvD,KAAK,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC;gBAClB,MAAM,EAAE,mBAAW,CAAC,iBAAiB;gBACrC,MAAM,EAAE;oBACJ,MAAM,EAAE,IAAI;oBACZ,IAAI;oBACJ,IAAI;iBACP;aACJ,CAAC,CAAC;SACN;QACD,IAAI,IAAI,CAAC,QAAQ,EAAE;YACf,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;YACvB,IAAI,GAAG,EAAE,CAAC;YACV,OAAO,IAAI,CAAC;SACf;QACD,IAAI,GAAG,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;QACvB,IAAI,IAAI,CAAC,QAAQ,IAAI,CAAC,GAAG,EAAE;YACvB,MAAM,IAAI,wBAAgB,CAAC;gBACvB,OAAO,EAAE,GAAG,IAAI,CAAC,IAAI,cAAc;gBACnC,MAAM,EAAE,mBAAW,CAAC,gBAAgB;gBACpC,KAAK,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC;gBAClB,MAAM,EAAE;oBACJ,MAAM,EAAE,IAAI;oBACZ,IAAI;oBACJ,IAAI;iBACP;aACJ,CAAC,CAAC;SACN;QACD,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC;QACtB,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AAlGD,2BAkGC","sourcesContent":["import { CommandpostError, ErrorReason } from \"./error\";\n\n// jsdoc, see constructor.\nexport default class Argument {\n /** argument name */\n name: string;\n /** this argument is required */\n required: boolean;\n /** this argument is variadic */\n variadic: boolean;\n\n /**\n * class of argument.\n * ```\n * cmd --path foo/bar buzz.txt\n * ↑ this one!\n * ```\n * @param arg pass '<foo>'(required) or '[foo]'(optional) or '<foo...>'(required & variadic) or '[foo...]'(optional & variadic)\n * @class\n */\n constructor(arg: string) {\n switch (arg.charAt(0)) {\n case \"<\":\n this.required = true;\n this.name = arg.slice(1, -1);\n break;\n case \"[\":\n this.required = false;\n this.name = arg.slice(1, -1);\n break;\n default:\n throw new CommandpostError({\n message: `unsupported format: ${arg}`,\n parts: [arg],\n reason: ErrorReason.UnsupportedFormatArgument,\n params: {\n origin: this,\n arg,\n },\n });\n }\n if (/\\.\\.\\.$/.test(this.name)) {\n this.name = this.name.slice(0, -3);\n this.variadic = true;\n } else {\n this.variadic = false;\n }\n }\n\n /**\n * parse args.\n * build to opts.\n *\n * e.g. #1\n * instance member: name=foo, required=true, variadic=false\n * method arguments: opts={}, args=[\"foo!\", \"bar!\"].\n * opts are modified to { foo: \"foo!\" } and return [\"bar!\"].\n *\n * e.g. #2\n * instance member: name=foo, required=false, variadic=true\n * method arguments: opts={}, args=[\"foo!\", \"bar!\"].\n * opts are modified to { foo: [\"foo!\", \"bar!\"] } and return [].\n *\n * @param opts build target object\n * @param args\n * @returns {string[]} rest args\n */\n parse(opts: any, args: string[]): string[] {\n if (this.required && this.variadic && args.length === 0) {\n throw new CommandpostError({\n message: `${this.name} requires more than one argument`,\n parts: [this.name],\n reason: ErrorReason.ArgumentsRequired,\n params: {\n origin: this,\n opts,\n args,\n },\n });\n }\n if (this.variadic) {\n opts[this.name] = args;\n args = [];\n return args;\n }\n let arg = args.shift();\n if (this.required && !arg) {\n throw new CommandpostError({\n message: `${this.name} is required`,\n reason: ErrorReason.ArgumentRequired,\n parts: [this.name],\n params: {\n origin: this,\n opts,\n args,\n },\n });\n }\n opts[this.name] = arg;\n return args;\n }\n}\n"]}
\No newline at end of file