From f901bbd21bce234f80ea803ee2a6c2af6ff9f1b7 Mon Sep 17 00:00:00 2001 From: Mike Urbanski Date: Thu, 14 Nov 2019 09:12:13 -0800 Subject: [PATCH] initial commit --- README.me | 1 + src/click_extensions.py | 70 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 README.me create mode 100644 src/click_extensions.py diff --git a/README.me b/README.me new file mode 100644 index 0000000..25c00e5 --- /dev/null +++ b/README.me @@ -0,0 +1 @@ +Click extensions diff --git a/src/click_extensions.py b/src/click_extensions.py new file mode 100644 index 0000000..c06e2a5 --- /dev/null +++ b/src/click_extensions.py @@ -0,0 +1,70 @@ +import click + + +class ClickMutex(click.Option): + def __init__(self, *args, **kwargs): + self.exclusive_with:list = kwargs.pop("exclusive_with") + + assert self.exclusive_with, "'exclusive_with' parameter required" + super(ClickMutex, self).__init__(*args, **kwargs) + + def handle_parse_result(self, ctx, opts, args): + current_opt:bool = self.name in opts + for mutex_opt in self.exclusive_with: + if mutex_opt in opts: + if current_opt: + raise click.UsageError("Illegal usage: '" + str(self.name) + + "' is mutually exclusive with " + str(mutex_opt) + ".") + else: + self.prompt = None + return super(ClickMutex, self).handle_parse_result(ctx, opts, args) + + +class ClickRequiredIfPresent(click.Option): + def __init__(self, *args, **kwargs): + self.required_if:list = kwargs.pop("required_if") + + assert self.required_if, "'required_if' parameter required" + super(ClickRequiredIfPresent, self).__init__(*args, **kwargs) + + def handle_parse_result(self, ctx, opts, args): + current_opt:bool = self.name in opts + for req_opt in self.required_if: + if req_opt in opts: + if not current_opt: + raise click.UsageError("'" + str(self.name) + "' is required if '" + + str(req_opt) + "' is specified.") + else: + self.prompt = None + return super(ClickRequiredIfPresent, self).handle_parse_result(ctx, opts, args) + + +class ClickRequireExactlyOneOf(click.Option): + def __init__(self, *args, **kwargs): + self.require_group:list = kwargs.pop("require_group") + + assert self.require_group, "'require_group' parameter required" + super(ClickRequireExactlyOneOf, self).__init__(*args, **kwargs) + + def handle_parse_result(self, ctx, opts, args): + print(ctx) + print(opts) + print(args) + count = 0 + for req_opt in self.require_group: + if req_opt in opts: + count += 1 + if count > 1: + raise click.UsageError("Exactly one of these must be specified: " + ', '.join(self.require_group)) + + return super(ClickRequireExactlyOneOf, self).handle_parse_result(ctx, opts, args) + + +class ClickCommaSeparatedList(click.ParamType): + name = "CSV" + + def convert(self, value, param, ctx): + return value.split(',') + + +CSV = ClickCommaSeparatedList()