-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreeter.ts
More file actions
81 lines (75 loc) · 2.02 KB
/
Copy pathgreeter.ts
File metadata and controls
81 lines (75 loc) · 2.02 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
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env npx tsx
/**
* Simple CLI example
*
* A greeter that demonstrates:
*
* - Boolean flags (--shout, --verbose)
* - String options with defaults (--greeting)
* - Positional arguments (name)
* - Aliases (-s, -v, -g)
*
* Usage: npx tsx examples/greeter.ts World npx tsx examples/greeter.ts World
* --shout npx tsx examples/greeter.ts World -g "Hey there" npx tsx
* examples/greeter.ts --help
*/
import { bargs, opt, pos } from '../src/index.js';
// Define global options
const globalOptions = opt.options({
greeting: opt.string({
aliases: ['g'],
default: 'Hello',
description: 'The greeting to use',
}),
shout: opt.boolean({
aliases: ['s'],
default: false,
description: 'SHOUT THE GREETING',
}),
verbose: opt.boolean({
aliases: ['v'],
default: false,
description: 'Show extra output',
}),
});
// Define the positionals for the greet command
const greetPositionals = pos.positionals(
pos.string({
description: 'Name to greet',
name: 'name',
required: true,
}),
);
// Build and run the CLI
// Using the (Parser, handler) form for full type inference of merged globals
await bargs('greeter', {
description: 'A friendly greeter CLI',
version: '1.0.0',
})
.globals(globalOptions)
// The (Parser, handler, description) form gives us merged global + command types!
.command(
'greet',
greetPositionals,
({ positionals, values }) => {
const [name] = positionals;
// values has full type: { greeting: string, shout: boolean, verbose: boolean }
// NO type assertions needed!
let message = `${values.greeting}, ${name}!`;
if (values.shout) {
message = message.toUpperCase();
}
if (values.verbose) {
console.log('Configuration:', {
greeting: values.greeting,
name,
shout: values.shout,
});
}
console.log(message);
},
'Greet someone by name',
)
// Make 'greet' the default so users don't need to type it
.defaultCommand('greet')
.parseAsync();