Commander.js CLI framework patterns including Command class, options, arguments, nested subcommands, and Option class usage. Use when building Node.js CLIs, implementing Commander.js commands, creating TypeScript CLI tools, adding command options/arguments, or when user mentions Commander.js, CLI commands, command options, or nested subcommands.
/plugin marketplace add vanman2024/cli-builder/plugin install cli-builder@cli-builderThis skill is limited to using the following tools:
examples/advanced-option-class.mdexamples/basic-usage.mdexamples/nested-commands-demo.mdexamples/options-arguments-demo.mdscripts/extract-command-help.shscripts/generate-command.shscripts/generate-subcommand.shscripts/test-commander-cli.shscripts/validate-commander-structure.shtemplates/basic-commander.jstemplates/basic-commander.tstemplates/command-with-arguments.tstemplates/command-with-options.tstemplates/commander-with-inquirer.tstemplates/commander-with-validation.tstemplates/commonjs-commander.jstemplates/full-cli-example.tstemplates/nested-subcommands.tstemplates/option-class-advanced.tstemplates/package.json.templateProvides comprehensive Commander.js patterns, templates, and examples for building robust Node.js CLI applications with TypeScript support.
Commander.js is the complete solution for Node.js command-line interfaces. This skill provides battle-tested patterns for:
Create program instance:
import { Command } from 'commander';
const program = new Command();
program
.name('mycli')
.description('CLI description')
.version('1.0.0');
Add simple command:
program
.command('init')
.description('Initialize project')
.action(() => {
// Command logic
});
Parse arguments:
program.parse();
Use options for named flags with values:
program
.command('deploy')
.description('Deploy application')
.option('-e, --env <environment>', 'target environment', 'dev')
.option('-f, --force', 'force deployment', false)
.option('-v, --verbose', 'verbose output')
.action((options) => {
console.log('Environment:', options.env);
console.log('Force:', options.force);
console.log('Verbose:', options.verbose);
});
Use arguments for positional parameters:
program
.command('deploy <environment>')
.description('Deploy to environment')
.argument('<environment>', 'target environment')
.argument('[region]', 'optional region', 'us-east-1')
.action((environment, region, options) => {
console.log(`Deploying to ${environment} in ${region}`);
});
For advanced option configuration:
import { Command, Option } from 'commander';
program
.command('deploy')
.addOption(
new Option('-m, --mode <mode>', 'deployment mode')
.choices(['fast', 'safe', 'rollback'])
.default('safe')
.makeOptionMandatory()
)
.addOption(
new Option('-r, --replicas <count>', 'replica count')
.argParser(parseInt)
.default(3)
)
.action((options) => {
console.log(`Mode: ${options.mode}, Replicas: ${options.replicas}`);
});
Create command hierarchies:
const config = program
.command('config')
.description('Manage configuration');
config
.command('get <key>')
.description('Get config value')
.action((key) => {
console.log(`Config ${key}:`, getConfig(key));
});
config
.command('set <key> <value>')
.description('Set config value')
.action((key, value) => {
setConfig(key, value);
console.log(`✓ Set ${key} = ${value}`);
});
config
.command('list')
.description('List all config')
.action(() => {
console.log(getAllConfig());
});
Accept multiple values:
program
.command('add <items...>')
.description('Add multiple items')
.action((items) => {
console.log('Adding items:', items);
});
// Usage: mycli add item1 item2 item3
Transform argument values:
program
.command('wait <delay>')
.description('Wait for specified time')
.argument('<delay>', 'delay in seconds', parseFloat)
.action((delay) => {
console.log(`Waiting ${delay} seconds...`);
});
Options available to all commands:
program
.option('-c, --config <path>', 'config file path')
.option('-v, --verbose', 'verbose output')
.option('--no-color', 'disable colors');
program
.command('deploy')
.action((options, command) => {
const globalOpts = command.parent?.opts();
console.log('Config:', globalOpts?.config);
console.log('Verbose:', globalOpts?.verbose);
});
program
.command('deploy <environment>')
.action((environment) => {
if (!['dev', 'staging', 'prod'].includes(environment)) {
throw new Error(`Invalid environment: ${environment}`);
}
// Deploy logic
});
program.exitOverride();
try {
program.parse();
} catch (err) {
console.error('Error:', err.message);
process.exit(1);
}
new Command()
.name('cli-name')
.description('CLI description')
.version('1.0.0')
.command('subcommand')
-v, --verbose (boolean)-p, --port <port> (required value)-p, --port [port] (optional value)--no-color (inverse boolean)--files <files...> (multiple values)<name>[name]<items...> or [items...].choices(['a', 'b', 'c']): Restrict to specific values.default(value): Set default value.argParser(fn): Custom parsing function.makeOptionMandatory(): Require option.conflicts(option): Mutually exclusive options.implies(option): Implies another option.env(name): Read from environment variable// No arguments
.action(() => {})
// With options only
.action((options) => {})
// With arguments
.action((arg1, arg2, options) => {})
// With command reference
.action((options, command) => {})
Use template: templates/basic-commander.ts
Use template: templates/option-class-advanced.ts
Use template: templates/commander-with-inquirer.ts
Use template: templates/commander-with-validation.ts
Use template: templates/nested-subcommands.ts
import inquirer from 'inquirer';
program
.command('setup')
.action(async () => {
const answers = await inquirer.prompt([
{ type: 'input', name: 'name', message: 'Project name:' },
{ type: 'list', name: 'template', message: 'Template:', choices: ['basic', 'advanced'] }
]);
// Use answers
});
import chalk from 'chalk';
program
.command('deploy')
.action(() => {
console.log(chalk.green('✓ Deployment successful'));
console.log(chalk.red('✗ Deployment failed'));
});
import ora from 'ora';
program
.command('build')
.action(async () => {
const spinner = ora('Building...').start();
await build();
spinner.succeed('Build complete');
});
const config = program.command('config');
config.command('get <key>').action(getConfig);
config.command('set <key> <value>').action(setConfig);
config.command('list').action(listConfig);
config.command('delete <key>').action(deleteConfig);
program.command('create <name>').action(create);
program.command('read <id>').action(read);
program.command('update <id>').action(update);
program.command('delete <id>').action(deleteItem);
program.command('list').action(list);
program
.command('deploy')
.addOption(new Option('-e, --env <env>').choices(['dev', 'staging', 'prod']))
.option('-f, --force', 'force deployment')
.action(deploy);
Solution: Ensure program.parse() is called
Solution: Check action handler signature matches argument count
Solution: Verify subcommand is attached before parse()
Solution: Install @types/node and configure tsconfig
Solution: Commander.js auto-generates help from descriptions
✅ Command structure follows Commander.js conventions ✅ Options and arguments properly typed ✅ Help text is clear and descriptive ✅ Error handling covers edge cases ✅ CLI tested with various inputs ✅ TypeScript compiles without errors ✅ Commands execute as expected
click-patterns - Python Click framework patternstyper-patterns - Python Typer framework patternsclap-patterns - Rust Clap framework patternsSkill Type: Framework Patterns + Code Templates Language: TypeScript/JavaScript (Node.js) Framework: Commander.js v12+ Auto-invocation: Yes (via description matching)
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.