Skip to content

Commit 2045543

Browse files
committed
single extractor: returning undefined from output() omits value from result object
1 parent 47201c9 commit 2045543

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class CommandLineArgs {
4545
} else {
4646
extraction = fromTo(this.argv, { from: def.from, to, remove: true })
4747
}
48+
result[def.name] = await def.output(extraction)
49+
4850
} else if (def.extractor === 'single') {
4951
if (def.repeatable) {
5052
extraction = []
@@ -60,10 +62,13 @@ class CommandLineArgs {
6062
} else {
6163
extraction = single(this.argv, def.single, { remove: true })
6264
}
65+
const output = await def.output(extraction)
66+
if (output !== undefined) {
67+
result[def.name] = output
68+
}
6369
} else {
6470
throw new Error('Extractor not found: ' + def.extractor)
6571
}
66-
result[def.name] = await def.output(extraction)
6772
}
6873

6974
/* Do the positionals backwards, so removing them doesn't mess up the position config */

test/test.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ test.set('--option <no value>', async function () {
4242
a.deepEqual(cla.argv, ['one', 'two', '--two', 'three'])
4343
})
4444

45-
test.set('single --option flag', async function () {
45+
test.set('single --option flag, value set', async function () {
4646
const argv = ['one', 'two', '--one', '--two']
4747
const optionDefinitions = [
4848
{
4949
name: 'one',
5050
extractor: 'single',
5151
single: '--one',
52-
output: extraction => true
52+
output: extraction => extraction.length === 1 ? true : undefined
5353
}
5454
]
5555
const cla = new CommandLineArgs(argv)
@@ -58,6 +58,38 @@ test.set('single --option flag', async function () {
5858
a.deepEqual(cla.argv, ['one', 'two', '--two'])
5959
})
6060

61+
test.set('single --option flag, value set not, output returns false', async function () {
62+
const argv = ['one', 'two', '--two']
63+
const optionDefinitions = [
64+
{
65+
name: 'one',
66+
extractor: 'single',
67+
single: '--one',
68+
output: extraction => extraction.length === 1
69+
}
70+
]
71+
const cla = new CommandLineArgs(argv)
72+
const result = await cla.parse(optionDefinitions)
73+
a.deepEqual(result, { one: false })
74+
a.deepEqual(cla.argv, ['one', 'two', '--two'])
75+
})
76+
77+
test.set('single --option flag, not set. Returning undefined omits option from result.', async function () {
78+
const argv = ['one', 'two', '--two']
79+
const optionDefinitions = [
80+
{
81+
name: 'one',
82+
extractor: 'single',
83+
single: '--one',
84+
output: extraction => extraction.length === 1 ? true : undefined
85+
}
86+
]
87+
const cla = new CommandLineArgs(argv)
88+
const result = await cla.parse(optionDefinitions)
89+
a.deepEqual(result, {})
90+
a.deepEqual(cla.argv, ['one', 'two', '--two'])
91+
})
92+
6193
test.set('single regex', async function () {
6294
const argv = ['one', 'two', '--one', '--two']
6395
const optionDefinitions = [

0 commit comments

Comments
 (0)