|
1 | | -import { massarg } from '.' |
2 | | -import MassargCommand from './command' |
3 | | -import { ParseError } from './error' |
| 1 | +import z from 'zod' |
| 2 | +import { ValidationError } from './error' |
4 | 3 |
|
5 | | -type A = { test: boolean } |
6 | | -const echoCmd = massarg<any>({ |
7 | | - name: 'echo', |
8 | | - description: 'Echo back the arguments', |
9 | | - aliases: ['e'], |
10 | | - run: (opts) => { |
11 | | - console.log('Echoing back', opts) |
12 | | - }, |
| 4 | +export const ExampleConfig = z.object({ |
| 5 | + description: z.string().optional(), |
| 6 | + input: z.string().optional(), |
| 7 | + output: z.string().optional(), |
13 | 8 | }) |
14 | | -const addCmd = massarg<{ component: string }>({ |
15 | | - name: 'add', |
16 | | - description: 'Add a component', |
17 | | - aliases: ['a'], |
18 | | - run: (opts, parser) => { |
19 | | - parser.printHelp() |
20 | | - console.log('Adding component', opts.component) |
21 | | - }, |
22 | | -}) |
23 | | - .option({ |
24 | | - name: 'component', |
25 | | - description: |
26 | | - 'Component to add. Ut consectetur eu et occaecat enim magna amet eiusmod laboris deserunt proident culpa nulla ipsum adipiscing ullamco laboris sed est', |
27 | | - aliases: ['c'], |
28 | | - // aliases: "" as never, |
29 | | - }) |
30 | | - .option({ |
31 | | - name: 'classes', |
32 | | - description: 'Classes to add', |
33 | | - aliases: ['l'], |
34 | | - array: true, |
35 | | - }) |
36 | | - .option({ |
37 | | - name: 'custom', |
38 | | - description: 'Custom option', |
39 | | - aliases: ['x'], |
40 | | - parse: (value) => { |
41 | | - const asNumber = Number(value) |
42 | | - if (isNaN(asNumber)) { |
43 | | - throw new ParseError({ |
44 | | - path: ['custom'], |
45 | | - message: 'Custom option must be a number', |
46 | | - code: 'invalid_number', |
47 | | - }) |
48 | | - } |
49 | | - return { |
50 | | - value: asNumber, |
51 | | - half: asNumber / 2, |
52 | | - double: asNumber * 2, |
53 | | - } |
54 | | - }, |
55 | | - }) |
56 | | - |
57 | | -const removeCmd = new MassargCommand<{ component: string }>({ |
58 | | - name: 'remove', |
59 | | - description: 'Remove a component', |
60 | | - aliases: ['r'], |
61 | | - run: (opts) => { |
62 | | - console.log('Removing component', opts.component) |
63 | | - }, |
64 | | -}).option({ |
65 | | - name: 'component', |
66 | | - description: 'Component to remove', |
67 | | - aliases: ['c'], |
68 | | -}) |
69 | | - |
70 | | -const args = massarg<A>({ |
71 | | - name: 'my-cli', |
72 | | - description: 'This is an example CLI', |
73 | | - bindHelpOption: true, |
74 | | - bindHelpCommand: true, |
75 | | -}) |
76 | | - .main((opts, parser) => { |
77 | | - console.log('Main command - printing all opts') |
78 | | - console.log(opts, '\n') |
79 | | - parser.printHelp() |
80 | | - }) |
81 | | - .command(echoCmd) |
82 | | - .command(addCmd) |
83 | | - .command(removeCmd) |
84 | | - .flag({ |
85 | | - name: 'bool', |
86 | | - description: 'Example boolean option', |
87 | | - aliases: ['b'], |
88 | | - }) |
89 | | - .option({ |
90 | | - name: 'number', |
91 | | - description: 'Example number option', |
92 | | - aliases: ['n'], |
93 | | - type: 'number', |
94 | | - }) |
| 9 | +export type ExampleConfig = z.infer<typeof ExampleConfig> |
95 | 10 |
|
96 | | -// console.log("Opts:", args.getArgs(process.argv.slice(2)), "\n") |
| 11 | +export default class MassargExample { |
| 12 | + description: string | undefined |
| 13 | + input: string | undefined |
| 14 | + output: string | undefined |
97 | 15 |
|
98 | | -args.parse(process.argv.slice(2)) |
| 16 | + constructor(config: ExampleConfig) { |
| 17 | + ExampleConfig.parse(config) |
| 18 | + if ( |
| 19 | + config.description === undefined && |
| 20 | + config.input === undefined && |
| 21 | + config.output === undefined |
| 22 | + ) { |
| 23 | + throw new ValidationError({ |
| 24 | + code: 'invalid_example', |
| 25 | + message: 'Example must have at least one of description, input, or output', |
| 26 | + path: ['example'], |
| 27 | + }) |
| 28 | + } |
| 29 | + this.description = config.description |
| 30 | + this.input = config.input |
| 31 | + this.output = config.output |
| 32 | + } |
| 33 | +} |
| 34 | +export { MassargExample } |
0 commit comments