diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2012-06-11 09:31:28 +0100 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2012-06-11 09:49:40 +0100 |
commit | 46bbab126411f66ab660efcb44a1a8768eba8753 (patch) | |
tree | f27d659b4590defc42263bc8876e6eda5ea7d3c3 /alot/utils | |
parent | e6fd44a6c8826da142322b9d0edc3367432b50ec (diff) |
add BooleanAction for argparse
This action allows to set a boolean flag
via --foo=BAR, where BAR is y/n,true/false etc.
Diffstat (limited to 'alot/utils')
-rw-r--r-- | alot/utils/__init__.py | 0 | ||||
-rw-r--r-- | alot/utils/booleanaction.py | 30 |
2 files changed, 30 insertions, 0 deletions
diff --git a/alot/utils/__init__.py b/alot/utils/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/alot/utils/__init__.py 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) |