frontends (tools/misc): new FlagsHandler class, a simple class to help handling option flags

This commit is contained in:
Goffi 2018-03-21 19:07:06 +01:00
commit 03e9e2b281

View file

@ -60,3 +60,31 @@ class InputHistory(object):
if callback is not None:
callback(self.input_history_tmp[index_tmp])
self.input_history_index = index_tmp
class FlagsHandler(object):
"""Small class to handle easily option flags
the instance is initialized with an iterable
then attribute return True if flag is set, False else.
WARNING: If a flag is checked, it is removed (i.e. unset)
this is done to use bool(flags_handler) to check if all flags
have been used
"""
def __init__(self, flags):
self.flags = set(flags or [])
def __getattr__(self, flag):
try:
self.flags.remove(flag)
except KeyError:
return False
else:
return True
def __len__(self):
return len(self.flags)
def __iter__(self):
return self.flags.__iter__()