Skip to content

Commit

Permalink
Fixed an issue with defaultValue combined with defaultOption #47
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Apr 22, 2017
1 parent 3ccb028 commit 3a5fde0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Describes a command-line option. Additionally, you can add `description` and `ty
* [.multiple](#module_definition--OptionDefinition.OptionDefinition+multiple) : <code>boolean</code>
* [.defaultOption](#module_definition--OptionDefinition.OptionDefinition+defaultOption) : <code>boolean</code>
* [.defaultValue](#module_definition--OptionDefinition.OptionDefinition+defaultValue) : <code>\*</code>
* [.group](#module_definition--OptionDefinition.OptionDefinition+group) : <code>string</code> &#124; <code>Array.&lt;string&gt;</code>
* [.group](#module_definition--OptionDefinition.OptionDefinition+group) : <code>string</code> \| <code>Array.&lt;string&gt;</code>

<a name="module_definition--OptionDefinition.OptionDefinition+name"></a>

Expand Down Expand Up @@ -305,7 +305,7 @@ An initial value for the option.
**Kind**: instance property of <code>[OptionDefinition](#exp_module_definition--OptionDefinition)</code>
<a name="module_definition--OptionDefinition.OptionDefinition+group"></a>

### option.group : <code>string</code> &#124; <code>Array.&lt;string&gt;</code>
### option.group : <code>string</code> \| <code>Array.&lt;string&gt;</code>
When your app has a large amount of options it makes sense to organise them in groups.

There are two automatic groups: `_all` (contains all options) and `_none` (contains options without a `group` specified in their definition).
Expand Down
2 changes: 1 addition & 1 deletion lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Output {
def = this.definitions.getDefault()
if (def) {
/* if it's not a `multiple` and the defaultOption has already been set, move on */
if (!def.multiple && t.isDefined(this.output[def.name])) {
if (!def.multiple && t.isDefined(this.output[def.name]) && this.output[def.name] !== def.defaultValue) {
if (t.isDefined(value)) this.unknown.push(value)
return true
/* in the case we're setting an --option=value value on a multiple defaultOption, tag the value onto the previous unknown */
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"author": "Lloyd Brookes <[email protected]>",
"license": "MIT",
"devDependencies": {
"coveralls": "^2.11.15",
"jsdoc-to-markdown": "^2.0.1",
"coveralls": "^2.13.0",
"jsdoc-to-markdown": "^3.0.0",
"test-runner": "^0.3.0"
},
"dependencies": {
Expand Down
19 changes: 19 additions & 0 deletions test/default-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,22 @@ runner.test('default value: is arrayifed if multiple set', function () {
one: [ '2' ]
})
})

runner.test('default value: combined with defaultOption', function () {
const defs = [
{ name: 'path', defaultOption: true, defaultValue: './' },
]

let argv = [ '--path', 'test' ]
a.deepStrictEqual(commandLineArgs(defs, { argv }), {
path: 'test'
})
argv = [ 'test' ]
a.deepStrictEqual(commandLineArgs(defs, { argv }), {
path: 'test'
})
argv = [ ]
a.deepStrictEqual(commandLineArgs(defs, { argv }), {
path: './'
})
})

0 comments on commit 3a5fde0

Please sign in to comment.