Skip to content

Commit fa76e99

Browse files
committed
Added repeatable
1 parent 30a2366 commit fa76e99

4 files changed

Lines changed: 57 additions & 3 deletions

File tree

dist/index.cjs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,23 @@ class CommandLineArgs {
178178
let extraction;
179179
if (def.extractor === 'fromTo') {
180180
const to = def.toPreset ? toPresets[def.toPreset] : def.to;
181-
extraction = fromTo(this.argv, { from: def.from, to, remove: true });
181+
if (def.repeatable) {
182+
extraction = [];
183+
let keepGoing = true;
184+
while (keepGoing) {
185+
const argLength = this.argv.length;
186+
const extractionStep = fromTo(this.argv, { from: def.from, to, remove: true });
187+
if (extractionStep.length) {
188+
extraction.push(extractionStep);
189+
}
190+
keepGoing = argLength !== this.argv.length;
191+
}
192+
console.log(extraction);
193+
// extraction = extraction.flat()
194+
// extraction = [extraction[0], ...extraction.slice(1).filter(e => e !== extraction[0])]
195+
} else {
196+
extraction = fromTo(this.argv, { from: def.from, to, remove: true });
197+
}
182198
} else if (def.extractor === 'single') {
183199
extraction = single(this.argv, def.single, { remove: true });
184200
} else {

index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,23 @@ class CommandLineArgs {
3131
let extraction
3232
if (def.extractor === 'fromTo') {
3333
const to = def.toPreset ? toPresets[def.toPreset] : def.to
34-
extraction = fromTo(this.argv, { from: def.from, to, remove: true })
34+
if (def.repeatable) {
35+
extraction = []
36+
let keepGoing = true
37+
while (keepGoing) {
38+
const argLength = this.argv.length
39+
const extractionStep = fromTo(this.argv, { from: def.from, to, remove: true })
40+
if (extractionStep.length) {
41+
extraction.push(extractionStep)
42+
}
43+
keepGoing = argLength !== this.argv.length
44+
}
45+
// console.log(extraction)
46+
// extraction = extraction.flat()
47+
// extraction = [extraction[0], ...extraction.slice(1).filter(e => e !== extraction[0])]
48+
} else {
49+
extraction = fromTo(this.argv, { from: def.from, to, remove: true })
50+
}
3551
} else if (def.extractor === 'single') {
3652
extraction = single(this.argv, def.single, { remove: true })
3753
} else {

lib/option-definition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
property name on the result
1212
¬
1313
extractor
14-
from-to, single or positional.
14+
Choose from `from-to`, `single` or `positional`.
1515
¬
1616
from
1717
used by extractor 'from-to'

test/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { strict as a } from 'assert'
22
import CommandLineArgs from 'command-line-args'
3+
import util from 'node:util'
4+
util.inspect.defaultOptions.depth = 6
5+
util.inspect.defaultOptions.breakLength = process?.stdout?.columns || 80
6+
util.inspect.defaultOptions.maxArrayLength = Infinity
37

48
const [test, only, skip] = [new Map(), new Map(), new Map()]
59

@@ -412,4 +416,22 @@ test.set('Multiple parses to consume all args', async function () {
412416
a.deepEqual(result2, { extraction: [ 'server', '--verbose'] })
413417
})
414418

419+
test.set('Repeating options', async function () {
420+
const argv = ['--var', 'one', 'something', '--var', 'two', 'another']
421+
const cla = new CommandLineArgs(argv)
422+
const result = await cla.parse([
423+
{
424+
name: 'var',
425+
extractor: 'fromTo',
426+
from: '--var',
427+
toPreset: 'multipleOptionValue',
428+
repeatable: true,
429+
output: e => Object.fromEntries(e.map(args => args.slice(1)))
430+
}
431+
])
432+
433+
// this.data = { result }
434+
a.deepEqual(result, { var: { one: 'something', two: 'another' } })
435+
})
436+
415437
export { test, only, skip }

0 commit comments

Comments
 (0)