Skip to content

Commit

Permalink
Merge branch 'value-equals-default-value'
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed May 2, 2017
2 parents f16b8c8 + 2ce90aa commit fc21ce7
Show file tree
Hide file tree
Showing 22 changed files with 410 additions and 234 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tmp
.coveralls.yml
coverage
2 changes: 0 additions & 2 deletions example/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ Example output:
$ mocha example/mocha.js --value 3 --no-colors
Array
#indexOf()
✓ should pass when the supplied value is between 1 and 3
1 passing (7ms)
*/
4 changes: 2 additions & 2 deletions lib/argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Argv extends Array {
if (this.some(optEquals.test.bind(optEquals))) {
const expandedArgs = []
this.forEach(arg => {
const matches = arg.match(optEquals.re)
const matches = arg.match(optEquals)
if (matches) {
expandedArgs.push(matches[1], option.VALUE_MARKER + matches[2])
} else {
Expand All @@ -53,7 +53,7 @@ class Argv extends Array {
const combinedArg = option.combined
const hasGetopt = this.some(combinedArg.test.bind(combinedArg))
if (hasGetopt) {
findReplace(this, combinedArg.re, arg => {
findReplace(this, combinedArg, arg => {
arg = arg.slice(1)
return arg.split('').map(letter => '-' + letter)
})
Expand Down
20 changes: 11 additions & 9 deletions lib/command-line-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ module.exports = commandLineArgs
function commandLineArgs (optionDefinitions, options) {
options = options || {}
const Definitions = require('./definitions')
const option = require('./option')
const Argv = require('./argv')
const Output = require('./output')
const GroupedOutput = require('./grouped-output')

const definitions = new Definitions()
definitions.load(optionDefinitions)
const argv = new Argv()
Expand All @@ -41,16 +37,22 @@ function commandLineArgs (optionDefinitions, options) {
argv.expandGetoptNotation()
argv.validate(definitions, options)

const output = definitions.isGrouped() ? new GroupedOutput(definitions, options) : new Output(definitions, options)
const OutputClass = definitions.isGrouped() ? require('./grouped-output') : require('./output')
const output = new OutputClass(definitions, options)
let optionName

argv.forEach(arg => {
const option = require('./option')
for (const arg of argv) {
if (option.isOption(arg)) {
optionName = output.set(arg) ? undefined : arg
optionName = output.setFlag(arg) ? undefined : arg
} else {
optionName = output.set(optionName, arg) ? undefined : optionName
if (optionName) {
optionName = output.setOptionValue(optionName, arg) ? undefined : optionName
} else {
optionName = output.setValue(arg) ? undefined : optionName
}
}
})
}

return output.toObject()
}
6 changes: 1 addition & 5 deletions lib/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,7 @@ class OptionDefinition {
}

isBoolean (value) {
if (this.type) {
return this.type === Boolean || (t.isFunction(this.type) && this.type.name === 'Boolean')
} else {
return false
}
return this.type === Boolean || (t.isFunction(this.type) && this.type.name === 'Boolean')
}
}

Expand Down
6 changes: 5 additions & 1 deletion lib/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ const t = require('typical')
*/
class Definitions extends Array {
load (definitions) {
this.clear()
arrayify(definitions).forEach(def => this.push(new Definition(def)))
this.validate()
}

clear () {
this.length = 0
}

/**
* validate option definitions
* @returns {string}
Expand Down Expand Up @@ -123,7 +128,6 @@ class Definitions extends Array {
whereNotGrouped () {
return this.filter(def => !containsValidGroup(def))
}

}

function halt (name, message) {
Expand Down
19 changes: 11 additions & 8 deletions lib/grouped-output.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
'use strict'
const t = require('typical')
const arrayify = require('array-back')
const Output = require('./output')

class GroupedOutput extends Output {
toObject () {
const superOutput = super.toObject()
delete superOutput._unknown
const grouped = {
_all: this.output
_all: superOutput
}
if (this.unknown.length) grouped._unknown = this.unknown

this.definitions.whereGrouped().forEach(def => {
arrayify(def.group).forEach(groupName => {
const outputValue = this.output[def.name]
for (const groupName of arrayify(def.group)) {
grouped[groupName] = grouped[groupName] || {}
if (t.isDefined(this.output[def.name])) {
grouped[groupName][def.name] = this.output[def.name]
if (outputValue && outputValue.isDefined()) {
grouped[groupName][def.name] = outputValue.value
}
})
}
})

this.definitions.whereNotGrouped().forEach(def => {
if (t.isDefined(this.output[def.name])) {
const outputValue = this.output[def.name]
if (outputValue && outputValue.isDefined()) {
if (!grouped._none) grouped._none = {}
grouped._none[def.name] = this.output[def.name]
grouped._none[def.name] = outputValue.value
}
})
return grouped
Expand Down
34 changes: 8 additions & 26 deletions lib/option.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
'use strict'

/**
* A module for testing for and extracting names from options (e.g. `--one`, `-o`)
*
* @module option
* @private
*/

class Arg {
constructor (re) {
this.re = re
}

class ArgRegExp extends RegExp {
name (arg) {
return arg.match(this.re)[1]
return arg.match(this)[1]
}
test (arg) {
return this.re.test(arg)
}
}

const option = {
short: new Arg(/^-([^\d-])$/),
long: new Arg(/^--(\S+)/),
combined: new Arg(/^-([^\d-]{2,})$/),
isOption (arg) { return this.short.test(arg) || this.long.test(arg) },
optEquals: new Arg(/^(--\S+?)=(.*)/),
VALUE_MARKER: '552f3a31-14cd-4ced-bd67-656a659e9efb' // must be unique
}

module.exports = option
exports.short = new ArgRegExp('^-([^\\d-])$')
exports.long = new ArgRegExp('^--(\\S+)')
exports.combined = new ArgRegExp('^-([^\\d-]{2,})$')
exports.isOption = arg => exports.short.test(arg) || exports.long.test(arg)
exports.optEquals = new ArgRegExp('^(--\\S+?)=(.*)')
exports.VALUE_MARKER = '552f3a31-14cd-4ced-bd67-656a659e9efb' // must be unique
Loading

0 comments on commit fc21ce7

Please sign in to comment.