Ejemplo n.º 1
0
def get_help(parser: argparse.ArgumentParser) -> str:
    """Get the help message for the main 'pylint-config' command.

    Taken from argparse.ArgumentParser.format_help.
    """
    formatter = parser._get_formatter()

    # usage
    formatter.add_usage(parser.usage, parser._actions,
                        parser._mutually_exclusive_groups)

    # description
    formatter.add_text(parser.description)

    # positionals, optionals and user-defined groups
    for action_group in parser._action_groups:
        if action_group.title == "Subcommands":
            formatter.start_section(action_group.title)
            formatter.add_text(action_group.description)
            formatter.add_arguments(action_group._group_actions)
            formatter.end_section()

    # epilog
    formatter.add_text(parser.epilog)

    # determine help from format above
    return formatter.format_help()
Ejemplo n.º 2
0
def format_args(parser: ArgumentParser, action: Action) -> str:
    """Produce an example to be used together with the flag of the given action.

    Warning:
        This function uses non-public API from Python's stdlib :mod:`argparse`.
    """
    formatter = parser._get_formatter()
    return formatter._format_args(action, action.dest)
Ejemplo n.º 3
0
 def __call__(self,
              parser: argparse.ArgumentParser,
              namespace: argparse.Namespace,
              values: Union[str, Sequence[Any], None],
              option_string: Optional[str] = None) -> NoReturn:
     formatter = parser._get_formatter()
     formatter.add_text(self.version)
     parser._print_message(formatter.format_help(), self.stdout)
     parser.exit()
Ejemplo n.º 4
0
def _build_hint(parser: argparse.ArgumentParser, arg_action: argparse.Action) -> str:
    """Build tab completion hint for a given argument"""
    # Check if hinting is disabled for this argument
    suppress_hint = getattr(arg_action, ATTR_SUPPRESS_TAB_HINT, False)
    if suppress_hint or arg_action.help == argparse.SUPPRESS:
        return ''
    else:
        # Use the parser's help formatter to display just this action's help text
        formatter = parser._get_formatter()
        formatter.start_section("Hint")
        formatter.add_argument(arg_action)
        formatter.end_section()
        return formatter.format_help()
Ejemplo n.º 5
0
 def __init__(self, parser: argparse.ArgumentParser, arg_action: argparse.Action) -> None:
     """
     CompletionError which occurs when there are no results. If hinting is allowed, then its message will
     be a hint about the argument being tab completed.
     :param parser: ArgumentParser instance which owns the action being tab completed
     :param arg_action: action being tab completed
     """
     # Check if hinting is disabled
     suppress_hint = getattr(arg_action, ATTR_SUPPRESS_TAB_HINT, False)
     if suppress_hint or arg_action.help == argparse.SUPPRESS:
         hint_str = ''
     else:
         # Use the parser's help formatter to print just this action's help text
         formatter = parser._get_formatter()
         formatter.start_section("Hint")
         formatter.add_argument(arg_action)
         formatter.end_section()
         hint_str = formatter.format_help()
     # Set apply_style to False because we don't want hints to look like errors
     super().__init__(hint_str, apply_style=False)
Ejemplo n.º 6
0
def _format_help(parser: argparse.ArgumentParser):
    """
    Notes
    -----
    This is an experimental function. Do not use it.
    """
    formatter = parser._get_formatter()

    # description
    formatter.add_text(parser.description)

    # positionals, optionals and user-defined groups
    for action_group in parser._action_groups:
        formatter.start_section(action_group.title)
        formatter.add_text(action_group.description)
        formatter.add_arguments(action_group._group_actions)
        formatter.end_section()

    # epilog
    # formatter.add_text(parser.epilog)

    # determine help from format above
    return formatter.format_help()