@@ -10,8 +10,8 @@ test.set('Input argv is not mutated', async function () {
1010 name : 'one' ,
1111 extractor : 'fromTo' ,
1212 from : '--one' ,
13- to : 'singleOptionValue' , // TODO: Rename as "toPreset" to be more explicit we're using a preset.
14- output : extraction => extraction [ 1 ] // TODO: Define a FromToExtractor derived class which already includes this specific output value by default.
13+ toPreset : 'singleOptionValue' ,
14+ output : extraction => extraction [ 1 ]
1515 }
1616 ]
1717 const cla = new CommandLineArgs ( argv )
@@ -28,7 +28,7 @@ test.set('--option <no value>', async function () {
2828 name : 'one' ,
2929 extractor : 'fromTo' ,
3030 from : '--one' ,
31- to : 'singleOptionValue' ,
31+ toPreset : 'singleOptionValue' ,
3232 output : extraction => extraction [ 1 ]
3333 }
3434 ]
@@ -38,7 +38,7 @@ test.set('--option <no value>', async function () {
3838 a . deepEqual ( cla . argv , [ 'one' , 'two' , '--two' , 'three' ] )
3939} )
4040
41- test . set ( '--option flag' , async function ( ) {
41+ test . set ( 'single --option flag' , async function ( ) {
4242 const argv = [ 'one' , 'two' , '--one' , '--two' ]
4343 const optionDefinitions = [
4444 {
@@ -54,6 +54,28 @@ test.set('--option flag', async function () {
5454 a . deepEqual ( cla . argv , [ 'one' , 'two' , '--two' ] )
5555} )
5656
57+ test . set ( 'single regex' , async function ( ) {
58+ const argv = [ 'one' , 'two' , '--one' , '--two' ]
59+ const optionDefinitions = [
60+ {
61+ name : 'one' ,
62+ extractor : 'single' ,
63+ single : / - - o [ n ] + e / ,
64+ output : extraction => true
65+ }
66+ ]
67+ const cla = new CommandLineArgs ( argv )
68+ const result = cla . parse ( optionDefinitions )
69+ a . deepEqual ( result , { one : true } )
70+ a . deepEqual ( cla . argv , [ 'one' , 'two' , '--two' ] )
71+
72+ const argv2 = [ 'one' , 'two' , '--onnnnne' , '--two' ]
73+ const cla2 = new CommandLineArgs ( argv2 )
74+ const result2 = cla2 . parse ( optionDefinitions )
75+ a . deepEqual ( result , { one : true } )
76+ a . deepEqual ( cla . argv , [ 'one' , 'two' , '--two' ] )
77+ } )
78+
5779test . set ( '--option flag not present' , async function ( ) {
5880 const argv = [ 'one' , 'two' , '--not-one' , '--two' ]
5981 const optionDefinitions = [
@@ -90,7 +112,7 @@ test.set('Positionals must be args 1 and 2, with options following', async funct
90112 name : 'option' ,
91113 extractor : 'fromTo' ,
92114 from : [ '--option' , '-o' ] ,
93- to : 'singleOptionValue' ,
115+ toPreset : 'singleOptionValue' ,
94116 output : extraction => extraction [ 1 ]
95117 } ,
96118 {
@@ -157,7 +179,7 @@ test.set('Positional, one single, one from-to', async function () {
157179 name : 'exchange' ,
158180 extractor : 'fromTo' ,
159181 from : '--exchange' ,
160- to : 'singleOptionValue' ,
182+ toPreset : 'singleOptionValue' ,
161183 output : extraction => extraction [ 1 ]
162184 } ,
163185 ] )
@@ -216,7 +238,7 @@ test.set('Magic arg values: file1 file2 extra-large verbose output final', async
216238 name : 'output' ,
217239 extractor : 'fromTo' ,
218240 from : 'output' ,
219- to : 'singleOptionValue' ,
241+ toPreset : 'singleOptionValue' ,
220242 output : extraction => extraction [ 1 ]
221243 }
222244 // {
@@ -265,29 +287,29 @@ test.set('self-defining options', async function () {
265287 '3000'
266288 ]
267289
268- /* Instead of "dynamic definition" magic, just run the parser over argv multiple times, one for each desire result object */
290+ /* Instead of "dynamic definition" magic, just run the parser over argv multiple times, one for each desired result object */
269291 /* Parse the args first for `--configName.optionName.optionValueType` format args, creating option definitions */
270292
271293 const optionDefinitions = [
272294 {
273295 name : 'one' ,
274296 extractor : 'fromTo' ,
275297 from : / ^ - - s e r v e r \. s t r i n g \[ \] / ,
276- to : 'multipleOptionValue' ,
298+ toPreset : 'multipleOptionValue' ,
277299 output : e => e . slice ( 1 )
278300 } ,
279301 {
280302 name : 'two' ,
281303 extractor : 'fromTo' ,
282304 from : / ^ - - s e r v e r \. n u m b e r \. / ,
283- to : 'singleOptionValue' ,
305+ toPreset : 'singleOptionValue' ,
284306 output : e => e [ 1 ]
285307 } ,
286308 {
287309 name : 'broke' ,
288310 extractor : 'fromTo' ,
289311 from : / ^ - - s e r v e r \. n u m b e r \. / ,
290- to : 'singleOptionValue' ,
312+ toPreset : 'singleOptionValue' ,
291313 output : e => Number ( e [ 1 ] )
292314 } ,
293315 {
@@ -300,7 +322,7 @@ test.set('self-defining options', async function () {
300322 name : 'four' ,
301323 extractor : 'fromTo' ,
302324 from : / ^ - - s e r v e r \. s t r i n g \. / ,
303- to : 'singleOptionValue' ,
325+ toPreset : 'singleOptionValue' ,
304326 output : e => e [ 1 ]
305327 }
306328 ]
@@ -365,4 +387,29 @@ skip.set('to not found: no args matched', async function () {
365387 } )
366388} )
367389
390+ test . set ( 'Multiple parses to consume all args' , async function ( ) {
391+ const argv = [ 'server' , '--option' , 'value' , 'clive' , 'server' , '--verbose' ]
392+ const commandLineArgs = new CommandLineArgs ( argv )
393+ const result1 = commandLineArgs . parse ( [
394+ {
395+ name : 'extraction' ,
396+ extractor : 'fromTo' ,
397+ from : 'server' ,
398+ to : 'server'
399+ }
400+ ] )
401+ const result2 = commandLineArgs . parse ( [
402+ {
403+ name : 'extraction' ,
404+ extractor : 'fromTo' ,
405+ from : 'server' ,
406+ to : 'server'
407+ }
408+ ] )
409+ // this.data = { result1, result2 }
410+
411+ a . deepEqual ( result1 , { extraction : [ 'server' , '--option' , 'value' , 'clive' ] } )
412+ a . deepEqual ( result2 , { extraction : [ 'server' , '--verbose' ] } )
413+ } )
414+
368415export { test , only , skip }
0 commit comments