← 返回 Skills 市场
75
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install commander
功能描述
Commander.js is the most popular command-line interface (CLI) framework for Node.js. Used to create professional CLI programs, supporting option parsing, sub...
使用说明 (SKILL.md)
Commander.js
Trigger Scenarios
- Quickly create CLI scaffolding
- Define options and arguments
- Create subcommands
- Customize help information
- TypeScript support
Quick Start
// 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();
Option Definition
Basic Option Types
// Boolean option (no argument)
program.option('-d, --debug', 'Enable debug mode');
// Value option (requires argument)
program.option('-p, --port \x3Cnumber>', '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 \x3Ctype>', 'Must choose a cheese type');
// Variadic option (array)
program.option('--items \x3Citems...>', 'Multiple items');
Accessing Option Values
const options = program.opts();
console.log(options.debug); // or program.opts().debug
// Camel-case naming conversion: --template-engine → opts().templateEngine
Custom Option Processing
program.option('--date \x3Cdate>', 'Date', (value) => new Date(value));
Subcommands
Inline Subcommands
program.command('serve')
.description('Start server')
.option('-p, --port \x3Cport>')
.action((options) => {
console.log('Server running on port', options.port);
});
program.command('build')
.description('Build project')
.option('--watch')
.action((options) => {
console.log('Building...');
});
Standalone Executable Subcommands
// Main program
program.command('git hooks', { isDefault: true });
// Subcommand file: commands/git-hook.js
#!/usr/bin/env node
console.log('Running git hook');
Argument Definition
// Required argument
program.argument('\x3Cfile>', 'Input file');
// Optional argument
program.argument('[file]', 'Input file (optional)');
// Variadic argument
program.argument('\x3Cfiles...>', 'Multiple files');
// Access in action handler
program.command('split')
.argument('\x3Cstring>', 'String to split')
.option('--separator \x3Cchar>', 'Separator', ',')
.action((str, options) => {
console.log(str.split(options.separator));
});
Help System
Automatic Help
node app.js --help
node app.js help command
Custom Help
program.on('option:help', () => {
console.log('Custom help information');
program.help();
});
program.addHelpCommand(false); // Disable help subcommand
Global Object Shortcut
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();
TypeScript Support
import { Command } from 'commander';
const program = new Command();
program
.name('ts-cli')
.option('-n, --name \x3Cname>', 'Name')
.action((options) => {
console.log('Hello', options.name);
});
program.parse();
Common Configuration
// 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');
});
Best Practices
- Use a local Command object (not global) for easier unit testing
- Always call program.parse() to start parsing
- Prefer .option() over directly processing process.argv
- Use camelCase option names for automatic conversion:
--template-engine→opts().templateEngine - Use .description() to provide clear help information
如何使用
- 确保已安装 OpenClaw(本地或 Docker 部署)
- 在对话框中输入安装命令:
/install commander - 安装完成后,直接呼叫该 Skill 的名称或使用
/commander触发 - 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
Initial release of the Commander.js skill.
- Provides guidance for building CLI tools with Commander.js in Node.js.
- Covers option/argument definitions, subcommands, and help customization.
- Includes TypeScript usage examples.
- Shares best practices for configuration and testing.
元数据
常见问题
Commander.js is the most popular command-line interface (CLI) framework for Node.js. 是什么?
Commander.js is the most popular command-line interface (CLI) framework for Node.js. Used to create professional CLI programs, supporting option parsing, sub... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 75 次。
如何安装 Commander.js is the most popular command-line interface (CLI) framework for Node.js.?
在 OpenClaw 或 Claude Code 对话框中运行命令「/install commander」即可一键安装,无需额外配置。
Commander.js is the most popular command-line interface (CLI) framework for Node.js. 是免费的吗?
是的,Commander.js is the most popular command-line interface (CLI) framework for Node.js. 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。
Commander.js is the most popular command-line interface (CLI) framework for Node.js. 支持哪些平台?
Commander.js is the most popular command-line interface (CLI) framework for Node.js. 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。
谁开发了 Commander.js is the most popular command-line interface (CLI) framework for Node.js.?
由 OpenLark(@openlark)开发并维护,当前版本 v1.0.0。
推荐 Skills