Merge branch 'main' into dual-flags-competition
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 0f71fe7, 3619563 · Browse files · 3 files·+75 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+75 added−28 removed
- other:+75 added−28 removed
Provenance
- Commit
98cba78- Tree
2dbdffb- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
0f71fe7- Parent 2
3619563
3 files changed+75 added−28 removed
modified:docs/options.md+42 added
228 unmodified lines229 230 invoke(commit, args=['-m', 'foo', '-m', 'bar', '-m', 'here'])231 ```232 233+## Combining short options234+235+Short options made of a single character can be combined into one argument: `-abc` is equivalent to `-a -b -c`. This is the standard POSIX behavior for short option stacking, and it is the reason a repeated flag like `-vvv` works with the [Counting](#counting) feature.236+237+```{eval-rst}238+.. click:example::239+240+ @click.command()241+ @click.option('-a', is_flag=True)242+ @click.option('-b', is_flag=True)243+ @click.option('-c', is_flag=True)244+ def cli(a, b, c):245+ click.echo(f"a={a} b={b} c={c}")246+247+.. click:run::248+249+ invoke(cli, args=['-a', '-b', '-c'])250+ invoke(cli, args=['-abc'])251+```252+253+If the last option in the combination takes a value, the value can either follow as the next argument or be attached directly:254+255+```{eval-rst}256+.. click:example::257+258+ @click.command()259+ @click.option('-v', is_flag=True)260+ @click.option('-n', type=int)261+ def cli(v, n):262+ click.echo(f"v={v} n={n}")263+264+.. click:run::265+266+ invoke(cli, args=['-v', '-n', '5'])267+ invoke(cli, args=['-vn', '5'])268+ invoke(cli, args=['-vn5'])269+```270+271+```{note}272+Multi-character short option names are not supported. An argument like `-dbg` is interpreted as the combination of `-d`, `-b`, and `-g`, so Click reports `No such option: -d` if `-d` is not declared. For longer option names, use a long option with the `--` prefix (like `--debug`).273+```274+275 ## Counting276 277 To count the occurrence of an option, set `count=True`. If the option is not278 passed on the command line, then the count is 0. Counting is commonly used for516 unmodified linesmodified:src/click/core.py+26 added-28 removed
91 unmodified lines92 93 raise RuntimeError(message)94 95 96+def _format_deprecated_label(deprecated: bool | str) -> str:97+ """Return the parenthesized deprecation label shown in help text."""98+ label = _("deprecated").upper()99+ if isinstance(deprecated, str):100+ return f"({label}: {deprecated})"101+ return f"({label})"102+103+104+def _format_deprecated_suffix(deprecated: bool | str) -> str:105+ """Return the trailing reason for a ``DeprecationWarning`` message,106+ prefixed with a space, or an empty string when no reason was given.107+ """108+ if isinstance(deprecated, str):109+ return f" {deprecated}"110+ return ""111+112+113 def batch(iterable: cabc.Iterable[V], batch_size: int) -> list[tuple[V, ...]]:114 return list(zip(*repeat(iter(iterable), batch_size), strict=False))115 116 1041 unmodified lines1158 else:1159 text = ""1160 1161 if self.deprecated:1145- localised_deprectated = _("deprecated").upper()1146- deprecated_message = (1147- f"({localised_deprectated}: {self.deprecated})"1148- if isinstance(self.deprecated, str)1149- else f"({localised_deprectated})"1150- )1151- text = f"{_(text)} {deprecated_message}"1162+ text = f"{_(text)} {_format_deprecated_label(self.deprecated)}"1163 1164 return text.strip()1165 1166 def format_help(self, ctx: Context, formatter: HelpFormatter) -> None:21 unmodified lines1188 else:1189 text = ""1190 1191 if self.deprecated:1181- localised_deprectated = _("deprecated").upper()1182- deprecated_message = (1183- f"({localised_deprectated}: {self.deprecated})"1184- if isinstance(self.deprecated, str)1185- else f"({localised_deprectated})"1186- )1187- text = f"{_(text)} {deprecated_message}"1192+ text = f"{_(text)} {_format_deprecated_label(self.deprecated)}"1193 1194 if text:1195 formatter.write_paragraph()1196 98 unmodified lines1295 """Given a context, this invokes the attached callback (if it exists)1296 in the right way.1297 """1298 if self.deprecated:1294- extra_message = (1295- f" {self.deprecated}" if isinstance(self.deprecated, str) else ""1296- )1299 message = _(1300 "DeprecationWarning: The command {name!r} is deprecated.{extra_message}"1299- ).format(name=self.name, extra_message=extra_message)1301+ ).format(1302+ name=self.name,1303+ extra_message=_format_deprecated_suffix(self.deprecated),1304+ )1305 echo(style(message, fg="red"), err=True)1306 1307 if self.callback is not None:1308 return ctx.invoke(self.callback, **ctx.params)1305 unmodified lines2614 self.deprecated2615 and value is not UNSET2616 and source < ParameterSource.DEFAULT_MAP2617 ):2613- extra_message = (2614- f" {self.deprecated}" if isinstance(self.deprecated, str) else ""2615- )2618 message = _(2619 "DeprecationWarning: The {param_type} {name!r} is deprecated."2620 "{extra_message}"2621 ).format(2622 param_type=self.param_type_name,2623 name=self.human_readable_name,2622- extra_message=extra_message,2624+ extra_message=_format_deprecated_suffix(self.deprecated),2625 )2626 echo(style(message, fg="red"), err=True)2627 2628 # Process the value through the parameter's type.188 unmodified lines2817 else:2818 prompt_text = prompt2819 2820 if deprecated:2819- deprecated_message = (2820- f"(DEPRECATED: {deprecated})"2821- if isinstance(deprecated, str)2822- else "(DEPRECATED)"2823- )2824- help = help + deprecated_message if help is not None else deprecated_message2821+ label = _format_deprecated_label(deprecated)2822+ help = f"{help} {label}" if help is not None else label2823 2824 self.prompt = prompt_text2825 self.confirmation_prompt = confirmation_prompt2826 self.prompt_required = prompt_required709 unmodified linesmodified:CHANGES.rst+7 added
63 unmodified lines64 within ``ParameterSource.DEFAULT``, an option that received an explicit65 ``default=`` keyword wins over a sibling whose default was auto-derived.66 The 8.3.x first-wins fallback for remaining ties was reverted to the67 pre-8.3.x last-wins fallback. :issue:`3403` :pr:`3404`68+- Fix missing space between option help text and the ``(DEPRECATED)``69+ label, and localize the option label so it matches the command label.70+ The label and the ``DeprecationWarning`` reason suffix are now produced71+ by shared helpers. :pr:`3423`72+- Document short option stacking (``-abc`` is parsed as ``-a -b -c``) and73+ clarify that multi-character short option names are not supported.74+ :issue:`2779` :pr:`3431`75 76 Version 8.3.377 -------------78 1364 unmodified lines