From b453e30eeb9931b604759510221ff1cc5df8bf3e Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas@kluyver.me.uk>
Date: Thu, 9 Jul 2020 16:50:42 +0100
Subject: [PATCH] Simplify creating command line options for boolean parameters

---
 xfel_calibrate/calibrate.py | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/xfel_calibrate/calibrate.py b/xfel_calibrate/calibrate.py
index 3b83c2ad6..0db3e31f7 100755
--- a/xfel_calibrate/calibrate.py
+++ b/xfel_calibrate/calibrate.py
@@ -388,23 +388,18 @@ def add_args_from_nb(nb, parser, cvar=None, overwrite_reqs=False):
                                     required=required,
                                     action=make_intelli_list(ltype) if range_allowed else None)
         elif p.type == bool:
-            # check if an input arg is given with an extra "-no" for
-            # forcing to convert a bool to False.
-            # Otherwise leave the default value from the notebook
-            # or convert to true if the bool arg is given.
-            if consolize_name("--no-{}".format(p.name)) in sys.argv:
-                pars_group.add_argument("--{}".format(consolize_name(p.name)),
-                                        action="store_false",
-                                        default=False,
-                                        help=helpstr,
-                                        required=required)
-                sys.argv.remove(consolize_name("--no-{}".format(p.name)))
-            else:
-                pars_group.add_argument("--{}".format(consolize_name(p.name)),
-                                        action="store_true",
-                                        default=default,
-                                        help=helpstr,
-                                        required=required)
+            # For a boolean, make --XYZ and --no-XYZ options.
+            alt_group = pars_group.add_mutually_exclusive_group(required=required)
+            alt_group.add_argument("--no-{}".format(consolize_name(p.name)),
+                                   action="store_false",
+                                   default=default,
+                                   help=helpstr,
+                                   dest=p.name)
+            alt_group.add_argument("--{}".format(consolize_name(p.name)),
+                                   action="store_true",
+                                   default=default,
+                                   help=helpstr,
+                                   dest=p.name)
         else:
             pars_group.add_argument("--{}".format(consolize_name(p.name)),
                                     type=p.type,
-- 
GitLab