File size: 4,845 Bytes
51143b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
  "cases": [
    {
      "id": "click-core-help-window",
      "code": "        formatter = ctx.make_formatter()\n        self.format_usage(ctx, formatter)\n        return formatter.getvalue().rstrip(\"\\n\")\n\n    def get_params(self, ctx: Context) -> list[Parameter]:\n        params = self.params\n        help_option = self.get_help_option(ctx)\n\n        if help_option is not None:\n            params = [*params, help_option]\n\n        if __debug__:\n            import warnings\n\n            opts = [opt for param in params for opt in param.opts]\n            opts_counter = Counter(opts)\n            duplicate_opts = (opt for opt, count in opts_counter.items() if count > 1)\n\n            for duplicate_opt in duplicate_opts:\n                warnings.warn(\n                    (\n                        f\"The parameter {duplicate_opt} is used more than once. \"\n                        \"Remove its duplicate as parameters should be unique.\"\n                    ),\n                    stacklevel=3,\n                )\n\n        return params\n\n    def format_usage(self, ctx: Context, formatter: HelpFormatter) -> None:\n        \"\"\"Writes the usage line into the formatter.\n\n        This is a low-level method called by :meth:`get_usage`.\n        \"\"\"\n        pieces = self.collect_usage_pieces(ctx)\n        formatter.write_usage(ctx.command_path, \" \".join(pieces))\n\n    def collect_usage_pieces(self, ctx: Context) -> list[str]:\n        \"\"\"Returns all the pieces that go into the usage line and returns\n        it as a list of strings.\n        \"\"\"\n        rv = [self.options_metavar] if self.options_metavar else []\n\n        for param in self.get_params(ctx):\n            rv.extend(param.get_usage_pieces(ctx))\n\n        return rv\n\n    def get_help_option_names(self, ctx: Context) -> list[str]:\n        \"\"\"Returns the names for the help option.\"\"\"\n        all_names = set(ctx.help_option_names)\n        for param in self.params:\n            all_names.difference_update(param.opts)\n            all_names.difference_update(param.secondary_opts)\n        return list(all_names)\n\n    def get_help_option(self, ctx: Context) -> Option | None:\n        \"\"\"Returns the help option object.\n\n        Skipped if :attr:`add_help_option` is ``False``.\n\n        .. versionchanged:: 8.1.8\n            The help option is now cached to avoid creating it multiple times.\n        \"\"\"\n        help_option_names = self.get_help_option_names(ctx)\n\n        if not help_option_names or not self.add_help_option:\n            return None\n\n        # Cache the help option object in private _help_option attribute to\n        # avoid creating it multiple times. Not doing this will break the\n        # callback ordering by iter_params_for_processing(), which relies on\n        # object comparison.\n        if self._help_option is None:\n            # Avoid circular import.\n            from .decorators import help_option\n\n            # Apply help_option decorator and pop resulting option\n            help_option(*help_option_names)(self)\n            self._help_option = self.params.pop()  # type: ignore[assignment]\n\n        return self._help_option\n\n    def make_parser(self, ctx: Context) -> _OptionParser:\n        \"\"\"Creates the underlying option parser for this command.\"\"\"\n        parser = _OptionParser(ctx)\n",
      "query": "Find how Click builds its help option, detects existing option flags, caches the option and returns it. Preserve details needed to diagnose parameter-name collisions."
    },
    {
      "id": "click-help-decorator",
      "code": "def help_option(*param_decls: str, **kwargs: t.Any) -> t.Callable[[FC], FC]:\n    \"\"\"Pre-configured ``--help`` option which immediately prints the help page\n    and exits the program.\n\n    :param param_decls: One or more option names. Defaults to the single\n        value ``\"--help\"``.\n    :param kwargs: Extra arguments are passed to :func:`option`.\n    \"\"\"\n\n    def show_help(ctx: Context, param: Parameter, value: bool) -> None:\n        \"\"\"Callback that print the help page on ``<stdout>`` and exits.\"\"\"\n        if value and not ctx.resilient_parsing:\n            echo(ctx.get_help(), color=ctx.color)\n            ctx.exit()\n\n    if not param_decls:\n        param_decls = (\"--help\",)\n\n    kwargs.setdefault(\"is_flag\", True)\n    kwargs.setdefault(\"expose_value\", False)\n    kwargs.setdefault(\"is_eager\", True)\n    kwargs.setdefault(\"help\", _(\"Show this message and exit.\"))\n    kwargs.setdefault(\"callback\", show_help)\n\n    return option(*param_decls, **kwargs)\n",
      "query": "Show how the help option decorator constructs the flag and attaches it to a command."
    }
  ],
  "source_parent": "8c1a0a7abbc1c36f70d1f65f3604acc46c5ce6ab"
}