Skip to content

Commit

Permalink
Extract arguments from usage as objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
flatheadmill committed Apr 4, 2016
1 parent 804cb73 commit 3b9d6bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
11 changes: 10 additions & 1 deletion t/arguable/usage.t.js
Expand Up @@ -22,7 +22,16 @@ function prove (assert) {

var usage = extractUsage(__filename, 'en_US', [])
assert(usage.chooseUsage('en_US', []), message, 'usage')
assert(usage.getPattern([]), '-c,--config:$|-\t,--longonly:!|', 'pattern')
assert(usage.getPattern([]), [
{
short: 'c',
long: 'config',
arguable: true,
}, {
short: null,
long: 'longonly',
arguable: false
}], 'patterns')
assert(usage.chooseUsage('en_GB', []), message, 'usage')

var usage = extractUsage(path.join(__dirname, 'sub.js'), 'en_US', [])
Expand Down
14 changes: 9 additions & 5 deletions usage.js
Expand Up @@ -12,22 +12,26 @@ function Usage (language, branch, dictionary, command) {
}

Usage.prototype.getPattern = function (command) {
var pattern = ''
var patterns = []
// Extract a definition of the command line arguments from the usage message
// while tiding the usage message; removing special characters that are flags
// to Arguable that do not belong in the usage message printed to `stdout`.
this.chooseUsage(this.language, command).split(/\r?\n/).forEach(function (line) {
var verbose, terse = '-\t', type = '!', out = '', $, trim = /^$/
if ($ = /^(?:[\s*@]*(-[\w\d])[@\s]*,)?[@\s]*(--\w[-\w\d_]*)(?:[\s@]*[\[<]([^\]>]+)[\]>][\s@]*)?/.exec(line)) {
out = $[0], terse = $[1] || '-\t'
, verbose = $[2]
out = $[0], terse = $[1] ? $[1].substring(1) : null
, verbose = $[2].substring(2)
, type = $[3] && (numeric.test($[3]) ? '#' : '$') || '!'
, line = line.substring(out.length)
pattern += terse + ',' + verbose + ':' + type + '|'
patterns.push({
short: terse,
long: verbose,
arguable: type != '!'
})
if (!line.length) trim = /\s+$/
}
}, this)
return pattern
return patterns
}

Usage.prototype.chooseUsage = function (language, command) {
Expand Down

0 comments on commit 3b9d6bb

Please sign in to comment.