Generate Commander.js CLI project structure with TypeScript, commands, options, and best practices. Creates a complete scaffolded CLI application ready for development.
Generate a complete Commander.js CLI application with TypeScript, proper project structure, and best practices.
Invoke this skill when you need to:
| Parameter | Type | Required |
|---|
| Description |
|---|
| projectName | string | Yes | Name of the CLI project (kebab-case) |
| description | string | Yes | Short description of the CLI |
| commands | array | No | List of commands to scaffold |
| typescript | boolean | No | Use TypeScript (default: true) |
| packageManager | string | No | npm, yarn, or pnpm (default: npm) |
{
"commands": [
{
"name": "init",
"description": "Initialize a new project",
"options": [
{ "flags": "-t, --template <name>", "description": "Template to use" },
{ "flags": "-f, --force", "description": "Overwrite existing files" }
],
"arguments": [
{ "name": "<directory>", "description": "Target directory" }
]
}
]
}
<projectName>/
├── package.json
├── tsconfig.json
├── .gitignore
├── README.md
├── src/
│ ├── index.ts # Entry point with shebang
│ ├── cli.ts # Main CLI setup
│ ├── commands/
│ │ ├── index.ts # Command exports
│ │ └── <command>.ts # Individual commands
│ ├── utils/
│ │ ├── logger.ts # Logging utilities
│ │ └── config.ts # Configuration helpers
│ └── types/
│ └── index.ts # Type definitions
├── bin/
│ └── <projectName> # Compiled binary entry
└── tests/
└── commands/
└── <command>.test.ts
import { Command } from 'commander';
import { version } from '../package.json';
const program = new Command();
program
.name('<projectName>')
.description('<description>')
.version(version, '-v, --version', 'Display version number');
// Register commands
program.addCommand(initCommand);
program.addCommand(buildCommand);
// Global options
program
.option('-d, --debug', 'Enable debug mode')
.option('-q, --quiet', 'Suppress output');
// Error handling
program.exitOverride();
try {
program.parse();
} catch (err) {
// Handle gracefully
}
import { Command } from 'commander';
export const <name>Command = new Command('<name>')
.description('<description>')
.argument('<required>', 'Required argument description')
.argument('[optional]', 'Optional argument description')
.option('-o, --option <value>', 'Option description', 'default')
.action(async (required, optional, options) => {
// Command implementation
});
{
"dependencies": {
"commander": "^12.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"typescript": "^5.0.0",
"tsx": "^4.0.0",
"vitest": "^1.0.0"
}
}