From 4ea6a8df3dcbcef3209f86df1f27366fbb20440c Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Wed, 25 Jan 2017 09:47:27 -0800 Subject: utils/argparse.py: Various cleanups Hide internal values from export and add a message to the TypeError raised by the if the argument isn't in _TRUISH or _FALSISH. --- alot/utils/argparse.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'alot/utils') diff --git a/alot/utils/argparse.py b/alot/utils/argparse.py index 968e9097..4922c247 100644 --- a/alot/utils/argparse.py +++ b/alot/utils/argparse.py @@ -1,5 +1,6 @@ # encoding=utf-8 # Copyright (C) 2011-2012 Patrick Totzke +# Copyright © 2017 Dylan Baker # 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 @@ -19,28 +20,28 @@ from __future__ import absolute_import import argparse +import itertools -TRUEISH = ['true', 'yes', 'on', '1', 't', 'y'] -FALSISH = ['false', 'no', 'off', '0', 'f', 'n'] +_TRUEISH = ['true', 'yes', 'on', '1', 't', 'y'] +_FALSISH = ['false', 'no', 'off', '0', 'f', 'n'] -def boolean(string): +def _boolean(string): string = string.lower() - if string in FALSISH: + if string in _FALSISH: return False - elif string in TRUEISH: + elif string in _TRUEISH: return True else: - raise ValueError() + raise ValueError('Option must be one of: {}'.format( + ', '.join(itertools.chain(iter(_TRUEISH), iter(_FALSISH))))) class BooleanAction(argparse.Action): - """ - argparse action that can be used to store boolean values - """ + """Argparse action that can be used to store boolean values.""" def __init__(self, *args, **kwargs): - kwargs['type'] = boolean + kwargs['type'] = _boolean kwargs['metavar'] = 'BOOL' argparse.Action.__init__(self, *args, **kwargs) -- cgit v1.2.3