@@ -2172,6 +2172,53 @@ class TestNegativeNumber(ParserTestCase):
21722172        ('--float -.5_000' , NS (int = None , float = - 0.5 )),
21732173    ]
21742174
2175+ class  TestArgumentAndSubparserSuggestions (TestCase ):
2176+     """Test error handling and suggestion when a user makes a typo""" 
2177+ 
2178+     def  test_wrong_argument_error_with_suggestions (self ):
2179+         parser  =  ErrorRaisingArgumentParser (suggest_on_error = True )
2180+         parser .add_argument ('foo' , choices = ['bar' , 'baz' ])
2181+         with  self .assertRaises (ArgumentParserError ) as  excinfo :
2182+             parser .parse_args (('bazz' ,))
2183+         self .assertIn (
2184+             "maybe you meant 'baz'? (choose from 'bar', 'baz')" ,
2185+             excinfo .exception .stderr ,
2186+         )
2187+ 
2188+     def  test_wrong_argument_error_no_suggestions (self ):
2189+         parser  =  ErrorRaisingArgumentParser (suggest_on_error = False )
2190+         parser .add_argument ('foo' , choices = ['bar' , 'baz' ])
2191+         with  self .assertRaises (ArgumentParserError ) as  excinfo :
2192+             parser .parse_args (('bazz' ,))
2193+         self .assertIn (
2194+             "invalid choice: 'bazz' (choose from 'bar', 'baz')" ,
2195+             excinfo .exception .stderr ,
2196+         )
2197+ 
2198+     def  test_wrong_argument_subparsers_with_suggestions (self ):
2199+         parser  =  ErrorRaisingArgumentParser (suggest_on_error = True )
2200+         subparsers  =  parser .add_subparsers (required = True )
2201+         subparsers .add_parser ('foo' )
2202+         subparsers .add_parser ('bar' )
2203+         with  self .assertRaises (ArgumentParserError ) as  excinfo :
2204+             parser .parse_args (('baz' ,))
2205+         self .assertIn (
2206+             "maybe you meant 'bar'? (choose from 'foo', 'bar')" ,
2207+             excinfo .exception .stderr ,
2208+         )
2209+ 
2210+     def  test_wrong_argument_subparsers_no_suggestions (self ):
2211+         parser  =  ErrorRaisingArgumentParser (suggest_on_error = False )
2212+         subparsers  =  parser .add_subparsers (required = True )
2213+         subparsers .add_parser ('foo' )
2214+         subparsers .add_parser ('bar' )
2215+         with  self .assertRaises (ArgumentParserError ) as  excinfo :
2216+             parser .parse_args (('baz' ,))
2217+         self .assertIn (
2218+             "invalid choice: 'baz' (choose from 'foo', 'bar')" ,
2219+             excinfo .exception .stderr ,
2220+         )
2221+ 
21752222class  TestInvalidAction (TestCase ):
21762223    """Test invalid user defined Action""" 
21772224
@@ -2390,18 +2437,6 @@ def test_required_subparsers_no_destination_error(self):
23902437            'error: the following arguments are required: {foo,bar}\n $' 
23912438        )
23922439
2393-     def  test_wrong_argument_subparsers_no_destination_error (self ):
2394-         parser  =  ErrorRaisingArgumentParser ()
2395-         subparsers  =  parser .add_subparsers (required = True )
2396-         subparsers .add_parser ('foo' )
2397-         subparsers .add_parser ('bar' )
2398-         with  self .assertRaises (ArgumentParserError ) as  excinfo :
2399-             parser .parse_args (('baz' ,))
2400-         self .assertIn (
2401-             "error: argument {foo,bar}: invalid choice: 'baz', maybe you meant 'bar'? (choose from 'foo', 'bar')" ,
2402-             excinfo .exception .stderr ,
2403-         )
2404- 
24052440    def  test_optional_subparsers (self ):
24062441        parser  =  ErrorRaisingArgumentParser ()
24072442        subparsers  =  parser .add_subparsers (dest = 'command' , required = False )
@@ -2726,7 +2761,7 @@ def test_single_parent_mutex(self):
27262761        parser  =  ErrorRaisingArgumentParser (parents = [self .ab_mutex_parent ])
27272762        self ._test_mutex_ab (parser .parse_args )
27282763
2729-     def  test_single_granparent_mutex (self ):
2764+     def  test_single_grandparent_mutex (self ):
27302765        parents  =  [self .ab_mutex_parent ]
27312766        parser  =  ErrorRaisingArgumentParser (add_help = False , parents = parents )
27322767        parser  =  ErrorRaisingArgumentParser (parents = [parser ])
0 commit comments