Make mutially exclusive option output much nicer info

This commit is contained in:
Joshua Boniface 2018-06-05 20:03:40 -04:00
parent 1e11ab25d1
commit 62c5099d8f
1 changed files with 18 additions and 8 deletions

26
pvcf.py
View File

@ -221,23 +221,33 @@ def searchClusterByName(zk, name):
# #
class MutuallyExclusiveOption(click.Option): class MutuallyExclusiveOption(click.Option):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.mutually_exclusive = set(kwargs.pop('mutually_exclusive', [])) meargs = kwargs.pop('mutually_exclusive', [])
_me_arg = []
_me_func = []
for arg in meargs:
_me_arg.append(arg['argument'])
_me_func.append(arg['function'])
self.me_arg = set(_me_arg)
self.me_func = set(_me_func)
help = kwargs.get('help', '') help = kwargs.get('help', '')
if self.mutually_exclusive: if self.me_func:
ex_str = ', '.join(self.mutually_exclusive) ex_str = ', '.join(self.me_arg)
kwargs['help'] = help + ( kwargs['help'] = help + (
' NOTE: This argument is mutually exclusive with ' ' Mutually exclusive with `' + ex_str + '`.'
'arguments: [' + ex_str + '].'
) )
super(MutuallyExclusiveOption, self).__init__(*args, **kwargs) super(MutuallyExclusiveOption, self).__init__(*args, **kwargs)
def handle_parse_result(self, ctx, opts, args): def handle_parse_result(self, ctx, opts, args):
if self.mutually_exclusive.intersection(opts) and self.name in opts: if self.me_func.intersection(opts) and self.name in opts:
raise click.UsageError( raise click.UsageError(
"Illegal usage: `{}` is mutually exclusive with " "Illegal usage: `{}` is mutually exclusive with "
"arguments `{}`.".format( "arguments `{}`.".format(
self.name, self.opts[-1],
', '.join(self.mutually_exclusive) ', '.join(self.me_arg)
) )
) )