yargs
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
cliui | 7.0.4 | 7.24 kB | ISC | prod | |
escalade | 3.1.1 | 4.21 kB | MIT | prod | |
get-caller-file | 2.0.5 | 2.33 kB | ISC | prod | |
require-directory | 2.1.1 | 4.27 kB | MIT | prod | |
string-width | 4.2.3 | 2.33 kB | MIT | prod | |
y18n | 5.0.8 | 6.01 kB | ISC | prod | |
yargs-parser | 20.2.9 | 26.98 kB | ISC | prod |
Yargs is a stunning Node.js library that aids in building futuristic, highly-interactive command line tools. By parsing arguments and rendering a user-friendly interface, Yargs brings to the table an array of features to simplify command line syntax. Yargs assists in handling commands and grouped options, creating dynamic help menus, delivering bash-completion shortcuts for commands and options, and much more.
Using Yargs involves implementing it within your Node.js applications. Here is a simple usage example:
#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).argv
if (argv.ships > 3 && argv.distance < 53.5) {
console.log('Plunder more riffiwobbles!')
} else {
console.log('Retreat from the xupptumblers!')
}
In the above code, Yargs is imported and its 'hideBin' function is used to handle the process arguments array. The 'argv' object is then used to access the command line arguments.
Here is a more detailed example:
#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
yargs(hideBin(process.argv))
.command('serve [port]', 'start the server', (yargs) => {
return yargs
.positional('port', {
describe: 'port to bind on',
default: 5000
})
}, (argv) => {
if (argv.verbose) console.info(`start server on :${argv.port}`)
serve(argv.port)
})
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Run with verbose logging'
})
.parse()
In this more intricate example, a command is defined using the 'command' function, a 'verbose' option is added with the 'option' function, and 'parse' is used to parse the arguments.
Yargs' comprehensive documentation can be found by visiting the 'Yargs' API on the GitHub repository page. The documentation offers in-depth insight into Yargs' workings through a systematic table of contents. This resource comes packed with examples, parsing tricks, advanced topics, and tips for contributing to the project. For those looking to delve deeper into the exciting world of Yargs, the repository's 'docs' directory holds a wealth of information.