diff options
author | Fabrice Douchant <Fabrice.Douchant@logilab.fr> | 2008-10-30 14:46:12 +0100 |
---|---|---|
committer | Fabrice Douchant <Fabrice.Douchant@logilab.fr> | 2008-10-30 14:46:12 +0100 |
commit | be8b57ad915d263e8c9118eb6a81be283b10f92a (patch) | |
tree | 679fdf1cee945b93fc6edb915cb61031a16685cb | |
parent | a1af7257ffd5a75b5296a8baaba0be9cd22d6835 (diff) | |
parent | 7564ccf79cd69054bcab0d117182e9b49ce40deb (diff) | |
download | logilab-common-be8b57ad915d263e8c9118eb6a81be283b10f92a.tar.gz |
-rw-r--r-- | configuration.py | 7 | ||||
-rw-r--r-- | optik_ext.py | 14 | ||||
-rw-r--r-- | test/unittest_configuration.py | 8 |
3 files changed, 21 insertions, 8 deletions
diff --git a/configuration.py b/configuration.py index 1890ccb..012d4a2 100644 --- a/configuration.py +++ b/configuration.py @@ -396,6 +396,13 @@ class OptionsManagerMixIn(object): use with optik/optparse """ opt_dict = copy(opt_dict) + # if yn option then create right action depending of default value + if "type" in opt_dict and opt_dict['type'] == "yn": + if "default" in opt_dict and opt_dict['default']: + opt_dict['action'] = "store_false" + else: + opt_dict['action'] = "store_true" + del opt_dict['type'] if 'action' in opt_dict: self._nocallback_options[provider] = opt_name else: diff --git a/optik_ext.py b/optik_ext.py index 123a2a6..3c6dff9 100644 --- a/optik_ext.py +++ b/optik_ext.py @@ -29,7 +29,7 @@ try: # python >= 2.3 from optparse import OptionParser as BaseParser, Option as BaseOption, \ OptionGroup, OptionValueError, OptionError, Values, HelpFormatter, \ - NO_DEFAULT + NO_DEFAULT, SUPPRESS_HELP except ImportError: # python < 2.3 from optik import OptionParser as BaseParser, Option as BaseOption, \ @@ -78,13 +78,13 @@ def check_yn(option, opt, value): """check a yn value return true for yes and false for no """ - if isinstance(value, int): - return bool(value) if value in ('y', 'yes'): return True if value in ('n', 'no'): return False - msg = "option %s: invalid yn value %r, should be in (y, yes, n, no)" + if value in (True, False): + return value + msg = "option %s: invalid yn value %r, should be True or False" raise OptionValueError(msg % (opt, value)) def check_named(option, opt, value): @@ -153,6 +153,7 @@ class Option(BaseOption): """ TYPES = BaseOption.TYPES + ('regexp', 'csv', 'yn', 'named', 'password', 'multiple_choice', 'file', 'font', 'color') + ATTRS = BaseOption.ATTRS + ['hide'] TYPE_CHECKER = copy(BaseOption.TYPE_CHECKER) TYPE_CHECKER['regexp'] = check_regexp TYPE_CHECKER['csv'] = check_csv @@ -166,6 +167,11 @@ class Option(BaseOption): TYPES += ('date',) TYPE_CHECKER['date'] = check_date + def __init__(self, *opts, **attrs): + BaseOption.__init__(self, *opts, **attrs) + if hasattr(self, "hide") and self.hide: + self.help = SUPPRESS_HELP + def _check_choice(self): """FIXME: need to override this due to optik misdesign""" if self.type in ("choice", "multiple_choice"): diff --git a/test/unittest_configuration.py b/test/unittest_configuration.py index a60d6e7..a6b9037 100644 --- a/test/unittest_configuration.py +++ b/test/unittest_configuration.py @@ -57,7 +57,7 @@ class ConfigurationTC(TestCase): def test_load_command_line_configuration(self): cfg = self.cfg args = cfg.load_command_line_configuration(['--choice', 'ye', '--number', '4', - '--multiple=1,2,3', '--dothis=n', + '--multiple=1,2,3', '--dothis', 'other', 'arguments']) self.assertEquals(args, ['other', 'arguments']) self.assertEquals(cfg['dothis'], False) @@ -198,7 +198,7 @@ named=key:val Options: -h, --help show this help message and exit - --dothis=<y or n> + --dothis -v<string>, --value=<string> --multiple=<comma separated values> you can also document the option [current: yop,yep] @@ -215,7 +215,7 @@ Options: options: -h, --help show this help message and exit - --dothis=<y or n> + --dothis -v<string>, --value=<string> --multiple=<comma separated values> you can also document the option [current: yop,yep] @@ -232,7 +232,7 @@ options: options: -h, --help show this help message and exit - --dothis=<y or n> + --dothis -v<string>, --value=<string> --multiple=<comma separated values> you can also document the option |