diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2017-01-25 09:27:25 -0800 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2017-01-25 10:27:53 -0800 |
commit | 0ec97d98e82d10024419199b93c1d13a1cedc5eb (patch) | |
tree | 354b6e27dc4270849cb9a34617a411068edc637a /alot/utils/argparse.py | |
parent | c67a62ec0804a588df577360e80922e2a2ffb2ef (diff) |
Move utils/booleanaction.py to utils/argparse.py
This module is going to be enhanced with additional components in later
patches in this series, so it needs a more generic name.
Diffstat (limited to 'alot/utils/argparse.py')
-rw-r--r-- | alot/utils/argparse.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/alot/utils/argparse.py b/alot/utils/argparse.py new file mode 100644 index 00000000..968e9097 --- /dev/null +++ b/alot/utils/argparse.py @@ -0,0 +1,48 @@ +# encoding=utf-8 +# Copyright (C) 2011-2012 Patrick Totzke <patricktotzke@gmail.com> + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +"""Custom extensions of the argparse module.""" + +from __future__ import absolute_import + +import argparse + + +TRUEISH = ['true', 'yes', 'on', '1', 't', 'y'] +FALSISH = ['false', 'no', 'off', '0', 'f', 'n'] + + +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['metavar'] = 'BOOL' + argparse.Action.__init__(self, *args, **kwargs) + + def __call__(self, parser, namespace, values, option_string=None): + setattr(namespace, self.dest, values) |