def _print_options_help(self, scope: str, show_advanced_and_deprecated: bool) -> None: """Prints a human-readable help message for the options registered on this object. Assumes that self._help_request is an instance of OptionsHelp. """ help_formatter = HelpFormatter( show_advanced=show_advanced_and_deprecated, show_deprecated=show_advanced_and_deprecated, color=self.color, ) oshi = self._all_help_info.scope_to_help_info.get(scope) if not oshi: return formatted_lines = help_formatter.format_options(oshi) goal_info = self._all_help_info.name_to_goal_info.get(scope) if goal_info: related_scopes = sorted( set(goal_info.consumed_scopes) - {GLOBAL_SCOPE, goal_info.name}) if related_scopes: related_subsystems_label = self.maybe_green( "Related subsystems:") formatted_lines.append( f"{related_subsystems_label} {', '.join(related_scopes)}") formatted_lines.append("") for line in formatted_lines: print(line)
def test_suppress_advanced(self): args = ['--foo'] kwargs = {'advanced': True} lines = HelpFormatter(scope='', show_recursive=False, show_advanced=False, color=False).format_options('', '', [(args, kwargs)]) self.assertEquals(0, len(lines)) lines = HelpFormatter(scope='', show_recursive=True, show_advanced=True, color=False).format_options('', '', [(args, kwargs)]) print(lines) self.assertEquals(5, len(lines))
def test_suppress_deprecated(self): args = ["--foo"] kwargs = {"removal_version": "33.44.55"} lines = HelpFormatter( scope="", show_advanced=False, show_deprecated=False, color=False ).format_options(scope="", description="", option_registrations_iter=[(args, kwargs)]) assert len(lines) == 5 assert not any("--foo" in line for line in lines) lines = HelpFormatter( scope="", show_advanced=True, show_deprecated=True, color=False ).format_options(scope="", description="", option_registrations_iter=[(args, kwargs)]) assert len(lines) == 15
def _format_help(self, scope_info): """Return a help message for the options registered on this object. Assumes that self._help_request is an instance of OptionsHelp. :param scope_info: Scope of the options. """ scope = scope_info.scope description = scope_info.description show_recursive = self._help_request.advanced show_advanced = self._help_request.advanced color = sys.stdout.isatty() help_formatter = HelpFormatter(scope, show_recursive, show_advanced, color) return '\n'.join(help_formatter.format_options(scope, description, self._options.get_parser(scope).option_registrations_iter()))
def _format_help(self, scope_info, description): """Return a help message for the options registered on this object. Assumes that self._help_request is an instance of OptionsHelp. :param scope_info: Scope of the options. :param description: Description of scope. """ scope = scope_info.scope show_recursive = self._help_request.advanced show_advanced = self._help_request.advanced color = sys.stdout.isatty() help_formatter = HelpFormatter(scope, show_recursive, show_advanced, color) return '\n'.join(help_formatter.format_options(scope, description, self._options.get_parser(scope).option_registrations_iter()))
def _format_for_single_option(**kwargs): ohi = OptionHelpInfo( display_args=("--foo", ), comma_separated_display_args="--foo", scoped_cmd_line_args=("--foo", ), unscoped_cmd_line_args=("--foo", ), env_var="PANTS_FOO", config_key="foo", typ=bool, default=None, help="help for foo", deprecated_message=None, removal_version=None, removal_hint=None, choices=None, comma_separated_choices=None, value_history=OptionValueHistory( (RankedValue(Rank.HARDCODED, None), )), ) ohi = replace(ohi, **kwargs) lines = HelpFormatter(show_advanced=False, show_deprecated=False, color=False).format_option(ohi) choices = kwargs.get("choices") assert len(lines) == 7 if choices else 6 if choices: assert f"one of: [{', '.join(choices)}]" == lines[3].strip() assert "help for foo" in lines[6 if choices else 5] return lines[4] if choices else lines[3]
def _format_help(self, scope_info: ScopeInfo, show_advanced_and_deprecated: bool) -> str: """Return a help message for the options registered on this object. Assumes that self._help_request is an instance of OptionsHelp. :param scope_info: Scope of the options. """ scope = scope_info.scope description = scope_info.description help_formatter = HelpFormatter( scope=scope, show_advanced=show_advanced_and_deprecated, show_deprecated=show_advanced_and_deprecated, color=self._use_color, ) formatted_lines = help_formatter.format_options( scope, description, self._options.get_parser(scope).option_registrations_iter()) return "\n".join(formatted_lines)
def test_suppress_advanced(self): args = ['--foo'] kwargs = {'advanced': True} lines = HelpFormatter(scope='', show_recursive=False, show_advanced=False, color=False).format_options( scope='', description='', option_registrations_iter=[(args, kwargs)]) assert len(lines) == 5 assert not any("--foo" in line for line in lines) lines = HelpFormatter(scope='', show_recursive=True, show_advanced=True, color=False).format_options( scope='', description='', option_registrations_iter=[(args, kwargs)]) assert len(lines) == 14
def format_help_for_foo(self, **kwargs): ohi = OptionHelpInfo(registering_class=type(None), display_args=['--foo'], scoped_cmd_line_args=['--foo'], unscoped_cmd_line_args=['--foo'], typ=bool, fromfile=False, default=None, help='help for foo', deprecated_version=None, deprecated_message=None, deprecated_hint=None, choices=None) ohi = ohi._replace(**kwargs) lines = HelpFormatter(scope='', show_recursive=False, show_advanced=False, color=False).format_option(ohi) self.assertEquals(len(lines), 2) self.assertIn('help for foo', lines[1]) return lines[0]
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 format_help_for_foo(self, **kwargs): ohi = OptionHelpInfo(registering_class=type(None), display_args=['--foo'], scoped_cmd_line_args=['--foo'], unscoped_cmd_line_args=['--foo'], typ=bool, default=None, help='help for foo', deprecated_message=None, removal_version=None, removal_hint=None, choices=None) ohi = replace(ohi, **kwargs) lines = HelpFormatter(scope='', show_recursive=False, show_advanced=False, color=False).format_option(ohi) assert len(lines) == 2 assert 'help for foo' in lines[1] return lines[0]
def format_help_for_foo(self, **kwargs): ohi = OptionHelpInfo( registering_class=type(None), display_args=["--foo"], comma_separated_display_args="--foo", scoped_cmd_line_args=["--foo"], unscoped_cmd_line_args=["--foo"], typ=bool, default=None, help="help for foo", deprecated_message=None, removal_version=None, removal_hint=None, choices=None, ) ohi = replace(ohi, **kwargs) lines = HelpFormatter(scope="", show_advanced=False, show_deprecated=False, color=False).format_option(ohi) assert len(lines) == 3 assert "help for foo" in lines[2] return lines[1]