Yargs te ayuda a construir herramientas interactivas de línea de comandos analizando argumentos y generando una interfaz elegante.
Uso básico
const argv = require('yargs').argv;
argv._ // Array of positional arguments: [ ... ]
argv.$0 // Executable name: "node bin/mybin"
argv.verbose // Boolean / string flag value: --verbose
Salida de versión y ayuda
const argv = require('yargs')
// Version configuration
.alias('v', 'version')
.version(function() { return require('../package').version; })
.describe('v', 'show version information')
// Help configuration
.alias('h', 'help')
.help('help')
.usage('Usage: $0 -x [num]')
.showHelpOnFail(false, "Specify --help for available options")
Configuración de opciones
.option('f', {
alias: 'file',
describe: 'Path to target file',
type: 'string', /* 'array' | 'boolean' | 'string' */
nargs: 1,
demand: true,
demand: 'file is required',
default: '/etc/passwd'
// also supports: count: true, requiresArg: true
})
.options({
f: { /* option hash */ }
})
Comandos y ejemplos
.example('$0 -f config.json', 'Run with configuration file')
.epilog('Copyright 2026')
.command('start', 'Start the server application')
Conteo y apilamiento de flags
.count('verbose')
// Passing -vvv results in:
argv.verbose // => 3
Modo estricto (Strict Mode)
.strict()
Métodos programáticos
yargs.showHelp()
const helpText = yargs.help() // => string output