Skip to content

Commit a525234

Browse files
juergbaBenjamin E. Coe
authored andcommitted
feat: options that have had their default value used are now tracked (#211)
1 parent 84a401f commit a525234

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,14 @@ yargs engine.
9999
* `argv`: an object representing the parsed value of `args`
100100
* `key/value`: key value pairs for each argument and their aliases.
101101
* `_`: an array representing the positional arguments.
102+
* [optional] `--`: an array with arguments after the end-of-options flag `--`.
102103
* `error`: populated with an error object if an exception occurred during parsing.
103104
* `aliases`: the inferred list of aliases built by combining lists in `opts.alias`.
104-
* `newAliases`: any new aliases added via camel-case expansion.
105-
* `configuration`: the configuration loaded from the `yargs` stanza in package.json.
105+
* `newAliases`: any new aliases added via camel-case expansion:
106+
* `boolean`: `{ fooBar: true }`
107+
* `defaulted`: any new argument created by `opts.default`, no aliases included.
108+
* `boolean`: `{ foo: true }`
109+
* `configuration`: given by default settings and `opts.configuration`.
106110
107111
<a name="configuration"></a>
108112

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function parse (args, opts) {
3535
var notFlagsOption = configuration['populate--']
3636
var notFlagsArgv = notFlagsOption ? '--' : '_'
3737
var newAliases = {}
38+
var defaulted = {}
3839
// allow a i18n handler to be passed in, default to a fake one (util.format).
3940
var __ = opts.__ || util.format
4041
var error = null
@@ -317,7 +318,7 @@ function parse (args, opts) {
317318
applyEnvVars(argv, false)
318319
setConfig(argv)
319320
setConfigObjects()
320-
applyDefaultsAndAliases(argv, flags.aliases, defaults)
321+
applyDefaultsAndAliases(argv, flags.aliases, defaults, true)
321322
applyCoercions(argv)
322323
if (configuration['set-placeholder-key']) setPlaceholderKeys(argv)
323324

@@ -627,10 +628,11 @@ function parse (args, opts) {
627628
return argv
628629
}
629630

630-
function applyDefaultsAndAliases (obj, aliases, defaults) {
631+
function applyDefaultsAndAliases (obj, aliases, defaults, canLog = false) {
631632
Object.keys(defaults).forEach(function (key) {
632633
if (!hasKey(obj, key.split('.'))) {
633634
setKey(obj, key.split('.'), defaults[key])
635+
if (canLog) defaulted[key] = true
634636

635637
;(aliases[key] || []).forEach(function (x) {
636638
if (hasKey(obj, x.split('.'))) return
@@ -895,6 +897,7 @@ function parse (args, opts) {
895897
error: error,
896898
aliases: flags.aliases,
897899
newAliases: newAliases,
900+
defaulted: defaulted,
898901
configuration: configuration
899902
}
900903
}

test/yargs-parser.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,43 @@ describe('yargs-parser', function () {
12151215
parse.should.have.property('t', false).and.be.a('boolean')
12161216
parse.should.have.property('_').and.deep.equal(['moo'])
12171217
})
1218+
1219+
describe('track defaulted', function () {
1220+
it('should log defaulted options - not specified by user', function () {
1221+
var parsed = parser.detailed('', {
1222+
default: { foo: 'abc', 'bar.prop': 33, baz: 'x' },
1223+
configObjects: [{ baz: 'xyz' }]
1224+
})
1225+
parsed.argv.should.deep.equal({ '_': [], baz: 'xyz', foo: 'abc', bar: { prop: 33 } })
1226+
parsed.defaulted.should.deep.equal({ foo: true, 'bar.prop': true })
1227+
})
1228+
1229+
it('should not log defaulted options - specified without value', function () {
1230+
var parsed = parser.detailed('--foo --bar.prop', {
1231+
default: { foo: 'abc', 'bar.prop': 33 }
1232+
})
1233+
parsed.argv.should.deep.equal({ '_': [], foo: 'abc', bar: { prop: 33 } })
1234+
parsed.defaulted.should.deep.equal({})
1235+
})
1236+
1237+
it('should log defaulted options - no aliases included', function () {
1238+
var parsed = parser.detailed('', {
1239+
default: { kaa: 'abc' },
1240+
alias: { foo: 'kaa' }
1241+
})
1242+
parsed.argv.should.deep.equal({ '_': [], kaa: 'abc', foo: 'abc' })
1243+
parsed.defaulted.should.deep.equal({ kaa: true })
1244+
})
1245+
1246+
it('setting an alias excludes associated key from defaulted', function () {
1247+
var parsed = parser.detailed('--foo abc', {
1248+
default: { kaa: 'abc' },
1249+
alias: { foo: 'kaa' }
1250+
})
1251+
parsed.argv.should.deep.equal({ '_': [], kaa: 'abc', foo: 'abc' })
1252+
parsed.defaulted.should.deep.equal({})
1253+
})
1254+
})
12181255
})
12191256

12201257
describe('camelCase', function () {

0 commit comments

Comments
 (0)