jackspeak 1.3.8 → 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 +8 -0
  2. package/index.js +43 -0
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -25,6 +25,14 @@ as argument names as well; just put them in different sections.
25
25
  explicitly set.
26
26
  - `original` The `argv` array before any expansions.
27
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.
28
36
 
29
37
  - `usage` String or Array
30
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: {},
@@ -582,6 +623,8 @@ const parse_ = j => {
582
623
  Object.defineProperty(j.result._, 'usage', {
583
624
  value: () => console.log(usage(j))
584
625
  })
626
+ Object.defineProperty(j.result._, 'update', { value: update(j) })
627
+ Object.defineProperty(j.result._, 'reparse', { value: reparse(j) })
585
628
  Object.defineProperty(j.result._, 'explicit', { value: j.explicit })
586
629
  Object.defineProperty(j.result._, 'parsed', { value: argv })
587
630
  Object.defineProperty(j.result._, 'original', { value: original })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jackspeak",
3
- "version": "1.3.8",
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": "^13.0.0-rc.25"
16
+ "tap": "^14.0.0"
17
17
  },
18
18
  "files": [
19
19
  "index.js"