• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 18252174847

05 Oct 2025 01:36AM UTC coverage: 43.382% (-36.9%) from 80.261%
18252174847

push

github

web-flow
run tests on mac arm (#22717)

Just doing the minimal to pull forward the x86_64 pattern.

ref #20993

25776 of 59416 relevant lines covered (43.38%)

1.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.29
/src/python/pants/help/help_formatter.py
1
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
3✔
5

6
import json
3✔
7
import textwrap
3✔
8
from enum import Enum
3✔
9

10
from pants.help.help_info_extracter import OptionHelpInfo, OptionScopeHelpInfo, to_help_str
3✔
11
from pants.help.maybe_color import MaybeColor
3✔
12
from pants.option.ranked_value import Rank, RankedValue
3✔
13
from pants.util.docutil import bin_name, terminal_width
3✔
14
from pants.util.strutil import hard_wrap
3✔
15

16

17
class HelpFormatter(MaybeColor):
3✔
18
    def __init__(self, *, show_advanced: bool, show_deprecated: bool, color: bool) -> None:
3✔
19
        super().__init__(color=color)
×
20
        self._show_advanced = show_advanced
×
21
        self._show_deprecated = show_deprecated
×
22
        self._width = terminal_width()
×
23

24
    def format_options(self, oshi: OptionScopeHelpInfo) -> list[str]:
3✔
25
        """Return a help message for the specified options."""
26
        lines = []
×
27

28
        def add_option(ohis, *, category=None) -> None:
×
29
            lines.append("")
×
30
            goal_or_subsystem = "goal" if oshi.is_goal else "subsystem"
×
31
            display_scope = f"`{oshi.scope}` {goal_or_subsystem}" if oshi.scope else "Global"
×
32
            if category:
×
33
                title = f"{display_scope} {category} options"
×
34
                lines.append(self.maybe_green(f"{title}\n{'-' * len(title)}"))
×
35
            else:
36
                # The basic options section gets the description and options scope info.
37
                # No need to repeat those in the advanced section.
38
                title = f"{display_scope} options"
×
39
                lines.append(self.maybe_green(f"{title}\n{'-' * len(title)}\n"))
×
40
                lines.extend(hard_wrap(oshi.description, width=self._width))
×
41
                lines.append(" ")
×
42
                lines.append(f"Activated by {self.maybe_magenta(oshi.provider)}")
×
43
                config_section = f"[{oshi.scope or 'GLOBAL'}]"
×
44
                lines.append(f"Config section: {self.maybe_magenta(config_section)}")
×
45
            lines.append(" ")
×
46
            if not ohis:
×
47
                lines.append("None available.")
×
48
                return
×
49
            for ohi in ohis:
×
50
                lines.extend([*self.format_option(ohi), ""])
×
51

52
        add_option(oshi.basic)
×
53
        show_advanced = self._show_advanced or (not oshi.basic and oshi.advanced)
×
54
        if show_advanced:  # show advanced options if there are no basic ones.
×
55
            add_option(oshi.advanced, category="advanced")
×
56
        if self._show_deprecated and oshi.deprecated:
×
57
            add_option(oshi.deprecated, category="deprecated")
×
58
        if not show_advanced and oshi.advanced:
×
59
            lines.append(
×
60
                self.maybe_green(
61
                    f"Advanced options available. You can list them by running "
62
                    f"{bin_name()} help-advanced {oshi.scope}."
63
                )
64
            )
65
        return [*lines, ""]
×
66

67
    def format_option(self, ohi: OptionHelpInfo) -> list[str]:
3✔
68
        """Format the help output for a single option.
69

70
        :param ohi: Extracted information for option to print
71
        :return: Formatted help text for this option
72
        """
73

74
        def maybe_parens(s: str | None) -> str:
×
75
            return f" ({s})" if s else ""
×
76

77
        def format_value(ranked_val: RankedValue, prefix: str, left_padding: str) -> list[str]:
×
78
            if isinstance(ranked_val.value, (list, dict)):
×
79
                is_enum_list = (
×
80
                    isinstance(ranked_val.value, list)
81
                    and len(ranked_val.value) > 0
82
                    and isinstance(ranked_val.value[0], Enum)
83
                )
84
                normalized_val = (
×
85
                    [enum_elmt.value for enum_elmt in ranked_val.value]
86
                    if is_enum_list
87
                    else ranked_val.value
88
                )
89
                val_lines = json.dumps(normalized_val, sort_keys=True, indent=4).split("\n")
×
90
            else:
91
                val_lines = [to_help_str(ranked_val.value)]
×
92
            val_lines[0] = f"{prefix}{val_lines[0]}"
×
93
            val_lines[-1] = f"{val_lines[-1]}{maybe_parens(ranked_val.details)}"
×
94
            val_lines = [self.maybe_cyan(f"{left_padding}{line}") for line in val_lines]
×
95
            return val_lines
×
96

97
        def wrap(s: str) -> list[str]:
×
98
            return hard_wrap(s, indent=len(indent), width=self._width)
×
99

100
        indent = "      "
×
101

102
        arg_lines = [f"  {self.maybe_magenta(args)}" for args in ohi.display_args]
×
103
        arg_lines.append(self.maybe_magenta(f"  {ohi.env_var}"))
×
104
        arg_lines.append(self.maybe_magenta(f"  {ohi.config_key}"))
×
105

106
        choices = "" if ohi.choices is None else f"one of: [{', '.join(ohi.choices)}]"
×
107
        choices_lines = [
×
108
            f"{indent}{'  ' if i != 0 else ''}{self.maybe_cyan(s)}"
109
            for i, s in enumerate(textwrap.wrap(f"{choices}", self._width))
110
        ]
111

112
        deprecated_lines = []
×
113
        if ohi.deprecated_message:
×
114
            maybe_colorize = self.maybe_red if ohi.deprecation_active else self.maybe_yellow
×
115
            deprecated_lines.extend(wrap(maybe_colorize(ohi.deprecated_message)))
×
116
            if ohi.removal_hint:
×
117
                deprecated_lines.extend(wrap(maybe_colorize(ohi.removal_hint)))
×
118

119
        default_lines = format_value(RankedValue(Rank.HARDCODED, ohi.default), "default: ", indent)
×
120
        if not ohi.value_history:
×
121
            # Should never happen, but this keeps mypy happy.
122
            raise ValueError("No value history - options not parsed.")
×
123

124
        final_val = ohi.value_history.final_value
×
125
        curr_value_lines = format_value(final_val, "current value: ", indent)
×
126

127
        interesting_ranked_values = [
×
128
            rv
129
            for rv in reversed(ohi.value_history.ranked_values)
130
            if rv.rank not in (Rank.NONE, Rank.HARDCODED, final_val.rank)
131
        ]
132
        value_derivation_lines = [
×
133
            line
134
            for rv in interesting_ranked_values
135
            for line in format_value(rv, "overrode: ", f"{indent}    ")
136
        ]
137
        description_lines = wrap(ohi.help)
×
138
        if ohi.target_field_name:
×
139
            description_lines.extend(
×
140
                wrap(
141
                    f"\nCan be overriden by field `{ohi.target_field_name}` on "
142
                    "`local_environment`, `docker_environment`, or `remote_environment` targets."
143
                )
144
            )
145
        lines = [
×
146
            *arg_lines,
147
            *choices_lines,
148
            *default_lines,
149
            *curr_value_lines,
150
            *value_derivation_lines,
151
            *deprecated_lines,
152
            *description_lines,
153
        ]
154
        return lines
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc