def test_fromfile(self): ohi = HelpInfoExtracter('').get_option_help_info([], {}) self.assertFalse(ohi.fromfile) kwargs = {'fromfile': False} ohi = HelpInfoExtracter('').get_option_help_info([], kwargs) self.assertFalse(ohi.fromfile) kwargs = {'fromfile': True} ohi = HelpInfoExtracter('').get_option_help_info([], kwargs) self.assertTrue(ohi.fromfile)
def format_options(self, scope, description, option_registrations_iter): """Return a help message for the specified options. :param option_registrations_iter: An iterator over (args, kwargs) pairs, as passed in to options registration. """ oshi = HelpInfoExtracter(self._scope).get_option_scope_help_info(option_registrations_iter) lines = [] def add_option(ohis, *, category=None): lines.append('') display_scope = scope or 'Global' if category: lines.append(self._maybe_green(f'{display_scope} {category}:')) else: lines.append(self._maybe_green(f'{display_scope}:')) if description: lines.append(description) lines.append(' ') if not ohis: lines.append('No options available.') return for ohi in ohis: lines.extend(self.format_option(ohi)) add_option(oshi.basic) if self._show_recursive: add_option(oshi.recursive, category='recursive') if self._show_advanced: add_option(oshi.advanced, category='advanced') return [*lines, '\n']
def format_options(self, scope, description, option_registrations_iter): """Return a help message for the specified options. :API: public :param option_registrations_iter: An iterator over (args, kwargs) pairs, as passed in to options registration. """ oshi = HelpInfoExtracter( self._scope).get_option_scope_help_info(option_registrations_iter) lines = [] def add_option(category, ohis): if ohis: lines.append('') display_scope = scope or 'Global' if category: lines.append( self._maybe_blue('{} {} options:'.format( display_scope, category))) else: lines.append( self._maybe_blue('{} options:'.format(display_scope))) if description: lines.append(description) lines.append(' ') for ohi in ohis: lines.extend(self.format_option(ohi)) add_option('', oshi.basic) if self._show_recursive: add_option('recursive', oshi.recursive) if self._show_advanced: add_option('advanced', oshi.advanced) return lines
def do_test(args, kwargs, expected_display_args, expected_scoped_cmd_line_args): # The scoped and unscoped args are the same in global scope. expected_unscoped_cmd_line_args = expected_scoped_cmd_line_args ohi = HelpInfoExtracter('').get_option_help_info(args, kwargs) self.assertListEqual(expected_display_args, ohi.display_args) self.assertListEqual(expected_scoped_cmd_line_args, ohi.scoped_cmd_line_args) self.assertListEqual(expected_unscoped_cmd_line_args, ohi.unscoped_cmd_line_args)
def test_deprecated(): kwargs = {"removal_version": "999.99.9", "removal_hint": "do not use this"} ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], kwargs) assert "999.99.9" == ohi.removal_version assert "do not use this" == ohi.removal_hint assert ohi.deprecated_message is not None assert ohi.deprecation_active
def format_options(self, scope, description, option_registrations_iter): """Return a help message for the specified options. :param scope: The options scope. :param description: The description of the scope. :param option_registrations_iter: An iterator over (args, kwargs) pairs, as passed in to options registration. """ oshi = HelpInfoExtracter( self._scope).get_option_scope_help_info(option_registrations_iter) lines = [] def add_option(ohis, *, category=None): lines.append("") display_scope = scope or "Global" if category: lines.append(self._maybe_green(f"{display_scope} {category}:")) else: lines.append(self._maybe_green(f"{display_scope}:")) if description: lines.append(description) lines.append(" ") if not ohis: lines.append("No options available.") return for ohi in ohis: lines.extend(self.format_option(ohi)) add_option(oshi.basic) if self._show_advanced: add_option(oshi.advanced, category="advanced") if self._show_deprecated: add_option(oshi.deprecated, category="deprecated") return [*lines, "\n"]
def test_deprecation_start_version_future(): kwargs = { "deprecation_start_version": "999.99.8", "removal_version": "999.99.9" } ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], kwargs) assert "999.99.9" == ohi.removal_version assert not ohi.deprecation_active
def do_test(kwargs, expected_basic=False, expected_advanced=False): def exp_to_len(exp): return int(exp) # True -> 1, False -> 0. oshi = HelpInfoExtracter("").get_option_scope_help_info([([], kwargs)]) self.assertEqual(exp_to_len(expected_basic), len(oshi.basic)) self.assertEqual(exp_to_len(expected_advanced), len(oshi.advanced))
def test_deprecated(self): kwargs = { 'deprecated_version': '999.99.9', 'deprecated_hint': 'do not use this' } ohi = HelpInfoExtracter('').get_option_help_info([], kwargs) self.assertEquals('999.99.9', ohi.deprecated_version) self.assertEquals('do not use this', ohi.deprecated_hint) self.assertIsNotNone(ohi.deprecated_message)
def do_test(args, kwargs, expected_display_args, expected_scoped_cmd_line_args): # The scoped and unscoped args are the same in global scope. expected_unscoped_cmd_line_args = expected_scoped_cmd_line_args ohi = HelpInfoExtracter("").get_option_help_info(args, kwargs) assert tuple(expected_display_args) == ohi.display_args assert tuple(expected_scoped_cmd_line_args) == ohi.scoped_cmd_line_args assert tuple( expected_unscoped_cmd_line_args) == ohi.unscoped_cmd_line_args
def test_deprecated(self): kwargs = { "removal_version": "999.99.9", "removal_hint": "do not use this" } ohi = HelpInfoExtracter("").get_option_help_info([], kwargs) self.assertEqual("999.99.9", ohi.removal_version) self.assertEqual("do not use this", ohi.removal_hint) self.assertIsNotNone(ohi.deprecated_message)
def do_test(args, kwargs, expected_display_args, expected_scoped_cmd_line_args, expected_unscoped_cmd_line_args): ohi = HelpInfoExtracter('bar.baz').get_option_help_info( args, kwargs) self.assertListEqual(expected_display_args, ohi.display_args) self.assertListEqual(expected_scoped_cmd_line_args, ohi.scoped_cmd_line_args) self.assertListEqual(expected_unscoped_cmd_line_args, ohi.unscoped_cmd_line_args)
def get_scope_data(scope): ret = [] for si in ScopeInfoIterator(self.context.options.known_scope_to_info).iterate([scope]): help_info = HelpInfoExtracter(si.scope).get_option_scope_help_info_from_parser( self.context.options.get_parser(si.scope)) ret.append({ # We don't use _asdict(), because then .description wouldn't be available. 'scope_info': si, # We do use _asdict() here, so our mustache library can do property expansion. 'help_info': help_info._asdict(), }) return ret
def do_test( args, kwargs, expected_display_args, expected_scoped_cmd_line_args, expected_unscoped_cmd_line_args, ): ohi = HelpInfoExtracter("bar.baz").get_option_help_info(args, kwargs) assert tuple(expected_display_args) == ohi.display_args assert tuple(expected_scoped_cmd_line_args) == ohi.scoped_cmd_line_args assert tuple( expected_unscoped_cmd_line_args) == ohi.unscoped_cmd_line_args
def do_test(kwargs, expected_basic=False, expected_advanced=False): def exp_to_len(exp): return int(exp) # True -> 1, False -> 0. parser = Parser( env={}, config=Config.load([]), scope_info=GlobalOptions.get_scope_info(), ) parser.register("--foo", **kwargs) oshi = HelpInfoExtracter("").get_option_scope_help_info( "", parser, False) assert exp_to_len(expected_basic) == len(oshi.basic) assert exp_to_len(expected_advanced) == len(oshi.advanced)
def _format_for_global_scope(show_advanced, show_deprecated, args, kwargs): parser = Parser( env={}, config=Config.load([]), scope_info=GlobalOptions.get_scope_info(), parent_parser=None, ) parser.register(*args, **kwargs) # Force a parse to generate the derivation history. parser.parse_args(Parser.ParseArgsRequest((), OptionValueContainerBuilder(), [], False)) oshi = HelpInfoExtracter("").get_option_scope_help_info("", parser, False) return HelpFormatter( show_advanced=show_advanced, show_deprecated=show_deprecated, color=False ).format_options(oshi)
def do_test(args, kwargs, expected_default_str): # Defaults are computed in the parser and added into the kwargs, so we # must jump through this hoop in this test. parser = Parser( env={}, config=Config.load([]), scope_info=GlobalOptions.get_scope_info(), ) parser.register(*args, **kwargs) oshi = HelpInfoExtracter(parser.scope).get_option_scope_help_info( "description", parser, False) assert oshi.description == "description" assert len(oshi.basic) == 1 ohi = oshi.basic[0] assert to_help_str(ohi.default) == expected_default_str
def extracter() -> HelpInfoExtracter: return HelpInfoExtracter("test")
def test_not_deprecated(): ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], {}) assert ohi.removal_version is None assert not ohi.deprecation_active
def test_choices_enum(self) -> None: kwargs = {"type": LogLevel} ohi = HelpInfoExtracter("").get_option_help_info([], kwargs) assert ohi.choices == "info, debug"
def test_choices(self) -> None: kwargs = {"choices": ["info", "debug"]} ohi = HelpInfoExtracter("").get_option_help_info([], kwargs) assert ohi.choices == "info, debug"
def test_passthrough(self): kwargs = {"passthrough": True, "type": list, "member_type": str} ohi = HelpInfoExtracter("").get_option_help_info(["--thing"], kwargs) assert 2 == len(ohi.display_args) assert any(args.startswith("--thing") for args in ohi.display_args) assert any(args.startswith("... -- ") for args in ohi.display_args)
def test_choices() -> None: kwargs = {"choices": ["info", "debug"]} ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], kwargs) assert ohi.choices == ("info", "debug")
def test_list_of_enum() -> None: kwargs = {"type": list, "member_type": LogLevel} ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], kwargs) assert ohi.choices == ("info", "debug")
def test_choices(self) -> None: kwargs = {'choices': ['info', 'debug']} ohi = HelpInfoExtracter('').get_option_help_info([], kwargs) assert ohi.choices == 'info, debug'
def do_test(args, kwargs, expected_default): ohi = HelpInfoExtracter('').get_option_help_info(args, kwargs) self.assertEqual(expected_default, ohi.default)
def test_choices_enum() -> None: kwargs = {"type": LogLevelSimple} ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], kwargs) assert ohi.choices == ("info", "debug")