Skip to content

Instantly share code, notes, and snippets.

@stanchan
Created March 17, 2017 00:01
Show Gist options
  • Save stanchan/bce1c2d030c76fe9223b5ff6ad0f03db to your computer and use it in GitHub Desktop.
Save stanchan/bce1c2d030c76fe9223b5ff6ad0f03db to your computer and use it in GitHub Desktop.
Python Click Mutually Exclusive Options
from click import command, option, Option, UsageError
class MutuallyExclusiveOption(Option):
def __init__(self, *args, **kwargs):
self.mutually_exclusive = set(kwargs.pop('mutually_exclusive', []))
help = kwargs.get('help', '')
if self.mutually_exclusive:
ex_str = ', '.join(self.mutually_exclusive)
kwargs['help'] = help + (
' NOTE: This argument is mutually exclusive with '
' arguments: [' + ex_str + '].'
)
super(MutuallyExclusiveOption, self).__init__(*args, **kwargs)
def handle_parse_result(self, ctx, opts, args):
if self.mutually_exclusive.intersection(opts) and self.name in opts:
raise UsageError(
"Illegal usage: `{}` is mutually exclusive with "
"arguments `{}`.".format(
self.name,
', '.join(self.mutually_exclusive)
)
)
return super(MutuallyExclusiveOption, self).handle_parse_result(
ctx,
opts,
args
)
@command(help="Run the command.")
@option('--jar-file', cls=MutuallyExclusiveOption,
help="The jar file the topology lives in.",
mutually_exclusive=["other_arg"])
@option('--other-arg',
cls=MutuallyExclusiveOption,
help="Another argument.",
mutually_exclusive=["jar_file"])
def cli(jar_file, other_arg):
print "Running cli."
print "jar-file: {}".format(jar_file)
print "other-arg: {}".format(other_arg)
if __name__ == '__main__':
cli()
@deklanowski
Copy link

Very nice solution! +1

@rishianand06
Copy link

In my case, I want to put condition like if this Option 1 is passed, then don't allow to pass Option 2. Not getting click UsageError error.
Ideally if user try to pass both of the options while running the cli command, it should give usage error.
Please help me if I m missing something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment