Writes a usage line into the buffer. :param prog: the program name. :param args: whitespace separated list of arguments. :param prefix: The prefix for the first line. Defaults to ``"Usage: "``.
(self, prog: str, args: str = "", prefix: str | None = None)
| 156 | self.current_indent -= self.indent_increment |
| 157 | |
| 158 | def write_usage(self, prog: str, args: str = "", prefix: str | None = None) -> None: |
| 159 | """Writes a usage line into the buffer. |
| 160 | |
| 161 | :param prog: the program name. |
| 162 | :param args: whitespace separated list of arguments. |
| 163 | :param prefix: The prefix for the first line. Defaults to |
| 164 | ``"Usage: "``. |
| 165 | """ |
| 166 | if prefix is None: |
| 167 | prefix = "{usage} ".format(usage=_("Usage:")) |
| 168 | |
| 169 | usage_prefix = f"{prefix:>{self.current_indent}}{prog} " |
| 170 | text_width = self.width - self.current_indent |
| 171 | |
| 172 | if not args: |
| 173 | # Without args, the prefix's trailing space and the wrap_text |
| 174 | # call that would normally place args on the line are both |
| 175 | # unnecessary. Emit just the prefix line. |
| 176 | self.write(usage_prefix.rstrip(" ")) |
| 177 | self.write("\n") |
| 178 | return |
| 179 | |
| 180 | if text_width >= (term_len(usage_prefix) + 20): |
| 181 | # The arguments will fit to the right of the prefix. |
| 182 | indent = " " * term_len(usage_prefix) |
| 183 | self.write( |
| 184 | wrap_text( |
| 185 | args, |
| 186 | text_width, |
| 187 | initial_indent=usage_prefix, |
| 188 | subsequent_indent=indent, |
| 189 | ) |
| 190 | ) |
| 191 | else: |
| 192 | # The prefix is too long, put the arguments on the next line. |
| 193 | self.write(usage_prefix) |
| 194 | self.write("\n") |
| 195 | indent = " " * (max(self.current_indent, term_len(prefix)) + 4) |
| 196 | self.write( |
| 197 | wrap_text( |
| 198 | args, text_width, initial_indent=indent, subsequent_indent=indent |
| 199 | ) |
| 200 | ) |
| 201 | |
| 202 | self.write("\n") |
| 203 | |
| 204 | def write_heading(self, heading: str) -> None: |
| 205 | """Writes a heading into the buffer.""" |