Skip to content

Commit 19386ee

Browse files
pvdlgbcoe
authored andcommitted
feat: add set-placeholder-key configuration (#123)
If `true`, add a placeholder for each known key for which the corresponding CLI argument is not set. The default value for the placeholder is `undefined`.
1 parent 8c9706f commit 19386ee

3 files changed

Lines changed: 102 additions & 2 deletions

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,27 @@ node example.js a -b -- x y
298298
{ _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
299299
```
300300
301+
### set placeholder key
302+
303+
* default: `false`.
304+
* key: `set-placeholder-key`.
305+
306+
Should a placeholder be added for keys not set via the corresponding CLI argument?
307+
308+
_If disabled:_
309+
310+
```sh
311+
node example.js -a 1 -c 2
312+
{ _: [], a: 1, c: 2 }
313+
```
314+
315+
_If enabled:_
316+
317+
```sh
318+
node example.js -a 1 -c 2
319+
{ _: [], a: 1, b: undefined, c: 2 }
320+
```
321+
301322
## Special Thanks
302323
303324
The yargs project evolves from optimist and minimist. It owes its

index.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ function parse (args, opts) {
2020
'duplicate-arguments-array': true,
2121
'flatten-duplicate-arrays': true,
2222
'populate--': false,
23-
'combine-arrays': false
23+
'combine-arrays': false,
24+
'set-placeholder-key': false
2425
}, opts.configuration)
2526
var defaults = opts.default || {}
2627
var configObjects = opts.configObjects || []
@@ -44,41 +45,50 @@ function parse (args, opts) {
4445
configs: {},
4546
defaulted: {},
4647
nargs: {},
47-
coercions: {}
48+
coercions: {},
49+
keys: []
4850
}
4951
var negative = /^-[0-9]+(\.[0-9]+)?/
5052
var negatedBoolean = new RegExp('^--' + configuration['negation-prefix'] + '(.+)')
5153

5254
;[].concat(opts.array).filter(Boolean).forEach(function (key) {
5355
flags.arrays[key] = true
56+
flags.keys.push(key)
5457
})
5558

5659
;[].concat(opts.boolean).filter(Boolean).forEach(function (key) {
5760
flags.bools[key] = true
61+
flags.keys.push(key)
5862
})
5963

6064
;[].concat(opts.string).filter(Boolean).forEach(function (key) {
6165
flags.strings[key] = true
66+
flags.keys.push(key)
6267
})
6368

6469
;[].concat(opts.number).filter(Boolean).forEach(function (key) {
6570
flags.numbers[key] = true
71+
flags.keys.push(key)
6672
})
6773

6874
;[].concat(opts.count).filter(Boolean).forEach(function (key) {
6975
flags.counts[key] = true
76+
flags.keys.push(key)
7077
})
7178

7279
;[].concat(opts.normalize).filter(Boolean).forEach(function (key) {
7380
flags.normalize[key] = true
81+
flags.keys.push(key)
7482
})
7583

7684
Object.keys(opts.narg || {}).forEach(function (k) {
7785
flags.nargs[k] = opts.narg[k]
86+
flags.keys.push(k)
7887
})
7988

8089
Object.keys(opts.coerce || {}).forEach(function (k) {
8190
flags.coercions[k] = opts.coerce[k]
91+
flags.keys.push(k)
8292
})
8393

8494
if (Array.isArray(opts.config) || typeof opts.config === 'string') {
@@ -289,6 +299,7 @@ function parse (args, opts) {
289299
setConfigObjects()
290300
applyDefaultsAndAliases(argv, flags.aliases, defaults)
291301
applyCoercions(argv)
302+
if (configuration['set-placeholder-key']) setPlaceholderKeys(argv)
292303

293304
// for any counts either not in args or without an explicit default, set to 0
294305
Object.keys(flags.counts).forEach(function (key) {
@@ -556,6 +567,15 @@ function parse (args, opts) {
556567
})
557568
}
558569

570+
function setPlaceholderKeys (argv) {
571+
flags.keys.forEach((key) => {
572+
// don't set placeholder keys for dot notation options 'foo.bar'.
573+
if (~key.indexOf('.')) return
574+
if (typeof argv[key] === 'undefined') argv[key] = undefined
575+
})
576+
return argv
577+
}
578+
559579
function applyDefaultsAndAliases (obj, aliases, defaults) {
560580
Object.keys(defaults).forEach(function (key) {
561581
if (!hasKey(obj, key.split('.'))) {

test/yargs-parser.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,65 @@ describe('yargs-parser', function () {
23242324
result.should.have.property('--').and.deep.equal(['--not-a-flag', '-', '-h', '-multi', '--', 'eek'])
23252325
})
23262326
})
2327+
2328+
describe('set-placeholder-key', function () {
2329+
it('should not set placeholder key by default', function () {
2330+
var parsed = parser([], {
2331+
string: ['a']
2332+
})
2333+
parsed.should.not.have.property('a')
2334+
})
2335+
2336+
it('should set placeholder key to "undefined"', function () {
2337+
var parsed = parser([], {
2338+
array: ['a'],
2339+
boolean: ['b'],
2340+
string: ['c'],
2341+
number: ['d'],
2342+
count: ['e'],
2343+
normalize: ['f'],
2344+
narg: {g: 2},
2345+
coerce: {
2346+
h: function (arg) {
2347+
return arg
2348+
}
2349+
},
2350+
configuration: {'set-placeholder-key': true}
2351+
})
2352+
parsed.should.have.property('a')
2353+
expect(parsed.a).to.be.equal(undefined)
2354+
parsed.should.have.property('b')
2355+
expect(parsed.b).to.be.equal(undefined)
2356+
parsed.should.have.property('c')
2357+
expect(parsed.c).to.be.equal(undefined)
2358+
parsed.should.have.property('d')
2359+
expect(parsed.d).to.be.equal(undefined)
2360+
parsed.should.have.property('e')
2361+
expect(parsed.f).to.be.equal(undefined)
2362+
parsed.should.have.property('g')
2363+
expect(parsed.g).to.be.equal(undefined)
2364+
parsed.should.have.property('h')
2365+
expect(parsed.h).to.be.equal(undefined)
2366+
})
2367+
2368+
it('should not set placeholder for key with a default value', function () {
2369+
var parsed = parser([], {
2370+
string: ['a'],
2371+
default: {a: 'hello'},
2372+
configuration: {'set-placeholder-key': true}
2373+
})
2374+
parsed.a.should.equal('hello')
2375+
})
2376+
2377+
it('should not set placeholder key with dot notation', function () {
2378+
var parsed = parser([], {
2379+
string: ['a.b']
2380+
})
2381+
parsed.should.not.have.property('a')
2382+
parsed.should.not.have.property('b')
2383+
parsed.should.not.have.property('a.b')
2384+
})
2385+
})
23272386
})
23282387

23292388
// addresses: https://github.com/yargs/yargs-parser/issues/41

0 commit comments

Comments
 (0)