Yargs helpt je interactieve opdrachtregelprogramma's te bouwen door argumenten te parsen en een overzichtelijke interface te genereren.
Basisgebruik
const argv = require('yargs').argv;
argv._ // Array of positional arguments: [ ... ]
argv.$0 // Executable name: "node bin/mybin"
argv.verbose // Boolean / string flag value: --verbose
Versie- & Hulpuitvoer
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")
Optieconfiguratie
.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 */ }
})
Opdrachten & Voorbeelden
.example('$0 -f config.json', 'Run with configuration file')
.epilog('Copyright 2026')
.command('start', 'Start the server application')
Vlaggen tellen & stapelen
.count('verbose')
// Passing -vvv results in:
argv.verbose // => 3
Strikte modus (Strict Mode)
.strict()
Programmatische methoden
yargs.showHelp()
const helpText = yargs.help() // => string output