1 | const { InvalidArgumentError } = require('./error.js');
|
2 |
|
3 |
|
4 |
|
5 | class Option {
|
6 | |
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | constructor(flags, description) {
|
14 | this.flags = flags;
|
15 | this.description = description || '';
|
16 |
|
17 | this.required = flags.includes('<');
|
18 | this.optional = flags.includes('[');
|
19 |
|
20 | this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
21 | this.mandatory = false;
|
22 | const optionFlags = splitOptionFlags(flags);
|
23 | this.short = optionFlags.shortFlag;
|
24 | this.long = optionFlags.longFlag;
|
25 | this.negate = false;
|
26 | if (this.long) {
|
27 | this.negate = this.long.startsWith('--no-');
|
28 | }
|
29 | this.defaultValue = undefined;
|
30 | this.defaultValueDescription = undefined;
|
31 | this.parseArg = undefined;
|
32 | this.hidden = false;
|
33 | this.argChoices = undefined;
|
34 | }
|
35 |
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | default(value, description) {
|
45 | this.defaultValue = value;
|
46 | this.defaultValueDescription = description;
|
47 | return this;
|
48 | };
|
49 |
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | argParser(fn) {
|
58 | this.parseArg = fn;
|
59 | return this;
|
60 | };
|
61 |
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | makeOptionMandatory(mandatory = true) {
|
70 | this.mandatory = !!mandatory;
|
71 | return this;
|
72 | };
|
73 |
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | hideHelp(hide = true) {
|
82 | this.hidden = !!hide;
|
83 | return this;
|
84 | };
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 | _concatValue(value, previous) {
|
91 | if (previous === this.defaultValue || !Array.isArray(previous)) {
|
92 | return [value];
|
93 | }
|
94 |
|
95 | return previous.concat(value);
|
96 | }
|
97 |
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | choices(values) {
|
106 | this.argChoices = values;
|
107 | this.parseArg = (arg, previous) => {
|
108 | if (!values.includes(arg)) {
|
109 | throw new InvalidArgumentError(`Allowed choices are ${values.join(', ')}.`);
|
110 | }
|
111 | if (this.variadic) {
|
112 | return this._concatValue(arg, previous);
|
113 | }
|
114 | return arg;
|
115 | };
|
116 | return this;
|
117 | };
|
118 |
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | name() {
|
126 | if (this.long) {
|
127 | return this.long.replace(/^--/, '');
|
128 | }
|
129 | return this.short.replace(/^-/, '');
|
130 | };
|
131 |
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | attributeName() {
|
141 | return camelcase(this.name().replace(/^no-/, ''));
|
142 | };
|
143 |
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | is(arg) {
|
153 | return this.short === arg || this.long === arg;
|
154 | };
|
155 | }
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | function camelcase(str) {
|
166 | return str.split('-').reduce((str, word) => {
|
167 | return str + word[0].toUpperCase() + word.slice(1);
|
168 | });
|
169 | }
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | function splitOptionFlags(flags) {
|
178 | let shortFlag;
|
179 | let longFlag;
|
180 |
|
181 |
|
182 | const flagParts = flags.split(/[ |,]+/);
|
183 | if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1])) shortFlag = flagParts.shift();
|
184 | longFlag = flagParts.shift();
|
185 |
|
186 | if (!shortFlag && /^-[^-]$/.test(longFlag)) {
|
187 | shortFlag = longFlag;
|
188 | longFlag = undefined;
|
189 | }
|
190 | return { shortFlag, longFlag };
|
191 | }
|
192 |
|
193 | exports.Option = Option;
|
194 | exports.splitOptionFlags = splitOptionFlags;
|