From ae1e7675869c90b879112a296110fd04f8c30ce3 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 4 Nov 2019 15:08:13 +0000 Subject: [PATCH] python: Provide feature for argparse --[no-]foo options This is surprisingly awkward. StackExchange has one. Signed-off-by: Ian Jackson --- CREDITS | 1 + Makefile.in | 2 +- argparseactionnoyes.py | 37 +++++++++++++++++++++++++++++++++++++ debian/copyright | 1 + 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 argparseactionnoyes.py diff --git a/CREDITS b/CREDITS index 9d170d6..57c3f67 100644 --- 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 diff --git a/Makefile.in b/Makefile.in index 136882d..0423cf4 100644 --- a/Makefile.in +++ b/Makefile.in @@ -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 index 0000000..a7eef77 --- /dev/null +++ b/argparseactionnoyes.py @@ -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) diff --git a/debian/copyright b/debian/copyright index c932e4f..243dbd0 100644 --- a/debian/copyright +++ b/debian/copyright @@ -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 -- 2.30.2