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aACN;YACD,IAAI,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;YAC9C,IAAI,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC9B,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;SACxB;aAAM,IAAI,IAAI,CAAC,QAAQ,EAAE;YACtB,IAAI,IAAI,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;gBAClC,IAAI,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;gBAC9C,IAAI,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBAC9B,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;aACxB;iBAAM;gBACH,IAAI,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;gBAC9C,IAAI,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;gBAC3C,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;aACxB;SACJ;aAAM;YACH,IAAI,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;YAC5C,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;SACxB;IACL,CAAC;CACJ;AAlID,yBAkIC","sourcesContent":["import { CommandpostError, ErrorReason } from \"./error\";\nimport * as utils from \"./utils\";\n\n// jsdoc, see constructor.\nexport default class Option {\n /** this option need parameter value. it is required. */\n required: boolean;\n /** this option need parameter value. it is optional. */\n optional: boolean;\n /** this option is default true. if specified this option, value is become false. */\n no: boolean;\n /** short style. e.g. -o */\n short?: string;\n /** long style. e.g. --option */\n long: string;\n /** description of this option */\n description: string;\n\n /**\n * class of option.\n * ```\n * cmd --path foo/bar buzz.txt\n * ↑ this one!\n * ```\n * @param flags pass '-f, --foo'(boolean) or '--foo'(boolean) or '--foo <bar>'(string[]) or '--foo [bar]'(string[]).\n * @param description\n * @param defaultValue\n * @class\n */\n constructor(public flags: string, description?: string, public defaultValue?: any) {\n this.required = flags.indexOf(\"<\") !== -1;\n this.optional = flags.indexOf(\"[\") !== -1;\n this.no = flags.indexOf(\"-no-\") === -1;\n let splittedFlags = flags.split(/[ ,|]+/);\n if (splittedFlags.length > 1 && !/^[[<]/.test(splittedFlags[1])) {\n this.short = splittedFlags.shift()!;\n }\n this.long = splittedFlags.shift()!;\n this.description = description || \"\";\n if (typeof this.defaultValue === \"undefined\") {\n if (this.required || this.optional) {\n this.defaultValue = \"\";\n } else {\n this.defaultValue = !this.no;\n }\n }\n }\n\n /**\n * name of this option.\n * @returns {any}\n */\n name() {\n return this.long.replace(\"--\", \"\").replace(\"no-\", \"\");\n }\n\n /**\n * check arg is matches this option.\n * @param arg\n * @returns {boolean}\n */\n is(arg: string) {\n return arg === this.short || arg === this.long;\n }\n\n /**\n * parse args.\n * build to opts.\n *\n * e.g. #1\n * instance member: required=true, optional=false, short=-f, long=--foo\n * method arguments: opts={}, args=[\"--foo\", \"foo!\", \"bar!\"].\n * opts are modified to { foo: [\"foo!\"] } and return [\"bar!\"].\n *\n * e.g. #2\n * instance member: required=true, optional=false, short=-f, long=--foo\n * method arguments: opts={ foo: [\"foo?\"] }, args=[\"--foo\", \"foo!\", \"bar!\"].\n * opts are modified to { foo: [\"foo?\", \"foo!\"] } and return [\"bar!\"].\n *\n * e.g. #3\n * instance member: required=false, optional=false, short=-f, long=--foo\n * method arguments: opts={}, args=[\"-f\", \"foo!\", \"bar!\"].\n * opts are modified to { foo: true } and return [\"foo!\", \"bar!\"].\n *\n * @param opts\n * @param args\n * @returns {string[]}\n */\n parse(opts: any, args: string[]): string[] {\n if (!this.is(args[0])) {\n throw new CommandpostError({\n message: `${args[0]} is not match ${this.short} or ${this.long}`,\n reason: ErrorReason.OptionNameMismatch,\n parts: [args[0]],\n params: {\n option: this,\n opts,\n args,\n },\n });\n }\n let next = args[1];\n let propertyName = utils.kebabToLowerCamelCase(this.name());\n if (this.required) {\n if (next == null) {\n throw new CommandpostError({\n message: `${args[0]} is required parameter value`,\n reason: ErrorReason.OptionValueRequired,\n parts: [args[0]],\n params: {\n option: this,\n opts,\n args,\n },\n });\n }\n opts[propertyName] = opts[propertyName] || [];\n opts[propertyName].push(next);\n return args.slice(2);\n } else if (this.optional) {\n if (next != null && !/^-/.test(next)) {\n opts[propertyName] = opts[propertyName] || [];\n opts[propertyName].push(next);\n return args.slice(2);\n } else {\n opts[propertyName] = opts[propertyName] || [];\n opts[propertyName].push(this.defaultValue);\n return args.slice(1);\n }\n } else {\n opts[propertyName] = this.no ? true : false;\n return args.slice(1);\n }\n }\n}\n"]}
\No newline at end of file