@@ -285,6 +285,7 @@ def _add_signature_arguments(
285
285
286
286
## Add parameter arguments ##
287
287
added_args : List [str ] = []
288
+
288
289
for param in params :
289
290
self ._add_signature_parameter (
290
291
container ,
@@ -298,8 +299,78 @@ def _add_signature_arguments(
298
299
as_positional = as_positional ,
299
300
)
300
301
302
+ if hasattr (function_or_class , "__scriptconfig__" ):
303
+ # Integrate with scriptconfig style classes.
304
+ # When a function/class has a __scriptconfig__ object that means it
305
+ # should accept any of the options defined in the config as keyword
306
+ # arguments. We can utilize additional metadata stored in the
307
+ # scriptconfig object to enrich our CLI.
308
+ config_cls = function_or_class .__scriptconfig__
309
+ self ._add_scriptconfig_arguments (
310
+ config_cls = config_cls ,
311
+ added_args = added_args ,
312
+ function_or_class = function_or_class ,
313
+ component = component ,
314
+ container = container ,
315
+ nested_key = nested_key ,
316
+ fail_untyped = fail_untyped ,
317
+ as_positional = as_positional ,
318
+ sub_configs = sub_configs ,
319
+ skip = skip ,
320
+ linked_targets = linked_targets ,
321
+ )
322
+
301
323
return added_args
302
324
325
+ def _add_scriptconfig_arguments (
326
+ self ,
327
+ config_cls ,
328
+ added_args ,
329
+ function_or_class ,
330
+ component ,
331
+ container ,
332
+ nested_key ,
333
+ fail_untyped : bool = True ,
334
+ as_positional : bool = False ,
335
+ sub_configs : bool = False ,
336
+ skip : Optional [Set [Union [str , int ]]] = None ,
337
+ linked_targets : Optional [Set [str ]] = None ,
338
+ ):
339
+
340
+ for key , value in config_cls .__default__ .items ():
341
+ from scriptconfig import value as value_mod
342
+
343
+ if not isinstance (value , value_mod .Value ):
344
+ # hack
345
+ value = value_mod .Value (value )
346
+ type_ = value .parsekw ["type" ]
347
+ if type_ is None or not isinstance (type_ , type ):
348
+ annotation = inspect ._empty
349
+ else :
350
+ annotation = type_
351
+ param = ParamData (
352
+ name = key ,
353
+ annotation = annotation ,
354
+ kind = inspect .Parameter .KEYWORD_ONLY ,
355
+ default = value .value ,
356
+ doc = value .parsekw ["help" ],
357
+ component = component ,
358
+ parent = function_or_class ,
359
+ short_aliases = value .short_alias ,
360
+ long_aliases = value .alias ,
361
+ )
362
+ self ._add_signature_parameter (
363
+ container ,
364
+ nested_key ,
365
+ param ,
366
+ added_args ,
367
+ skip = {s for s in (skip or []) if isinstance (s , str )},
368
+ fail_untyped = fail_untyped ,
369
+ sub_configs = sub_configs ,
370
+ linked_targets = linked_targets ,
371
+ as_positional = as_positional ,
372
+ )
373
+
303
374
def _add_signature_parameter (
304
375
self ,
305
376
container ,
@@ -355,8 +426,9 @@ def _add_signature_parameter(
355
426
kwargs ["required" ] = True
356
427
is_subclass_typehint = False
357
428
is_dataclass_like_typehint = is_dataclass_like (annotation )
358
- dest = (nested_key + "." if nested_key else "" ) + name
359
- args = [dest if is_required and as_positional else "--" + dest ]
429
+ # dest = (nested_key + "." if nested_key else "") + name
430
+ # args = [dest if is_required and as_positional else "--" + dest]
431
+ args , dest = param ._resolve_args_and_dest (is_required , as_positional , nested_key )
360
432
if param .origin :
361
433
parser = container
362
434
if not isinstance (container , ArgumentParser ):
0 commit comments