@@ -145,7 +145,9 @@ def from_callable_or_type(
145
145
)
146
146
147
147
# Helptext for this field; used as description for grouping arguments.
148
- class_field_name = _strings .make_field_name ([field .intern_name ])
148
+ class_field_name = _strings .make_field_name (
149
+ [intern_prefix , field .intern_name ]
150
+ )
149
151
if field .helptext is not None :
150
152
helptext_from_intern_prefixed_field_name [class_field_name ] = (
151
153
field .helptext
@@ -235,10 +237,20 @@ def apply_args(
235
237
"""Create defined arguments and subparsers."""
236
238
237
239
# Make argument groups.
238
- def format_group_name (prefix : str ) -> str :
239
- return (prefix + " options" ).strip ()
240
+ def format_group_name (group_name : str ) -> str :
241
+ return (group_name + " options" ).strip ()
242
+
243
+ def group_name_from_arg (arg : _arguments .ArgumentDefinition ) -> str :
244
+ prefix = arg .lowered .name_or_flag
245
+ if prefix .startswith ("--" ):
246
+ prefix = prefix [2 :]
247
+ if "." in prefix :
248
+ prefix = prefix .rpartition ("." )[0 ]
249
+ else :
250
+ prefix = ""
251
+ return prefix
240
252
241
- group_from_prefix : Dict [str , argparse ._ArgumentGroup ] = {
253
+ group_from_group_name : Dict [str , argparse ._ArgumentGroup ] = {
242
254
"" : parser ._action_groups [1 ],
243
255
** {
244
256
cast (str , group .title ).partition (" " )[0 ]: group
@@ -251,9 +263,10 @@ def format_group_name(prefix: str) -> str:
251
263
# Add each argument group. Note that groups with only suppressed arguments won't
252
264
# be added.
253
265
for arg in self .args :
266
+ group_name = group_name_from_arg (arg )
254
267
if (
255
268
arg .lowered .help is not argparse .SUPPRESS
256
- and arg . extern_prefix not in group_from_prefix
269
+ and group_name not in group_from_group_name
257
270
):
258
271
description = (
259
272
parent .helptext_from_intern_prefixed_field_name .get (
@@ -262,24 +275,23 @@ def format_group_name(prefix: str) -> str:
262
275
if parent is not None
263
276
else None
264
277
)
265
- group_from_prefix [ arg . extern_prefix ] = parser .add_argument_group (
266
- format_group_name (arg . extern_prefix ),
278
+ group_from_group_name [ group_name ] = parser .add_argument_group (
279
+ format_group_name (group_name ),
267
280
description = description ,
268
281
)
269
282
270
- # Add each argument.
271
- for arg in self .args :
283
+ # Add each argument.
272
284
if arg .field .is_positional ():
273
285
arg .add_argument (positional_group )
274
286
continue
275
287
276
- if arg . extern_prefix in group_from_prefix :
277
- arg .add_argument (group_from_prefix [ arg . extern_prefix ])
288
+ if group_name in group_from_group_name :
289
+ arg .add_argument (group_from_group_name [ group_name ])
278
290
else :
279
291
# Suppressed argument: still need to add them, but they won't show up in
280
292
# the helptext so it doesn't matter which group.
281
293
assert arg .lowered .help is argparse .SUPPRESS
282
- arg .add_argument (group_from_prefix ["" ])
294
+ arg .add_argument (group_from_group_name ["" ])
283
295
284
296
for child in self .child_from_prefix .values ():
285
297
child .apply_args (parser , parent = self )
0 commit comments