-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdemo-cli-cac.js
More file actions
executable file
·70 lines (59 loc) · 1.79 KB
/
demo-cli-cac.js
File metadata and controls
executable file
·70 lines (59 loc) · 1.79 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
import cac from 'cac';
import tab from '../../dist/src/cac.js';
const cli = cac('demo-cli-cac');
// Define version and help
cli.version('1.0.0');
cli.help();
// Global options
cli.option('-c, --config <file>', 'Specify config file');
cli.option('-d, --debug', 'Enable debugging');
// Start command
cli
.command('start', 'Start the application')
.option('-p, --port <port>', 'Port to use', { default: '3000' })
.action((options) => {
console.log('Starting application...');
console.log('Options:', options);
});
// Build command
cli
.command('build', 'Build the application')
.option('-m, --mode <mode>', 'Build mode', { default: 'production' })
.action((options) => {
console.log('Building application...');
console.log('Options:', options);
});
// Set up completion using the cac adapter
const completion = await tab(cli);
// custom config for options
for (const command of completion.commands.values()) {
for (const [optionName, config] of command.options.entries()) {
if (optionName === '--port') {
config.handler = () => {
return [
{ value: '3000', description: 'Default port' },
{ value: '8080', description: 'Alternative port' },
];
};
}
if (optionName === '--mode') {
config.handler = () => {
return [
{ value: 'development', description: 'Development mode' },
{ value: 'production', description: 'Production mode' },
{ value: 'test', description: 'Test mode' },
];
};
}
if (optionName === '--config') {
config.handler = () => {
return [
{ value: 'config.json', description: 'JSON config file' },
{ value: 'config.js', description: 'JavaScript config file' },
];
};
}
}
}
cli.parse();