Yargs ajuda a criar ferramentas interativas de linha de comando analisando argumentos e gerando uma interface 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
Saída de versão e ajuda
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")
Configuração de opções
.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 e exemplos
.example('$0 -f config.json', 'Run with configuration file')
.epilog('Copyright 2026')
.command('start', 'Start the server application')
Contagem e empilhamento de flags
.count('verbose')
// Passing -vvv results in:
argv.verbose // => 3
Modo estrito (Strict Mode)
.strict()
Métodos programáticos
yargs.showHelp()
const helpText = yargs.help() // => string output