yargs 3.31.0 → 3.32.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## Change Log
2
2
 
3
+ ### v3.32.0 (2016/1/14 10:13 +07:00)
4
+
5
+ - [#344](https://github.com/bcoe/yargs/pull/344) yargs now has a code of conduct and contributor guidelines (@bcoe)
6
+ - [#341](https://github.com/bcoe/yargs/issues/341) Fix edge-case with camel-case arguments (@davibe)
7
+ - [#331](https://github.com/bcoe/yargs/pull/331) Handle parsing a raw argument string (@kellyselden)
8
+ - [#325](https://github.com/bcoe/yargs/pull/325) Tweaks to make tests pass again on Windows (@isaacs)
9
+ - [#321](https://github.com/bcoe/yargs/pull/321) Custom config parsing function (@bcoe)
10
+
3
11
  ### v3.31.0 (2015/12/03 10:15 +07:00)
4
12
 
5
13
  - [#239](https://github.com/bcoe/yargs/pull/239) Pass argv to commands (@bcoe)
package/README.md CHANGED
@@ -549,15 +549,28 @@ var argv = require('yargs')
549
549
  .argv;
550
550
  ```
551
551
 
552
- <a name="config"></a>.config(key, [description])
552
+ <a name="config"></a>.config(key, [description], [parseFn])
553
553
  ------------
554
554
 
555
555
  Tells the parser that if the option specified by `key` is passed in, it
556
556
  should be interpreted as a path to a JSON config file. The file is loaded
557
- and parsed, and its properties are set as arguments. If present, the
558
- `description` parameter customizes the description of the config (`key`) option
557
+ and parsed, and its properties are set as arguments.
558
+
559
+ An optional `description` can be provided to customize the config (`key`) option
559
560
  in the usage string.
560
561
 
562
+ An optional `parseFn` can be used to provide a custom parser. The parsing
563
+ function must be synchronous, and should return an object containing
564
+ key value pairs or an error.
565
+
566
+ ```js
567
+ var argv = require('yargs')
568
+ .config('settings', function (configPath) {
569
+ return JSON.parse(fs.readFileSync(configPath, 'utf-8'))
570
+ })
571
+ .argv
572
+ ```
573
+
561
574
  <a name="count"></a>.count(key)
562
575
  ------------
563
576
 
@@ -912,6 +925,7 @@ Valid `opt` keys include:
912
925
  - `boolean`: boolean, interpret option as a boolean flag, see [`boolean()`](#boolean)
913
926
  - `choices`: value or array of values, limit valid option arguments to a predefined set, see [`choices()`](#choices)
914
927
  - `config`: boolean, interpret option as a path to a JSON config file, see [`config()`](#config)
928
+ - `configParser`: function, provide a custom config parsing function, see [`config()`](#config)
915
929
  - `count`: boolean, interpret option as a count of boolean flags, see [`count()`](#count)
916
930
  - `default`: value, set a default value for the option, see [`default()`](#default)
917
931
  - `defaultDescription`: string, use this description for the default value in help content, see [`default()`](#default)
@@ -932,6 +946,8 @@ Valid `opt` keys include:
932
946
 
933
947
  Parse `args` instead of `process.argv`. Returns the `argv` object.
934
948
 
949
+ `args` may either be a pre-processed argv array, or a raw argument string.
950
+
935
951
  .require(key, [msg | boolean])
936
952
  ------------------------------
937
953
  .required(key, [msg | boolean])
package/index.js CHANGED
@@ -2,6 +2,7 @@ var assert = require('assert')
2
2
  var Completion = require('./lib/completion')
3
3
  var Parser = require('./lib/parser')
4
4
  var path = require('path')
5
+ var tokenizeArgString = require('./lib/tokenize-arg-string')
5
6
  var Usage = require('./lib/usage')
6
7
  var Validation = require('./lib/validation')
7
8
  var Y18n = require('y18n')
@@ -58,7 +59,7 @@ function Argv (processArgs, cwd) {
58
59
  requiresArg: [],
59
60
  count: [],
60
61
  normalize: [],
61
- config: [],
62
+ config: {},
62
63
  envPrefix: undefined
63
64
  }
64
65
 
@@ -67,6 +68,7 @@ function Argv (processArgs, cwd) {
67
68
  completion = Completion(self, usage)
68
69
 
69
70
  demanded = {}
71
+ groups = {}
70
72
 
71
73
  exitProcess = true
72
74
  strict = false
@@ -116,9 +118,15 @@ function Argv (processArgs, cwd) {
116
118
  return self
117
119
  }
118
120
 
119
- self.config = function (key, msg) {
121
+ self.config = function (key, msg, parseFn) {
122
+ if (typeof msg === 'function') {
123
+ parseFn = msg
124
+ msg = null
125
+ }
120
126
  self.describe(key, msg || usage.deferY18nLookup('Path to JSON config file'))
121
- options.config.push.apply(options.config, [].concat(key))
127
+ ;(Array.isArray(key) ? key : [key]).forEach(function (k) {
128
+ options.config[k] = parseFn || true
129
+ })
122
130
  return self
123
131
  }
124
132
 
@@ -294,7 +302,7 @@ function Argv (processArgs, cwd) {
294
302
  if (demand) {
295
303
  self.demand(key, demand)
296
304
  } if ('config' in opt) {
297
- self.config(key)
305
+ self.config(key, opt.configParser)
298
306
  } if ('default' in opt) {
299
307
  self.default(key, opt.default)
300
308
  } if ('nargs' in opt) {
@@ -336,7 +344,11 @@ function Argv (processArgs, cwd) {
336
344
 
337
345
  var groups = {}
338
346
  self.group = function (opts, groupName) {
339
- groups[groupName] = (groups[groupName] || []).concat(opts)
347
+ var seen = {}
348
+ groups[groupName] = (groups[groupName] || []).concat(opts).filter(function (key) {
349
+ if (seen[key]) return false
350
+ return (seen[key] = true)
351
+ })
340
352
  return self
341
353
  }
342
354
  self.getGroups = function () {
@@ -495,6 +507,8 @@ function Argv (processArgs, cwd) {
495
507
  })
496
508
 
497
509
  function parseArgs (args) {
510
+ args = normalizeArgs(args)
511
+
498
512
  var parsed = Parser(args, options, y18n)
499
513
  var argv = parsed.argv
500
514
  var aliases = parsed.aliases
@@ -615,7 +629,14 @@ function Argv (processArgs, cwd) {
615
629
  })
616
630
  }
617
631
 
618
- sigletonify(self)
632
+ function normalizeArgs (args) {
633
+ if (typeof args === 'string') {
634
+ return tokenizeArgString(args)
635
+ }
636
+ return args
637
+ }
638
+
639
+ singletonify(self)
619
640
  return self
620
641
  }
621
642
 
@@ -633,7 +654,7 @@ function rebase (base, dir) {
633
654
  require('yargs').argv
634
655
  to get a parsed version of process.argv.
635
656
  */
636
- function sigletonify (inst) {
657
+ function singletonify (inst) {
637
658
  Object.keys(inst).forEach(function (key) {
638
659
  if (key === 'argv') {
639
660
  Argv.__defineGetter__(key, inst.__lookupGetter__(key))
package/lib/parser.js CHANGED
@@ -34,8 +34,8 @@ module.exports = function (args, opts, y18n) {
34
34
  flags.normalize[key] = true
35
35
  })
36
36
 
37
- ;[].concat(opts.config).filter(Boolean).forEach(function (key) {
38
- flags.configs[key] = true
37
+ Object.keys(opts.config).forEach(function (k) {
38
+ flags.configs[k] = opts.config[k]
39
39
  })
40
40
 
41
41
  var aliases = {}
@@ -43,6 +43,7 @@ module.exports = function (args, opts, y18n) {
43
43
 
44
44
  extendAliases(opts.key)
45
45
  extendAliases(opts.alias)
46
+ extendAliases(opts.default)
46
47
 
47
48
  var defaults = opts['default'] || {}
48
49
  Object.keys(defaults).forEach(function (key) {
@@ -341,7 +342,22 @@ module.exports = function (args, opts, y18n) {
341
342
  var configPath = argv[configKey] || configLookup[configKey]
342
343
  if (configPath) {
343
344
  try {
344
- var config = require(path.resolve(process.cwd(), configPath))
345
+ var config = null
346
+ var resolvedConfigPath = path.resolve(process.cwd(), configPath)
347
+
348
+ if (typeof flags.configs[configKey] === 'function') {
349
+ try {
350
+ config = flags.configs[configKey](resolvedConfigPath)
351
+ } catch (e) {
352
+ config = e
353
+ }
354
+ if (config instanceof Error) {
355
+ error = config
356
+ return
357
+ }
358
+ } else {
359
+ config = require(resolvedConfigPath)
360
+ }
345
361
 
346
362
  Object.keys(config).forEach(function (key) {
347
363
  // setting arguments via CLI takes precedence over
@@ -421,6 +437,11 @@ module.exports = function (args, opts, y18n) {
421
437
  // extend the aliases list with inferred aliases.
422
438
  function extendAliases (obj) {
423
439
  Object.keys(obj || {}).forEach(function (key) {
440
+ // short-circuit if we've already added a key
441
+ // to the aliases array, for example it might
442
+ // exist in both 'opts.default' and 'opts.key'.
443
+ if (aliases[key]) return
444
+
424
445
  aliases[key] = [].concat(opts.alias[key] || [])
425
446
  // For "--option-name", also set argv.optionName
426
447
  aliases[key].concat(key).forEach(function (x) {
@@ -0,0 +1,32 @@
1
+ // take an un-split argv string and tokenize it.
2
+ module.exports = function (argString) {
3
+ var i = 0
4
+ var c = null
5
+ var opening = null
6
+ var args = []
7
+
8
+ for (var ii = 0; ii < argString.length; ii++) {
9
+ c = argString.charAt(ii)
10
+
11
+ // split on spaces unless we're in quotes.
12
+ if (c === ' ' && !opening) {
13
+ i++
14
+ continue
15
+ }
16
+
17
+ // don't split the string if we're in matching
18
+ // opening or closing single and double quotes.
19
+ if (c === opening) {
20
+ opening = null
21
+ continue
22
+ } else if ((c === "'" || c === '"') && !opening) {
23
+ opening = c
24
+ continue
25
+ }
26
+
27
+ if (!args[i]) args[i] = ''
28
+ args[i] += c
29
+ }
30
+
31
+ return args
32
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yargs",
3
- "version": "3.31.0",
3
+ "version": "3.32.0",
4
4
  "description": "Light-weight option parsing with an argv hash. No optstrings attached.",
5
5
  "main": "./index.js",
6
6
  "files": [
@@ -26,13 +26,14 @@
26
26
  "es6-promise": "^3.0.2",
27
27
  "hashish": "0.0.4",
28
28
  "mocha": "^2.3.4",
29
- "nyc": "^4.0.1",
29
+ "nyc": "^5.2.0",
30
30
  "standard": "^5.4.1",
31
31
  "which": "^1.1.2",
32
32
  "win-spawn": "^2.0.0"
33
33
  },
34
34
  "scripts": {
35
- "test": "standard && nyc ./node_modules/.bin/_mocha --timeout=4000 --check-leaks",
35
+ "pretest": "standard",
36
+ "test": "nyc --cache mocha --timeout=4000 --check-leaks",
36
37
  "coverage": "nyc report --reporter=text-lcov | coveralls"
37
38
  },
38
39
  "repository": {
@@ -96,6 +97,6 @@
96
97
  ],
97
98
  "license": "MIT",
98
99
  "engine": {
99
- "node": ">=0.4"
100
+ "node": ">=0.10"
100
101
  }
101
102
  }