@@ -283,6 +283,57 @@ describe('parser', () => {
283283 } ) ;
284284 } ) ;
285285
286+ it ( 'should throw error in strict mode when unmatched arguments are present' , ( ) => {
287+ expect ( ( ) =>
288+ parser ( { strict : true } )
289+ . option ( 'foo' , { type : 'string' } )
290+ . parse ( [ '--foo' , 'hello' , 'world' ] )
291+ ) . toThrowAggregateErrorContaining ( 'Unknown argument: world' ) ;
292+ } ) ;
293+
294+ it ( 'should throw error in strict mode for unmatched flags' , ( ) => {
295+ expect ( ( ) =>
296+ parser ( { strict : true } )
297+ . option ( 'foo' , { type : 'string' } )
298+ . parse ( [ '--foo' , 'hello' , '--bar' , '42' ] )
299+ ) . toThrowAggregateErrorContaining ( 'Unknown argument: --bar' , 'Unknown argument: 42' ) ;
300+ } ) ;
301+
302+ it ( 'should not throw error in strict mode when all arguments are matched' , ( ) => {
303+ expect (
304+ parser ( { strict : true } )
305+ . option ( 'foo' , { type : 'string' } )
306+ . option ( 'bar' , { type : 'number' } )
307+ . parse ( [ '--foo' , 'hello' , '--bar' , '42' ] )
308+ ) . toEqual ( { foo : 'hello' , bar : 42 , unmatched : [ ] } ) ;
309+ } ) ;
310+
311+ it ( 'should allow strict mode to be disabled (default behavior)' , ( ) => {
312+ expect (
313+ parser ( { strict : false } )
314+ . option ( 'foo' , { type : 'string' } )
315+ . parse ( [ '--foo' , 'hello' , 'world' ] )
316+ ) . toEqual ( { foo : 'hello' , unmatched : [ 'world' ] } ) ;
317+ } ) ;
318+
319+ it ( 'should allow enabling strict mode via .strict() method' , ( ) => {
320+ expect ( ( ) =>
321+ parser ( )
322+ . option ( 'foo' , { type : 'string' } )
323+ . strict ( )
324+ . parse ( [ '--foo' , 'hello' , 'world' ] )
325+ ) . toThrowAggregateErrorContaining ( 'Unknown argument: world' ) ;
326+ } ) ;
327+
328+ it ( 'should allow disabling strict mode via .strict(false) method' , ( ) => {
329+ expect (
330+ parser ( { strict : true } )
331+ . option ( 'foo' , { type : 'string' } )
332+ . strict ( false )
333+ . parse ( [ '--foo' , 'hello' , 'world' ] )
334+ ) . toEqual ( { foo : 'hello' , unmatched : [ 'world' ] } ) ;
335+ } ) ;
336+
286337 it ( 'should have correct types with coerce' , ( ) => {
287338 const parsed = parser ( )
288339 . option ( 'foo' , { type : 'string' , coerce : ( s ) => Number ( s ) } )
0 commit comments