Last active
October 10, 2023 22:37
-
-
Save alexanderchan/5ee3608aed94fc1bcf3458953e0c1420 to your computer and use it in GitHub Desktop.
quick commander example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
import { config } from 'dotenv' | |
import { Command } from 'commander' | |
import { z } from 'zod' | |
config() | |
const exampleCommand = new Command() | |
.command('example-command') | |
.argument('[args...]', 'example args') | |
.description('example command') | |
.option('-e, --example <example>', 'example option', 'default') | |
.action(async (args, opts) => { | |
try { | |
const exampleCommandSchema = z.object({ | |
args: z.array(z.string()).optional(), | |
example: z.string().optional(), | |
}) | |
const options = exampleCommandSchema.parse({ | |
args, | |
...opts, | |
}) | |
console.log(options) | |
} catch (error) { | |
console.error(error) | |
process.exit(1) | |
} | |
}) | |
const program = new Command().name('example-program').description('example program with 1 command') | |
program.addCommand(exampleCommand, { isDefault: true }) | |
program.parse(process.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment