Advanced yargs patterns for Node.js CLI argument parsing with subcommands, options, middleware, and validation
View on GitHubvanman2024/cli-builder
cli-builder
plugins/cli-builder/skills/yargs-patterns/SKILL.md
January 22, 2026
Select agents to install to:
npx add-skill https://github.com/vanman2024/cli-builder/blob/main/plugins/cli-builder/skills/yargs-patterns/SKILL.md -a claude-code --skill yargs-patternsInstallation paths:
.claude/skills/yargs-patterns/# yargs Patterns Skill
Comprehensive patterns and templates for building CLI applications with yargs, the modern Node.js argument parsing library.
## Overview
yargs is a powerful argument parsing library for Node.js that provides:
- Automatic help generation
- Rich command syntax (positional args, options, flags)
- Type coercion and validation
- Subcommands with isolated option namespaces
- Middleware for preprocessing
- Completion scripts for bash/zsh
## Quick Reference
### Basic Setup
```javascript
#!/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();
```
### Subcommands
```javascript
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();
```
### Options and Flags
```javascript
yargs(hideBin(p