Skip to content

Commit d8f14d6

Browse files
committed
support boolean options
1 parent 571f12e commit d8f14d6

File tree

8 files changed

+302
-218
lines changed

8 files changed

+302
-218
lines changed

examples/demo.cac.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,59 @@ const cli = cac('vite');
77
cli
88
.option('-c, --config <file>', `Use specified config file`)
99
.option('-m, --mode <mode>', `Set env mode`)
10-
.option('-l, --logLevel <level>', `info | warn | error | silent`);
10+
.option('-l, --logLevel <level>', `info | warn | error | silent`)
11+
.option('-d, --debug', 'Enable debug mode', { default: false })
12+
.option('-v, --verbose', 'Enable verbose output', { default: false })
13+
.option('--no-color', 'Disable colored output');
1114

1215
cli
1316
.command('dev', 'Start dev server')
1417
.option('-H, --host [host]', `Specify hostname`)
1518
.option('-p, --port <port>', `Specify port`)
16-
.action((options) => {});
19+
.option('--watch', 'Watch for changes', { default: false })
20+
.option('--no-hmr', 'Disable hot module replacement')
21+
.option('--force', 'Force restart server')
22+
.action((options) => {
23+
console.log('Dev server options:', options);
24+
});
25+
26+
cli
27+
.command('build', 'Build project')
28+
.option('--minify', 'Enable minification', { default: true })
29+
.option('--sourcemap', 'Generate source maps')
30+
.option('--no-cache', 'Disable build cache')
31+
.action((options) => {
32+
console.log('Build options:', options);
33+
});
1734

1835
cli
1936
.command('serve', 'Start the server')
2037
.option('-H, --host [host]', `Specify hostname`)
2138
.option('-p, --port <port>', `Specify port`)
22-
.action((options) => {});
39+
.option('--https', 'Enable HTTPS')
40+
.option('--cors', 'Enable CORS')
41+
.option('--no-compression', 'Disable compression')
42+
.action((options) => {
43+
console.log('Serve options:', options);
44+
});
2345

24-
cli.command('dev build', 'Build project').action((options) => {});
46+
cli.command('dev build', 'Build project').action((options) => { });
2547

2648
cli
2749
.command('copy <source> <destination>', 'Copy files')
28-
.action((source, destination, options) => {});
50+
.option('--overwrite', 'Overwrite existing files')
51+
.option('--dry-run', 'Show what would be copied')
52+
.action((source, destination, options) => {
53+
console.log('Copy options:', { source, destination, ...options });
54+
});
2955

30-
cli.command('lint [...files]', 'Lint project').action((files, options) => {});
56+
cli
57+
.command('lint [...files]', 'Lint project')
58+
.option('--fix', 'Automatically fix problems')
59+
.option('--strict', 'Enable strict mode')
60+
.action((files, options) => {
61+
console.log('Lint options:', { files, ...options });
62+
});
3163

3264
// Note: With the new t.ts API, handlers are configured through the completionConfig parameter
3365
// rather than by modifying the returned completion object directly

examples/demo.citty.ts

Lines changed: 98 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,126 @@
1-
import {
2-
defineCommand,
3-
createMain,
4-
type CommandDef,
5-
type ArgsDef,
6-
} from 'citty';
1+
import { defineCommand, createMain } from 'citty';
72
import tab from '../src/citty';
83

