@@ -270,17 +270,23 @@ def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: d
270
270
return
271
271
272
272
if all (o .hidden for o in self .get_options (ctx ).values ()):
273
- error_text = (f'Need at least one non-hidden option in RequiredAnyOptionGroup '
274
- f'"{ self .get_default_name (ctx )} ".' )
275
- raise TypeError (error_text )
273
+ cls_name = self .__class__ .__name__
274
+ group_name = self .get_default_name (ctx )
275
+
276
+ raise TypeError (
277
+ f"Need at least one non-hidden option in group '{ group_name } ' ('{ cls_name } ')."
278
+ )
276
279
277
280
option_names = set (self .get_options (ctx ))
278
281
279
282
if not option_names .intersection (opts ):
280
- error_text = f'Missing one of the required options from " { self .get_default_name (ctx )} " option group:'
281
- error_text += f' \n { self .get_error_hint (ctx )} '
283
+ group_name = self .get_default_name (ctx )
284
+ option_info = self .get_error_hint (ctx )
282
285
283
- raise click .UsageError (error_text , ctx = ctx )
286
+ raise click .UsageError (
287
+ f"At least one of the following options from '{ group_name } ' group is required:\n { option_info } " ,
288
+ ctx = ctx
289
+ )
284
290
285
291
286
292
class RequiredAllOptionGroup (OptionGroup ):
@@ -301,12 +307,14 @@ def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: d
301
307
option_names = set (self .get_options (ctx ))
302
308
303
309
if not option_names .issubset (opts ):
310
+ group_name = self .get_default_name (ctx )
304
311
required_names = option_names .difference (option_names .intersection (opts ))
312
+ option_info = self .get_error_hint (ctx , required_names )
305
313
306
- error_text = f'Missing required options from " { self . get_default_name ( ctx ) } " option group:'
307
- error_text += f' \n { self . get_error_hint ( ctx , required_names ) } '
308
-
309
- raise click . UsageError ( error_text , ctx = ctx )
314
+ raise click . UsageError (
315
+ f"Missing required options from ' { group_name } ' group: \n { option_info } " ,
316
+ ctx = ctx
317
+ )
310
318
311
319
312
320
class MutuallyExclusiveOptionGroup (OptionGroup ):
@@ -330,9 +338,14 @@ def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: d
330
338
given_option_count = len (given_option_names )
331
339
332
340
if given_option_count > 1 :
333
- error_text = 'The given mutually exclusive options cannot be used at the same time:'
334
- error_text += f'\n { self .get_error_hint (ctx , given_option_names )} '
335
- raise click .UsageError (error_text , ctx = ctx )
341
+ group_name = self .get_default_name (ctx )
342
+ option_info = self .get_error_hint (ctx , given_option_names )
343
+
344
+ raise click .UsageError (
345
+ f"Mutually exclusive options from '{ group_name } ' group "
346
+ f"cannot be used at the same time:\n { option_info } " ,
347
+ ctx = ctx
348
+ )
336
349
337
350
338
351
class RequiredMutuallyExclusiveOptionGroup (MutuallyExclusiveOptionGroup ):
@@ -353,17 +366,21 @@ def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: d
353
366
given_option_names = option_names .intersection (opts )
354
367
355
368
if len (given_option_names ) == 0 :
356
- error_text = ('Missing one of the required mutually exclusive options from '
357
- f'"{ self .get_default_name (ctx )} " option group:' )
358
- error_text += f'\n { self .get_error_hint (ctx )} '
359
- raise click .UsageError (error_text , ctx = ctx )
369
+ group_name = self .get_default_name (ctx )
370
+ option_info = self .get_error_hint (ctx )
371
+
372
+ raise click .UsageError (
373
+ "Missing one of the required mutually exclusive options from "
374
+ f"'{ group_name } ' option group:\n { option_info } " ,
375
+ ctx = ctx
376
+ )
360
377
361
378
362
379
class AllOptionGroup (OptionGroup ):
363
380
"""Option group with required all/none options of this group
364
381
365
382
`AllOptionGroup` defines the behavior:
366
- - All options from the group must be set or None must be set.
383
+ - All options from the group must be set or None must be set
367
384
"""
368
385
369
386
@property
@@ -378,9 +395,11 @@ def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: d
378
395
option_names = set (self .get_options (ctx ))
379
396
380
397
if not option_names .isdisjoint (opts ) and option_names .intersection (opts ) != option_names :
381
- error_text = f'All options should be specified or None should be specified from the group ' \
382
- f'"{ self .get_default_name (ctx )} ".'
383
- error_text += f'\n Missing required options from "{ self .get_default_name (ctx )} " option group.'
384
- error_text += f'\n { self .get_error_hint (ctx )} '
385
- error_text += '\n '
386
- raise click .UsageError (error_text , ctx = ctx )
398
+ group_name = self .get_default_name (ctx )
399
+ option_info = self .get_error_hint (ctx )
400
+
401
+ raise click .UsageError (
402
+ "All options should be specified or none should be specified from the group "
403
+ f"'{ group_name } '. Missing required options:\n { option_info } " ,
404
+ ctx = ctx
405
+ )
0 commit comments