fork of hey-api/openapi-ts because I need some additional things
1import { Command, CommanderError } from 'commander';
2
3import pkg from '../../package.json';
4import { createClient } from '../index';
5import { cliToConfig } from './adapter';
6
7const binName = Object.keys(pkg.bin)[0]!;
8
9const program = new Command()
10 .name(binName)
11 .description('Generate Python code from OpenAPI specifications')
12 .version(pkg.version);
13
14program
15 .option('-i, --input <path...>', 'OpenAPI specification (path, URL, or string)')
16 .option('-o, --output <path...>', 'Output folder(s)')
17 .option('-c, --client <name>', 'HTTP client to generate')
18 .option('-p, --plugins [names...]', 'Plugins to use')
19 .option('-f, --file <path>', 'Path to config file')
20 .option('-d, --debug', 'Enable debug logging')
21 .option('-s, --silent', 'Suppress all output')
22 .option('-l, --logs <path>', 'Logs folder path')
23 .option('--no-log-file', 'Disable log file output')
24 .option('--dry-run', 'Skip writing files')
25 .option('-w, --watch [interval]', 'Watch for changes')
26 .action(async (options) => {
27 const config = cliToConfig(options);
28
29 const context = await createClient(config as Parameters<typeof createClient>[0]);
30
31 const hasActiveWatch = context[0]?.config.input.some((input) => input.watch?.enabled);
32
33 if (!hasActiveWatch) {
34 process.exit(0);
35 }
36 });
37
38export async function runCli(): Promise<void> {
39 try {
40 await program.parseAsync(process.argv);
41 } catch (error) {
42 if (error instanceof CommanderError && 'code' in error) {
43 if (error.code === 'commander.optionMissingArgument') {
44 console.error(`\nMissing required argument. Run '${binName} --help' for usage.\n`);
45 } else if (error.code === 'commander.unknownOption') {
46 console.error(`\nUnknown option. Run '${binName} --help' for available options.\n`);
47 }
48
49 process.exit(error.exitCode);
50 }
51
52 console.error('Unexpected error:', error);
53 process.exit(1);
54 }
55}