optionator
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
@aashutoshrathi/word-wrap | 1.2.6 | 4.03 kB | MIT | prod | |
deep-is | 0.1.4 | 3.32 kB | MIT | prod | |
fast-levenshtein | 2.0.6 | 3.61 kB | MIT | prod | |
levn | 0.4.1 | 7.29 kB | MIT | prod | |
prelude-ls | 1.2.1 | 9.19 kB | MIT | prod | |
type-check | 0.4.0 | 6.62 kB | MIT | prod |
Optionator is a powerful JavaScript/Node.js library designed for option parsing and help generation. By ensuring that only valid input is accepted, Optionator minimizes the potential for errors in your application. For instance, if an option is mistyped, Optionator will show an error along with a suggestion for the intended option. Similarly, if an option's argument is of the wrong type, Optionator will reject it rather than letting it destabilize your application. Optionator's help text adapts to fit different sizes of console windows and its flexible input options (arrays, strings, or objects) greatly facilitate testing. This makes Optionator a practical solution for developers who require rigorous, adaptable, and easy-to-use parsing capabilities.
To utilize Optionator in your project, you need to first install it by using the npm install command: npm install optionator
. After the installation is complete, you can start using Optionator in your code. Here is an example of how to initialize and use Optionator:
var optionator = require('optionator')({
prepend: 'Usage: cmd [options]',
append: 'Version 1.0.0',
options: [{
option: 'help',
alias: 'h',
type: 'Boolean',
description: 'displays help'
}, {
option: 'count',
alias: 'c',
type: 'Int',
description: 'number of things',
example: 'cmd --count 2'
}]
});
var options = optionator.parseArgv(process.argv);
if (options.help) {
console.log(optionator.generateHelp());
}
In this code snippet, optionator
is required and configured with options. The parseArgv
function is then used to parse the command-line arguments, and the results are stored in the options
variable. If the --help
option is given, optionator.generateHelp()
is used to display the help message.
The parse
function can process your input according to the specified settings:
parse(['node', 't.js', '--count', '2', 'positional']); // {count: 2, _: ['positional']}
parse('--count 2 positional'); // {count: 2, _: ['positional']}
parse({count: 2, _:['positional']}); // {count: 2, _: ['positional']}
The generateHelp
function produces help text based on your specified settings:
generateHelp(); /*
"Usage: cmd [options] positional
-h, --help displays help
-c, --count Int number of things
Version 1.0.0
"*/
Use generateHelpForOption
to produce expanded help text for a specific option:
generateHelpForOption('count'); /*
"-c, --count Int
description: number of things
example: cmd --count 2
"*/
For a comprehensive guide on using Optionator, consult the official GitHub repository at git://github.com/gkz/optionator.git. Here you'll find a detailed specification of option settings, argument formatting, and more. It also provides information about the algorithms and libraries used behind the scenes. The usage guides and examples should provide you with all the information needed to begin implementing Optionator in your projects.