Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/index.ls
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions test/tests.ls
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,29 @@ 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']
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'
Expand Down Expand Up @@ -401,6 +424,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'
Expand All @@ -417,6 +481,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

Expand Down