File tree Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -259,11 +259,18 @@ def test_dict_args(parser):
259
259
"--model-name=something.something" ,
260
260
"--hf-overrides.key1" ,
261
261
"val1" ,
262
+ # Test nesting
262
263
"--hf-overrides.key2.key3" ,
263
264
"val2" ,
264
265
"--hf-overrides.key2.key4" ,
265
266
"val3" ,
267
+ # Test = sign
266
268
"--hf-overrides.key5=val4" ,
269
+ # Test underscore to dash conversion
270
+ "--hf_overrides.key_6" ,
271
+ "val5" ,
272
+ "--hf_overrides.key-7.key_8" ,
273
+ "val6" ,
267
274
]
268
275
parsed_args = parser .parse_args (args )
269
276
assert parsed_args .model_name == "something.something"
@@ -274,6 +281,10 @@ def test_dict_args(parser):
274
281
"key4" : "val3" ,
275
282
},
276
283
"key5" : "val4" ,
284
+ "key_6" : "val5" ,
285
+ "key-7" : {
286
+ "key_8" : "val6" ,
287
+ },
277
288
}
278
289
279
290
Original file line number Diff line number Diff line change @@ -1456,17 +1456,24 @@ def parse_args( # type: ignore[override]
1456
1456
if '--config' in args :
1457
1457
args = self ._pull_args_from_config (args )
1458
1458
1459
+ def repl (match : re .Match ) -> str :
1460
+ """Replaces underscores with dashes in the matched string."""
1461
+ return match .group (0 ).replace ("_" , "-" )
1462
+
1463
+ # Everything between the first -- and the first .
1464
+ pattern = re .compile (r"(?<=--)[^\.]*" )
1465
+
1459
1466
# Convert underscores to dashes and vice versa in argument names
1460
1467
processed_args = []
1461
1468
for arg in args :
1462
1469
if arg .startswith ('--' ):
1463
1470
if '=' in arg :
1464
1471
key , value = arg .split ('=' , 1 )
1465
- key = '--' + key [ len ( '--' ):]. replace ( '_' , '-' )
1472
+ key = pattern . sub ( repl , key , count = 1 )
1466
1473
processed_args .append (f'{ key } ={ value } ' )
1467
1474
else :
1468
- processed_args . append ( '--' +
1469
- arg [ len ( '--' ):]. replace ( '_' , '-' ) )
1475
+ key = pattern . sub ( repl , arg , count = 1 )
1476
+ processed_args . append ( key )
1470
1477
elif arg .startswith ('-O' ) and arg != '-O' and len (arg ) == 2 :
1471
1478
# allow -O flag to be used without space, e.g. -O3
1472
1479
processed_args .append ('-O' )
You can’t perform that action at this time.
0 commit comments