Yargs vous aide à créer des outils en ligne de commande interactifs en analysant les arguments et en générant une aide soignée.
Utilisation de base
const argv = require('yargs').argv;
argv._ // Array of positional arguments: [ ... ]
argv.$0 // Executable name: "node bin/mybin"
argv.verbose // Boolean / string flag value: --verbose
Sortie version et aide
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")
Configuration des options
.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 */ }
})
Commandes et exemples
.example('$0 -f config.json', 'Run with configuration file')
.epilog('Copyright 2026')
.command('start', 'Start the server application')
Comptage et cumul de drapeaux
.count('verbose')
// Passing -vvv results in:
argv.verbose // => 3
Mode strict (Strict Mode)
.strict()
Méthodes programmatiques
yargs.showHelp()
const helpText = yargs.help() // => string output