9-
const main = defineCommand({
4+
const cli = defineCommand({
105
meta: {
11-
name: 'vite',
12-
version: '0.0.0',
13-
description: 'Vite CLI',
6+
name: 'mycli',
7+
version: '1.0.0',
8+
description: 'My CLI app',
149
},
1510
args: {
16-
project: {
17-
type: 'positional',
18-
description: 'Project name',
19-
required: true,
11+
debug: {
12+
type: 'boolean',
13+
description: 'Enable debug mode',
2014
},
21-
config: {
22-
type: 'string',
23-
description: 'Use specified config file',
24-
alias: 'c',
15+
verbose: {
16+
type: 'boolean',
17+
description: 'Enable verbose output',
2518
},
26-
mode: {
27-
type: 'string',
28-
description: 'Set env mode',
29-
alias: 'm',
30-
},
31-
logLevel: {
32-
type: 'string',
33-
description: 'info | warn | error | silent',
34-
alias: 'l',
35-
},
36-
},
37-
run: (_ctx) => {},
38-
});
39-
40-
const devCommand = defineCommand({
41-
meta: {
42-
name: 'dev',
43-
description: 'Start dev server',
44-
},
45-
args: {
46-
host: {
47-
type: 'string',
48-
description: 'Specify hostname',
49-
alias: 'H',
50-
},
51-
port: {
19+
config: {
5220
type: 'string',
53-
description: 'Specify port',
54-
alias: 'p',
55-
},
56-
},
57-
run: () => {},
58-
});
59-
60-
const buildCommand = defineCommand({
61-
meta: {
62-
name: 'build',
63-
description: 'Build project',
64-
},
65-
run: () => {},
66-
});
67-
68-
const copyCommand = defineCommand({
69-
meta: {
70-
name: 'copy',
71-
description: 'Copy files',
72-
},
73-
args: {
74-
source: {
75-
type: 'positional',
76-
description: 'Source file or directory',
77-
required: true,
78-
},
79-
destination: {
80-
type: 'positional',
81-
description: 'Destination file or directory',
82-
required: true,
83-
},
84-
},
85-
run: () => {},
86-
});
87-
88-
const lintCommand = defineCommand({
89-
meta: {
90-
name: 'lint',
91-
description: 'Lint project',
92-
},
93-
args: {
94-
files: {
95-
type: 'positional',
96-
description: 'Files to lint',
97-
required: false,
21+
description: 'Config file path',
9822
},
9923
},
100-
run: () => {},
101-
});
102-
103-
main.subCommands = {
104-
dev: devCommand,
105-
build: buildCommand,
106-
copy: copyCommand,
107-
lint: lintCommand,
108-
} as Record<string, CommandDef<ArgsDef>>;
109-
110-
const completion = await tab(main, {
111-
args: {
112-
project: function (complete) {
113-
complete('my-app', 'My application');
114-
complete('my-lib', 'My library');
115-
complete('my-tool', 'My tool');
116-
},
117-
},
118-
options: {
119-
config: function (this: any, complete) {
120-
complete('vite.config.ts', 'Vite config file');
121-
complete('vite.config.js', 'Vite config file');
122-
},
123-
mode: function (this: any, complete) {
124-
complete('development', 'Development mode');
125-
complete('production', 'Production mode');
126-
},
127-
logLevel: function (this: any, complete) {
128-
complete('info', 'Info level');
129-
complete('warn', 'Warn level');
130-
complete('error', 'Error level');
131-
complete('silent', 'Silent level');
132-
},
133-
},
134-
13524
subCommands: {
136-
copy: {
25+
dev: {
26+
meta: {
27+
name: 'dev',
28+
description: 'Start development server',
29+
},
13730
args: {
138-
source: function (complete) {
139-
complete('src/', 'Source directory');
140-
complete('dist/', 'Distribution directory');
141-
complete('public/', 'Public assets');
31+
port: {
32+
type: 'string',
33+
description: 'Port number',
34+
},
35+
host: {
36+
type: 'string',
37+
description: 'Host to bind to',
14238
},
143-
destination: function (complete) {
144-
complete('build/', 'Build output');
145-
complete('release/', 'Release directory');
146-
complete('backup/', 'Backup location');
39+
watch: {
40+
type: 'boolean',
41+
description: 'Watch for changes',
14742
},
43+
hmr: {
44+
type: 'boolean',
45+
description: 'Enable hot module replacement',
46+
default: true,
47+
},
48+
},
49+
run: () => {
50+
console.log('Starting development server...');
14851
},
14952
},
150-
lint: {
53+
build: {
54+
meta: {
55+
name: 'build',
56+
description: 'Build the project',
57+
},
15158
args: {
152-
files: function (complete) {
153-
complete('main.ts', 'Main file');
154-
complete('index.ts', 'Index file');
59+
minify: {
60+
type: 'boolean',
61+
description: 'Enable minification',
62+
default: true,
63+
},
64+
sourcemap: {
65+
type: 'boolean',
66+
description: 'Generate source maps',
15567
},
68+
cache: {
69+
type: 'boolean',
70+
description: 'Enable build cache',
71+
default: true,
72+
},
73+
},
74+
run: () => {
75+
console.log('Building project...');
15676
},
15777
},
158-
dev: {
159-
options: {
160-
port: function (this: any, complete) {
161-
complete('3000', 'Development server port');
162-
complete('8080', 'Alternative port');
78+
serve: {
79+
meta: {
80+
name: 'serve',
81+
description: 'Serve the application',
82+
},
83+
args: {
84+
port: {
85+
type: 'string',
86+
description: 'Port number',
87+
},
88+
https: {
89+
type: 'boolean',
90+
description: 'Enable HTTPS',
16391
},
164-
host: function (this: any, complete) {
165-
complete('localhost', 'Localhost');
166-
complete('0.0.0.0', 'All interfaces');
92+
cors: {
93+
type: 'boolean',
94+
description: 'Enable CORS',
16795
},
16896
},
97+
run: () => {
98+
console.log('Serving application...');
99+
},
169100
},
170101
},
102+
run: () => {
103+
console.log('Running CLI...');
104+
},
171105
});
172106

173-
void completion;
107+
await tab(cli, {
108+
options: {
109+
port: (complete) => {
110+
complete('3000', 'Default port');
111+
complete('8080', 'Alternative port');
112+
},
113+
host: (complete) => {
114+
complete('localhost', 'Local development');
115+
complete('0.0.0.0', 'All interfaces');
116+
},
117+
config: (complete) => {
118+
complete('config.json', 'JSON config file');
119+
complete('config.yaml', 'YAML config file');
120+
},
121+
},
122+
});
174123

175-
const cli = createMain(main);
176-
cli();
124+
// Create and run the CLI
125+
const main = createMain(cli);
126+
main();

0 commit comments

Comments
 (0)