File tree Expand file tree Collapse file tree 2 files changed +19
-2
lines changed Expand file tree Collapse file tree 2 files changed +19
-2
lines changed Original file line number Diff line number Diff line change 7
7
import argparse
8
8
import ast
9
9
import atexit
10
+ import difflib
10
11
import json
11
12
import os
12
13
import platform
@@ -60,6 +61,18 @@ class LocustArgumentParser(configargparse.ArgumentParser):
60
61
optionally exclude arguments from the UI.
61
62
"""
62
63
64
+ def error (self , message ):
65
+ # Extract the unknown option from the error message
66
+ if "unrecognized arguments:" in message :
67
+ bad_arg = message .split ("unrecognized arguments:" )[1 ].strip ().split ()[0 ]
68
+ # Compare with known arguments
69
+ options = [action .option_strings for action in self ._actions ]
70
+ options = [opt for sublist in options for opt in sublist ] # flatten
71
+ suggestion = difflib .get_close_matches (bad_arg , options , n = 1 )
72
+ if suggestion :
73
+ message += f"\n Did you mean '{ suggestion [0 ]} '?"
74
+ self .exit (2 , f"{ self .prog } : error: { message } \n " )
75
+
63
76
def add_argument (self , * args , ** kwargs ) -> configargparse .Action :
64
77
"""
65
78
This method supports the same args as ArgumentParser.add_argument(..)
Original file line number Diff line number Diff line change @@ -294,8 +294,9 @@ def test_parse_locustfile_invalid_directory_error(self):
294
294
)
295
295
296
296
def test_unknown_command_line_arg (self ):
297
+ err = StringIO ()
297
298
with self .assertRaises (SystemExit ):
298
- with mock .patch ("sys.stderr" , new = StringIO () ):
299
+ with mock .patch ("sys.stderr" , new = err ):
299
300
parse_options (
300
301
args = [
301
302
"-f" ,
@@ -309,10 +310,13 @@ def test_unknown_command_line_arg(self):
309
310
"--reset-stats" ,
310
311
"--stop-timeout" ,
311
312
"5" ,
312
- "--unknown-flag" ,
313
+ "--unknown-flag-extra-files " ,
313
314
"MyUserClass" ,
314
315
]
315
316
)
317
+ err .seek (0 )
318
+ stderr = err .read ()
319
+ self .assertIn ("Did you mean '--extra-files'" , stderr )
316
320
317
321
def test_custom_argument (self ):
318
322
@locust .events .init_command_line_parser .add_listener
You can’t perform that action at this time.
0 commit comments