chiark / gitweb /
make-secnet-sites: Provide ArgActionLambda
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 1 Dec 2019 15:55:48 +0000 (15:55 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 15 Feb 2020 21:56:52 +0000 (21:56 +0000)
optparse has a helpful lambda facility, so you can pass a lambda
directly to add_argument.  But optparse is deprecated so we are using
argparse, in which this facility was unaccountably removed.

Instead you have to subclass argparse.Action.

The resulting ArgActionLambda will still be a bit awkward to use but a
lot better than lifting what ought to be each lambda body into its own
subclass.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
make-secnet-sites

index 21ff4604ca0c26bee4771f6c22252d8c32ba4b2a..64459475d904b91fb4d825c0fcafe006ff388a25 100755 (executable)
@@ -232,6 +232,13 @@ class Tainted:
                ok=self._re_ok(Tainted.bad_base91,what,4096)
                return self._rtn(ok)
 
+class ArgActionLambda(argparse.Action):
+       def __init__(self, fn, **kwargs):
+               self.fn=fn
+               argparse.Action.__init__(self,**kwargs)
+       def __call__(self,ap,ns,values,option_string):
+               self.fn(values,ns,ap,option_string)
+
 def parse_args():
        global service
        global inputfile