@@ -129,18 +129,6 @@ def forbidden_option_attrs(self) -> List[str]:
129
129
"""
130
130
return []
131
131
132
- def get_default_name (self , ctx : click .Context ) -> str :
133
- """Returns default name for the group
134
-
135
- :param ctx: Click Context object
136
- :return: group default name
137
- """
138
- if self .name :
139
- return self .name
140
-
141
- option_names = '|' .join (self .get_option_names (ctx ))
142
- return f'({ option_names } )'
143
-
144
132
def get_help_record (self , ctx : click .Context ) -> Optional [Tuple [str , str ]]:
145
133
"""Returns the help record for the group
146
134
@@ -150,14 +138,20 @@ def get_help_record(self, ctx: click.Context) -> Optional[Tuple[str, str]]:
150
138
if all (o .hidden for o in self .get_options (ctx ).values ()):
151
139
return None
152
140
153
- name = self .get_default_name ( ctx )
141
+ name = self .name
154
142
help_ = self .help if self .help else ''
155
143
156
144
extra = ', ' .join (self .name_extra )
157
145
if extra :
158
146
extra = f'[{ extra } ]'
159
147
160
- name = f'{ name } : { extra } '
148
+ if name :
149
+ name = f'{ name } : { extra } '
150
+ elif extra :
151
+ name = f'{ extra } :'
152
+
153
+ if not name and not help_ :
154
+ return None
161
155
162
156
return name , help_
163
157
@@ -251,6 +245,9 @@ def _option_memo(self, func):
251
245
option = params [- 1 ]
252
246
self ._options [func ][option .name ] = option
253
247
248
+ def _group_name_str (self ) -> str :
249
+ return f"'{ self .name } '" if self .name else "the"
250
+
254
251
255
252
class RequiredAnyOptionGroup (OptionGroup ):
256
253
"""Option group with required any options of this group
@@ -272,20 +269,20 @@ def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: d
272
269
273
270
if all (o .hidden for o in self .get_options (ctx ).values ()):
274
271
cls_name = self .__class__ .__name__
275
- group_name = self .get_default_name ( ctx )
272
+ group_name = self ._group_name_str ( )
276
273
277
274
raise TypeError (
278
- f"Need at least one non-hidden option in group ' { group_name } ' (' { cls_name } ' )."
275
+ f"Need at least one non-hidden option in { group_name } option group ( { cls_name } )."
279
276
)
280
277
281
278
option_names = set (self .get_options (ctx ))
282
279
283
280
if not option_names .intersection (opts ):
284
- group_name = self .get_default_name ( ctx )
281
+ group_name = self ._group_name_str ( )
285
282
option_info = self .get_error_hint (ctx )
286
283
287
284
raise click .UsageError (
288
- f"At least one of the following options from ' { group_name } ' group is required:\n { option_info } " ,
285
+ f"At least one of the following options from { group_name } option group is required:\n { option_info } " ,
289
286
ctx = ctx
290
287
)
291
288
@@ -308,12 +305,12 @@ def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: d
308
305
option_names = set (self .get_options (ctx ))
309
306
310
307
if not option_names .issubset (opts ):
311
- group_name = self .get_default_name ( ctx )
308
+ group_name = self ._group_name_str ( )
312
309
required_names = option_names .difference (option_names .intersection (opts ))
313
310
option_info = self .get_error_hint (ctx , required_names )
314
311
315
312
raise click .UsageError (
316
- f"Missing required options from ' { group_name } ' group:\n { option_info } " ,
313
+ f"Missing required options from { group_name } option group:\n { option_info } " ,
317
314
ctx = ctx
318
315
)
319
316
@@ -339,11 +336,11 @@ def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: d
339
336
given_option_count = len (given_option_names )
340
337
341
338
if given_option_count > 1 :
342
- group_name = self .get_default_name ( ctx )
339
+ group_name = self ._group_name_str ( )
343
340
option_info = self .get_error_hint (ctx , given_option_names )
344
341
345
342
raise click .UsageError (
346
- f"Mutually exclusive options from ' { group_name } ' group "
343
+ f"Mutually exclusive options from { group_name } option group "
347
344
f"cannot be used at the same time:\n { option_info } " ,
348
345
ctx = ctx
349
346
)
@@ -367,12 +364,12 @@ def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: d
367
364
given_option_names = option_names .intersection (opts )
368
365
369
366
if len (given_option_names ) == 0 :
370
- group_name = self .get_default_name ( ctx )
367
+ group_name = self ._group_name_str ( )
371
368
option_info = self .get_error_hint (ctx )
372
369
373
370
raise click .UsageError (
374
371
"Missing one of the required mutually exclusive options from "
375
- f"' { group_name } ' option group:\n { option_info } " ,
372
+ f"{ group_name } option group:\n { option_info } " ,
376
373
ctx = ctx
377
374
)
378
375
@@ -396,11 +393,11 @@ def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: d
396
393
option_names = set (self .get_options (ctx ))
397
394
398
395
if not option_names .isdisjoint (opts ) and option_names .intersection (opts ) != option_names :
399
- group_name = self .get_default_name ( ctx )
396
+ group_name = self ._group_name_str ( )
400
397
option_info = self .get_error_hint (ctx )
401
398
402
399
raise click .UsageError (
403
- "All options should be specified or none should be specified from the group "
404
- f"' { group_name } '. Missing required options:\n { option_info } " ,
400
+ f "All options from { group_name } option group should be specified or none should be specified. "
401
+ f"Missing required options:\n { option_info } " ,
405
402
ctx = ctx
406
403
)
0 commit comments