diff options
author | Guido Günther <agx@sigxcpu.org> | 2011-01-08 13:36:21 +0100 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2011-01-08 15:23:16 +0100 |
commit | 79ed2e0f088d0b374f5dbed12051770236268238 (patch) | |
tree | 96b8cb9e94425115759aaa97f324c2ac0c5c4a2b /gbp/tristate.py | |
parent | fc1d47d222e76e1c89b8c022f7f285bddbc17f96 (diff) |
Use tristate option for --color=value
this allows true and false as alias for on and off.
Diffstat (limited to 'gbp/tristate.py')
-rw-r--r-- | gbp/tristate.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/gbp/tristate.py b/gbp/tristate.py index ae47d03f..95391b90 100644 --- a/gbp/tristate.py +++ b/gbp/tristate.py @@ -16,10 +16,11 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA class Tristate(object): - """Tri-state value: on, off or auto""" - AUTO = -1 - ON = True - OFF = False + """Tri-state value: on, off or auto """ + ON = True # state is on == do it + OFF = False # state is off == don't do it + AUTO = -1 # autodetect == do if possible + # We accept true as alias for on and false as alias for off _VALID_NAMES = [ 'on', 'off', 'true', 'false', 'auto' ] @@ -63,3 +64,16 @@ class Tristate(object): def is_off(self): return [False, True][self._state == self.OFF] + def do(self, function, *args, **kwargs): + """ + Run function if tristate is on or auto, only report a failure if + tristate is on since failing is o.k. for autodetect. + """ + if self.is_off(): + return True + + success = function(*args, **kwargs) + if not success: + return [True, False][self.is_on()] + + return True |