jackspeak
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -0,3 +1,7 @@ | ||
# 0.1 | ||
Automatic `--help` usage output handling | ||
# 0.0 | ||
Basic functionality. No help or usage output support yet. |
83
index.js
@@ -11,3 +11,9 @@ 'use strict' | ||
const assert = require('assert') | ||
const cliui = require('cliui') | ||
const path = require('path') | ||
// only way to trigger is run as non-module, which can't be instrumented | ||
/* istanbul ignore next */ | ||
const $0 = require.main ? path.basename(require.main.filename) : '$0' | ||
const _flag = Symbol('flag') | ||
@@ -38,2 +44,60 @@ const _list = Symbol('list') | ||
const trim = string => string | ||
// remove single line breaks | ||
.replace(/([^\n])\n[ \t]*([^\n])/g, '$1 $2') | ||
// normalize mid-line whitespace | ||
.replace(/([^\n])[ \t]+([^\n])/g, '$1 $2') | ||
// two line breaks are enough | ||
.replace(/\n{3,}/g, '\n\n') | ||
.trim() | ||
const ranUsage = Symbol('ranUsage') | ||
const usage = options => { | ||
options[ranUsage] = true | ||
let maxWidth = 0 | ||
const table = Object.keys(options).filter( | ||
k => k !== 'argv' && | ||
k !== 'help' && | ||
k !== 'usage' | ||
).map(k => { | ||
const arg = options[k] | ||
const val = arg[_opt] ? `[${arg.hint || k}]` : '' | ||
const short = arg.short ? ` -${arg.short}${val}` : '' | ||
const desc = trim(arg.description || '[no description provided]') | ||
const mult = arg[_list] ? `${ | ||
desc.indexOf('\n') === -1 ? '\n' : '\n\n' | ||
}Can be set multiple times` : '' | ||
// XXX negated counters and shorthands | ||
const row = [ | ||
`--${k}${val ? `=${val}`: ''}${short}`, | ||
`${desc}${mult}` | ||
] | ||
maxWidth = Math.min(30, Math.max(row[0].length + 4, maxWidth)) | ||
return row | ||
}) | ||
const ui = cliui() | ||
ui.div('Usage:') | ||
ui.div({ | ||
text: (options.usage || `${$0} <options>`), | ||
padding: [0, 0, 1, 2] | ||
}) | ||
if (options.help) | ||
ui.div({text: options.help, paddign: [1, 0, 0, 0]}) | ||
if (table.length) | ||
ui.div({text: 'Options:', padding: [1, 0, 1, 0]}) | ||
table.forEach(row => { | ||
ui.div({ | ||
text: row[0], | ||
width: maxWidth, | ||
padding: [0, 2, 0, 2] | ||
}, { text: row[1] }) | ||
ui.div() | ||
}) | ||
return ui.toString() | ||
} | ||
const jack = options_ => { | ||
@@ -94,2 +158,7 @@ // don't monkey with the originals | ||
`short ${arg.short} used for ${name} and ${short[arg.short]}`) | ||
assert(arg.short !== 'h', | ||
`${name} using 'h' short arg, reserved for --help`) | ||
assert(arg.short !== '?', | ||
`${name} using '?' short arg, reserved for --help`) | ||
short[arg.short] = name | ||
@@ -154,3 +223,5 @@ if (arg[_flag]) | ||
const soslice = arg.slice(f + 1) | ||
const soval = soslice.charAt(0) === '=' ? soslice : '=' + soslice | ||
const soval = !soslice || soslice.charAt(0) === '=' | ||
? soslice : '=' + soslice | ||
expand.push('--' + so + soval) | ||
@@ -169,4 +240,2 @@ f = arg.length | ||
// XXX special handling for -h, --help | ||
const argsplit = arg.split('=') | ||
@@ -177,2 +246,10 @@ const literalKey = argsplit.shift() | ||
if (literalKey === '-h' || | ||
literalKey === '--help' || | ||
literalKey === '-?') { | ||
if (!options[ranUsage]) | ||
console.log(usage(options)) | ||
continue | ||
} | ||
const spec = options[key] | ||
@@ -179,0 +256,0 @@ |
{ | ||
"name": "jackspeak", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "A very strict and proper argument parser.", | ||
@@ -20,3 +20,5 @@ "main": "index.js", | ||
], | ||
"dependencies": {} | ||
"dependencies": { | ||
"cliui": "^4.1.0" | ||
} | ||
} |
@@ -106,3 +106,7 @@ # jackspeak | ||
covlevel: opt({ | ||
alias: ['--statements=${value}', '--lines=${value}', '--branches=${value}] | ||
alias: [ | ||
'--statements=${value}', | ||
'--lines=${value}', | ||
'--branches=${value}' | ||
] | ||
}), | ||
@@ -109,0 +113,0 @@ |
12472
251
137
1
+ Addedcliui@^4.1.0
+ Addedansi-regex@2.1.13.0.1(transitive)
+ Addedcliui@4.1.0(transitive)
+ Addedcode-point-at@1.1.0(transitive)
+ Addedis-fullwidth-code-point@1.0.02.0.0(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
+ Addedstring-width@1.0.22.1.1(transitive)
+ Addedstrip-ansi@3.0.14.0.0(transitive)
+ Addedwrap-ansi@2.1.0(transitive)