Optionator
Optionator is an option parsing and help generation library.
Why?
The problem with other option parsers, such as yargs or minimist, is they just accept all input, valid or not. With Optionator, if you mistype an option, it will give you an error (with a suggestion for what you meant). If you give the wrong type of argument for an option, it will give you an error rather than supplying the wrong input to your application.
Over helpful features include reformatting the help text based on the size of the console, so that it fits even if the console is narrow, and accepting not just an array (eg. process.argv), but a string or object as well, making things like testing much easier.
About
Optionator uses type-check and levn behind the scenes to cast and verify input according the specified types.
Optionator is used by Grasp, eslint, LiveScript, esmangle, escodegen, and more.
MIT license. Version 0.5.0
For updates on Optionator, follow me on twitter.
Usage
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. This in turn returns an object with three properties, parse, generateHelp, and generateHelpForOption, which are all functions.
parse(input, parseOptions)
parse processes the input according to your settings, and returns an object with the results.
arguments
input -
[String] | Object | String- the input you wish to parseparseOptions -
{slice: Int}- all options optionalslicespecifies how much to slice away from the beginning if the input is an array or string - by default0for string,2for array (works withprocess.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.
example
generateHelp(helpOptions)
generateHelp produces help text based on your settings.
arguments
helpOptions -
{showHidden: Boolean, interpolate: Object}- all options optionalshowHiddenspecifies whether to show options withhidden: truespecified, by default it isfalseinterpolatespecify data to be interpolated inprependandappendtext,{{key}}is the format - eg.generateHelp({interpolate:{version: '0.4.2'}}), will change thisappendtext:Version {{version}}toVersion 0.4.2
returns
String - the generated help text
example
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.
arguments
optionName -
String- the name of the option to display
returns
String - the generated help text for the option
example
Settings Format
When your require('optionator'), you get a function that takes in a settings object. This object has the type:
Top Level Properties
prependis an optional string to be placed before the options in the help textappendis an optional string to be placed after the options in the help textoptionsis a required array specifying your options and headings, the options and headings will be displayed in the order specifiedhelpStyleis an optional object which enables you to change the default appearance of some aspects of the help textmutuallyExclusiveis an optional array of arrays of either strings or arrays of strings. The top level array is a list of rules, each rule is a list of elements - each element can be either a string (the name of an option), or a list of strings (a group of option names) - there will be an error if more than one element is presentconcatRepeatedArraysis an optional boolean (defaults tofalse) - when set totrueand 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. optiongof type[String]:-g a -g b -g c,dwill result in['a','b','c','d']mergeRepeatedObjectsis an optional boolean (defaults tofalse) - when set totrueand 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. optiongof typeObject:-g a:1 -g b:2 -g c:3,d:4will result in{a: 1, b: 2, c: 3, d: 4}
Heading Properties
headinga required string, the name of the heading
Option Properties
optionthe required name of the option - use dash-case, without the leading dashesaliasis an optional string or array of strings which specify any aliases for the optiontypeis a required string in the type check format, this will be used to cast the inputted value and validate itenumis an optional array of strings, each string will be parsed by levn - the argument value must be one of the resulting values - each potential value must validate against the specifiedtypedefaultis a optional string, which will be parsed by levn and used as the default value if none is set - the value must validate against the specifiedtyperestPositionalis an optional boolean - if set totrue, everything after the option will be taken to be a positional argument, even if it looks like a named argumentrequiredis an optional boolean - if set totrue, the option parsing will fail if the option is not definedoverrideRequiredis a optional boolean - if set totrueand 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--helpor--versionflagsdependsOnis 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']) other options are setdescriptionis an optional string, which will be displayed next to the option in the help textlongDescriptionis an optional string, it will be displayed instead of thedescriptionwhengenerateHelpForOptionis usedexampleis an optional string or array of strings with example(s) for the option - these will be displayed whengenerateHelpForOptionis used
Help Style Properties
aliasSeparatoris an optional string, separates multiple names from each other - default: ' ,'typeSeparatoris an optional string, separates the type from the names - default: ' 'descriptionSeparatoris an optional string , separates the description from the padded name and type - default: ' 'initialIndentis an optional int - the amount of indent for options - default: 2secondaryIndentis an optional int - the amount of indent if wrapped fully (in addition to the initial indent) - default: 4maxPadFactoris an optional number - affects the default level of padding for the names/type, it is multiplied by the average of the length of the names/type - default: 1.5
Argument Format
At the highest level there are two types of arguments: named, and positional.
Name arguments of any length are prefixed with -- (eg. --go), and those of one character may be prefixed with either -- or - (eg. -g).
There are two types of named arguments: boolean flags (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 it will automatically be made into a boolean flag. Any other type results in a named argument that takes a value.
For more information about how to properly set types to get the value you want, take a look at the type check and levn pages.
You can group single character arguments that use a single -, 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.
Positional arguments are all those values which do not fall under the above - they can be anywhere, not just at the end. For example, 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.
Everything after an -- is positional, even if it looks like a named argument.
You may optionally use = to separate option names from values, for example: --count=2.
If you specify the option 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.
If duplicate named arguments are present, the last one will be taken.
Technical About
optionator is written in LiveScript - a language that compiles to JavaScript. It uses levn to cast arguments to their specified type, and uses type-check to validate values. It also uses the prelude.ls library.