1 | "use strict";
|
2 | var __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 | };
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const error_1 = require("./error");
|
11 | const utils = __importStar(require("./utils"));
|
12 |
|
13 | class Option {
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
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 |
|
48 |
|
49 |
|
50 | name() {
|
51 | return this.long.replace("--", "").replace("no-", "");
|
52 | }
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 | is(arg) {
|
59 | return arg === this.short || arg === this.long;
|
60 | }
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
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 | }
|
134 | exports.default = Option;
|
135 |
|
\ | No newline at end of file |