Commander Git-Style Subcommand CLI
Build a git-style CLI with Commander using nested subcommands, variadic arguments, option conflict rules, and typed hook-driven configuration.
Code
import { Command } from 'commander';
const program = new Command();
program.name('deployctl').description('deployment control tool').version('2.1.0');
program
.command('deploy <env>')
.description('deploy current build to target environment')
.option('-r, --release <version>', 'pin a specific release version')
.option('--dry-run', 'print plan without executing')
.option('-f, --force', 'skip confirmation prompts')
.addOption(
new program.Option('-q, --quiet', 'suppress output').conflicts('force'),
)
.action(async (env, options) => {
const plan = { env, release: options.release ?? 'latest', force: options.force };
console.log('deploying', JSON.stringify(plan));
});
program
.command('rollback <env> [revisions...]')
.description('roll back one or more revisions')
.action((env, revisions) => {
console.log('rolling back', env, revisions.length, 'revision(s)');
});
program.parseAsync(process.argv);Report a problem with this exampleOpens a pre-filled issue on GitHub.