Skip to content

Instantly share code, notes, and snippets.

@alexanderchan
Last active October 10, 2023 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderchan/5ee3608aed94fc1bcf3458953e0c1420 to your computer and use it in GitHub Desktop.
Save alexanderchan/5ee3608aed94fc1bcf3458953e0c1420 to your computer and use it in GitHub Desktop.
quick commander example
#!/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