summaryrefslogtreecommitdiff
path: root/alot/utils/booleanaction.py
blob: 7bc81bb73da37a19f12f5882efa481996fc727e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Copyright (C) 2011-2012  Patrick Totzke <patricktotzke@gmail.com>
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file
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):
    """
    argparse action that can be used to store boolean values
    """
    def __init__(self, *args, **kwargs):
        kwargs['type'] = boolean
        kwargs['choices'] = TRUEISH + FALSISH
        kwargs['metavar'] = 'BOOL'
        argparse.Action.__init__(self, *args, **kwargs)