@xmorse/cac 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +536 -0
- package/deno/CAC.ts +306 -0
- package/deno/Command.ts +257 -0
- package/deno/Option.ts +46 -0
- package/deno/deno.ts +5 -0
- package/deno/index.ts +9 -0
- package/deno/utils.ts +129 -0
- package/dist/index.d.ts +189 -0
- package/dist/index.js +645 -0
- package/dist/index.mjs +638 -0
- package/index-compat.js +11 -0
- package/mod.js +2 -0
- package/mod.ts +2 -0
- package/package.json +103 -0
package/deno/utils.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import Option from "./Option.ts";
|
|
2
|
+
export const removeBrackets = (v: string) => v.replace(/[<[].+/, '').trim();
|
|
3
|
+
export const findAllBrackets = (v: string) => {
|
|
4
|
+
const ANGLED_BRACKET_RE_GLOBAL = /<([^>]+)>/g;
|
|
5
|
+
const SQUARE_BRACKET_RE_GLOBAL = /\[([^\]]+)\]/g;
|
|
6
|
+
const res = [];
|
|
7
|
+
const parse = (match: string[]) => {
|
|
8
|
+
let variadic = false;
|
|
9
|
+
let value = match[1];
|
|
10
|
+
if (value.startsWith('...')) {
|
|
11
|
+
value = value.slice(3);
|
|
12
|
+
variadic = true;
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
required: match[0].startsWith('<'),
|
|
16
|
+
value,
|
|
17
|
+
variadic
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
let angledMatch;
|
|
21
|
+
while (angledMatch = ANGLED_BRACKET_RE_GLOBAL.exec(v)) {
|
|
22
|
+
res.push(parse(angledMatch));
|
|
23
|
+
}
|
|
24
|
+
let squareMatch;
|
|
25
|
+
while (squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v)) {
|
|
26
|
+
res.push(parse(squareMatch));
|
|
27
|
+
}
|
|
28
|
+
return res;
|
|
29
|
+
};
|
|
30
|
+
interface MriOptions {
|
|
31
|
+
alias: {
|
|
32
|
+
[k: string]: string[];
|
|
33
|
+
};
|
|
34
|
+
boolean: string[];
|
|
35
|
+
}
|
|
36
|
+
export const getMriOptions = (options: Option[]) => {
|
|
37
|
+
const result: MriOptions = {
|
|
38
|
+
alias: {},
|
|
39
|
+
boolean: []
|
|
40
|
+
};
|
|
41
|
+
for (const [index, option] of options.entries()) {
|
|
42
|
+
// We do not set default values in mri options
|
|
43
|
+
// Since its type (typeof) will be used to cast parsed arguments.
|
|
44
|
+
// Which mean `--foo foo` will be parsed as `{foo: true}` if we have `{default:{foo: true}}`
|
|
45
|
+
|
|
46
|
+
// Set alias
|
|
47
|
+
if (option.names.length > 1) {
|
|
48
|
+
result.alias[option.names[0]] = option.names.slice(1);
|
|
49
|
+
}
|
|
50
|
+
// Set boolean
|
|
51
|
+
if (option.isBoolean) {
|
|
52
|
+
if (option.negated) {
|
|
53
|
+
// For negated option
|
|
54
|
+
// We only set it to `boolean` type when there's no string-type option with the same name
|
|
55
|
+
const hasStringTypeOption = options.some((o, i) => {
|
|
56
|
+
return i !== index && o.names.some(name => option.names.includes(name)) && typeof o.required === 'boolean';
|
|
57
|
+
});
|
|
58
|
+
if (!hasStringTypeOption) {
|
|
59
|
+
result.boolean.push(option.names[0]);
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
result.boolean.push(option.names[0]);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
};
|
|
68
|
+
export const findLongest = (arr: string[]) => {
|
|
69
|
+
return arr.sort((a, b) => {
|
|
70
|
+
return a.length > b.length ? -1 : 1;
|
|
71
|
+
})[0];
|
|
72
|
+
};
|
|
73
|
+
export const padRight = (str: string, length: number) => {
|
|
74
|
+
return str.length >= length ? str : `${str}${' '.repeat(length - str.length)}`;
|
|
75
|
+
};
|
|
76
|
+
export const camelcase = (input: string) => {
|
|
77
|
+
return input.replace(/([a-z])-([a-z])/g, (_, p1, p2) => {
|
|
78
|
+
return p1 + p2.toUpperCase();
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
export const setDotProp = (obj: {
|
|
82
|
+
[k: string]: any;
|
|
83
|
+
}, keys: string[], val: any) => {
|
|
84
|
+
let i = 0;
|
|
85
|
+
let length = keys.length;
|
|
86
|
+
let t = obj;
|
|
87
|
+
let x;
|
|
88
|
+
for (; i < length; ++i) {
|
|
89
|
+
x = t[keys[i]];
|
|
90
|
+
t = t[keys[i]] = i === length - 1 ? val : x != null ? x : !!~keys[i + 1].indexOf('.') || !(+keys[i + 1] > -1) ? {} : [];
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
export const setByType = (obj: {
|
|
94
|
+
[k: string]: any;
|
|
95
|
+
}, transforms: {
|
|
96
|
+
[k: string]: any;
|
|
97
|
+
}) => {
|
|
98
|
+
for (const key of Object.keys(transforms)) {
|
|
99
|
+
const transform = transforms[key];
|
|
100
|
+
if (transform.shouldTransform) {
|
|
101
|
+
obj[key] = Array.prototype.concat.call([], obj[key]);
|
|
102
|
+
if (typeof transform.transformFunction === 'function') {
|
|
103
|
+
obj[key] = obj[key].map(transform.transformFunction);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
export const getFileName = (input: string) => {
|
|
109
|
+
const m = /([^\\\/]+)$/.exec(input);
|
|
110
|
+
return m ? m[1] : '';
|
|
111
|
+
};
|
|
112
|
+
export const camelcaseOptionName = (name: string) => {
|
|
113
|
+
// Camelcase the option name
|
|
114
|
+
// Don't camelcase anything after the dot `.`
|
|
115
|
+
return name.split('.').map((v, i) => {
|
|
116
|
+
return i === 0 ? camelcase(v) : v;
|
|
117
|
+
}).join('.');
|
|
118
|
+
};
|
|
119
|
+
export class CACError extends Error {
|
|
120
|
+
constructor(message: string) {
|
|
121
|
+
super(message);
|
|
122
|
+
this.name = this.constructor.name;
|
|
123
|
+
if (typeof Error.captureStackTrace === 'function') {
|
|
124
|
+
Error.captureStackTrace(this, this.constructor);
|
|
125
|
+
} else {
|
|
126
|
+
this.stack = new Error(message).stack;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
|
|
3
|
+
interface OptionConfig {
|
|
4
|
+
default?: any;
|
|
5
|
+
type?: any[];
|
|
6
|
+
}
|
|
7
|
+
declare class Option {
|
|
8
|
+
rawName: string;
|
|
9
|
+
description: string;
|
|
10
|
+
/** Option name */
|
|
11
|
+
name: string;
|
|
12
|
+
/** Option name and aliases */
|
|
13
|
+
names: string[];
|
|
14
|
+
isBoolean?: boolean;
|
|
15
|
+
required?: boolean;
|
|
16
|
+
config: OptionConfig;
|
|
17
|
+
negated: boolean;
|
|
18
|
+
constructor(rawName: string, description: string, config?: OptionConfig);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface CommandArg {
|
|
22
|
+
required: boolean;
|
|
23
|
+
value: string;
|
|
24
|
+
variadic: boolean;
|
|
25
|
+
}
|
|
26
|
+
interface HelpSection {
|
|
27
|
+
title?: string;
|
|
28
|
+
body: string;
|
|
29
|
+
}
|
|
30
|
+
interface CommandConfig {
|
|
31
|
+
allowUnknownOptions?: boolean;
|
|
32
|
+
ignoreOptionDefaultValue?: boolean;
|
|
33
|
+
}
|
|
34
|
+
type HelpCallback = (sections: HelpSection[]) => void | HelpSection[];
|
|
35
|
+
type CommandExample = ((bin: string) => string) | string;
|
|
36
|
+
declare class Command {
|
|
37
|
+
rawName: string;
|
|
38
|
+
description: string;
|
|
39
|
+
config: CommandConfig;
|
|
40
|
+
cli: CAC;
|
|
41
|
+
options: Option[];
|
|
42
|
+
aliasNames: string[];
|
|
43
|
+
name: string;
|
|
44
|
+
args: CommandArg[];
|
|
45
|
+
commandAction?: (...args: any[]) => any;
|
|
46
|
+
usageText?: string;
|
|
47
|
+
versionNumber?: string;
|
|
48
|
+
examples: CommandExample[];
|
|
49
|
+
helpCallback?: HelpCallback;
|
|
50
|
+
globalCommand?: GlobalCommand;
|
|
51
|
+
constructor(rawName: string, description: string, config: CommandConfig, cli: CAC);
|
|
52
|
+
usage(text: string): this;
|
|
53
|
+
allowUnknownOptions(): this;
|
|
54
|
+
ignoreOptionDefaultValue(): this;
|
|
55
|
+
version(version: string, customFlags?: string): this;
|
|
56
|
+
example(example: CommandExample): this;
|
|
57
|
+
/**
|
|
58
|
+
* Add a option for this command
|
|
59
|
+
* @param rawName Raw option name(s)
|
|
60
|
+
* @param description Option description
|
|
61
|
+
* @param config Option config
|
|
62
|
+
*/
|
|
63
|
+
option(rawName: string, description: string, config?: OptionConfig): this;
|
|
64
|
+
alias(name: string): this;
|
|
65
|
+
action(callback: (...args: any[]) => any): this;
|
|
66
|
+
isMatched(args: string[]): {
|
|
67
|
+
matched: boolean;
|
|
68
|
+
consumedArgs: number;
|
|
69
|
+
};
|
|
70
|
+
get isDefaultCommand(): boolean;
|
|
71
|
+
get isGlobalCommand(): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Check if an option is registered in this command
|
|
74
|
+
* @param name Option name
|
|
75
|
+
*/
|
|
76
|
+
hasOption(name: string): Option | undefined;
|
|
77
|
+
outputHelp(): void;
|
|
78
|
+
outputVersion(): void;
|
|
79
|
+
checkRequiredArgs(): void;
|
|
80
|
+
/**
|
|
81
|
+
* Check if the parsed options contain any unknown options
|
|
82
|
+
*
|
|
83
|
+
* Exit and output error when true
|
|
84
|
+
*/
|
|
85
|
+
checkUnknownOptions(): void;
|
|
86
|
+
/**
|
|
87
|
+
* Check if the required string-type options exist
|
|
88
|
+
*/
|
|
89
|
+
checkOptionValue(): void;
|
|
90
|
+
}
|
|
91
|
+
declare class GlobalCommand extends Command {
|
|
92
|
+
constructor(cli: CAC);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface ParsedArgv {
|
|
96
|
+
args: ReadonlyArray<string>;
|
|
97
|
+
options: {
|
|
98
|
+
[k: string]: any;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
declare class CAC extends EventEmitter {
|
|
102
|
+
/** The program name to display in help and version message */
|
|
103
|
+
name: string;
|
|
104
|
+
commands: Command[];
|
|
105
|
+
globalCommand: GlobalCommand;
|
|
106
|
+
matchedCommand?: Command;
|
|
107
|
+
matchedCommandName?: string;
|
|
108
|
+
/**
|
|
109
|
+
* Raw CLI arguments
|
|
110
|
+
*/
|
|
111
|
+
rawArgs: string[];
|
|
112
|
+
/**
|
|
113
|
+
* Parsed CLI arguments
|
|
114
|
+
*/
|
|
115
|
+
args: ParsedArgv['args'];
|
|
116
|
+
/**
|
|
117
|
+
* Parsed CLI options, camelCased
|
|
118
|
+
*/
|
|
119
|
+
options: ParsedArgv['options'];
|
|
120
|
+
showHelpOnExit?: boolean;
|
|
121
|
+
showVersionOnExit?: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* @param name The program name to display in help and version message
|
|
124
|
+
*/
|
|
125
|
+
constructor(name?: string);
|
|
126
|
+
/**
|
|
127
|
+
* Add a global usage text.
|
|
128
|
+
*
|
|
129
|
+
* This is not used by sub-commands.
|
|
130
|
+
*/
|
|
131
|
+
usage(text: string): this;
|
|
132
|
+
/**
|
|
133
|
+
* Add a sub-command
|
|
134
|
+
*/
|
|
135
|
+
command(rawName: string, description?: string, config?: CommandConfig): Command;
|
|
136
|
+
/**
|
|
137
|
+
* Add a global CLI option.
|
|
138
|
+
*
|
|
139
|
+
* Which is also applied to sub-commands.
|
|
140
|
+
*/
|
|
141
|
+
option(rawName: string, description: string, config?: OptionConfig): this;
|
|
142
|
+
/**
|
|
143
|
+
* Show help message when `-h, --help` flags appear.
|
|
144
|
+
*
|
|
145
|
+
*/
|
|
146
|
+
help(callback?: HelpCallback): this;
|
|
147
|
+
/**
|
|
148
|
+
* Show version number when `-v, --version` flags appear.
|
|
149
|
+
*
|
|
150
|
+
*/
|
|
151
|
+
version(version: string, customFlags?: string): this;
|
|
152
|
+
/**
|
|
153
|
+
* Add a global example.
|
|
154
|
+
*
|
|
155
|
+
* This example added here will not be used by sub-commands.
|
|
156
|
+
*/
|
|
157
|
+
example(example: CommandExample): this;
|
|
158
|
+
/**
|
|
159
|
+
* Output the corresponding help message
|
|
160
|
+
* When a sub-command is matched, output the help message for the command
|
|
161
|
+
* Otherwise output the global one.
|
|
162
|
+
*
|
|
163
|
+
*/
|
|
164
|
+
outputHelp(): void;
|
|
165
|
+
/**
|
|
166
|
+
* Output the version number.
|
|
167
|
+
*
|
|
168
|
+
*/
|
|
169
|
+
outputVersion(): void;
|
|
170
|
+
private setParsedInfo;
|
|
171
|
+
unsetMatchedCommand(): void;
|
|
172
|
+
/**
|
|
173
|
+
* Parse argv
|
|
174
|
+
*/
|
|
175
|
+
parse(argv?: string[], {
|
|
176
|
+
/** Whether to run the action for matched command */
|
|
177
|
+
run, }?: {
|
|
178
|
+
run?: boolean | undefined;
|
|
179
|
+
}): ParsedArgv;
|
|
180
|
+
private mri;
|
|
181
|
+
runMatchedCommand(): any;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @param name The program name to display in help and version message
|
|
186
|
+
*/
|
|
187
|
+
declare const cac: (name?: string) => CAC;
|
|
188
|
+
|
|
189
|
+
export { CAC, Command, cac, cac as default };
|