chiark / gitweb /
python: Provide feature for argparse --[no-]foo options
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 4 Nov 2019 15:08:13 +0000 (15:08 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 7 Nov 2019 00:05:51 +0000 (00:05 +0000)
This is surprisingly awkward.  StackExchange has one.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
CREDITS
Makefile.in
argparseactionnoyes.py [new file with mode: 0644]
debian/copyright

diff --git a/CREDITS b/CREDITS
index 9d170d6ef7d9a23ee21e2f8aeb83d677b49b727e..57c3f67150dc512fc05e1ab241b371bd3202d6bd 100644 (file)
--- a/CREDITS
+++ b/CREDITS
@@ -13,3 +13,4 @@ Simon Tatham, Jonathan Amery, Ian Jackson - testing and debugging
 Simon Tatham - RSA signatures using Chinese Remainder Theorem
 Simon Tatham - endianness cleanups in transform.c
 Richard Kettlewell, Matthew Vernon, Peter Benie - assorted bugfixes
+"Omnifarious" and "btel" on Stackoverflow - python yes/no arg parsing
index 136882d451e68391ca7d304120c5db2b87eaf76d..0423cf44ebf5a87a13b618664c544f4e2bafa291 100644 (file)
@@ -55,7 +55,7 @@ OBJECTS:=secnet.o util.o conffile.yy.o conffile.tab.o conffile.o modules.o \
        hackypar.o
 # version.o is handled specially below and in the link rule for secnet.
 
-PYMODULES := ipaddrset.py
+PYMODULES := ipaddrset.py argparseactionnoyes.py
 
 TEST_OBJECTS:=eax-aes-test.o eax-serpent-test.o eax-serpentbe-test.o \
                eax-test.o aes.o
diff --git a/argparseactionnoyes.py b/argparseactionnoyes.py
new file mode 100644 (file)
index 0000000..a7eef77
--- /dev/null
@@ -0,0 +1,37 @@
+# Python argparse --[no-]foo options
+#
+# Copyright 2012 "Omnifarious" (a user on StackOverFlow)
+# Copyright 2013 "btel" (a user on StackOverFlow)
+#
+# https://stackoverflow.com/questions/9234258/in-python-argparse-is-it-possible-to-have-paired-no-something-something-arg/20422915#20422915
+#
+# CC-BY-SA 4.0
+# by virtue of
+# https://stackoverflow.com/legal/terms-of-service#licensing
+# which says everything is CC-BY-SA and has a link to v4.0
+# (And which is therefore compatible with secnet's GPLv3+)
+#
+# all retrieved 4.11.2019
+
+import argparse
+
+class ActionNoYes(argparse.Action):
+    def __init__(self, option_strings, dest, default=None, required=False, help=None):
+
+        if default is None:
+            raise ValueError('You must provide a default with Yes/No action')
+        if len(option_strings)!=1:
+            raise ValueError('Only single argument is allowed with YesNo action')
+        opt = option_strings[0]
+        if not opt.startswith('--'):
+            raise ValueError('Yes/No arguments must be prefixed with --')
+
+        opt = opt[2:]
+        opts = ['--' + opt, '--no-' + opt]
+        super(ActionNoYes, self).__init__(opts, dest, nargs=0, const=None, 
+                                          default=default, required=required, help=help)
+    def __call__(self, parser, namespace, values, option_strings=None):
+        if option_strings.startswith('--no-'):
+            setattr(namespace, self.dest, False)
+        else:
+            setattr(namespace, self.dest, True)
index c932e4fc8a33c91594efa6c030e574c2ac4bc4fb..243dbd0a138cc769efd03f58cb2c24b9aa995ecb 100644 (file)
@@ -18,6 +18,7 @@ secnet is
   Copyright 2012      Matthew Vernon
   Copyright 2013      Mark Wooding
   Copyright 1995-2013 Simon Tatham
+  Copyright 2012,2013 "Omnifarious" and "btel" on Stackoverflow
 
 The Debian package, additionally, is:
   Copyright 2001      Joey Hess