-
Notifications
You must be signed in to change notification settings - Fork 6
/
types.ts
98 lines (90 loc) · 2.61 KB
/
types.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { command, program } from '../src/index.js'
/**
* Keep in mind that argument/option types are not validated at runtime.
* For example, when providing a default value with type boolean, it can be set
* to a string value at runtime.
*/
const string = command('string')
.argument('arg1', { description: 'Required string argument' })
.argument('arg2', { optional: true, description: 'Optional string argument' })
.argument('arg3', { variadic: true, description: 'Variadic string argument' })
.option('opt1', { type: 'string', description: 'String option' })
.option('opt2', {
default: 'foo',
description: 'String option with default',
})
.action((args) => {
console.log('Args are', args)
})
const number = command('number')
.argument('arg1', { type: 'number', description: 'Required number argument' })
.argument('arg2', {
type: 'number',
optional: true,
description: 'Optional number argument',
})
.argument('arg3', {
type: 'number',
variadic: true,
description: 'Variadic number argument',
})
.option('opt1', { type: 'number', description: 'number option' })
.option('opt2', { default: 100, description: 'Number option with default' })
.action((args) => {
console.log('Args are', args)
})
const boolean = command('boolean')
.argument('arg1', {
type: 'boolean',
description: 'Required boolean argument',
})
.argument('arg2', {
type: 'boolean',
optional: true,
description: 'Optional boolean argument',
})
.argument('arg3', {
type: 'boolean',
variadic: true,
description: 'Variadic boolean argument',
})
.option('opt1', { type: 'boolean', description: 'number option' })
.option('opt2', {
type: 'boolean',
default: false,
description: 'Number option with default',
})
.action((args) => {
console.log('Args are', args)
})
const choices = command('choices')
.argument('arg', {
description: 'Argument with choices',
choices: ['foo', 'bar'] as const,
})
.option('opt', {
description: 'Option with choices',
choices: ['option1', 'option2'] as const,
default: 'option3',
})
.action((args) => {
console.log('Args are', args)
})
const defaultValues = command('default')
.argument('arg', {
description: 'Optional argument with default value',
default: 5,
optional: true,
})
.option('opt', { description: 'Default value', default: true })
.action((args) => {
console.log('Args are', args)
})
const app = program()
.description('All argument and option types')
.add(string)
.add(number)
.add(boolean)
.add(choices)
.add(defaultValues)
app.runOrRepl()