From 46bbab126411f66ab660efcb44a1a8768eba8753 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Mon, 11 Jun 2012 09:31:28 +0100 Subject: add BooleanAction for argparse This action allows to set a boolean flag via --foo=BAR, where BAR is y/n,true/false etc. --- alot/utils/__init__.py | 0 alot/utils/booleanaction.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 alot/utils/__init__.py create mode 100644 alot/utils/booleanaction.py (limited to 'alot/utils') diff --git a/alot/utils/__init__.py b/alot/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/alot/utils/booleanaction.py b/alot/utils/booleanaction.py new file mode 100644 index 00000000..37fd804f --- /dev/null +++ b/alot/utils/booleanaction.py @@ -0,0 +1,30 @@ +""" +This defines the ConfigureAction for argparse found here: +http://code.google.com/p/argparse/issues/detail?id=2#c6 + +We use it to set booelan arguments for command parameters +to False using a `--no-` prefix. +""" +import argparse +import re + + +TRUEISH = ['1', 't', 'true', 'yes', 'on'] +FALSISH = ['0', 'f', 'false', 'no', 'off'] + +def boolean(string): + string = string.lower() + if string in FALSISH: + return False + elif string in TRUEISH: + return True + else: + raise ValueError() + + +class BooleanAction(argparse.Action): + def __init__(self, *args, **kwargs): + kwargs['type'] = boolean + kwargs['choices'] = TRUEISH + FALSISH + #%kwargs['metavar'] = 'BOOL' + argparse.Action.__init__(self, *args, **kwargs) -- cgit v1.2.3