Optionatoruses[type-check](https://github.com/gkz/type-check) and [levn](https://github.com/gkz/levn) behind the scenes to cast and verify input according the specified types.
`require('optionator');` returns a function. It has one property, `VERSION`, the current version of the library as a string. This function is called with an object specifying your options and other information, see the [settings format section](#settings-format). This in turn returns an object with three properties, `parse`, `parseArgv`, `generateHelp`, and `generateHelpForOption`,whichareallfunctions.
```js
varoptionator=require('optionator')({
prepend:'Usage:cmd[options]',
append:'Version1.0.0',
options:[{
option:'help',
alias:'h',
type:'Boolean',
description:'displayshelp'
},{
option:'count',
alias:'c',
type:'Int',
description:'numberofthings',
example:'cmd--count2'
}]
});
varoptions=optionator.parseArgv(process.argv);
if(options.help){
console.log(optionator.generateHelp());
}
...
```
###parse(input,parseOptions)
`parse` processes the `input`accordingtoyoursettings,andreturnsanobjectwiththeresults.
-`slice` specifies how much to slice away from the beginning if the input is an array or string - by default `0` for string, `2` for array (works with `process.argv`)
#####returns
`Object` - the parsed options, each key is a camelCase version of the option name (specified in dash-case), and each value is the processed value for that option. Positional values are in an array under the `_`key.
-`showHidden` specifies whether to show options with `hidden: true` specified, by default it is `false`
-`interpolate` specify data to be interpolated in `prepend` and `append` text, `{{key}}` is the format - eg. `generateHelp({interpolate:{version: '0.4.2'}})`, will change this `append` text: `Version {{version}}` to `Version 0.4.2`
#####returns
`String`-thegeneratedhelptext
#####example
```js
generateHelp();/*
"Usage: cmd [options] positional
-h, --help displays help
-c, --count Int number of things
Version 1.0.0
"*/
```
###generateHelpForOption(optionName)
`generateHelpForOption` produces expanded help text for the specified with `optionName` option. If an `example` was specified for the option, it will be displayed, and if a `longDescription` was specified, it will display that instead of the `description`.
concatRepeatedArrays:Boolean|(Boolean,Object),// deprecated, set in defaults object
mergeRepeatedObjects:Boolean,// deprecated, set in defaults object
positionalAnywhere:Boolean,
typeAliases:Object,
defaults:Object
}
Allofthepropertiesareoptional(the`Maybe` has been excluded for brevities sake), except for having either `heading: String` or `option: String` in each object in the `options`array.
*`concatRepeatedArrays` see description under the "Option Properties" heading - use at the top level is deprecated, if you want to set this for all options, use the `defaults`property
*`mergeRepeatedObjects` see description under the "Option Properties" heading - use at the top level is deprecated, if you want to set this for all options, use the `defaults`property
*`positionalAnywhere` is an optional boolean (defaults to `true`) - when `true` it allows positional arguments anywhere, when `false`, all arguments after the first positional one are taken to be positional as well, even if they look like a flag. For example, with `positionalAnywhere: false`, the arguments `--flag --boom 12 --crack` would have two positional arguments: `12` and `--crack`
*`typeAliases` is an optional object, it allows you to set aliases for types, eg. `{Path: 'String'}` would allow you to use the type `Path` as an alias for the type `String`
*`defaults` is an optional object following the option properties format, which specifies default values for all options. A default will be overridden if manually set. For example, you can do `default: { type: "String" }` to set the default type of all options to `String`, and then override that default in an individual option by setting the `type`property
*`type`isarequiredstringinthe[typecheck](https://github.com/gkz/type-check) [format](https://github.com/gkz/type-check#type-format), this will be used to cast the inputted value and validate it
*`enum` is an optional array of strings, each string will be parsed by [levn](https://github.com/gkz/levn) - the argument value must be one of the resulting values - each potential value must validate against the specified `type`
*`default` is a optional string, which will be parsed by [levn](https://github.com/gkz/levn) and used as the default value if none is set - the value must validate against the specified `type`
*`restPositional` is an optional boolean - if set to `true`,everythingaftertheoptionwillbetakentobeapositionalargument,evenifitlookslikeanamedargument
*`required` is an optional boolean - if set to `true`,theoptionparsingwillfailiftheoptionisnotdefined
*`overrideRequired` is a optional boolean - if set to `true` and the option is used, and there is another option which is required but not set, it will override the need for the required option and there will be no error - this is useful if you have required options and want to use `--help` or `--version`flags
*`concatRepeatedArrays` is an optional boolean or tuple with boolean and options object (defaults to `false`) - when set to `true` and an option contains an array value and is repeated, the subsequent values for the flag will be appended rather than overwriting the original value - eg. option `g` of type `[String]`: `-g a -g b -g c,d` will result in `['a','b','c','d']`
Youcansupplyanoptionsobjectbygivingthefollowingvalue:`[true, options]`. The one currently supported option is `oneValuePerFlag`,thisonlyallowsonearrayvalueperflag.Thisisusefulifyourpotentialvaluescontainacomma.
*`mergeRepeatedObjects` is an optional boolean (defaults to `false`) - when set to `true` and an option contains an object value and is repeated, the subsequent values for the flag will be merged rather than overwriting the original value - eg. option `g` of type `Object`: `-g a:1 -g b:2 -g c:3,d:4` will result in `{a: 1, b: 2, c: 3, d: 4}`
*`dependsOn` is an optional string or array of strings - if simply a string (the name of another option), it will make sure that that other option is set, if an array of strings, depending on whether `'and'` or `'or'` is first, it will either check whether all (`['and', 'option-a', 'option-b']`), or at least one (`['or', 'option-a', 'option-b']`)otheroptionsareset
Nameargumentsofanylengthareprefixedwith`--` (eg. `--go`), and those of one character may be prefixed with either `--` or `-` (eg. `-g`).
Therearetwotypesofnamedarguments:booleanflags(eg.`--problemo`, `-p`) which take no value and result in a `true` if they are present, the falsey `undefined` if they are not present, or `false` if present and explicitly prefixed with `no` (eg. `--no-problemo`). Named arguments with values (eg. `--tseries 800`, `-t 800`) are the other type. If the option has a type `Boolean`itwillautomaticallybemadeintoabooleanflag.Anyothertyperesultsinanamedargumentthattakesavalue.
Formoreinformationabouthowtoproperlysettypestogetthevalueyouwant,takealookatthe[typecheck](https://github.com/gkz/type-check) and [levn](https://github.com/gkz/levn) pages.
Youcangroupsinglecharacterargumentsthatuseasingle`-`, however all except the last must be boolean flags (which take no value). The last may be a boolean flag, or an argument which takes a value - eg. `-ba 2` is equivalent to `-b -a 2`.
Positionalargumentsareallthosevalueswhichdonotfallundertheabove-theycanbeanywhere,notjustattheend.Forexample,in`cmd -b one -a 2 two` where `b` is a boolean flag, and `a` has the type `Number`, there are two positional arguments, `one` and `two`.
Youmayoptionallyuse`=` to separate option names from values, for example: `--count=2`.
Ifyouspecifytheoption`NUM`, then any argument using a single `-` followed by a number will be valid and will set the value of `NUM`. Eg. `-2` will be parsed into `NUM: 2`.
`optionator`iswrittenin[LiveScript](http://livescript.net/) - a language that compiles to JavaScript. It uses [levn](https://github.com/gkz/levn) to cast arguments to their specified type, and uses [type-check](https://github.com/gkz/type-check) to validate values. It also uses the [prelude.ls](http://preludels.com/) library.