File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -64,6 +64,15 @@ export class ArgumentParser {
6464 }
6565 }
6666
67+ for ( const option of this . command . options ) {
68+ if (
69+ matches [ option . key ] === undefined &&
70+ option . default !== undefined
71+ ) {
72+ matches [ option . key ] = option . default ;
73+ }
74+ }
75+
6776 if ( this . positionalIndex < requiredPositionals . length ) {
6877 throw new ArgumentError (
6978 `Missing positional arguments: ${ requiredPositionals
@@ -185,7 +194,8 @@ export class ArgumentParser {
185194 const choices = option . choices . join ( ', ' ) ;
186195
187196 throw new ArgumentError (
188- `Invalid choice for argument: ${ option . key } (${ value } ). Allowed choices are [${ choices } ]`
197+ `Invalid choice for argument: ${ option . key } (${ value } ). ` +
198+ `Allowed choices are [${ choices } ]`
189199 ) ;
190200 }
191201 }
Original file line number Diff line number Diff line change @@ -105,6 +105,40 @@ describe('ArgumentParser', () => {
105105 } ) ;
106106 } ) ;
107107
108+ it ( 'populates default values for positionals' , ( ) => {
109+ command . positional = [
110+ {
111+ key : 'input' ,
112+ type : OptionType . string ,
113+ default : 'defaultInput.txt' ,
114+ } ,
115+ ] ;
116+ command . options = [ ] ;
117+ command . init ( ) ;
118+
119+ const result = parser . parse ( [ ] ) ;
120+ expect ( result ) . toEqual ( {
121+ input : 'defaultInput.txt' ,
122+ } ) ;
123+ } ) ;
124+
125+ it ( 'populates default values for optionals' , ( ) => {
126+ command . positional = [ ] ;
127+ command . options = [
128+ {
129+ key : 'defaultOption' ,
130+ type : OptionType . string ,
131+ default : 'defaultValue' ,
132+ } ,
133+ ] ;
134+ command . init ( ) ;
135+
136+ const result = parser . parse ( [ ] ) ;
137+ expect ( result ) . toEqual ( {
138+ defaultOption : 'defaultValue' ,
139+ } ) ;
140+ } ) ;
141+
108142 it ( 'throws an error for missing value for string option' , ( ) => {
109143 const args = [ '--output' ] ;
110144 expect ( ( ) => parser . parse ( args ) ) . toThrowError (
You can’t perform that action at this time.
0 commit comments