diff options
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) |