Forked from jacobtolar/click_mutually_exclusive_argument.py
Last active
June 9, 2024 23:52
-
-
Save listx/e06c7561bddfe47346e41a23a3026f33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from click import command, option, Option, UsageError | |
class MutuallyExclusiveOption(Option): | |
mutex_groups = {} | |
def __init__(self, *args, **kwargs): | |
opts_list = kwargs.pop('mutex_group', "") | |
self.mutex_group_key = ','.join(opts_list) | |
self.mutex_groups[self.mutex_group_key] = 0 | |
help = kwargs.get('help', '') | |
kwargs['help'] = help + ( | |
' NOTE: This argument may be one of: ' | |
'[' + self.mutex_group_key + '].' | |
) | |
super(MutuallyExclusiveOption, self).__init__(*args, **kwargs) | |
def handle_parse_result(self, ctx, opts, args): | |
if self.name in self.mutex_group_key and self.name in opts: | |
self.mutex_groups[self.mutex_group_key] += 1 | |
if self.mutex_groups[self.mutex_group_key] > 1: | |
exclusive_against = self.mutex_group_key.split(',') | |
exclusive_against.remove(self.name) | |
raise UsageError( | |
"Illegal usage: `{}` is mutually exclusive against " | |
"arguments {}.".format( | |
self.name, | |
exclusive_against | |
) | |
) | |
return super(MutuallyExclusiveOption, self).handle_parse_result( | |
ctx, | |
opts, | |
args | |
) | |
my_mutex_group = ["other_arg", "jar_file"] | |
@command(help="Run the command.") | |
@option('--jar-file', cls=MutuallyExclusiveOption, | |
help="The jar file the topology lives in.", | |
mutex_group=my_mutex_group) | |
@option('--other-arg', | |
cls=MutuallyExclusiveOption, | |
help="Another argument.", | |
mutex_group=my_mutex_group) | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sh-3.2$ python click_mututally_exclusive_argument.py --help | |
Usage: click_mututally_exclusive_argument.py [OPTIONS] | |
Run the command. | |
Options: | |
--jar-file TEXT The jar file the topology lives in. NOTE: This argument | |
may be one of: [other_arg,jar_file]. | |
--other-arg TEXT Another argument. NOTE: This argument may be one of: | |
[other_arg,jar_file]. | |
--help Show this message and exit. | |
sh-3.2$ python click_mututally_exclusive_argument.py --jar-file some/jar.jar | |
Running cli. | |
jar-file: some/jar.jar | |
other-arg: None | |
sh-3.2$ python click_mututally_exclusive_argument.py --other-arg some/arg.txt | |
Running cli. | |
jar-file: None | |
other-arg: some/arg.txt | |
sh-3.2$ python click_mututally_exclusive_argument.py --other-arg some/arg.txt --jar-file some/jar.jar | |
Error: Illegal usage: `jar_file` is mutually exclusive against arguments ['other_arg']. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment