From 4132892c264464932225f681d3fc4e4e7c6a06e1 Mon Sep 17 00:00:00 2001 From: duralog Date: Thu, 20 Nov 2014 08:56:25 +0100 Subject: [PATCH 1/3] add initial parameter to parse function --- lib/index.js | 9 +++++--- src/index.ls | 3 ++- test/tests.ls | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 9a01d4b..bc9dabb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -136,11 +136,14 @@ return opt; }; parse = function(input, arg$){ - var slice, obj, positional, restPositional, overrideRequired, prop, setValue, setDefaults, checkRequired, mutuallyExclusiveError, checkMutuallyExclusive, checkDependency, checkDependencies, args, key, value, option, ref$, i$, len$, arg, that, result, short, argName, usingAssign, val, flags, len, j$, len1$, i, flag, opt, name, negated, noedName, valPrime; - slice = (arg$ != null + var ref$, slice, initial, obj, positional, restPositional, overrideRequired, 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; + ref$ = arg$ != null ? arg$ - : {}).slice; + : {}, slice = ref$.slice, initial = ref$.initial; obj = {}; + if (initial) { + import$(obj, initial); + } positional = []; restPositional = false; overrideRequired = false; diff --git a/src/index.ls b/src/index.ls index 4c3c47a..688174c 100644 --- a/src/index.ls +++ b/src/index.ls @@ -95,8 +95,9 @@ main = (lib-options) -> throw new Error "Invalid option '#{ name-to-raw name}'#{ if possibly-meant then " - perhaps you meant '#{ name-to-raw possibly-meant }'?" else '.'}" opt - parse = (input, {slice} = {}) -> + parse = (input, {slice, initial} = {}) -> obj = {} + obj <<< initial if initial positional = [] rest-positional = false override-required = false diff --git a/test/tests.ls b/test/tests.ls index 883bd56..d64604b 100644 --- a/test/tests.ls +++ b/test/tests.ls @@ -239,6 +239,19 @@ suite 'defaults' -> eq {list: [1,2]}, [], '', opt eq {list: [8,9]}, [], '--list 8,9', opt +suite 'initial' -> + test 'basic-initial' -> + opt = [option: 'go', type: 'String'] + more = {initial: go: 'boom'} + eq {go: 'boom'}, [], '', opt,, more + eq {go: 'haha'}, [], '--go haha', opt,, more + + test 'array-initial' -> + opt = [option: 'list', type: 'Array'] + more = {initial: list: [1,2]} + eq {list: [1,2]}, [], '', opt,, more + eq {list: [8,9]}, [], '--list 8,9', opt,, more + suite 'array/object input' -> opts = * option: 'el' @@ -401,6 +414,47 @@ suite 'concat repeated arrays' -> test 'overwrites non-array' -> eq {x: 3}, [], '-x 1 -x 2 -x 3', opts, more +suite 'merge repeated objects with initial' -> + opts = + * option: 'config' + alias: 'c' + type: 'Object' + * option: 'x' + type: 'Number' + * option: 'b' + type: 'Boolean' + + more = {+merge-repeated-objects} + + test 'basic' -> + eq {config: {a: 4, b: 5, c: 6}}, [], '-c a:4 -c b:5 -c c:6', opts, more, {initial: config: {a: 1, b: 2}} + + test 'same properties' -> + eq {config: {a: 0, b: 2}}, [], '-c a:1 -c a:2 -c a:0', opts, more, {initial: config: {a: 1, b: 2}} + + test 'multiple properties in one go' -> + eq {config: {a: 1, b: 2, c: 3, d: 4}}, [], '-c "c: 3, d: 4"', opts, more, {initial: config: {a: 1, b: 2}} + + test 'overwrites non-array' -> + eq {config: {a: 1, b: 2}, x: 0}, [], '-x 1 -x 2 -x 0', opts, more, {initial: config: {a: 1, b: 2}} + +suite 'concat repeated arrays and merge repeated objects' -> + opts = + * option: 'nums' + alias: 'n' + type: '[Number]' + * option: 'x' + type: 'Number' + + more = {+concat-repeated-arrays, +merge-repeated-objects} + + test 'basic' -> + eq {nums: [1,2,3,4,5,6]}, [], '-n 4 -n 5 -n 6', opts, more, {initial: nums: [1,2,3]} + + test 'overwrites non-array' -> + eq {x: 3, nums: [1,2,3]}, [], '-x 1 -x 2 -x 3', opts, more, {initial: nums: [1,2,3]} + + suite 'merge repeated objects' -> opts = * option: 'config' @@ -417,6 +471,9 @@ suite 'merge repeated objects' -> test 'same properties' -> eq {config: {a: 3}}, [], '-c a:1 -c a:2 -c a:3', opts, more + test 'same properties with falsy value' -> + eq {config: {a: 0}}, [], '-c a:1 -c a:2 -c a:0', opts, more + test 'multiple properties in one go' -> eq {config: {a: 1, b: 2, c: 3, d: 4}}, [], '-c "a:1,b:2" -c "c: 3, d: 4"', opts, more From e133e96ad425b4581788f2393a8bc8b330f07139 Mon Sep 17 00:00:00 2001 From: duralog Date: Thu, 20 Nov 2014 09:04:12 +0100 Subject: [PATCH 2/3] final touches --- .editorconfig | 13 +++++++++++++ test/tests.ls | 10 ++++++++++ 2 files changed, 23 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5d12634 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/test/tests.ls b/test/tests.ls index d64604b..c0c622d 100644 --- a/test/tests.ls +++ b/test/tests.ls @@ -239,6 +239,16 @@ suite 'defaults' -> eq {list: [1,2]}, [], '', opt eq {list: [8,9]}, [], '--list 8,9', opt + test 'number' -> + opt = [option: 'num', type: 'Number', default: '0'] + eq {num: 0}, [], '', opt + eq {num: 1}, [], '--num 1', opt + + test 'boolean' -> + opt = [option: 'bool', type: 'Boolean', default: 'false'] + eq {bool: false}, [], '', opt + eq {bool: true}, [], '--bool', opt + suite 'initial' -> test 'basic-initial' -> opt = [option: 'go', type: 'String'] From 8b213465e42a2d9ea31d3c9a0af06711c32fe120 Mon Sep 17 00:00:00 2001 From: duralog Date: Thu, 20 Nov 2014 09:24:36 +0100 Subject: [PATCH 3/3] add readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 04d2322..6f2788c 100644 --- a/README.md +++ b/README.md @@ -54,8 +54,9 @@ var optionator = require('optionator')({ ##### arguments * input - `[String] | Object | String` - the input you wish to parse -* parseOptions - `{slice: Int}` - all options optional +* parseOptions - `{slice: Int, initial Object}` - 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`) + - `initial` specifies an initial object to begin with. Helpful if you would like to override configuration options with command line arguments. ##### 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.