argv: Add support for boolean flags when equal sign is used

This commit is contained in:
Jonatan Nilsson 2021-10-14 09:52:56 +00:00
parent 129e50367d
commit 2ee285e1c4
2 changed files with 21 additions and 0 deletions

View file

@ -52,6 +52,8 @@ Argv.prototype.load = function () {
let equalSignIndex = key.indexOf('=') let equalSignIndex = key.indexOf('=')
if (equalSignIndex > 0) { if (equalSignIndex > 0) {
this.set(key.slice(0, equalSignIndex), key.slice(equalSignIndex + 1)) this.set(key.slice(0, equalSignIndex), key.slice(equalSignIndex + 1))
} else {
this.set(key, true)
} }
} else if (args[i + 1] && !args[i + 1].startsWith(this.prefix)) { } else if (args[i + 1] && !args[i + 1].startsWith(this.prefix)) {
this.set(key, args[i + 1]) this.set(key, args[i + 1])

View file

@ -140,6 +140,25 @@ t.describe('#load()', () => {
testety: 'hello', testety: 'hello',
}) })
}) })
t.test('equal sign should support boolean values', function() {
process.argv = ['bla', 'bla',
'--booleanone',
'--foobar=123',
'--testety=hello',
'--booleantwo',
]
let store = new Nconf.Argv({ useEqualsign: true })
store.load()
assert.strictEqual(store.get('booleanone'), true)
assert.strictEqual(store.get('booleantwo'), true)
assert.deepStrictEqual(store.get(), {
foobar: '123',
testety: 'hello',
booleanone: true,
booleantwo: true,
})
})
t.test('should be smart with the usage of equal sign', function() { t.test('should be smart with the usage of equal sign', function() {
process.argv = ['bla', 'bla', process.argv = ['bla', 'bla',