-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
65 lines (59 loc) · 1.65 KB
/
example.js
File metadata and controls
65 lines (59 loc) · 1.65 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
#!/usr/bin/env node
import { z } from 'zod';
import { defineConfig, defineCommand, defineOptions, processConfig } from './dist/index.js';
// Define a simple command to greet someone
const greetCommand = defineCommand({
description: 'Greet someone',
options: defineOptions(
z.object({
name: z.string().describe('Name to greet'),
loud: z.boolean().default(false).describe('Use uppercase'),
}),
{ n: 'name', l: 'loud' } // Short aliases
),
action: async (options) => {
const greeting = `Hello, ${options.name}!`;
console.log(options.loud ? greeting.toUpperCase() : greeting);
},
});
// Define a command that copies a file
const copyCommand = defineCommand({
description: 'Copy a file to another location',
args: z.tuple([
z.string().describe('Source file'),
z.string().describe('Destination file'),
]),
options: defineOptions(
z.object({
verbose: z.boolean().default(false).describe('Show detailed output'),
}),
{ v: 'verbose' }
),
action: async (options, args) => {
const [source, dest] = args;
if (options.verbose) {
console.log(`Copying ${source} to ${dest}...`);
}
console.log(`✅ Copied ${source} to ${dest}`);
},
});
// Configure the CLI
const config = defineConfig({
meta: {
name: 'simple-cli',
version: '1.0.0',
description: 'A simple example CLI',
},
commands: {
greet: greetCommand,
copy: copyCommand,
},
});
// Process and run
try {
const result = processConfig(config, process.argv.slice(2));
await result.command.action(result.options, result.args);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}