File tree Expand file tree Collapse file tree 2 files changed +88
-1
lines changed Expand file tree Collapse file tree 2 files changed +88
-1
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ export function parseOption(
36
36
) : TreeNode | false {
37
37
const o = token . option ( ) ;
38
38
const [ key , rawV ] = o . split ( '=' ) ;
39
+
39
40
if ( context . options . has ( key ) ) {
40
41
const option = context . options . get ( key ) ! ;
41
42
const name = camelCase ( option . name ) ;
@@ -45,7 +46,17 @@ export function parseOption(
45
46
return option . parse ( cursor , token , context ) ;
46
47
} else if ( option . type === 'boolean' ) {
47
48
// Parse boolean option
48
- context . result . options [ name ] = ! key . startsWith ( 'no-' ) ? true : false ;
49
+ const negative = key . startsWith ( 'no-' ) ;
50
+ if (
51
+ rawV === undefined ||
52
+ [ 'true' , 'yes' , 't' , 'y' ] . includes ( rawV . toLowerCase ( ) )
53
+ ) {
54
+ context . result . options [ name ] = ! negative ? true : false ;
55
+ } else if ( [ 'false' , 'no' , 'f' , 'n' ] . includes ( rawV . toLowerCase ( ) ) ) {
56
+ context . result . options [ name ] = ! negative ? false : true ;
57
+ } else {
58
+ throw new ParseError ( `Unexpected value ${ rawV } for ${ option . format } ` ) ;
59
+ }
49
60
} else if ( option . type === 'string' ) {
50
61
// Parse string option
51
62
if ( rawV !== undefined ) {
Original file line number Diff line number Diff line change @@ -491,6 +491,82 @@ describe('Option Parser', () => {
491
491
` ) ;
492
492
} ) ;
493
493
494
+ it ( 'should parse boolean option with value' , async ( ) => {
495
+ const cli = breadc ( 'cli' ) ;
496
+ cli . option ( '--open' ) ;
497
+ cli . command ( '' ) . action ( ( o ) => o ) ;
498
+
499
+ expect ( await cli . run ( [ '--open=true' ] ) ) . toMatchInlineSnapshot ( `
500
+ {
501
+ "--": [],
502
+ "open": true,
503
+ }
504
+ ` ) ;
505
+
506
+ expect ( await cli . run ( [ '--open=YES' ] ) ) . toMatchInlineSnapshot ( `
507
+ {
508
+ "--": [],
509
+ "open": true,
510
+ }
511
+ ` ) ;
512
+
513
+ expect ( await cli . run ( [ '--open=T' ] ) ) . toMatchInlineSnapshot ( `
514
+ {
515
+ "--": [],
516
+ "open": true,
517
+ }
518
+ ` ) ;
519
+
520
+ expect ( await cli . run ( [ '--open=y' ] ) ) . toMatchInlineSnapshot ( `
521
+ {
522
+ "--": [],
523
+ "open": true,
524
+ }
525
+ ` ) ;
526
+
527
+ expect ( await cli . run ( [ '--open=false' ] ) ) . toMatchInlineSnapshot ( `
528
+ {
529
+ "--": [],
530
+ "open": false,
531
+ }
532
+ ` ) ;
533
+
534
+ expect ( await cli . run ( [ '--open=No' ] ) ) . toMatchInlineSnapshot ( `
535
+ {
536
+ "--": [],
537
+ "open": false,
538
+ }
539
+ ` ) ;
540
+
541
+ expect ( await cli . run ( [ '--open=f' ] ) ) . toMatchInlineSnapshot ( `
542
+ {
543
+ "--": [],
544
+ "open": false,
545
+ }
546
+ ` ) ;
547
+
548
+ expect ( await cli . run ( [ '--open=N' ] ) ) . toMatchInlineSnapshot ( `
549
+ {
550
+ "--": [],
551
+ "open": false,
552
+ }
553
+ ` ) ;
554
+
555
+ expect ( await cli . run ( [ '--no-open=true' ] ) ) . toMatchInlineSnapshot ( `
556
+ {
557
+ "--": [],
558
+ "open": false,
559
+ }
560
+ ` ) ;
561
+
562
+ expect ( await cli . run ( [ '--no-open=false' ] ) ) . toMatchInlineSnapshot ( `
563
+ {
564
+ "--": [],
565
+ "open": true,
566
+ }
567
+ ` ) ;
568
+ } ) ;
569
+
494
570
it ( 'should parse string option' , async ( ) => {
495
571
const cli = breadc ( 'cli' ) ;
496
572
cli . option ( '--flag' ) ;
You can’t perform that action at this time.
0 commit comments