optionator
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -85,6 +85,8 @@ // Generated by LiveScript 1.2.0 | ||
return function(arg$){ | ||
var showHidden, maxWidth, output, out, data, optionCount, totalPreLen, preLens, i$, ref$, len$, item, that, pre, desc, preLen, sortedPreLens, maxPreLen, preLenMean, x, padAmount, descSepLen, fullWrapCount, partialWrapCount, descLen, totalLen, initialSpace, wrapAllFull, i, wrap; | ||
showHidden = (arg$ != null | ||
var ref$, showHidden, interpolate, ref1$, maxWidth, output, out, data, optionCount, totalPreLen, preLens, i$, len$, item, that, pre, desc, preLen, sortedPreLens, maxPreLen, preLenMean, x, padAmount, descSepLen, fullWrapCount, partialWrapCount, descLen, totalLen, initialSpace, wrapAllFull, i, wrap; | ||
ref$ = arg$ != null | ||
? arg$ | ||
: {}).showHidden; | ||
: {}, showHidden = ref$.showHidden, interpolate = (ref1$ = ref$.interpolate) != null | ||
? ref1$ | ||
: {}; | ||
maxWidth = stdout != null && stdout.isTTY ? stdout.columns - 1 : null; | ||
@@ -96,3 +98,3 @@ output = []; | ||
if (prepend) { | ||
out(prepend); | ||
out(interp(prepend, interpolate)); | ||
out(); | ||
@@ -203,3 +205,3 @@ } | ||
out(); | ||
out(append); | ||
out(interp(append, interpolate)); | ||
} | ||
@@ -222,2 +224,10 @@ return unlines(output); | ||
} | ||
function interp(string, object){ | ||
var key, value; | ||
for (key in object) { | ||
value = object[key]; | ||
string = string.replace(RegExp('{{' + key + '}}'), value); | ||
} | ||
return string; | ||
} | ||
module.exports = { | ||
@@ -224,0 +234,0 @@ generateHelp: generateHelp, |
// Generated by LiveScript 1.2.0 | ||
(function(){ | ||
var VERSION, ref$, id, map, compact, any, groupBy, chars, isItNaN, keys, Obj, deepIs, closestString, nameToRaw, camelize, dasherize, generateHelp, generateHelpForOption, parsedTypeCheck, parseType, parseLevn, camelizeKeys, parseString, main, toString$ = {}.toString; | ||
VERSION = '0.1.0'; | ||
var VERSION, ref$, id, map, compact, any, groupBy, chars, isItNaN, keys, Obj, deepIs, closestString, nameToRaw, camelize, dasherize, generateHelp, generateHelpForOption, parsedTypeCheck, parseType, parseLevn, camelizeKeys, parseString, main, toString$ = {}.toString, slice$ = [].slice; | ||
VERSION = '0.2.0'; | ||
ref$ = require('prelude-ls'), id = ref$.id, map = ref$.map, compact = ref$.compact, any = ref$.any, groupBy = ref$.groupBy, chars = ref$.chars, isItNaN = ref$.isItNaN, keys = ref$.keys, Obj = ref$.Obj; | ||
@@ -38,3 +38,3 @@ deepIs = require('deep-is'); | ||
traverse = function(options){ | ||
var i$, len$, option, name, e, parsedPossibilities, parsedType, j$, ref$, len1$, possibility, alias; | ||
var i$, len$, option, name, e, parsedPossibilities, parsedType, j$, ref$, len1$, possibility, that, rawDependsType, dependsOpts, dependsType, alias; | ||
if (toString$.call(options).slice(8, -1) !== 'Array') { | ||
@@ -86,2 +86,23 @@ throw new Error('No options defined.'); | ||
} | ||
if (that = option.dependsOn) { | ||
if (that.length) { | ||
ref$ = [].concat(option.dependsOn), rawDependsType = ref$[0], dependsOpts = slice$.call(ref$, 1); | ||
dependsType = rawDependsType.toLowerCase(); | ||
if (dependsOpts.length) { | ||
if (dependsType === 'and' || dependsType === 'or') { | ||
option.dependsOn = [dependsType].concat(slice$.call(dependsOpts)); | ||
} else { | ||
throw new Error("Option '" + name + "': If you have more than one dependency, you must specify either 'and' or 'or'"); | ||
} | ||
} else { | ||
if ((ref$ = dependsType.toLowerCase()) === 'and' || ref$ === 'or') { | ||
option.dependsOn = null; | ||
} else { | ||
option.dependsOn = ['and', rawDependsType]; | ||
} | ||
} | ||
} else { | ||
option.dependsOn = null; | ||
} | ||
} | ||
if (option.required) { | ||
@@ -120,3 +141,3 @@ required.push(name); | ||
parse = function(input, arg$){ | ||
var slice, obj, positional, restPositional, prop, setValue, setDefaults, checkRequired, mutuallyExclusiveError, checkMutuallyExclusive, args, key, value, option, i$, len$, arg, that, result, short, argName, usingAssign, val, flags, len, j$, len1$, i, flag, opt, name, negated, noedName, valPrime; | ||
var slice, obj, positional, restPositional, prop, setValue, setDefaults, checkRequired, mutuallyExclusiveError, checkMutuallyExclusive, checkDependency, checkDependencies, args, key, value, option, i$, len$, arg, that, result, short, argName, usingAssign, val, flags, len, j$, len1$, i, flag, opt, name, negated, noedName, valPrime; | ||
slice = (arg$ != null | ||
@@ -208,2 +229,32 @@ ? arg$ | ||
}; | ||
checkDependency = function(option){ | ||
var dependsOn, type, targetOptionNames, i$, len$, targetOptionName, targetOption; | ||
dependsOn = option.dependsOn; | ||
if (!dependsOn || option.dependenciesMet) { | ||
return true; | ||
} | ||
type = dependsOn[0], targetOptionNames = slice$.call(dependsOn, 1); | ||
for (i$ = 0, len$ = targetOptionNames.length; i$ < len$; ++i$) { | ||
targetOptionName = targetOptionNames[i$]; | ||
targetOption = obj[targetOptionName]; | ||
if (targetOption && checkDependency(targetOption)) { | ||
if (type === 'or') { | ||
return true; | ||
} | ||
} else if (type === 'and') { | ||
throw new Error("The option '" + option.option + "' did not have its dependencies met."); | ||
} | ||
} | ||
if (type === 'and') { | ||
return true; | ||
} else { | ||
throw new Error("The option '" + option.option + "' did not meet any of its dependencies."); | ||
} | ||
}; | ||
checkDependencies = function(){ | ||
var name; | ||
for (name in obj) { | ||
checkDependency(opts[name]); | ||
} | ||
}; | ||
switch (toString$.call(input).slice(8, -1)) { | ||
@@ -230,2 +281,3 @@ case 'String': | ||
checkMutuallyExclusive(); | ||
checkDependencies(); | ||
setDefaults(); | ||
@@ -326,2 +378,3 @@ checkRequired(); | ||
checkMutuallyExclusive(); | ||
checkDependencies(); | ||
setDefaults(); | ||
@@ -328,0 +381,0 @@ checkRequired(); |
{ | ||
"name": "optionator", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"author": "George Zahariev <z@georgezahariev.com>", | ||
"description": "option parsing", | ||
"homepage": "", | ||
"description": "option parsing and help generation", | ||
"homepage": "https://github.com/gkz/optionator", | ||
"keywords": "options", | ||
@@ -8,0 +8,0 @@ "files": [ |
@@ -1,7 +0,10 @@ | ||
# optionator | ||
# Optionator | ||
<a name="optionator" /> | ||
Optionator is an option parsing and help generation library. It uses [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. It can accept an array of arguments (like from `process.argv`), a string, or an object. | ||
Optionator is an option parsing and help generation library. | ||
Unlike other libraries, optionator prefers to fail with helpful error messages rather than silently accept poor input. | ||
## 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. | ||
@@ -14,2 +17,9 @@ $ cmd --halp | ||
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](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. | ||
Optionator is used by [Grasp](http://graspjs.com) and [eslint](https://github.com/eslint/eslint). | ||
MIT license. Version 0.1.1. | ||
@@ -19,3 +29,3 @@ | ||
For updates on optionator, [follow me on twitter](https://twitter.com/gkzahariev). | ||
For updates on Optionator, [follow me on twitter](https://twitter.com/gkzahariev). | ||
@@ -44,3 +54,3 @@ ## Usage | ||
### parse(input, options) | ||
### parse(input, parseOptions) | ||
`parse` processes the `input` according to your settings, and returns an object with the results. | ||
@@ -50,3 +60,4 @@ | ||
* input - `[String] | Object | String` - the input you wish to parse | ||
* options - `{slice: Int}` - the only current option is `slice`, which 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`) | ||
* parseOptions - `{slice: Int}` - all options optional | ||
- `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`) | ||
@@ -67,3 +78,5 @@ ##### returns | ||
##### arguments | ||
* options - `{showHidden: Boolean}` - the only current option is `showHidden`, which specifies whether to show options with `hidden: true` specified, by default it is `false` | ||
* helpOptions - `{showHidden: Boolean, interpolate: Object}` - all options optional | ||
- `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` | ||
@@ -117,2 +130,3 @@ ##### returns | ||
requried: Maybe Boolean, | ||
dependsOn: Maybe [String] | String | ||
description: Maybe String, | ||
@@ -151,2 +165,3 @@ longDescription: Maybe String, | ||
* `required` is an optional boolean - if set to `true`, the option parsing will fail if the option is not defined | ||
* `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']`) other options are set | ||
* `description` is an optional string, which will be displayed next to the option in the help text | ||
@@ -186,3 +201,2 @@ * `longDescription` is an optional string, it will be displayed instead of the `description` when `generateHelpForOption` is used | ||
## Technical About | ||
`optionator` is written in [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. |
52378
1192
195