@@ -30,6 +30,12 @@ const basicOptions = [
3030 type : Boolean ,
3131 description : 'boolean flag' ,
3232 } ,
33+ {
34+ name : 'num-flag' ,
35+ usage : '--num-flag <value>' ,
36+ type : Number ,
37+ description : 'number flag' ,
38+ } ,
3339 {
3440 name : 'string-flag' ,
3541 usage : '--string-flag <value>' ,
@@ -98,16 +104,37 @@ describe('arg-parser', () => {
98104 } ) ;
99105
100106 it ( 'parses basic flags' , ( ) => {
101- const res = argParser ( basicOptions , [ '--bool-flag' , '--string-flag' , 'val' ] , true ) ;
107+ const res = argParser ( basicOptions , [ '--bool-flag' , '--string-flag' , 'val' , '--num-flag' , '100' ] , true ) ;
102108 expect ( res . unknownArgs . length ) . toEqual ( 0 ) ;
103109 expect ( res . opts ) . toEqual ( {
104110 boolFlag : true ,
111+ numFlag : 100 ,
105112 stringFlag : 'val' ,
106113 stringFlagWithDefault : 'default-value' ,
107114 } ) ;
108115 expect ( warnMock . mock . calls . length ) . toEqual ( 0 ) ;
109116 } ) ;
110117
118+ it ( 'parses number flags' , ( ) => {
119+ const res = argParser ( basicOptions , [ '--num-flag' , '100' ] , true ) ;
120+ expect ( res . unknownArgs . length ) . toEqual ( 0 ) ;
121+ expect ( res . opts ) . toEqual ( {
122+ numFlag : 100 ,
123+ stringFlagWithDefault : 'default-value' ,
124+ } ) ;
125+ expect ( warnMock . mock . calls . length ) . toEqual ( 0 ) ;
126+ } ) ;
127+
128+ it ( 'parses number flags with = sign' , ( ) => {
129+ const res = argParser ( basicOptions , [ '--num-flag=10' ] , true ) ;
130+ expect ( res . unknownArgs . length ) . toEqual ( 0 ) ;
131+ expect ( res . opts ) . toEqual ( {
132+ numFlag : 10 ,
133+ stringFlagWithDefault : 'default-value' ,
134+ } ) ;
135+ expect ( warnMock . mock . calls . length ) . toEqual ( 0 ) ;
136+ } ) ;
137+
111138 it ( 'should not parse negated boolean flags which are not specified' , ( ) => {
112139 const res = argParser ( basicOptions , [ '--no-bool-flag' ] , true ) ;
113140 expect ( res . unknownArgs . includes ( '--no-bool-flag' ) ) . toBeTruthy ( ) ;
0 commit comments