Install
openclaw skills install commanderCommander.js is the most popular command-line interface (CLI) framework for Node.js. Used to create professional CLI programs, supporting option parsing, subcommands, automatic help generation, TypeScript, etc.
openclaw skills install commander// 1. Install
npm install commander
// 2. Create CLI
const { Command } = require('commander');
const program = new Command();
program
.name('my-cli')
.description('My CLI Tool')
.version('1.0.0');
program.parse();
// Boolean option (no argument)
program.option('-d, --debug', 'Enable debug mode');
// Value option (requires argument)
program.option('-p, --port <number>', 'Server port', '3000');
// Optional value option
program.option('--cheese [type]', 'Add cheese (optional type)');
// Negated boolean option
program.option('--no-sauce', 'No sauce');
// Required option
program.requiredOption('-c, --cheese <type>', 'Must choose a cheese type');
// Variadic option (array)
program.option('--items <items...>', 'Multiple items');
const options = program.opts();
console.log(options.debug); // or program.opts().debug
// Camel-case naming conversion: --template-engine → opts().templateEngine
program.option('--date <date>', 'Date', (value) => new Date(value));
program.command('serve')
.description('Start server')
.option('-p, --port <port>')
.action((options) => {
console.log('Server running on port', options.port);
});
program.command('build')
.description('Build project')
.option('--watch')
.action((options) => {
console.log('Building...');
});
// Main program
program.command('git hooks', { isDefault: true });
// Subcommand file: commands/git-hook.js
#!/usr/bin/env node
console.log('Running git hook');
// Required argument
program.argument('<file>', 'Input file');
// Optional argument
program.argument('[file]', 'Input file (optional)');
// Variadic argument
program.argument('<files...>', 'Multiple files');
// Access in action handler
program.command('split')
.argument('<string>', 'String to split')
.option('--separator <char>', 'Separator', ',')
.action((str, options) => {
console.log(str.split(options.separator));
});
node app.js --help
node app.js help command
program.on('option:help', () => {
console.log('Custom help information');
program.help();
});
program.addHelpCommand(false); // Disable help subcommand
For simple scripts, you can use the global object:
const { program } = require('commander');
program
.name('my-tool')
.option('-v, --verbose')
.action(() => {
console.log(program.opts().verbose ? 'Verbose mode' : 'Normal mode');
});
program.parse();
import { Command } from 'commander';
const program = new Command();
program
.name('ts-cli')
.option('-n, --name <name>', 'Name')
.action((options) => {
console.log('Hello', options.name);
});
program.parse();
// Configure parsing behavior
program.configureOutput({
writeErr: (str) => process.stderr.write(str),
outputWidth: 80,
});
// Enable conflict checks
program.allowUnknownOption();
program.passThroughOptions();
// Lifecycle hooks
program
.hook('preAction', (thisCommand) => {
console.log('Pre-action hook');
})
.hook('postAction', (thisCommand) => {
console.log('Post-action hook');
});
--template-engine → opts().templateEngine