UNPKG

4.59 kBJavaScriptView Raw
1"use strict";
2var __importStar = (this && this.__importStar) || function (mod) {
3 if (mod && mod.__esModule) return mod;
4 var result = {};
5 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6 result["default"] = mod;
7 return result;
8};
9Object.defineProperty(exports, "__esModule", { value: true });
10const error_1 = require("./error");
11const utils = __importStar(require("./utils"));
12// jsdoc, see constructor.
13class Option {
14 /**
15 * class of option.
16 * ```
17 * cmd --path foo/bar buzz.txt
18 * ↑ this one!
19 * ```
20 * @param flags pass '-f, --foo'(boolean) or '--foo'(boolean) or '--foo <bar>'(string[]) or '--foo [bar]'(string[]).
21 * @param description
22 * @param defaultValue
23 * @class
24 */
25 constructor(flags, description, defaultValue) {
26 this.flags = flags;
27 this.defaultValue = defaultValue;
28 this.required = flags.indexOf("<") !== -1;
29 this.optional = flags.indexOf("[") !== -1;
30 this.no = flags.indexOf("-no-") === -1;
31 let splittedFlags = flags.split(/[ ,|]+/);
32 if (splittedFlags.length > 1 && !/^[[<]/.test(splittedFlags[1])) {
33 this.short = splittedFlags.shift();
34 }
35 this.long = splittedFlags.shift();
36 this.description = description || "";
37 if (typeof this.defaultValue === "undefined") {
38 if (this.required || this.optional) {
39 this.defaultValue = "";
40 }
41 else {
42 this.defaultValue = !this.no;
43 }
44 }
45 }
46 /**
47 * name of this option.
48 * @returns {any}
49 */
50 name() {
51 return this.long.replace("--", "").replace("no-", "");
52 }
53 /**
54 * check arg is matches this option.
55 * @param arg
56 * @returns {boolean}
57 */
58 is(arg) {
59 return arg === this.short || arg === this.long;
60 }
61 /**
62 * parse args.
63 * build to opts.
64 *
65 * e.g. #1
66 * instance member: required=true, optional=false, short=-f, long=--foo
67 * method arguments: opts={}, args=["--foo", "foo!", "bar!"].
68 * opts are modified to { foo: ["foo!"] } and return ["bar!"].
69 *
70 * e.g. #2
71 * instance member: required=true, optional=false, short=-f, long=--foo
72 * method arguments: opts={ foo: ["foo?"] }, args=["--foo", "foo!", "bar!"].
73 * opts are modified to { foo: ["foo?", "foo!"] } and return ["bar!"].
74 *
75 * e.g. #3
76 * instance member: required=false, optional=false, short=-f, long=--foo
77 * method arguments: opts={}, args=["-f", "foo!", "bar!"].
78 * opts are modified to { foo: true } and return ["foo!", "bar!"].
79 *
80 * @param opts
81 * @param args
82 * @returns {string[]}
83 */
84 parse(opts, args) {
85 if (!this.is(args[0])) {
86 throw new error_1.CommandpostError({
87 message: `${args[0]} is not match ${this.short} or ${this.long}`,
88 reason: error_1.ErrorReason.OptionNameMismatch,
89 parts: [args[0]],
90 params: {
91 option: this,
92 opts,
93 args,
94 },
95 });
96 }
97 let next = args[1];
98 let propertyName = utils.kebabToLowerCamelCase(this.name());
99 if (this.required) {
100 if (next == null) {
101 throw new error_1.CommandpostError({
102 message: `${args[0]} is required parameter value`,
103 reason: error_1.ErrorReason.OptionValueRequired,
104 parts: [args[0]],
105 params: {
106 option: this,
107 opts,
108 args,
109 },
110 });
111 }
112 opts[propertyName] = opts[propertyName] || [];
113 opts[propertyName].push(next);
114 return args.slice(2);
115 }
116 else if (this.optional) {
117 if (next != null && !/^-/.test(next)) {
118 opts[propertyName] = opts[propertyName] || [];
119 opts[propertyName].push(next);
120 return args.slice(2);
121 }
122 else {
123 opts[propertyName] = opts[propertyName] || [];
124 opts[propertyName].push(this.defaultValue);
125 return args.slice(1);
126 }
127 }
128 else {
129 opts[propertyName] = this.no ? true : false;
130 return args.slice(1);
131 }
132 }
133}
134exports.default = Option;
135//# sourceMappingURL=option.js.map
\No newline at end of file