Advanced yargs patterns for Node.js CLI argument parsing with subcommands, options, middleware, and validation
/plugin marketplace add vanman2024/cli-builder/plugin install cli-builder@cli-builderThis skill inherits all available tools. When active, it can use any tool Claude has access to.
examples/subcommands-example.jsscripts/generate-completion.shtemplates/advanced-cli.jstemplates/basic-cli.jsComprehensive patterns and templates for building CLI applications with yargs, the modern Node.js argument parsing library.
yargs is a powerful argument parsing library for Node.js that provides:
#!/usr/bin/env node
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
yargs(hideBin(process.argv))
.command('* <name>', 'greet someone', (yargs) => {
yargs.positional('name', {
describe: 'Name to greet',
type: 'string'
});
}, (argv) => {
console.log(`Hello, ${argv.name}!`);
})
.parse();
yargs(hideBin(process.argv))
.command('init <project>', 'initialize a new project', (yargs) => {
yargs
.positional('project', {
describe: 'Project name',
type: 'string'
})
.option('template', {
alias: 't',
describe: 'Project template',
choices: ['basic', 'advanced', 'minimal'],
default: 'basic'
});
}, (argv) => {
console.log(`Initializing ${argv.project} with ${argv.template} template`);
})
.command('build [entry]', 'build the project', (yargs) => {
yargs
.positional('entry', {
describe: 'Entry point file',
type: 'string',
default: 'index.js'
})
.option('output', {
alias: 'o',
describe: 'Output directory',
type: 'string',
default: 'dist'
})
.option('minify', {
describe: 'Minify output',
type: 'boolean',
default: false
});
}, (argv) => {
console.log(`Building from ${argv.entry} to ${argv.output}`);
})
.parse();
yargs(hideBin(process.argv))
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Run with verbose logging',
default: false
})
.option('config', {
alias: 'c',
type: 'string',
description: 'Path to config file',
demandOption: true // Required option
})
.option('port', {
alias: 'p',
type: 'number',
description: 'Port number',
default: 3000
})
.option('env', {
alias: 'e',
type: 'string',
choices: ['development', 'staging', 'production'],
description: 'Environment'
})
.parse();
yargs(hideBin(process.argv))
.command('deploy <service>', 'deploy a service', (yargs) => {
yargs
.positional('service', {
describe: 'Service name',
type: 'string'
})
.option('version', {
describe: 'Version to deploy',
type: 'string',
coerce: (arg) => {
// Custom validation
if (!/^\d+\.\d+\.\d+$/.test(arg)) {
throw new Error('Version must be in format X.Y.Z');
}
return arg;
}
})
.option('replicas', {
describe: 'Number of replicas',
type: 'number',
default: 1
})
.check((argv) => {
// Cross-field validation
if (argv.replicas > 10 && argv.env === 'development') {
throw new Error('Cannot deploy more than 10 replicas in development');
}
return true;
});
}, (argv) => {
console.log(`Deploying ${argv.service} v${argv.version} with ${argv.replicas} replicas`);
})
.parse();
yargs(hideBin(process.argv))
.middleware((argv) => {
// Preprocessing middleware
if (argv.verbose) {
console.log('Running in verbose mode');
console.log('Arguments:', argv);
}
})
.middleware((argv) => {
// Load config file
if (argv.config) {
const config = require(path.resolve(argv.config));
return { ...argv, ...config };
}
})
.command('run', 'run the application', {}, (argv) => {
console.log('Application running with config:', argv);
})
.parse();
yargs(hideBin(process.argv))
.option('json', {
describe: 'Output as JSON',
type: 'boolean'
})
.option('yaml', {
describe: 'Output as YAML',
type: 'boolean'
})
.conflicts('json', 'yaml') // Can't use both
.option('output', {
describe: 'Output file',
type: 'string'
})
.option('format', {
describe: 'Output format',
choices: ['json', 'yaml'],
implies: 'output' // format requires output
})
.parse();
yargs(hideBin(process.argv))
.option('include', {
describe: 'Files to include',
type: 'array',
default: []
})
.option('exclude', {
describe: 'Files to exclude',
type: 'array',
default: []
})
.parse();
// Usage: cli --include file1.js file2.js --exclude test.js
yargs(hideBin(process.argv))
.option('verbose', {
alias: 'v',
describe: 'Verbosity level',
type: 'count' // -v, -vv, -vvv
})
.parse();
See templates/ directory for:
basic-cli.js - Simple CLI with commandsadvanced-cli.js - Full-featured CLI with validationconfig-cli.js - CLI with configuration file supportinteractive-cli.js - CLI with promptsplugin-cli.js - Plugin-based CLI architectureSee scripts/ directory for:
generate-completion.sh - Generate bash/zsh completionvalidate-args.js - Argument validation helpertest-cli.sh - CLI testing scriptSee examples/ directory for complete working examples.
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.