xref: /llvm-project/cross-project-tests/debuginfo-tests/dexter/dex/tools/help/Tool.py (revision 6779376ee917279b16e256839d236cfdf8fd9ab9)
1# DExTer : Debugging Experience Tester
2# ~~~~~~   ~         ~~         ~   ~~
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7"""Help tool."""
8
9import textwrap
10
11from dex.tools import ToolBase, get_tool_names, get_tools_directory, tool_main
12from dex.utils.Imports import load_module
13from dex.utils.ReturnCode import ReturnCode
14
15
16class Tool(ToolBase):
17    """Provides help info on subtools."""
18
19    @property
20    def name(self):
21        return "DExTer help"
22
23    @property
24    def _visible_tool_names(self):
25        return [t for t in get_tool_names() if not t.endswith("-")]
26
27    def add_tool_arguments(self, parser, defaults):
28        parser.description = Tool.__doc__
29        parser.add_argument(
30            "tool", choices=self._visible_tool_names, nargs="?", help="name of subtool"
31        )
32
33    def handle_options(self, defaults):
34        pass
35
36    @property
37    def _default_text(self):
38        s = "\n<b>The following subtools are available:</>\n\n"
39        tools_directory = get_tools_directory()
40        for tool_name in sorted(self._visible_tool_names):
41            internal_name = tool_name.replace("-", "_")
42            tool_doc = load_module(internal_name, tools_directory).Tool.__doc__
43            tool_doc = tool_doc.strip() if tool_doc else ""
44            tool_doc = textwrap.fill(" ".join(tool_doc.split()), 80)
45            s += "<g>{}</>\n{}\n\n".format(tool_name, tool_doc)
46        return s
47
48    def go(self) -> ReturnCode:
49        if self.context.options.tool is None:
50            self.context.o.auto(self._default_text)
51            return ReturnCode.OK
52
53        tool_name = self.context.options.tool.replace("-", "_")
54        tools_directory = get_tools_directory()
55        module = load_module(tool_name, tools_directory)
56        return tool_main(self.context, module.Tool(self.context), ["--help"])
57