UNPKG

1.47 kBPlain TextView Raw
1import { removeBrackets, camelcaseOptionName } from "./utils.ts";
2interface OptionConfig {
3 default?: any;
4 type?: any[];
5}
6export default class Option {
7 /** Option name */
8 name: string;
9 /** Option name and aliases */
10
11 names: string[];
12 isBoolean?: boolean; // `required` will be a boolean for options with brackets
13
14 required?: boolean;
15 config: OptionConfig;
16 negated: boolean;
17
18 constructor(public rawName: string, public description: string, config?: OptionConfig) {
19 this.config = Object.assign({}, config); // You may use cli.option('--env.* [value]', 'desc') to denote a dot-nested option
20
21 rawName = rawName.replace(/\.\*/g, '');
22 this.negated = false;
23 this.names = removeBrackets(rawName).split(',').map((v: string) => {
24 let name = v.trim().replace(/^-{1,2}/, '');
25
26 if (name.startsWith('no-')) {
27 this.negated = true;
28 name = name.replace(/^no-/, '');
29 }
30
31 return camelcaseOptionName(name);
32 }).sort((a, b) => a.length > b.length ? 1 : -1); // Sort names
33 // Use the longest name (last one) as actual option name
34
35 this.name = this.names[this.names.length - 1];
36
37 if (this.negated) {
38 this.config.default = true;
39 }
40
41 if (rawName.includes('<')) {
42 this.required = true;
43 } else if (rawName.includes('[')) {
44 this.required = false;
45 } else {
46 // No arg needed, it's boolean flag
47 this.isBoolean = true;
48 }
49 }
50
51}
52export type { OptionConfig };
\No newline at end of file