argi
v1.0.2
Published
A simple args parser
Downloads
11
Maintainers
Readme
argi
A simple args parser
Install
$ npm install --save argi
Usage
const argi = require('argi');
// Options
argi.option('name', 'description', { default: 'bob', aliases: 'n', boolean: false, hidden: false });
// Commands
argi.command('sayhi', 'description', { hidden: false });
// Parse argv
const parsed = argi.parse(process.argv.slice(2));
if (parsed.sayhi) {
console.log(`Hi ${ parsed.name }!`);
} else {
console.log(args.help());
}
API
argi.options(name, description[, options])
name
Type: string
The option's name.
description
Type: string
What the option does. Will be used for help.
options
Type: object
default
Type: string
The options default value.
aliases
Type: string
, array
Alias names for the option.
boolean
Type: boolean
Default: false
Specifies whether the option is a boolean(true
/false
) or a string. If set to true arguments after the option will count as a command or as a separate option.
Example:
...
argi.option('boolean', 'description', { boolean: true });
const parsed = argi.parse(['--boolean', 'value'])
parsed.boolean // => true
parsed._ // => ['value']
...
hidden
Type: boolean
Default: false
If option should be displayed in help.
argi.command(name, description[, options])
name
Type: string
The command's name.
description
Type: string
What the command does. Will be used for help.
options
Type: object
hidden
Type: boolean
Default: false
If command should be displayed in help.
argi.help()
Returns a help string. Will throw an error if it's called before argi.parse()
argi.parse(argv)
argv
An array of cli options. You should probably set it to process.argv.slice(2)
Returns an object with the option's values and if commands should be executed.
Example:
...
const parsed = argi.parse(['--test', '--opt', 'yes', 'build']);
// => {
// => test: true,
// => opt: 'yes',
// => build: true
// => }
...
License
MIT © Tobias Herber