1 | const { InvalidArgumentError } = require('./error.js');
|
2 |
|
3 | class Argument {
|
4 | |
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | constructor(name, description) {
|
14 | this.description = description || '';
|
15 | this.variadic = false;
|
16 | this.parseArg = undefined;
|
17 | this.defaultValue = undefined;
|
18 | this.defaultValueDescription = undefined;
|
19 | this.argChoices = undefined;
|
20 |
|
21 | switch (name[0]) {
|
22 | case '<':
|
23 | this.required = true;
|
24 | this._name = name.slice(1, -1);
|
25 | break;
|
26 | case '[':
|
27 | this.required = false;
|
28 | this._name = name.slice(1, -1);
|
29 | break;
|
30 | default:
|
31 | this.required = true;
|
32 | this._name = name;
|
33 | break;
|
34 | }
|
35 |
|
36 | if (this._name.length > 3 && this._name.slice(-3) === '...') {
|
37 | this.variadic = true;
|
38 | this._name = this._name.slice(0, -3);
|
39 | }
|
40 | }
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | name() {
|
49 | return this._name;
|
50 | }
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 | _concatValue(value, previous) {
|
57 | if (previous === this.defaultValue || !Array.isArray(previous)) {
|
58 | return [value];
|
59 | }
|
60 |
|
61 | return previous.concat(value);
|
62 | }
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | default(value, description) {
|
73 | this.defaultValue = value;
|
74 | this.defaultValueDescription = description;
|
75 | return this;
|
76 | }
|
77 |
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | argParser(fn) {
|
86 | this.parseArg = fn;
|
87 | return this;
|
88 | }
|
89 |
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | choices(values) {
|
98 | this.argChoices = values.slice();
|
99 | this.parseArg = (arg, previous) => {
|
100 | if (!this.argChoices.includes(arg)) {
|
101 | throw new InvalidArgumentError(
|
102 | `Allowed choices are ${this.argChoices.join(', ')}.`,
|
103 | );
|
104 | }
|
105 | if (this.variadic) {
|
106 | return this._concatValue(arg, previous);
|
107 | }
|
108 | return arg;
|
109 | };
|
110 | return this;
|
111 | }
|
112 |
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 | argRequired() {
|
119 | this.required = true;
|
120 | return this;
|
121 | }
|
122 |
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
128 | argOptional() {
|
129 | this.required = false;
|
130 | return this;
|
131 | }
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | function humanReadableArgName(arg) {
|
143 | const nameOutput = arg.name() + (arg.variadic === true ? '...' : '');
|
144 |
|
145 | return arg.required ? '<' + nameOutput + '>' : '[' + nameOutput + ']';
|
146 | }
|
147 |
|
148 | exports.Argument = Argument;
|
149 | exports.humanReadableArgName = humanReadableArgName;
|