0%

npm库-cac

cac: 构建 CLI 应用程序的 JavaScript 库。

https://www.npmjs.com/package/cac

Command And C onquer 是一个用于构建 CLI 应用程序的 JavaScript 库。

Option

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// cac-option.js

const cli = require('cac')('MyCli');

cli.option('--type <type>', 'Choose a project type', {

default: 'node',

});



cli.option('--name <name>', 'Provide your name');



cli.help();

cli.version('0.0.1');

const { options, args } = cli.parse();



console.log('name:', options.name);

console.log('type:', options.type);

console.log('args:', args);
image

Command

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//cac-command.js

const cli = require('cac')('MyCli');



cli

.command('rm <dir>', 'Remove a dir')

.option('--name <name>', 'Provide your name')

.action((dir, options) => {

console.log('remove ', dir);

console.log('options ', options);

});



cli.help();

cli.version('0.0.1');

cli.parse();
c07cfe4b-d7f2-4bdc-a4b4-b19a99b762a5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//cac-command.js

const cli = require('cac')('MyCli');



cli

.command('rm <dir>', 'Remove a dir')

.option('--name <name>', 'Provide your name')

.action((dir, options) => {

console.log('remove ', dir);

console.log('options ', options);

});

cli

.command('build <entry1> <entry2> [...otherFiles]', 'Remove a dir')

.option('--name <name>', 'Provide your name')

.action((entry1, entry2, otherFiles, options) => {

console.log('entry1 ', entry1);

console.log('entry2 ', entry2);

console.log('otherFiles ', otherFiles);

console.log('options ', options);

});



cli.help();

cli.version('0.0.1');

cli.parse();
image(1)