From 4d53287814a84e576b6e659341a867a820ba6cae Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Mon, 24 Jun 2019 23:45:01 +0200
Subject: [PATCH] [enh] #235: Add CLI mutually exclusive option class

- https://gist.github.com/jacobtolar/fb80d5552a9a9dfc32b12a829fa21c0c
---
 silkaj/cli_tools.py | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 silkaj/cli_tools.py

diff --git a/silkaj/cli_tools.py b/silkaj/cli_tools.py
new file mode 100644
index 00000000..867e33f8
--- /dev/null
+++ b/silkaj/cli_tools.py
@@ -0,0 +1,40 @@
+"""
+Copyright  2016-2019 Maël Azimi <m.a@moul.re>
+
+Silkaj is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Silkaj is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
+"""
+
+from click import 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(
+                "Usage: `{}` is mutually exclusive with "
+                "arguments `{}`.".format(self.name, ", ".join(self.mutually_exclusive))
+            )
+
+        return super(MutuallyExclusiveOption, self).handle_parse_result(ctx, opts, args)
-- 
GitLab