一键导入
yargs-patterns
Advanced yargs patterns for Node.js CLI argument parsing with subcommands, options, middleware, and validation
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Advanced yargs patterns for Node.js CLI argument parsing with subcommands, options, middleware, and validation
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Standard library Python argparse examples with subparsers, choices, actions, and nested command patterns. Use when building Python CLIs without external dependencies, implementing argument parsing, creating subcommands, or when user mentions argparse, standard library CLI, subparsers, argument validation, or nested commands.
Modern type-safe Rust CLI patterns with Clap derive macros, Parser trait, Subcommand enums, validation, and value parsers. Use when building CLI applications, creating Clap commands, implementing type-safe Rust CLIs, or when user mentions Clap, CLI patterns, Rust command-line, derive macros, Parser trait, Subcommands, or command-line interfaces.
Lightweight Go CLI patterns using urfave/cli. Use when building CLI tools, creating commands with flags, implementing subcommands, adding before/after hooks, organizing command categories, or when user mentions Go CLI, urfave/cli, cobra alternatives, CLI flags, CLI categories.
CLI testing strategies and patterns for Node.js (Jest) and Python (pytest, Click.testing.CliRunner). Use when writing tests for CLI tools, testing command execution, validating exit codes, testing output, implementing CLI test suites, or when user mentions CLI testing, Jest CLI tests, pytest CLI, Click.testing.CliRunner, command testing, or exit code validation.
Click framework examples and templates - decorators, nested commands, parameter validation. Use when building Python CLI with Click, implementing command groups, adding CLI options/arguments, validating CLI parameters, creating nested subcommands, or when user mentions Click framework, @click decorators, command-line interface.
Production-ready Cobra CLI patterns including command structure, flags (local and persistent), nested commands, PreRun/PostRun hooks, argument validation, and initialization patterns used by kubectl and hugo. Use when building Go CLIs, implementing Cobra commands, creating nested command structures, managing flags, validating arguments, or when user mentions Cobra, CLI development, command-line tools, kubectl patterns, or Go CLI frameworks.
| name | yargs-patterns |
| description | Advanced yargs patterns for Node.js CLI argument parsing with subcommands, options, middleware, and validation |
| tags | ["nodejs","cli","yargs","argument-parsing","validation"] |
Comprehensive 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.