jackspeak 1.3.5 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +9 -0
  2. package/index.js +48 -1
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -23,7 +23,16 @@ as argument names as well; just put them in different sections.
23
23
  - `usage` A function that dumps the help output to stdout.
24
24
  - `explicit` A `Set` containing the names of all arguments that were
25
25
  explicitly set.
26
+ - `original` The `argv` array before any expansions.
26
27
  - `parsed` The `argv` array once all aliases have been expanded.
28
+ - `reparse` A function that takes a string or array of strings, and
29
+ parses it according to the options initially provided. Note that
30
+ this does _not_ update the results object, it's just for expanding
31
+ aliases and short options.
32
+ - `update` A function that takes either a single `--key=val`, an array
33
+ of `--key=val` values, or an object (as read from an rc file, for
34
+ example), and updates the result data accordingly. This will _not_
35
+ update any options set explicitly in the original argv.
27
36
 
28
37
  - `usage` String or Array
29
38
 
package/index.js CHANGED
@@ -162,6 +162,47 @@ const usage = j => {
162
162
  // }
163
163
  const jack = (...sections) => execute(parse_(buildParser(newObj(), sections)))
164
164
 
165
+ const kvToArg = j => (k, v) =>
166
+ j.options[k] && isFlag(j.options[k]) ? (v ? `--${k}` : `--no-${k}`)
167
+ : `--${k}=${v}`
168
+
169
+ const objToArgv = j => obj => Object.keys(obj).reduce((set, k) => {
170
+ const toArg = kvToArg(j)
171
+ const val = obj[k]
172
+ if (Array.isArray(val))
173
+ set.push(...val.map(v => toArg(k, v)))
174
+ else
175
+ set.push(toArg(k, val))
176
+ return set
177
+ }, [])
178
+
179
+ const update = j => args => {
180
+ const argv = []
181
+ const toArgv = objToArgv(j)
182
+ if (typeof args === 'string')
183
+ argv.push(args)
184
+ else if (Array.isArray(args))
185
+ argv.push(...args)
186
+ else if (args)
187
+ argv.push(...toArgv(args))
188
+ const result = reparse(j)(argv)
189
+ Object.keys(result)
190
+ .filter(k => k !== '_' && !j.explicit.has(k))
191
+ .forEach(k => j.result[k] = result[k])
192
+ }
193
+
194
+ const reparse = j => args => {
195
+ const argv = Array.isArray(args) ? args : [args]
196
+ return parse_({
197
+ ...j,
198
+ explicit: new Set(),
199
+ result: { _: [] },
200
+ help: [],
201
+ main: null,
202
+ argv,
203
+ }).result
204
+ }
205
+
165
206
  const newObj = () => ({
166
207
  help: [],
167
208
  shortOpts: {},
@@ -402,7 +443,7 @@ const addHelpText = (j, name, val) => {
402
443
  val.alias ? `Alias for ${[].concat(val.alias).join(' ')}`
403
444
  : '[no description provided]'
404
445
  ))
405
- const mult = isList(val) ? `${
446
+ const mult = isList(val) && !isEnv(val) ? `${
406
447
  desc.indexOf('\n') === -1 ? '\n' : '\n\n'
407
448
  }Can be set multiple times` : ''
408
449
  const text = `${desc}${mult}`
@@ -479,6 +520,7 @@ ${wrap(spec.valid.join(' '), [ 0, 2, 0, 8 ])}`)
479
520
 
480
521
  const parse_ = j => {
481
522
  const argv = getArgv(j)
523
+ const original = [...argv]
482
524
 
483
525
  for (let i = 0; i < argv.length; i++) {
484
526
  const arg = argv[i]
@@ -561,6 +603,8 @@ const parse_ = j => {
561
603
 
562
604
  if (isList(spec)) {
563
605
  if (isOpt(spec)) {
606
+ if (!Array.isArray(j.result[name]))
607
+ j.result[name] = []
564
608
  set(j, name, spec, j.result[name].concat(val))
565
609
  } else {
566
610
  const v = j.result[name] || 0
@@ -579,8 +623,11 @@ const parse_ = j => {
579
623
  Object.defineProperty(j.result._, 'usage', {
580
624
  value: () => console.log(usage(j))
581
625
  })
626
+ Object.defineProperty(j.result._, 'update', { value: update(j) })
627
+ Object.defineProperty(j.result._, 'reparse', { value: reparse(j) })
582
628
  Object.defineProperty(j.result._, 'explicit', { value: j.explicit })
583
629
  Object.defineProperty(j.result._, 'parsed', { value: argv })
630
+ Object.defineProperty(j.result._, 'original', { value: original })
584
631
 
585
632
  return j
586
633
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jackspeak",
3
- "version": "1.3.5",
3
+ "version": "1.4.0",
4
4
  "description": "A very strict and proper argument parser.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "license": "ISC",
15
15
  "devDependencies": {
16
- "tap": "^12.5.1"
16
+ "tap": "^14.0.0"
17
17
  },
18
18
  "files": [
19
19
  "index.js"