[
  {
    "path": ".devcontainer/devcontainer.json",
    "content": "{\n  \"name\": \"pallets/click\",\n  \"image\": \"mcr.microsoft.com/devcontainers/python:3\",\n  \"customizations\": {\n    \"vscode\": {\n      \"settings\": {\n        \"python.defaultInterpreterPath\": \"${workspaceFolder}/.venv\",\n        \"python.terminal.activateEnvInCurrentTerminal\": true,\n        \"python.terminal.launchArgs\": [\n          \"-X\",\n          \"dev\"\n        ]\n      }\n    }\n  },\n  \"onCreateCommand\": \".devcontainer/on-create-command.sh\"\n}\n"
  },
  {
    "path": ".devcontainer/on-create-command.sh",
    "content": "#!/bin/bash\nset -e\npython3 -m venv --upgrade-deps .venv\n. .venv/bin/activate\npip install -r requirements/dev.txt\npip install -e .\npre-commit install --install-hooks\n"
  },
  {
    "path": ".editorconfig",
    "content": "root = true\n\n[*]\nindent_style = space\nindent_size = 4\ninsert_final_newline = true\ntrim_trailing_whitespace = true\nend_of_line = lf\ncharset = utf-8\nmax_line_length = 88\n\n[*.{css,html,js,json,jsx,scss,ts,tsx,yaml,yml}]\nindent_size = 2\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/bug-report.md",
    "content": "---\nname: Bug report\nabout: Report a bug in Click (not other projects which depend on Click)\n---\n\n<!--\nThis issue tracker is a tool to address bugs in Click itself. Please use\nGitHub Discussions or the Pallets Discord for questions about your own code.\n\nReplace this comment with a clear outline of what the bug is.\n-->\n\n<!--\nDescribe how to replicate the bug.\n\nInclude a minimal reproducible example that demonstrates the bug.\nInclude the full traceback if there was an exception.\n-->\n\n<!--\nDescribe the expected behavior that should have happened but didn't.\n-->\n\nEnvironment:\n\n- Python version:\n- Click version:\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/config.yml",
    "content": "blank_issues_enabled: false\ncontact_links:\n  - name: Questions on Discussions\n    url: https://github.com/pallets/click/discussions/\n    about: Ask questions about your own code on the Discussions tab.\n  - name: Questions on Chat\n    url: https://discord.gg/pallets\n    about: Ask questions about your own code on our Discord chat.\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/feature-request.md",
    "content": "---\nname: Feature request\nabout: Suggest a new feature for Click\n---\n\n<!--\nReplace this comment with a description of what the feature should do.\nInclude details such as links to relevant specs or previous discussions.\n-->\n\n<!--\nReplace this comment with an example of the problem which this feature\nwould resolve. Is this problem solvable without changes to Click, such\nas by subclassing or using an extension?\n-->\n"
  },
  {
    "path": ".github/pull_request_template.md",
    "content": "<!--\nBefore opening a PR, open a ticket describing the issue or feature the\nPR will address. An issue is not required for fixing typos in\ndocumentation, or other simple non-code changes.\n\nReplace this comment with a description of the change. Describe how it\naddresses the linked ticket.\n-->\n\n<!--\nLink to relevant issues or previous PRs, one per line. Use \"fixes\" to\nautomatically close an issue.\n\nfixes #<issue number>\n-->\n\n<!--\nEnsure each step in CONTRIBUTING.rst is complete, especially the following:\n\n- Add tests that demonstrate the correct behavior of the change. Tests\n  should fail without the change.\n- Add or update relevant docs, in the docs folder and in code.\n- Add an entry in CHANGES.rst summarizing the change and linking to the issue.\n- Add `.. versionchanged::` entries in any relevant code docs.\n-->\n"
  },
  {
    "path": ".github/workflows/lock.yaml",
    "content": "name: Lock inactive closed issues\n# Lock closed issues that have not received any further activity for two weeks.\n# This does not close open issues, only humans may do that. It is easier to\n# respond to new issues with fresh examples rather than continuing discussions\n# on old issues.\n\non:\n  schedule:\n    - cron: '0 0 * * *'\npermissions:\n  issues: write\n  pull-requests: write\n  discussions: write\nconcurrency:\n  group: lock\njobs:\n  lock:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: dessant/lock-threads@1bf7ec25051fe7c00bdd17e6a7cf3d7bfb7dc771 # v5.0.1\n        with:\n          issue-inactive-days: 14\n          pr-inactive-days: 14\n          discussion-inactive-days: 14\n"
  },
  {
    "path": ".github/workflows/pre-commit.yaml",
    "content": "name: pre-commit\non:\n  pull_request:\n  push:\n    branches: [main, stable]\njobs:\n  main:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1\n      - uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3\n        with:\n          enable-cache: true\n          prune-cache: false\n      - uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0\n        id: setup-python\n        with:\n          python-version-file: pyproject.toml\n      - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0\n        with:\n          path: ~/.cache/pre-commit\n          key: pre-commit|${{ hashFiles('pyproject.toml', '.pre-commit-config.yaml') }}\n      - run: uv run --locked --group pre-commit pre-commit run --show-diff-on-failure --color=always --all-files\n      - uses: pre-commit-ci/lite-action@5d6cc0eb514c891a40562a58a8e71576c5c7fb43 # v1.1.0\n        if: ${{ !cancelled() }}\n"
  },
  {
    "path": ".github/workflows/publish.yaml",
    "content": "name: Publish\non:\n  push:\n    tags: ['*']\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1\n      - uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3\n        with:\n          enable-cache: true\n          prune-cache: false\n      - uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0\n        with:\n          python-version-file: pyproject.toml\n      - run: echo \"SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct)\" >> $GITHUB_ENV\n      - run: uv build\n      - uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0\n        with:\n          path: ./dist\n  create-release:\n    needs: [build]\n    runs-on: ubuntu-latest\n    permissions:\n      contents: write\n    steps:\n      - uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0\n      - name: create release\n        run: gh release create --draft --repo ${{ github.repository }} ${{ github.ref_name }} artifact/*\n        env:\n          GH_TOKEN: ${{ github.token }}\n  publish-pypi:\n    needs: [build]\n    environment:\n      name: publish\n      url: https://pypi.org/project/click/${{ github.ref_name }}\n    runs-on: ubuntu-latest\n    permissions:\n      id-token: write\n    steps:\n      - uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0\n      - uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0\n        with:\n          packages-dir: artifact/\n"
  },
  {
    "path": ".github/workflows/test-flask.yaml",
    "content": "name: Test Flask Main\non:\n  pull_request:\n    paths-ignore: ['docs/**', 'README.md']\n  push:\n    branches: [main, stable]\n    paths-ignore: ['docs/**', 'README.md']\njobs:\n  flask-tests:\n    name: flask-tests\n    runs-on: ubuntu-latest\n    steps:\n      - uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3\n        with:\n          enable-cache: true\n          prune-cache: false\n      - run: git clone https://github.com/pallets/flask\n      - run: uv venv --python 3.14\n        working-directory: ./flask\n      - run: source .venv/bin/activate\n        working-directory: ./flask\n      - run: uv sync --all-extras\n        working-directory: ./flask\n      - run: uv run --with \"git+https://github.com/pallets/click.git@main\" -- pytest\n        working-directory: ./flask\n"
  },
  {
    "path": ".github/workflows/tests.yaml",
    "content": "name: Tests\non:\n  pull_request:\n    paths-ignore: ['docs/**', 'README.md']\n  push:\n    branches: [main, stable]\n    paths-ignore: ['docs/**', 'README.md']\njobs:\n  tests:\n    name: ${{ matrix.name || matrix.python }}\n    runs-on: ${{ matrix.os || 'ubuntu-latest' }}\n    strategy:\n      fail-fast: false\n      matrix:\n        include:\n          - {python: '3.14'}\n          - {name: free-threaded-latest, python: '3.14t'}\n          - {python: '3.13'}\n          - {name: Windows, python: '3.13', os: windows-latest}\n          - {name: Mac, python: '3.13', os: macos-latest}\n          - {python: '3.12'}\n          - {python: '3.11'}\n          - {python: '3.10'}\n          - {name: PyPy, python: 'pypy-3.11', tox: pypy3.11}\n    steps:\n      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1\n      - uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3\n        with:\n          enable-cache: true\n          prune-cache: false\n      - uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0\n        with:\n          python-version: ${{ matrix.python }}\n      - run: uv run --locked tox run -e ${{ matrix.tox || format('py{0}', matrix.python) }}\n  typing:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1\n      - uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3\n        with:\n          enable-cache: true\n          prune-cache: false\n      - uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0\n        with:\n          python-version-file: pyproject.toml\n      - name: cache mypy\n        uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0\n        with:\n          path: ./.mypy_cache\n          key: mypy|${{ hashFiles('pyproject.toml') }}\n      - run: uv run --locked tox run -e typing\n"
  },
  {
    "path": ".gitignore",
    "content": ".idea/\n.vscode/\n__pycache__/\ndist/\n.coverage*\nhtmlcov/\n.tox/\ndocs/_build/\n"
  },
  {
    "path": ".pre-commit-config.yaml",
    "content": "repos:\n  - repo: https://github.com/astral-sh/ruff-pre-commit\n    rev: 488940d9de1b658fac229e34c521d75a6ea476f2  # frozen: v0.14.5\n    hooks:\n      - id: ruff\n      - id: ruff-format\n  - repo: https://github.com/astral-sh/uv-pre-commit\n    rev: b6675a113e27a9b18f3d60c05794d62ca80c7ab5  # frozen: 0.9.9\n    hooks:\n      - id: uv-lock\n  - repo: https://github.com/pre-commit/pre-commit-hooks\n    rev: 3e8a8703264a2f4a69428a0aa4dcb512790b2c8c  # frozen: v6.0.0\n    hooks:\n      - id: check-merge-conflict\n      - id: debug-statements\n      - id: fix-byte-order-marker\n      - id: trailing-whitespace\n      - id: end-of-file-fixer\n"
  },
  {
    "path": ".readthedocs.yaml",
    "content": "version: 2\n# Do not specify sphinx key here to be in full control of build steps.\n# https://docs.readthedocs.com/platform/stable/build-customization.html#extend-or-override-the-build-process\n\nbuild:\n  os: ubuntu-24.04\n  tools:\n    python: '3.13'\n  jobs:\n    install:\n      - echo \"Installing dependencies\"\n      - asdf plugin add uv\n      - asdf install uv latest\n      - asdf global uv latest\n    build:\n      html:\n        - uv run --group docs sphinx-build -W -b dirhtml docs $READTHEDOCS_OUTPUT/html\n"
  },
  {
    "path": "CHANGES.rst",
    "content": ".. currentmodule:: click\n\nUnreleased\n\n-   Fix handling of ``flag_value`` when ``is_flag=False`` to allow such options to be\n    used without an explicit value. :issue:`3084`\n\nVersion 8.3.1\n--------------\n\nReleased 2025-11-15\n\n-   Don't discard pager arguments by correctly using ``subprocess.Popen``. :issue:`3039`\n    :pr:`3055`\n-   Replace ``Sentinel.UNSET`` default values by ``None`` as they're passed through\n    the ``Context.invoke()`` method. :issue:`3066` :issue:`3065` :pr:`3068`\n-   Fix conversion of ``Sentinel.UNSET`` happening too early, which caused incorrect\n    behavior for multiple parameters using the same name. :issue:`3071` :pr:`3079`\n-   Hide ``Sentinel.UNSET`` values as ``None`` when looking up for other parameters\n    through the context inside parameter callbacks. :issue:`3136` :pr:`3137`\n-   Fix rendering when ``prompt`` and ``confirm`` parameter ``prompt_suffix`` is\n    empty. :issue:`3019` :pr:`3021`\n-   When ``Sentinel.UNSET`` is found during parsing, it will skip calls to\n    ``type_cast_value``. :issue:`3069` :pr:`3090`\n\nVersion 8.3.0\n--------------\n\nReleased 2025-09-17\n\n-   **Improved flag option handling**: Reworked the relationship between ``flag_value``\n    and ``default`` parameters for better consistency:\n\n    * The ``default`` parameter value is now preserved as-is and passed directly\n      to CLI functions (no more unexpected transformations)\n    * Exception: flag options with ``default=True`` maintain backward compatibility\n      by defaulting to their ``flag_value``\n    * The ``default`` parameter can now be any type (``bool``, ``None``, etc.)\n    * Fixes inconsistencies reported in: :issue:`1992` :issue:`2514` :issue:`2610`\n      :issue:`3024` :pr:`3030`\n-   Allow ``default`` to be set on ``Argument`` for ``nargs = -1``. :issue:`2164`\n    :pr:`3030`\n-   Show correct auto complete value for ``nargs`` option in combination with flag\n    option :issue:`2813`\n-   Fix handling of quoted and escaped parameters in Fish autocompletion. :issue:`2995` :pr:`3013`\n-   Lazily import ``shutil``. :pr:`3023`\n-   Properly forward exception information to resources registered with\n    ``click.core.Context.with_resource()``. :issue:`2447` :pr:`3058`\n-   Fix regression related to EOF handling in ``CliRunner``. :issue:`2939` :pr:`2940`\n\nVersion 8.2.2\n-------------\n\nReleased 2025-07-31\n\n-   Fix reconciliation of ``default``, ``flag_value`` and ``type`` parameters for\n    flag options, as well as parsing and normalization of environment variables.\n    :issue:`2952` :pr:`2956`\n-   Fix typing issue in ``BadParameter`` and ``MissingParameter`` exceptions for the\n    parameter ``param_hint`` that did not allow for a sequence of string where the\n    underlying function ``_join_param_hints`` allows for it. :issue:`2777` :pr:`2990`\n-   Use the value of ``Enum`` choices to render their default value in help\n    screen. Refs :issue:`2911` :pr:`3004`\n-   Fix completion for the Z shell (``zsh``) for completion items containing\n    colons. :issue:`2703` :pr:`2846`\n-   Don't include envvar in error hint when not configured. :issue:`2971` :pr:`2972`\n-   Fix a rare race in ``click.testing.StreamMixer``'s finalization that manifested\n    as a ``ValueError`` on close in a multi-threaded test session.\n    :issue:`2993` :pr:`2991`\n\nVersion 8.2.1\n-------------\n\nReleased 2025-05-20\n\n-   Fix flag value handling for flag options with a provided type. :issue:`2894`\n    :issue:`2897` :pr:`2930`\n-   Fix shell completion for nested groups. :issue:`2906` :pr:`2907`\n-   Flush ``sys.stderr`` at the end of ``CliRunner.invoke``. :issue:`2682`\n-   Fix EOF handling for stdin input in CliRunner. :issue:`2787`\n\nVersion 8.2.0\n-------------\n\nReleased 2025-05-10\n\n-   Drop support for Python 3.7, 3.8, and 3.9. :pr:`2588` :pr:`2893`\n-   Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.\n    :pr:`2438`\n-   Use ``flit_core`` instead of ``setuptools`` as build backend. :pr:`2543`\n-   Deprecate the ``__version__`` attribute. Use feature detection, or\n    ``importlib.metadata.version(\"click\")``, instead. :issue:`2598`\n-   ``BaseCommand`` is deprecated. ``Command`` is the base class for all\n    commands. :issue:`2589`\n-   ``MultiCommand`` is deprecated. ``Group`` is the base class for all group\n    commands. :issue:`2590`\n-   The current parser and related classes and methods, are deprecated.\n    :issue:`2205`\n\n    -   ``OptionParser`` and the ``parser`` module, which is a modified copy of\n        ``optparse`` in the standard library.\n    -   ``Context.protected_args`` is unneeded. ``Context.args`` contains any\n        remaining arguments while parsing.\n    -   ``Parameter.add_to_parser`` (on both ``Argument`` and ``Option``) is\n        unneeded. Parsing works directly without building a separate parser.\n    -   ``split_arg_string`` is moved from ``parser`` to ``shell_completion``.\n\n-   Enable deferred evaluation of annotations with\n    ``from __future__ import annotations``. :pr:`2270`\n-   When generating a command's name from a decorated function's name, the\n    suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed.\n    :issue:`2322`\n-   Show the ``types.ParamType.name`` for ``types.Choice`` options within\n    ``--help`` message if ``show_choices=False`` is specified.\n    :issue:`2356`\n-   Do not display default values in prompts when ``Option.show_default`` is\n    ``False``. :pr:`2509`\n-   Add ``get_help_extra`` method on ``Option`` to fetch the generated extra\n    items used in ``get_help_record`` to render help text. :issue:`2516`\n    :pr:`2517`\n-   Keep stdout and stderr streams independent in ``CliRunner``. Always\n    collect stderr output and never raise an exception. Add a new\n    output stream to simulate what the user sees in its terminal. Removes\n    the ``mix_stderr`` parameter in ``CliRunner``. :issue:`2522` :pr:`2523`\n-   ``Option.show_envvar`` now also shows environment variable in error messages.\n    :issue:`2695` :pr:`2696`\n-   ``Context.close`` will be called on exit. This results in all\n    ``Context.call_on_close`` callbacks and context managers added via\n    ``Context.with_resource`` to be closed on exit as well. :pr:`2680`\n-   Add ``ProgressBar(hidden: bool)`` to allow hiding the progressbar. :issue:`2609`\n-   A ``UserWarning`` will be shown when multiple parameters attempt to use the\n    same name. :issue:`2396`\n-   When using ``Option.envvar`` with ``Option.flag_value``, the ``flag_value``\n    will always be used instead of the value of the environment variable.\n    :issue:`2746` :pr:`2788`\n-   Add ``Choice.get_invalid_choice_message`` method for customizing the\n    invalid choice message. :issue:`2621` :pr:`2622`\n-   If help is shown because ``no_args_is_help`` is enabled (defaults to ``True``\n    for groups, ``False`` for commands), the exit code is 2 instead of 0.\n    :issue:`1489` :pr:`1489`\n-   Contexts created during shell completion are closed properly, fixing\n    a ``ResourceWarning`` when using ``click.File``. :issue:`2644` :pr:`2800`\n    :pr:`2767`\n-   ``click.edit(filename)`` now supports passing an iterable of filenames in\n    case the editor supports editing multiple files at once. Its return type\n    is now also typed: ``AnyStr`` if ``text`` is passed, otherwise ``None``.\n    :issue:`2067` :pr:`2068`\n-   Specialized typing of ``progressbar(length=...)`` as ``ProgressBar[int]``.\n    :pr:`2630`\n-   Improve ``echo_via_pager`` behaviour in face of errors.\n    :issue:`2674`\n\n    -   Terminate the pager in case a generator passed to ``echo_via_pager``\n        raises an exception.\n    -   Ensure to always close the pipe to the pager process and wait for it\n        to terminate.\n    -   ``echo_via_pager`` will not ignore ``KeyboardInterrupt`` anymore. This\n        allows the user to search for future output of the generator when\n        using less and then aborting the program using ctrl-c.\n\n-   ``deprecated: bool | str`` can now be used on options and arguments. This\n    previously was only available for ``Command``. The message can now also be\n    customised by using a ``str`` instead of a ``bool``. :issue:`2263` :pr:`2271`\n\n    -   ``Command.deprecated`` formatting in ``--help`` changed from\n        ``(Deprecated) help`` to ``help (DEPRECATED)``.\n    -   Parameters cannot be required nor prompted or an error is raised.\n    -   A warning will be printed when something deprecated is used.\n\n-   Add a ``catch_exceptions`` parameter to ``CliRunner``. If\n    ``catch_exceptions`` is not passed to ``CliRunner.invoke``, the value\n    from ``CliRunner`` is used. :issue:`2817` :pr:`2818`\n-   ``Option.flag_value`` will no longer have a default value set based on\n    ``Option.default`` if ``Option.is_flag`` is ``False``. This results in\n    ``Option.default`` not needing to implement `__bool__`. :pr:`2829`\n-   Incorrect ``click.edit`` typing has been corrected. :pr:`2804`\n-   ``Choice`` is now generic and supports any iterable value.\n    This allows you to use enums and other non-``str`` values. :pr:`2796`\n    :issue:`605`\n-   Fix setup of help option's defaults when using a custom class on its\n    decorator. Removes ``HelpOption``. :issue:`2832` :pr:`2840`\n\nVersion 8.1.8\n-------------\n\nReleased 2024-12-19\n\n-   Fix an issue with type hints for ``click.open_file()``. :issue:`2717`\n-   Fix issue where error message for invalid ``click.Path`` displays on\n    multiple lines. :issue:`2697`\n-   Fixed issue that prevented a default value of ``\"\"`` from being displayed in\n    the help for an option. :issue:`2500`\n-   The test runner handles stripping color consistently on Windows.\n    :issue:`2705`\n-   Show correct value for flag default when using ``default_map``.\n    :issue:`2632`\n-   Fix ``click.echo(color=...)`` passing ``color`` to coloroma so it can be\n    forced on Windows. :issue:`2606`.\n-   More robust bash version check, fixing problem on Windows with git-bash.\n    :issue:`2638`\n-   Cache the help option generated by the ``help_option_names`` setting to\n    respect its eagerness. :pr:`2811`\n-   Replace uses of ``os.system`` with ``subprocess.Popen``. :issue:`1476`\n-   Exceptions generated during a command will use the context's ``color``\n    setting when being displayed. :issue:`2193`\n-   Error message when defining option with invalid name is more descriptive.\n    :issue:`2452`\n-   Refactor code generating default ``--help`` option to deduplicate code.\n    :pr:`2563`\n-   Test ``CLIRunner`` resets patched ``_compat.should_strip_ansi``.\n    :issue:`2732`\n\n\nVersion 8.1.7\n-------------\n\nReleased 2023-08-17\n\n-   Fix issue with regex flags in shell completion. :issue:`2581`\n-   Bash version detection issues a warning instead of an error. :issue:`2574`\n-   Fix issue with completion script for Fish shell. :issue:`2567`\n\n\nVersion 8.1.6\n-------------\n\nReleased 2023-07-18\n\n-   Fix an issue with type hints for ``@click.group()``. :issue:`2558`\n\n\nVersion 8.1.5\n-------------\n\nReleased 2023-07-13\n\n-   Fix an issue with type hints for ``@click.command()``, ``@click.option()``, and\n    other decorators. Introduce typing tests. :issue:`2558`\n\n\nVersion 8.1.4\n-------------\n\nReleased 2023-07-06\n\n-   Replace all ``typing.Dict`` occurrences to ``typing.MutableMapping`` for\n    parameter hints. :issue:`2255`\n-   Improve type hinting for decorators and give all generic types parameters.\n    :issue:`2398`\n-   Fix return value and type signature of `shell_completion.add_completion_class`\n    function. :pr:`2421`\n-   Bash version detection doesn't fail on Windows. :issue:`2461`\n-   Completion works if there is a dot (``.``) in the program name. :issue:`2166`\n-   Improve type annotations for pyright type checker. :issue:`2268`\n-   Improve responsiveness of ``click.clear()``. :issue:`2284`\n-   Improve command name detection when using Shiv or PEX. :issue:`2332`\n-   Avoid showing empty lines if command help text is empty. :issue:`2368`\n-   ZSH completion script works when loaded from ``fpath``. :issue:`2344`.\n-   ``EOFError`` and ``KeyboardInterrupt`` tracebacks are not suppressed when\n    ``standalone_mode`` is disabled. :issue:`2380`\n-   ``@group.command`` does not fail if the group was created with a custom\n    ``command_class``. :issue:`2416`\n-   ``multiple=True`` is allowed for flag options again and does not require\n    setting ``default=()``. :issue:`2246, 2292, 2295`\n-   Make the decorators returned by ``@argument()`` and ``@option()`` reusable when the\n    ``cls`` parameter is used. :issue:`2294`\n-   Don't fail when writing filenames to streams with strict errors. Replace invalid\n    bytes with the replacement character (``�``). :issue:`2395`\n-   Remove unnecessary attempt to detect MSYS2 environment. :issue:`2355`\n-   Remove outdated and unnecessary detection of App Engine environment. :pr:`2554`\n-   ``echo()`` does not fail when no streams are attached, such as with ``pythonw`` on\n    Windows. :issue:`2415`\n-   Argument with ``expose_value=False`` do not cause completion to fail. :issue:`2336`\n\n\nVersion 8.1.3\n-------------\n\nReleased 2022-04-28\n\n-   Use verbose form of ``typing.Callable`` for ``@command`` and\n    ``@group``. :issue:`2255`\n-   Show error when attempting to create an option with\n    ``multiple=True, is_flag=True``. Use ``count`` instead.\n    :issue:`2246`\n\n\nVersion 8.1.2\n-------------\n\nReleased 2022-03-31\n\n-   Fix error message for readable path check that was mixed up with the\n    executable check. :pr:`2236`\n-   Restore parameter order for ``Path``, placing the ``executable``\n    parameter at the end. It is recommended to use keyword arguments\n    instead of positional arguments. :issue:`2235`\n\n\nVersion 8.1.1\n-------------\n\nReleased 2022-03-30\n\n-   Fix an issue with decorator typing that caused type checking to\n    report that a command was not callable. :issue:`2227`\n\n\nVersion 8.1.0\n-------------\n\nReleased 2022-03-28\n\n-   Drop support for Python 3.6. :pr:`2129`\n-   Remove previously deprecated code. :pr:`2130`\n\n    -   ``Group.resultcallback`` is renamed to ``result_callback``.\n    -   ``autocompletion`` parameter to ``Command`` is renamed to\n        ``shell_complete``.\n    -   ``get_terminal_size`` is removed, use\n        ``shutil.get_terminal_size`` instead.\n    -   ``get_os_args`` is removed, use ``sys.argv[1:]`` instead.\n\n-   Rely on :pep:`538` and :pep:`540` to handle selecting UTF-8 encoding\n    instead of ASCII. Click's locale encoding detection is removed.\n    :issue:`2198`\n-   Single options boolean flags with ``show_default=True`` only show\n    the default if it is ``True``. :issue:`1971`\n-   The ``command`` and ``group`` decorators can be applied with or\n    without parentheses. :issue:`1359`\n-   The ``Path`` type can check whether the target is executable.\n    :issue:`1961`\n-   ``Command.show_default`` overrides ``Context.show_default``, instead\n    of the other way around. :issue:`1963`\n-   Parameter decorators and ``@group`` handles ``cls=None`` the same as\n    not passing ``cls``. ``@option`` handles ``help=None`` the same as\n    not passing ``help``. :issue:`#1959`\n-   A flag option with ``required=True`` requires that the flag is\n    passed instead of choosing the implicit default value. :issue:`1978`\n-   Indentation in help text passed to ``Option`` and ``Command`` is\n    cleaned the same as using the ``@option`` and ``@command``\n    decorators does. A command's ``epilog`` and ``short_help`` are also\n    processed. :issue:`1985`\n-   Store unprocessed ``Command.help``, ``epilog`` and ``short_help``\n    strings. Processing is only done when formatting help text for\n    output. :issue:`2149`\n-   Allow empty str input for ``prompt()`` when\n    ``confirmation_prompt=True`` and ``default=\"\"``. :issue:`2157`\n-   Windows glob pattern expansion doesn't fail if a value is an invalid\n    pattern. :issue:`2195`\n-   It's possible to pass a list of ``params`` to ``@command``. Any\n    params defined with decorators are appended to the passed params.\n    :issue:`2131`.\n-   ``@command`` decorator is annotated as returning the correct type if\n    a ``cls`` argument is used. :issue:`2211`\n-   A ``Group`` with ``invoke_without_command=True`` and ``chain=False``\n    will invoke its result callback with the group function's return\n    value. :issue:`2124`\n-   ``to_info_dict`` will not fail if a ``ParamType`` doesn't define a\n    ``name``. :issue:`2168`\n-   Shell completion prioritizes option values with option prefixes over\n    new options. :issue:`2040`\n-   Options that get an environment variable value using\n    ``autoenvvar_prefix`` treat an empty value as ``None``, consistent\n    with a direct ``envvar``. :issue:`2146`\n\n\nVersion 8.0.4\n-------------\n\nReleased 2022-02-18\n\n-   ``open_file`` recognizes ``Path(\"-\")`` as a standard stream, the\n    same as the string ``\"-\"``. :issue:`2106`\n-   The ``option`` and ``argument`` decorators preserve the type\n    annotation of the decorated function. :pr:`2155`\n-   A callable default value can customize its help text by overriding\n    ``__str__`` instead of always showing ``(dynamic)``. :issue:`2099`\n-   Fix a typo in the Bash completion script that affected file and\n    directory completion. If this script was generated by a previous\n    version, it should be regenerated. :issue:`2163`\n-   Fix typing for ``echo`` and ``secho`` file argument.\n    :issue:`2174, 2185`\n\n\nVersion 8.0.3\n-------------\n\nReleased 2021-10-10\n\n-   Fix issue with ``Path(resolve_path=True)`` type creating invalid\n    paths. :issue:`2088`\n-   Importing ``readline`` does not cause the ``confirm()`` prompt to\n    disappear when pressing backspace. :issue:`2092`\n-   Any default values injected by ``invoke()`` are cast to the\n    corresponding parameter's type. :issue:`2089, 2090`\n\n\nVersion 8.0.2\n-------------\n\nReleased 2021-10-08\n\n-   ``is_bool_flag`` is not set to ``True`` if ``is_flag`` is ``False``.\n    :issue:`1925`\n-   Bash version detection is locale independent. :issue:`1940`\n-   Empty ``default`` value is not shown for ``multiple=True``.\n    :issue:`1969`\n-   Fix shell completion for arguments that start with a forward slash\n    such as absolute file paths. :issue:`1929`\n-   ``Path`` type with ``resolve_path=True`` resolves relative symlinks\n    to be relative to the containing directory. :issue:`1921`\n-   Completion does not skip Python's resource cleanup when exiting,\n    avoiding some unexpected warning output. :issue:`1738, 2017`\n-   Fix type annotation for ``type`` argument in ``prompt`` function.\n    :issue:`2062`\n-   Fix overline and italic styles, which were incorrectly added when\n    adding underline. :pr:`2058`\n-   An option with ``count=True`` will not show \"[x>=0]\" in help text.\n    :issue:`2072`\n-   Default values are not cast to the parameter type twice during\n    processing. :issue:`2085`\n-   Options with ``multiple`` and ``flag_value`` use the flag value\n    instead of leaving an internal placeholder. :issue:`2001`\n\n\nVersion 8.0.1\n-------------\n\nReleased 2021-05-19\n\n-   Mark top-level names as exported so type checking understand imports\n    in user projects. :issue:`1879`\n-   Annotate ``Context.obj`` as ``Any`` so type checking allows all\n    operations on the arbitrary object. :issue:`1885`\n-   Fix some types that weren't available in Python 3.6.0. :issue:`1882`\n-   Fix type checking for iterating over ``ProgressBar`` object.\n    :issue:`1892`\n-   The ``importlib_metadata`` backport package is installed on Python <\n    3.8. :issue:`1889`\n-   Arguments with ``nargs=-1`` only use env var value if no command\n    line values are given. :issue:`1903`\n-   Flag options guess their type from ``flag_value`` if given, like\n    regular options do from ``default``. :issue:`1886`\n-   Added documentation that custom parameter types may be passed\n    already valid values in addition to strings. :issue:`1898`\n-   Resolving commands returns the name that was given, not\n    ``command.name``, fixing an unintended change to help text and\n    ``default_map`` lookups. When using patterns like ``AliasedGroup``,\n    override ``resolve_command`` to change the name that is returned if\n    needed. :issue:`1895`\n-   If a default value is invalid, it does not prevent showing help\n    text. :issue:`1889`\n-   Pass ``windows_expand_args=False`` when calling the main command to\n    disable pattern expansion on Windows. There is no way to escape\n    patterns in CMD, so if the program needs to pass them on as-is then\n    expansion must be disabled. :issue:`1901`\n\n\nVersion 8.0.0\n-------------\n\nReleased 2021-05-11\n\n-   Drop support for Python 2 and 3.5.\n-   Colorama is always installed on Windows in order to provide style\n    and color support. :pr:`1784`\n-   Adds a repr to Command, showing the command name for friendlier\n    debugging. :issue:`1267`, :pr:`1295`\n-   Add support for distinguishing the source of a command line\n    parameter. :issue:`1264`, :pr:`1329`\n-   Add an optional parameter to ``ProgressBar.update`` to set the\n    ``current_item``. :issue:`1226`, :pr:`1332`\n-   ``version_option`` uses ``importlib.metadata`` (or the\n    ``importlib_metadata`` backport) instead of ``pkg_resources``. The\n    version is detected based on the package name, not the entry point\n    name. The Python package name must match the installed package\n    name, or be passed with ``package_name=``. :issue:`1582`\n-   If validation fails for a prompt with ``hide_input=True``, the value\n    is not shown in the error message. :issue:`1460`\n-   An ``IntRange`` or ``FloatRange`` option shows the accepted range in\n    its help text. :issue:`1525`, :pr:`1303`\n-   ``IntRange`` and ``FloatRange`` bounds can be open (``<``) instead\n    of closed (``<=``) by setting ``min_open`` and ``max_open``. Error\n    messages have changed to reflect this. :issue:`1100`\n-   An option defined with duplicate flag names (``\"--foo/--foo\"``)\n    raises a ``ValueError``. :issue:`1465`\n-   ``echo()`` will not fail when using pytest's ``capsys`` fixture on\n    Windows. :issue:`1590`\n-   Resolving commands returns the canonical command name instead of the\n    matched name. This makes behavior such as help text and\n    ``Context.invoked_subcommand`` consistent when using patterns like\n    ``AliasedGroup``. :issue:`1422`\n-   The ``BOOL`` type accepts the values \"on\" and \"off\". :issue:`1629`\n-   A ``Group`` with ``invoke_without_command=True`` will always invoke\n    its result callback. :issue:`1178`\n-   ``nargs == -1`` and ``nargs > 1`` is parsed and validated for\n    values from environment variables and defaults. :issue:`729`\n-   Detect the program name when executing a module or package with\n    ``python -m name``. :issue:`1603`\n-   Include required parent arguments in help synopsis of subcommands.\n    :issue:`1475`\n-   Help for boolean flags with ``show_default=True`` shows the flag\n    name instead of ``True`` or ``False``. :issue:`1538`\n-   Non-string objects passed to ``style()`` and ``secho()`` will be\n    converted to string. :pr:`1146`\n-   ``edit(require_save=True)`` will detect saves for editors that exit\n    very fast on filesystems with 1 second resolution. :pr:`1050`\n-   New class attributes make it easier to use custom core objects\n    throughout an entire application. :pr:`938`\n\n    -   ``Command.context_class`` controls the context created when\n        running the command.\n    -   ``Context.invoke`` creates new contexts of the same type, so a\n        custom type will persist to invoked subcommands.\n    -   ``Context.formatter_class`` controls the formatter used to\n        generate help and usage.\n    -   ``Group.command_class`` changes the default type for\n        subcommands with ``@group.command()``.\n    -   ``Group.group_class`` changes the default type for subgroups\n        with ``@group.group()``. Setting it to ``type`` will create\n        subgroups of the same type as the group itself.\n    -   Core objects use ``super()`` consistently for better support of\n        subclassing.\n\n-   Use ``Context.with_resource()`` to manage resources that would\n    normally be used in a ``with`` statement, allowing them to be used\n    across subcommands and callbacks, then cleaned up when the context\n    ends. :pr:`1191`\n-   The result object returned by the test runner's ``invoke()`` method\n    has a ``return_value`` attribute with the value returned by the\n    invoked command. :pr:`1312`\n-   Required arguments with the ``Choice`` type show the choices in\n    curly braces to indicate that one is required (``{a|b|c}``).\n    :issue:`1272`\n-   If only a name is passed to ``option()``, Click suggests renaming it\n    to ``--name``. :pr:`1355`\n-   A context's ``show_default`` parameter defaults to the value from\n    the parent context. :issue:`1565`\n-   ``click.style()`` can output 256 and RGB color codes. Most modern\n    terminals support these codes. :pr:`1429`\n-   When using ``CliRunner.invoke()``, the replaced ``stdin`` file has\n    ``name`` and ``mode`` attributes. This lets ``File`` options with\n    the ``-`` value match non-testing behavior. :issue:`1064`\n-   When creating a ``Group``, allow passing a list of commands instead\n    of a dict. :issue:`1339`\n-   When a long option name isn't valid, use ``difflib`` to make better\n    suggestions for possible corrections. :issue:`1446`\n-   Core objects have a ``to_info_dict()`` method. This gathers\n    information about the object's structure that could be useful for a\n    tool generating user-facing documentation. To get the structure of\n    an entire CLI, use ``Context(cli).to_info_dict()``. :issue:`461`\n-   Redesign the shell completion system. :issue:`1484`, :pr:`1622`\n\n    -   Support Bash >= 4.4, Zsh, and Fish, with the ability for\n        extensions to add support for other shells.\n    -   Allow commands, groups, parameters, and types to override their\n        completions suggestions.\n    -   Groups complete the names commands were registered with, which\n        can differ from the name they were created with.\n    -   The ``autocompletion`` parameter for options and arguments is\n        renamed to ``shell_complete``. The function must take\n        ``ctx, param, incomplete``, must do matching rather than return\n        all values, and must return a list of strings or a list of\n        ``CompletionItem``. The old name and behavior is deprecated and\n        will be removed in 8.1.\n    -   The env var values used to start completion have changed order.\n        The shell now comes first, such as ``{shell}_source`` rather\n        than ``source_{shell}``, and is always required.\n\n-   Completion correctly parses command line strings with incomplete\n    quoting or escape sequences. :issue:`1708`\n-   Extra context settings (``obj=...``, etc.) are passed on to the\n    completion system. :issue:`942`\n-   Include ``--help`` option in completion. :pr:`1504`\n-   ``ParameterSource`` is an ``enum.Enum`` subclass. :issue:`1530`\n-   Boolean and UUID types strip surrounding space before converting.\n    :issue:`1605`\n-   Adjusted error message from parameter type validation to be more\n    consistent. Quotes are used to distinguish the invalid value.\n    :issue:`1605`\n-   The default value for a parameter with ``nargs`` > 1 and\n    ``multiple=True`` must be a list of tuples. :issue:`1649`\n-   When getting the value for a parameter, the default is tried in the\n    same section as other sources to ensure consistent processing.\n    :issue:`1649`\n-   All parameter types accept a value that is already the correct type.\n    :issue:`1649`\n-   For shell completion, an argument is considered incomplete if its\n    value did not come from the command line args. :issue:`1649`\n-   Added ``ParameterSource.PROMPT`` to track parameter values that were\n    prompted for. :issue:`1649`\n-   Options with ``nargs`` > 1 no longer raise an error if a default is\n    not given. Parameters with ``nargs`` > 1 default to ``None``, and\n    parameters with ``multiple=True`` or ``nargs=-1`` default to an\n    empty tuple. :issue:`472`\n-   Handle empty env vars as though the option were not passed. This\n    extends the change introduced in 7.1 to be consistent in more cases.\n    :issue:`1285`\n-   ``Parameter.get_default()`` checks ``Context.default_map`` to\n    handle overrides consistently in help text, ``invoke()``, and\n    prompts. :issue:`1548`\n-   Add ``prompt_required`` param to ``Option``. When set to ``False``,\n    the user will only be prompted for an input if no value was passed.\n    :issue:`736`\n-   Providing the value to an option can be made optional through\n    ``is_flag=False``, and the value can instead be prompted for or\n    passed in as a default value.\n    :issue:`549, 736, 764, 921, 1015, 1618`\n-   Fix formatting when ``Command.options_metavar`` is empty. :pr:`1551`\n-   Revert adding space between option help text that wraps.\n    :issue:`1831`\n-   The default value passed to ``prompt`` will be cast to the correct\n    type like an input value would be. :pr:`1517`\n-   Automatically generated short help messages will stop at the first\n    ending of a phrase or double linebreak. :issue:`1082`\n-   Skip progress bar render steps for efficiency with very fast\n    iterators by setting ``update_min_steps``. :issue:`676`\n-   Respect ``case_sensitive=False`` when doing shell completion for\n    ``Choice`` :issue:`1692`\n-   Use ``mkstemp()`` instead of ``mktemp()`` in pager implementation.\n    :issue:`1752`\n-   If ``Option.show_default`` is a string, it is displayed even if\n    ``default`` is ``None``. :issue:`1732`\n-   ``click.get_terminal_size()`` is deprecated and will be removed in\n    8.1. Use :func:`shutil.get_terminal_size` instead. :issue:`1736`\n-   Control the location of the temporary directory created by\n    ``CLIRunner.isolated_filesystem`` by passing ``temp_dir``. A custom\n    directory will not be removed automatically. :issue:`395`\n-   ``click.confirm()`` will prompt until input is given if called with\n    ``default=None``. :issue:`1381`\n-   Option prompts validate the value with the option's callback in\n    addition to its type. :issue:`457`\n-   ``confirmation_prompt`` can be set to a custom string. :issue:`723`\n-   Allow styled output in Jupyter on Windows. :issue:`1271`\n-   ``style()`` supports the ``strikethrough``, ``italic``, and\n    ``overline`` styles. :issue:`805, 1821`\n-   Multiline marker is removed from short help text. :issue:`1597`\n-   Restore progress bar behavior of echoing only the label if the file\n    is not a TTY. :issue:`1138`\n-   Progress bar output is shown even if execution time is less than 0.5\n    seconds. :issue:`1648`\n-   Progress bar ``item_show_func`` shows the current item, not the\n    previous item. :issue:`1353`\n-   The ``Path`` param type can be passed ``path_type=pathlib.Path`` to\n    return a path object instead of a string. :issue:`405`\n-   ``TypeError`` is raised when parameter with ``multiple=True`` or\n    ``nargs > 1`` has non-iterable default. :issue:`1749`\n-   Add a ``pass_meta_key`` decorator for passing a key from\n    ``Context.meta``. This is useful for extensions using ``meta`` to\n    store information. :issue:`1739`\n-   ``Path`` ``resolve_path`` resolves symlinks on Windows Python < 3.8.\n    :issue:`1813`\n-   Command deprecation notice appears at the start of the help text, as\n    well as in the short help. The notice is not in all caps.\n    :issue:`1791`\n-   When taking arguments from ``sys.argv`` on Windows, glob patterns,\n    user dir, and env vars are expanded. :issue:`1096`\n-   Marked messages shown by the CLI with ``gettext()`` to allow\n    applications to translate Click's built-in strings. :issue:`303`\n-   Writing invalid characters  to ``stderr`` when using the test runner\n    does not raise a ``UnicodeEncodeError``. :issue:`848`\n-   Fix an issue where ``readline`` would clear the entire ``prompt()``\n    line instead of only the input when pressing backspace. :issue:`665`\n-   Add all kwargs passed to ``Context.invoke()`` to ``ctx.params``.\n    Fixes an inconsistency when nesting ``Context.forward()`` calls.\n    :issue:`1568`\n-   The ``MultiCommand.resultcallback`` decorator is renamed to\n    ``result_callback``. The old name is deprecated. :issue:`1160`\n-   Fix issues with ``CliRunner`` output when using ``echo_stdin=True``.\n    :issue:`1101`\n-   Fix a bug of ``click.utils.make_default_short_help`` for which the\n    returned string could be as long as ``max_width + 3``. :issue:`1849`\n-   When defining a parameter, ``default`` is validated with\n    ``multiple`` and ``nargs``. More validation is done for values being\n    processed as well. :issue:`1806`\n-   ``HelpFormatter.write_text`` uses the full line width when wrapping\n    text. :issue:`1871`\n\n\nVersion 7.1.2\n-------------\n\nReleased 2020-04-27\n\n-   Revert applying shell quoting to commands for ``echo_with_pager``\n    and ``edit``. This was intended to allows spaces in commands, but\n    caused issues if the string was actually a command and arguments, or\n    on Windows. Instead, the string must be quoted manually as it should\n    appear on the command line. :issue:`1514`\n\n\nVersion 7.1.1\n-------------\n\nReleased 2020-03-09\n\n-   Fix ``ClickException`` output going to stdout instead of stderr.\n    :issue:`1495`\n\n\nVersion 7.1\n-----------\n\nReleased 2020-03-09\n\n-   Fix PyPI package name, \"click\" is lowercase again.\n-   Fix link in ``unicode_literals`` error message. :pr:`1151`\n-   Add support for colored output on UNIX Jupyter notebooks.\n    :issue:`1185`\n-   Operations that strip ANSI controls will strip the cursor hide/show\n    sequences. :issue:`1216`\n-   Remove unused compat shim for ``bytes``. :pr:`1195`\n-   Expand testing around termui, especially getchar on Windows.\n    :issue:`1116`\n-   Fix output on Windows Python 2.7 built with MSVC 14. :pr:`1342`\n-   Fix ``OSError`` when running in MSYS2. :issue:`1338`\n-   Fix ``OSError`` when redirecting to ``NUL`` stream on Windows.\n    :issue:`1065`\n-   Fix memory leak when parsing Unicode arguments on Windows.\n    :issue:`1136`\n-   Fix error in new AppEngine environments. :issue:`1462`\n-   Always return one of the passed choices for ``click.Choice``\n    :issue:`1277`, :pr:`1318`\n-   Add ``no_args_is_help`` option to ``click.Command``, defaults to\n    False :pr:`1167`\n-   Add ``show_default`` parameter to ``Context`` to enable showing\n    defaults globally. :issue:`1018`\n-   Handle ``env MYPATH=''`` as though the option were not passed.\n    :issue:`1196`\n-   It is once again possible to call ``next(bar)`` on an active\n    progress bar instance. :issue:`1125`\n-   ``open_file`` with ``atomic=True`` retains permissions of existing\n    files and respects the current umask for new files. :issue:`1376`\n-   When using the test ``CliRunner`` with ``mix_stderr=False``, if\n    ``result.stderr`` is empty it will not raise a ``ValueError``.\n    :issue:`1193`\n-   Remove the unused ``mix_stderr`` parameter from\n    ``CliRunner.invoke``. :issue:`1435`\n-   Fix ``TypeError`` raised when using bool flags and specifying\n    ``type=bool``. :issue:`1287`\n-   Newlines in option help text are replaced with spaces before\n    re-wrapping to avoid uneven line breaks. :issue:`834`\n-   ``MissingParameter`` exceptions are printable in the Python\n    interpreter. :issue:`1139`\n-   Fix how default values for file-type options are shown during\n    prompts. :issue:`914`\n-   Fix environment variable automatic generation for commands\n    containing ``-``. :issue:`1253`\n-   Option help text replaces newlines with spaces when rewrapping, but\n    preserves paragraph breaks, fixing multiline formatting.\n    :issue:`834, 1066, 1397`\n-   Option help text that is wrapped adds an extra newline at the end to\n    distinguish it from the next option. :issue:`1075`\n-   Consider ``sensible-editor`` when determining the editor to use for\n    ``click.edit()``. :pr:`1469`\n-   Arguments to system calls such as the executable path passed to\n    ``click.edit`` can contains spaces. :pr:`1470`\n-   Add ZSH completion autoloading and error handling. :issue:`1348`\n-   Add a repr to ``Command``, ``Group``, ``Option``, and ``Argument``,\n    showing the name for friendlier debugging. :issue:`1267`\n-   Completion doesn't consider option names if a value starts with\n    ``-`` after the ``--`` separator. :issue:`1247`\n-   ZSH completion escapes special characters in values. :pr:`1418`\n-   Add completion support for Fish shell. :pr:`1423`\n-   Decoding bytes option values falls back to UTF-8 in more cases.\n    :pr:`1468`\n-   Make the warning about old 2-arg parameter callbacks a deprecation\n    warning, to be removed in 8.0. This has been a warning since Click\n    2.0. :pr:`1492`\n-   Adjust error messages to standardize the types of quotes used so\n    they match error messages from Python.\n\n\nVersion 7.0\n-----------\n\nReleased 2018-09-25\n\n-   Drop support for Python 2.6 and 3.3. :pr:`967, 976`\n-   Wrap ``click.Choice``'s missing message. :issue:`202`, :pr:`1000`\n-   Add native ZSH autocompletion support. :issue:`323`, :pr:`865`\n-   Document that ANSI color info isn't parsed from bytearrays in Python\n    2. :issue:`334`\n-   Document byte-stripping behavior of ``CliRunner``. :issue:`334`,\n    :pr:`1010`\n-   Usage errors now hint at the ``--help`` option. :issue:`393`,\n    :pr:`557`\n-   Implement streaming pager. :issue:`409`, :pr:`889`\n-   Extract bar formatting to its own method. :pr:`414`\n-   Add ``DateTime`` type for converting input in given date time\n    formats. :pr:`423`\n-   ``secho``'s first argument can now be ``None``, like in ``echo``.\n    :pr:`424`\n-   Fixes a ``ZeroDivisionError`` in ``ProgressBar.make_step``, when the\n    arg passed to the first call of ``ProgressBar.update`` is 0.\n    :issue:`447`, :pr:`1012`\n-   Show progressbar only if total execution time is visible. :pr:`487`\n-   Added the ability to hide commands and options from help. :pr:`500`\n-   Document that options can be ``required=True``. :issue:`514`,\n    :pr:`1022`\n-   Non-standalone calls to ``Context.exit`` return the exit code,\n    rather than calling ``sys.exit``. :issue:`667`, :pr:`533, 1098`\n-   ``click.getchar()`` returns Unicode in Python 3 on Windows,\n    consistent with other platforms. :issue:`537, 821, 822, 1088`,\n    :pr:`1108`\n-   Added ``FloatRange`` type. :pr:`538, 553`\n-   Added support for bash completion of ``type=click.Choice`` for\n    ``Options`` and ``Arguments``. :issue:`535`, :pr:`681`\n-   Only allow one positional arg for ``Argument`` parameter\n    declaration. :issue:`568, 574`, :pr:`1014`\n-   Add ``case_sensitive=False`` as an option to Choice. :issue:`569`\n-   ``click.getchar()`` correctly raises ``KeyboardInterrupt`` on \"^C\"\n    and ``EOFError`` on \"^D\" on Linux. :issue:`583`, :pr:`1115`\n-   Fix encoding issue with ``click.getchar(echo=True)`` on Linux.\n    :pr:`1115`\n-   ``param_hint`` in errors now derived from param itself.\n    :issue:`598, 704`, :pr:`709`\n-   Add a test that ensures that when an argument is formatted into a\n    usage error, its metavar is used, not its name. :pr:`612`\n-   Allow setting ``prog_name`` as extra in ``CliRunner.invoke``.\n    :issue:`616`, :pr:`999`\n-   Help text taken from docstrings truncates at the ``\\f`` form feed\n    character, useful for hiding Sphinx-style parameter documentation.\n    :pr:`629, 1091`\n-   ``launch`` now works properly under Cygwin. :pr:`650`\n-   Update progress after iteration. :issue:`651`, :pr:`706`\n-   ``CliRunner.invoke`` now may receive ``args`` as a string\n    representing a Unix shell command. :pr:`664`\n-   Make ``Argument.make_metavar()`` default to type metavar. :pr:`675`\n-   Add documentation for ``ignore_unknown_options``. :pr:`684`\n-   Add bright colors support for ``click.style`` and fix the reset\n    option for parameters ``fg`` and ``bg``. :issue:`703`, :pr:`809`\n-   Add ``show_envvar`` for showing environment variables in help.\n    :pr:`710`\n-   Avoid ``BrokenPipeError`` during interpreter shutdown when stdout or\n    stderr is a closed pipe. :issue:`712`, :pr:`1106`\n-   Document customizing option names. :issue:`725`, :pr:`1016`\n-   Disable ``sys._getframes()`` on Python interpreters that don't\n    support it. :pr:`728`\n-   Fix bug in test runner when calling ``sys.exit`` with ``None``.\n    :pr:`739`\n-   Clarify documentation on command line options. :issue:`741`,\n    :pr:`1003`\n-   Fix crash on Windows console. :issue:`744`\n-   Fix bug that caused bash completion to give improper completions on\n    chained commands. :issue:`754`, :pr:`774`\n-   Added support for dynamic bash completion from a user-supplied\n    callback. :pr:`755`\n-   Added support for bash completions containing spaces. :pr:`773`\n-   Allow autocompletion function to determine whether or not to return\n    completions that start with the incomplete argument. :issue:`790`,\n    :pr:`806`\n-   Fix option naming routine to match documentation and be\n    deterministic. :issue:`793`, :pr:`794`\n-   Fix path validation bug. :issue:`795`, :pr:`1020`\n-   Add test and documentation for ``Option`` naming: functionality.\n    :pr:`799`\n-   Update doc to match arg name for ``path_type``. :pr:`801`\n-   Raw strings added so correct escaping occurs. :pr:`807`\n-   Fix 16k character limit of ``click.echo`` on Windows. :issue:`816`,\n    :pr:`819`\n-   Overcome 64k character limit when writing to binary stream on\n    Windows 7. :issue:`825`, :pr:`830`\n-   Add bool conversion for \"t\" and \"f\". :pr:`842`\n-   ``NoSuchOption`` errors take ``ctx`` so that ``--help`` hint gets\n    printed in error output. :pr:`860`\n-   Fixed the behavior of Click error messages with regards to Unicode\n    on 2.x and 3.x. Message is now always Unicode and the str and\n    Unicode special methods work as you expect on that platform.\n    :issue:`862`\n-   Progress bar now uses stderr by default. :pr:`863`\n-   Add support for auto-completion documentation. :issue:`866`,\n    :pr:`869`\n-   Allow ``CliRunner`` to separate stdout and stderr. :pr:`868`\n-   Fix variable precedence. :issue:`873`, :pr:`874`\n-   Fix invalid escape sequences. :pr:`877`\n-   Fix ``ResourceWarning`` that occurs during some tests. :pr:`878`\n-   When detecting a misconfigured locale, don't fail if the ``locale``\n    command fails. :pr:`880`\n-   Add ``case_sensitive=False`` as an option to ``Choice`` types.\n    :pr:`887`\n-   Force stdout/stderr writable. This works around issues with badly\n    patched standard streams like those from Jupyter. :pr:`918`\n-   Fix completion of subcommand options after last argument\n    :issue:`919`, :pr:`930`\n-   ``_AtomicFile`` now uses the ``realpath`` of the original filename\n    so that changing the working directory does not affect it. :pr:`920`\n-   Fix incorrect completions when defaults are present :issue:`925`,\n    :pr:`930`\n-   Add copy option attrs so that custom classes can be re-used.\n    :issue:`926`, :pr:`994`\n-   \"x\" and \"a\" file modes now use stdout when file is ``\"-\"``.\n    :pr:`929`\n-   Fix missing comma in ``__all__`` list. :pr:`935`\n-   Clarify how parameters are named. :issue:`949`, :pr:`1009`\n-   Stdout is now automatically set to non blocking. :pr:`954`\n-   Do not set options twice. :pr:`962`\n-   Move ``fcntl`` import. :pr:`965`\n-   Fix Google App Engine ``ImportError``. :pr:`995`\n-   Better handling of help text for dynamic default option values.\n    :pr:`996`\n-   Fix ``get_winter_size()`` so it correctly returns ``(0,0)``.\n    :pr:`997`\n-   Add test case checking for custom param type. :pr:`1001`\n-   Allow short width to address cmd formatting. :pr:`1002`\n-   Add details about Python version support. :pr:`1004`\n-   Added deprecation flag to commands. :pr:`1005`\n-   Fixed issues where ``fd`` was undefined. :pr:`1007`\n-   Fix formatting for short help. :pr:`1008`\n-   Document how ``auto_envvar_prefix`` works with command groups.\n    :pr:`1011`\n-   Don't add newlines by default for progress bars. :pr:`1013`\n-   Use Python sorting order for ZSH completions. :issue:`1047`,\n    :pr:`1059`\n-   Document that parameter names are converted to lowercase by default.\n    :pr:`1055`\n-   Subcommands that are named by the function now automatically have\n    the underscore replaced with a dash. If you register a function\n    named ``my_command`` it becomes ``my-command`` in the command line\n    interface.\n-   Hide hidden commands and options from completion. :issue:`1058`,\n    :pr:`1061`\n-   Fix absolute import blocking Click from being vendored into a\n    project on Windows. :issue:`1068`, :pr:`1069`\n-   Fix issue where a lowercase ``auto_envvar_prefix`` would not be\n    converted to uppercase. :pr:`1105`\n\n\nVersion 6.7\n-----------\n\nReleased 2017-01-06\n\n-   Make ``click.progressbar`` work with ``codecs.open`` files.\n    :pr:`637`\n-   Fix bug in bash completion with nested subcommands. :pr:`639`\n-   Fix test runner not saving caller env correctly. :pr:`644`\n-   Fix handling of SIGPIPE. :pr:`62`\n-   Deal with broken Windows environments such as Google App Engine's.\n    :issue:`711`\n\n\nVersion 6.6\n-----------\n\nReleased 2016-04-04\n\n-   Fix bug in ``click.Path`` where it would crash when passed a ``-``.\n    :issue:`551`\n\n\nVersion 6.4\n-----------\n\nReleased 2016-03-24\n\n-   Fix bug in bash completion where click would discard one or more\n    trailing arguments. :issue:`471`\n\n\nVersion 6.3\n-----------\n\nReleased 2016-02-22\n\n-   Fix argument checks for interpreter invoke with ``-m`` and ``-c`` on\n    Windows.\n-   Fixed a bug that cased locale detection to error out on Python 3.\n\n\nVersion 6.2\n-----------\n\nReleased 2015-11-27\n\n-   Correct fix for hidden progress bars.\n\n\nVersion 6.1\n-----------\n\nReleased 2015-11-27\n\n-   Resolved an issue with invisible progress bars no longer rendering.\n-   Disable chain commands with subcommands as they were inherently\n    broken.\n-   Fix ``MissingParameter`` not working without parameters passed.\n\n\nVersion 6.0\n-----------\n\nReleased 2015-11-24, codename \"pow pow\"\n\n-   Optimized the progressbar rendering to not render when it did not\n    actually change.\n-   Explicitly disallow ``nargs=-1`` with a set default.\n-   The context is now closed before it's popped from the stack.\n-   Added support for short aliases for the false flag on toggles.\n-   Click will now attempt to aid you with debugging locale errors\n    better by listing with the help of the OS what locales are\n    available.\n-   Click used to return byte strings on Python 2 in some unit-testing\n    situations. This has been fixed to correctly return unicode strings\n    now.\n-   For Windows users on Python 2, Click will now handle Unicode more\n    correctly handle Unicode coming in from the system. This also has\n    the disappointing side effect that filenames will now be always\n    unicode by default in the ``Path`` type which means that this can\n    introduce small bugs for code not aware of this.\n-   Added a ``type`` parameter to ``Path`` to force a specific string\n    type on the value.\n-   For users running Python on Windows the ``echo`` and ``prompt``\n    functions now work with full unicode functionality in the Python\n    windows console by emulating an output stream. This also applies to\n    getting the virtual output and input streams via\n    ``click.get_text_stream(...)``.\n-   Unittests now always force a certain virtual terminal width.\n-   Added support for allowing dashes to indicate standard streams to\n    the ``Path`` type.\n-   Multi commands in chain mode no longer propagate arguments left over\n    from parsing to the callbacks. It's also now disallowed through an\n    exception when optional arguments are attached to multi commands if\n    chain mode is enabled.\n-   Relaxed restriction that disallowed chained commands to have other\n    chained commands as child commands.\n-   Arguments with positive nargs can now have defaults implemented.\n    Previously this configuration would often result in slightly\n    unexpected values be returned.\n\n\nVersion 5.1\n-----------\n\nReleased 2015-08-17\n\n-   Fix a bug in ``pass_obj`` that would accidentally pass the context\n    too.\n\n\nVersion 5.0\n-----------\n\nReleased 2015-08-16, codename \"tok tok\"\n\n-   Removed various deprecated functionality.\n-   Atomic files now only accept the ``w`` mode.\n-   Change the usage part of help output for very long commands to wrap\n    their arguments onto the next line, indented by 4 spaces.\n-   Fix a bug where return code and error messages were incorrect when\n    using ``CliRunner``.\n-   Added ``get_current_context``.\n-   Added a ``meta`` dictionary to the context which is shared across\n    the linked list of contexts to allow click utilities to place state\n    there.\n-   Introduced ``Context.scope``.\n-   The ``echo`` function is now threadsafe: It calls the ``write``\n    method of the underlying object only once.\n-   ``prompt(hide_input=True)`` now prints a newline on ``^C``.\n-   Click will now warn if users are using ``unicode_literals``.\n-   Click will now ignore the ``PAGER`` environment variable if it is\n    empty or contains only whitespace.\n-   The ``click-contrib`` GitHub organization was created.\n\n\nVersion 4.1\n-----------\n\nReleased 2015-07-14\n\n-   Fix a bug where error messages would include a trailing ``None``\n    string.\n-   Fix a bug where Click would crash on docstrings with trailing\n    newlines.\n-   Support streams with encoding set to ``None`` on Python 3 by barfing\n    with a better error.\n-   Handle ^C in less-pager properly.\n-   Handle return value of ``None`` from ``sys.getfilesystemencoding``\n-   Fix crash when writing to unicode files with ``click.echo``.\n-   Fix type inference with multiple options.\n\n\nVersion 4.0\n-----------\n\nReleased 2015-03-31, codename \"zoom zoom\"\n\n-   Added ``color`` parameters to lots of interfaces that directly or\n    indirectly call into echoing. This previously was always\n    autodetection (with the exception of the ``echo_via_pager``\n    function). Now you can forcefully enable or disable it, overriding\n    the auto detection of Click.\n-   Added an ``UNPROCESSED`` type which does not perform any type\n    changes which simplifies text handling on 2.x / 3.x in some special\n    advanced usecases.\n-   Added ``NoSuchOption`` and ``BadOptionUsage`` exceptions for more\n    generic handling of errors.\n-   Added support for handling of unprocessed options which can be\n    useful in situations where arguments are forwarded to underlying\n    tools.\n-   Added ``max_content_width`` parameter to the context which can be\n    used to change the maximum width of help output. By default Click\n    will not format content for more than 80 characters width.\n-   Added support for writing prompts to stderr.\n-   Fix a bug when showing the default for multiple arguments.\n-   Added support for custom subclasses to ``option`` and ``argument``.\n-   Fix bug in ``clear()`` on Windows when colorama is installed.\n-   Reject ``nargs=-1`` for options properly. Options cannot be\n    variadic.\n-   Fixed an issue with bash completion not working properly for\n    commands with non ASCII characters or dashes.\n-   Added a way to manually update the progressbar.\n-   Changed the formatting of missing arguments. Previously the internal\n    argument name was shown in error messages, now the metavar is shown\n    if passed. In case an automated metavar is selected, it's stripped\n    of extra formatting first.\n\n\nVersion 3.3\n-----------\n\nReleased 2014-09-08\n\n-   Fixed an issue with error reporting on Python 3 for invalid\n    forwarding of commands.\n\n\nVersion 3.2\n-----------\n\nReleased 2014-08-22\n\n-   Added missing ``err`` parameter forwarding to the ``secho``\n    function.\n-   Fixed default parameters not being handled properly by the context\n    invoke method. This is a backwards incompatible change if the\n    function was used improperly.\n-   Removed the ``invoked_subcommands`` attribute largely. It is not\n    possible to provide it to work error free due to how the parsing\n    works so this API has been deprecated.\n-   Restored the functionality of ``invoked_subcommand`` which was\n    broken as a regression in 3.1.\n\n\nVersion 3.1\n-----------\n\nReleased 2014-08-13\n\n-   Fixed a regression that caused contexts of subcommands to be created\n    before the parent command was invoked which was a regression from\n    earlier Click versions.\n\n\nVersion 3.0\n-----------\n\nReleased 2014-08-12, codename \"clonk clonk\"\n\n-   Formatter now no longer attempts to accommodate for terminals\n    smaller than 50 characters. If that happens it just assumes a\n    minimal width.\n-   Added a way to not swallow exceptions in the test system.\n-   Added better support for colors with pagers and ways to override the\n    autodetection.\n-   The CLI runner's result object now has a traceback attached.\n-   Improved automatic short help detection to work better with dots\n    that do not terminate sentences.\n-   When defining options without actual valid option strings now,\n    Click will give an error message instead of silently passing. This\n    should catch situations where users wanted to created arguments\n    instead of options.\n-   Restructured Click internally to support vendoring.\n-   Added support for multi command chaining.\n-   Added support for defaults on options with ``multiple`` and options\n    and arguments with ``nargs != 1``.\n-   Label passed to ``progressbar`` is no longer rendered with\n    whitespace stripped.\n-   Added a way to disable the standalone mode of the ``main`` method on\n    a Click command to be able to handle errors better.\n-   Added support for returning values from command callbacks.\n-   Added simplifications for printing to stderr from ``echo``.\n-   Added result callbacks for groups.\n-   Entering a context multiple times defers the cleanup until the last\n    exit occurs.\n-   Added ``open_file``.\n\n\nVersion 2.6\n-----------\n\nReleased 2014-08-11\n\n-   Fixed an issue where the wrapped streams on Python 3 would be\n    reporting incorrect values for seekable.\n\n\nVersion 2.5\n-----------\n\nReleased 2014-07-28\n\n-   Fixed a bug with text wrapping on Python 3.\n\n\nVersion 2.4\n-----------\n\nReleased 2014-07-04\n\n-   Corrected a bug in the change of the help option in 2.3.\n\n\nVersion 2.3\n-----------\n\nReleased 2014-07-03\n\n-   Fixed an incorrectly formatted help record for count options.\n-   Add support for ansi code stripping on Windows if colorama is not\n    available.\n-   Restored the Click 1.0 handling of the help parameter for certain\n    edge cases.\n\n\nVersion 2.2\n-----------\n\nReleased 2014-06-26\n\n-   Fixed tty detection on PyPy.\n-   Fixed an issue that progress bars were not rendered when the context\n    manager was entered.\n\n\nVersion 2.1\n-----------\n\nReleased 2014-06-14\n\n-   Fixed the :func:`launch` function on windows.\n-   Improved the colorama support on windows to try hard to not screw up\n    the console if the application is interrupted.\n-   Fixed windows terminals incorrectly being reported to be 80\n    characters wide instead of 79\n-   Use colorama win32 bindings if available to get the correct\n    dimensions of a windows terminal.\n-   Fixed an issue with custom function types on Python 3.\n-   Fixed an issue with unknown options being incorrectly reported in\n    error messages.\n\n\nVersion 2.0\n-----------\n\nReleased 2014-06-06, codename \"tap tap tap\"\n\n-   Added support for opening stdin/stdout on Windows in binary mode\n    correctly.\n-   Added support for atomic writes to files by going through a\n    temporary file.\n-   Introduced :exc:`BadParameter` which can be used to easily perform\n    custom validation with the same error messages as in the type\n    system.\n-   Added :func:`progressbar`; a function to show progress bars.\n-   Added :func:`get_app_dir`; a function to calculate the home folder\n    for configs.\n-   Added transparent handling for ANSI codes into the :func:`echo`\n    function through ``colorama``.\n-   Added :func:`clear` function.\n-   Breaking change: parameter callbacks now get the parameter object\n    passed as second argument. There is legacy support for old callbacks\n    which will warn but still execute the script.\n-   Added :func:`style`, :func:`unstyle` and :func:`secho` for ANSI\n    styles.\n-   Added an :func:`edit` function that invokes the default editor.\n-   Added an :func:`launch` function that launches browsers and\n    applications.\n-   Nargs of -1 for arguments can now be forced to be a single item\n    through the required flag. It defaults to not required.\n-   Setting a default for arguments now implicitly makes it non\n    required.\n-   Changed \"yN\" / \"Yn\" to \"y/N\" and \"Y/n\" in confirmation prompts.\n-   Added basic support for bash completion.\n-   Added :func:`getchar` to fetch a single character from the terminal.\n-   Errors now go to stderr as intended.\n-   Fixed various issues with more exotic parameter formats like\n    DOS/Windows style arguments.\n-   Added :func:`pause` which works similar to the Windows ``pause`` cmd\n    built-in but becomes an automatic noop if the application is not run\n    through a terminal.\n-   Added a bit of extra information about missing choice parameters.\n-   Changed how the help function is implemented to allow global\n    overriding of the help option.\n-   Added support for token normalization to implement case insensitive\n    handling.\n-   Added support for providing defaults for context settings.\n\n\nVersion 1.1\n-----------\n\nReleased 2014-05-23\n\n-   Fixed a bug that caused text files in Python 2 to not accept native\n    strings.\n\n\nVersion 1.0\n-----------\n\nReleased 2014-05-21\n\n-   Initial release.\n"
  },
  {
    "path": "LICENSE.txt",
    "content": "Copyright 2014 Pallets\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\n1.  Redistributions of source code must retain the above copyright\n    notice, this list of conditions and the following disclaimer.\n\n2.  Redistributions in binary form must reproduce the above copyright\n    notice, this list of conditions and the following disclaimer in the\n    documentation and/or other materials provided with the distribution.\n\n3.  Neither the name of the copyright holder nor the names of its\n    contributors may be used to endorse or promote products derived from\n    this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\nPARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nHOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED\nTO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\nPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\nLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "README.md",
    "content": "<div align=\"center\"><img src=\"https://raw.githubusercontent.com/pallets/click/refs/heads/stable/docs/_static/click-name.svg\" alt=\"\" height=\"150\"></div>\n\n# Click\n\nClick is a Python package for creating beautiful command line interfaces\nin a composable way with as little code as necessary. It's the \"Command\nLine Interface Creation Kit\". It's highly configurable but comes with\nsensible defaults out of the box.\n\nIt aims to make the process of writing command line tools quick and fun\nwhile also preventing any frustration caused by the inability to\nimplement an intended CLI API.\n\nClick in three points:\n\n-   Arbitrary nesting of commands\n-   Automatic help page generation\n-   Supports lazy loading of subcommands at runtime\n\n\n## A Simple Example\n\n```python\nimport click\n\n@click.command()\n@click.option(\"--count\", default=1, help=\"Number of greetings.\")\n@click.option(\"--name\", prompt=\"Your name\", help=\"The person to greet.\")\ndef hello(count, name):\n    \"\"\"Simple program that greets NAME for a total of COUNT times.\"\"\"\n    for _ in range(count):\n        click.echo(f\"Hello, {name}!\")\n\nif __name__ == '__main__':\n    hello()\n```\n\n```\n$ python hello.py --count=3\nYour name: Click\nHello, Click!\nHello, Click!\nHello, Click!\n```\n\n\n## Donate\n\nThe Pallets organization develops and supports Click and other popular\npackages. In order to grow the community of contributors and users, and\nallow the maintainers to devote more time to the projects, [please\ndonate today][].\n\n[please donate today]: https://palletsprojects.com/donate\n\n## Contributing\n\nSee our [detailed contributing documentation][contrib] for many ways to\ncontribute, including reporting issues, requesting features, asking or answering\nquestions, and making PRs.\n\n[contrib]: https://palletsprojects.com/contributing/\n"
  },
  {
    "path": "docs/advanced.md",
    "content": "# Advanced Patterns\n\n```{currentmodule} click\n```\n\nIn addition to common functionality, Click offers some advanced features.\n\n```{contents}\n:depth: 1\n:local: true\n```\n\n## Callbacks and Eager Options\n\nSometimes, you want a parameter to completely change the execution flow.\nFor instance, this is the case when you want to have a `--version`\nparameter that prints out the version and then exits the application.\n\nNote: an actual implementation of a `--version` parameter that is\nreusable is available in Click as {func}`click.version_option`. The code\nhere is merely an example of how to implement such a flag.\n\nIn such cases, you need two concepts: eager parameters and a callback. An\neager parameter is a parameter handled before others, and a\ncallback is what executes after the parameter is handled. The eagerness\nis necessary so that an earlier required parameter does not produce an\nerror message. For instance, if `--version` was not eager and a\nparameter `--foo` was required and defined before, you would need to\nspecify it for `--version` to work. For more information, see\n{ref}`callback-evaluation-order`.\n\nA callback is a function invoked with three parameters: the\ncurrent {class}`Context`, the current {class}`Parameter`, and the value.\nThe context provides some useful features such as quitting the\napplication and gives access to other already processed parameters.\n\nHere's an example for a `--version` flag:\n\n```{eval-rst}\n.. click:example::\n\n    def print_version(ctx, param, value):\n        if not value or ctx.resilient_parsing:\n            return\n        click.echo('Version 1.0')\n        ctx.exit()\n\n    @click.command()\n    @click.option('--version', is_flag=True, callback=print_version,\n                  expose_value=False, is_eager=True)\n    def hello():\n        click.echo('Hello World!')\n\n\nWhat it looks like:\n\n\n.. click:run::\n\n    invoke(hello)\n    invoke(hello, args=['--version'])\n```\nThe `expose_value` parameter prevents the pretty pointless `version`\nparameter from being passed to the callback. If that was not specified, a\nboolean would be passed to the `hello` script. The `resilient_parsing`\nflag is applied to the context if Click wants to parse the command line\nwithout any destructive behavior that would change the execution flow. In\nthis case, because we would exit the program, we instead do nothing.\n\n## Callbacks for Validation\n\n```{versionchanged} 2.0\n```\n\nIf you want to apply custom validation logic, you can do this in the\nparameter callbacks. These callbacks can both modify values and\nraise errors if the validation does not work. The callback runs after\ntype conversion. It is called for all sources, including prompts.\n\nIn Click 1.0, you can only raise the {exc}`UsageError` but starting with\nClick 2.0, you can also raise the {exc}`BadParameter` error, which has the\nadded advantage that it will automatically format the error message to\nalso contain the parameter name.\n\n```{eval-rst}\n.. click:example::\n\n    def validate_rolls(ctx, param, value):\n        if isinstance(value, tuple):\n            return value\n\n        try:\n            rolls, _, dice = value.partition(\"d\")\n            return int(dice), int(rolls)\n        except ValueError:\n            raise click.BadParameter(\"format must be 'NdM'\")\n\n    @click.command()\n    @click.option(\n        \"--rolls\", type=click.UNPROCESSED, callback=validate_rolls,\n        default=\"1d6\", prompt=True,\n    )\n    def roll(rolls):\n        sides, times = rolls\n        click.echo(f\"Rolling a {sides}-sided dice {times} time(s)\")\n\n.. click:run::\n\n    invoke(roll, args=[\"--rolls=42\"])\n    println()\n    invoke(roll, args=[\"--rolls=2d12\"])\n    println()\n    invoke(roll, input=[\"42\", \"2d12\"])\n```\n\n## Parameter Modifications\n\nParameters (options and arguments) are forwarded to the command callbacks,\nas you have seen. One common way to prevent a parameter from being passed\nto the callback is the `expose_value` argument to a parameter which hides\nthe parameter entirely. The way this works is that the {class}`Context`\nobject has a {attr}`~Context.params` attribute which is a dictionary of\nall parameters. Whatever is in that dictionary is being passed to the\ncallbacks.\n\nThis can be used to make up additional parameters. Generally, this pattern\nis not recommended, but in some cases it can be useful. At the very least,\nit's good to know that the system works this way.\n\n```{eval-rst}\n.. click:example::\n\n    import urllib\n\n    def open_url(ctx, param, value):\n        if value is not None:\n            ctx.params['fp'] = urllib.urlopen(value)\n            return value\n\n    @click.command()\n    @click.option('--url', callback=open_url)\n    def cli(url, fp=None):\n        if fp is not None:\n            click.echo(f\"{url}: {fp.code}\")\n\nIn this case the callback returns the URL unchanged but also passes a\nsecond `fp` value to the callback. What's more recommended is to pass\nthe information in a wrapper, however:\n\n.. click:example::\n\n    import urllib\n\n    class URL(object):\n\n        def __init__(self, url, fp):\n            self.url = url\n            self.fp = fp\n\n    def open_url(ctx, param, value):\n        if value is not None:\n            return URL(value, urllib.urlopen(value))\n\n    @click.command()\n    @click.option('--url', callback=open_url)\n    def cli(url):\n        if url is not None:\n            click.echo(f\"{url.url}: {url.fp.code}\")\n\n```\n\n## Token Normalization\n\n```{versionadded} 2.0\n```\n\nStarting with Click 2.0, it's possible to provide a function used\nfor normalizing tokens. Tokens are option names, choice values, or command\nvalues. This can be used to implement case-insensitive options, for\ninstance.\n\nTo use this feature, the context needs to be passed a function that\nperforms the normalization of the token. Such as a function that converts the\ntoken to lowercase:\n\n```{eval-rst}\n.. click:example::\n\n    CONTEXT_SETTINGS = dict(token_normalize_func=lambda x: x.lower())\n\n    @click.command(context_settings=CONTEXT_SETTINGS)\n    @click.option('--name', default='Pete')\n    def cli(name):\n        click.echo(f\"Name: {name}\")\n\n# And how it works on the command line:\n\n.. click:run::\n\n    invoke(cli, prog_name='cli', args=['--NAME=Pete'])\n```\n\n## Invoking Other Commands\n\nSometimes, it might be interesting to invoke one command from another\ncommand. This is a pattern generally discouraged with Click, but\npossible nonetheless. For this, you can use the {func}`Context.invoke`\nor {func}`Context.forward` methods.\n\nThey work similarly, but the difference is that {func}`Context.invoke` merely\ninvokes another command with the arguments you provide as a caller,\nwhereas {func}`Context.forward` fills in the arguments from the current\ncommand. Both accept the command as the first argument, and everything else\nis passed onwards as you would expect.\n\nExample:\n\n```{eval-rst}\n.. click:example::\n\n    cli = click.Group()\n\n    @cli.command()\n    @click.option('--count', default=1)\n    def test(count):\n        click.echo(f'Count: {count}')\n\n    @cli.command()\n    @click.option('--count', default=1)\n    @click.pass_context\n    def dist(ctx, count):\n        ctx.forward(test)\n        ctx.invoke(test, count=42)\n\nAnd what it looks like:\n\n.. click:run::\n\n    invoke(cli, prog_name='cli', args=['dist'])\n```\n\n(forwarding-unknown-options)=\n\n## Forwarding Unknown Options\n\nIn some situations, it is interesting to be able to accept all unknown\noptions for further manual processing. Click can generally do that as of\nClick 4.0, but it has some limitations that lie in the nature of the\nproblem. The support for this is provided through a parser flag called\n`ignore_unknown_options` which will instruct the parser to collect all\nunknown options and to put them to the leftover argument instead of\ntriggering a parsing error.\n\nThis can generally be activated in two different ways:\n\n1. It can be enabled on custom {class}`Command` subclasses by changing\n   the {attr}`~Command.ignore_unknown_options` attribute.\n2. It can be enabled by changing the attribute of the same name on the\n   context class ({attr}`Context.ignore_unknown_options`). This is best\n   changed through the `context_settings` dictionary on the command.\n\nFor most situations, the easiest solution is the second. Once the behavior\nis changed, something needs to pick up those leftover options (which at\nthis point are considered arguments). For this again, you have two\noptions:\n\n1. You can use {func}`pass_context` to get the context passed. This will\n   only work if in addition to {attr}`~Context.ignore_unknown_options`\n   you also set {attr}`~Context.allow_extra_args` as otherwise the\n   command will abort with an error that there are leftover arguments.\n   If you go with this solution, the extra arguments will be collected in\n   {attr}`Context.args`.\n2. You can attach an {func}`argument` with `nargs` set to `-1` which\n   will eat up all leftover arguments. In this case it's recommended to\n   set the `type` to {data}`UNPROCESSED` to avoid any string processing\n   on those arguments as otherwise they are forced into Unicode strings\n   automatically which is often not what you want.\n\nIn the end, the result looks something like this:\n\n```{eval-rst}\n.. click:example::\n\n    import sys\n    from subprocess import call\n\n    @click.command(context_settings=dict(\n        ignore_unknown_options=True,\n    ))\n    @click.option('-v', '--verbose', is_flag=True, help='Enables verbose mode')\n    @click.argument('timeit_args', nargs=-1, type=click.UNPROCESSED)\n    def cli(verbose, timeit_args):\n        \"\"\"A fake wrapper around Python's timeit.\"\"\"\n        cmdline = ['echo', 'python', '-mtimeit'] + list(timeit_args)\n        if verbose:\n            click.echo(f\"Invoking: {' '.join(cmdline)}\")\n        call(cmdline)\n\n\nAnd the final output resembles the following:\n\n.. click:run::\n\n    invoke(cli, prog_name='cli', args=['--help'])\n    println()\n    invoke(cli, prog_name='cli', args=['-n', '100', 'a = 1; b = 2; a * b'])\n    println()\n    invoke(cli, prog_name='cli', args=['-v', 'a = 1; b = 2; a * b'])\n```\n\nAs you can see, Click handles the verbosity flag and everything else\nends up in the `timeit_args` variable for further processing, which then\nfor instance, allows invoking a subprocess. There are a few things that\nare important to know about how this ignoring of unhandled flag happens:\n\n- Unknown long options are generally ignored and not processed at all.\n  So for instance if `--foo=bar` or `--foo bar` are passed they\n  generally end up like that. Note that because the parser cannot know\n  if an option will accept an argument or not, the `bar` part might be\n  handled as an argument.\n- Unknown short options might be partially handled and reassembled if\n  necessary. For instance in the above example there is an option\n  called `-v` which enables verbose mode. If the command would be\n  ignored with `-va` then the `-v` part would be handled by Click\n  (as it is known) and `-a` would end up in the leftover parameters\n  for further processing.\n- Depending on what you plan on doing you might have some success by\n  disabling interspersed arguments\n  ({attr}`~Context.allow_interspersed_args`) which instructs the parser\n  to not allow arguments and options to be mixed. Depending on your\n   situation, this might improve your results.\n\nGenerally, combining the handling of options and arguments from your own\ncommands with those from another application is discouraged, and you\nshould avoid it if possible. It's a much better idea to have\neverything below a subcommand be forwarded to another application than to\nhandle some arguments yourself.\n\n## Managing Resources\n\nIt can be useful to open a resource in a group, to be made available to\nsubcommands. Many types of resources need to be closed or otherwise\ncleaned up after use. The standard way to do this in Python is by using\na context manager with the `with` statement.\n\nFor example, the `Repo` class from {doc}`complex` might actually be\ndefined as a context manager:\n\n```python\nclass Repo:\n    def __init__(self, home=None):\n        self.home = os.path.abspath(home or \".\")\n        self.db = None\n\n    def __enter__(self):\n        path = os.path.join(self.home, \"repo.db\")\n        self.db = open_database(path)\n        return self\n\n    def __exit__(self, exc_type, exc_value, tb):\n        self.db.close()\n```\n\nOrdinarily, it would be used with the `with` statement:\n\n```python\nwith Repo() as repo:\n    repo.db.query(...)\n```\n\nHowever, a `with` block in a group would exit and close the database\nbefore it could be used by a subcommand.\n\nInstead, use the context's {meth}`~click.Context.with_resource` method\nto enter the context manager and return the resource. When the group and\nany subcommands finish, the context's resources are cleaned up.\n\n```python\n@click.group()\n@click.option(\"--repo-home\", default=\".repo\")\n@click.pass_context\ndef cli(ctx, repo_home):\n    ctx.obj = ctx.with_resource(Repo(repo_home))\n\n@cli.command()\n@click.pass_obj\ndef log(obj):\n    # obj is the repo opened in the cli group\n    for entry in obj.db.query(...):\n        click.echo(entry)\n```\n\nIf the resource isn't a context manager, usually it can be wrapped in\none using something from {mod}`contextlib`. If that's not possible, use\nthe context's {meth}`~click.Context.call_on_close` method to register a\ncleanup function.\n\n```python\n@click.group()\n@click.option(\"--name\", default=\"repo.db\")\n@click.pass_context\ndef cli(ctx, repo_home):\n    ctx.obj = db = open_db(repo_home)\n\n    @ctx.call_on_close\n    def close_db():\n        db.record_use()\n        db.save()\n        db.close()\n```\n\n```{versionchanged} 8.2\n`Context.call_on_close` and context managers registered via `Context.with_resource`\nwill be closed when the CLI exits. These were previously not called on exit.\n```\n"
  },
  {
    "path": "docs/api.md",
    "content": "# API\n\n```{currentmodule} click\n```\n\nThis part of the documentation lists the full API reference of all public\nclasses and functions.\n\n```{contents}\n:depth: 1\n:local: true\n```\n\n## Decorators\n\n```{eval-rst}\n.. autofunction:: command\n```\n\n```{eval-rst}\n.. autofunction:: group\n```\n\n```{eval-rst}\n.. autofunction:: argument\n```\n\n```{eval-rst}\n.. autofunction:: option\n```\n\n```{eval-rst}\n.. autofunction:: password_option\n```\n\n```{eval-rst}\n.. autofunction:: confirmation_option\n```\n\n```{eval-rst}\n.. autofunction:: version_option\n```\n\n```{eval-rst}\n.. autofunction:: help_option\n```\n\n```{eval-rst}\n.. autofunction:: pass_context\n```\n\n```{eval-rst}\n.. autofunction:: pass_obj\n```\n\n```{eval-rst}\n.. autofunction:: make_pass_decorator\n```\n\n```{eval-rst}\n.. autofunction:: click.decorators.pass_meta_key\n\n```\n\n## Utilities\n\n```{eval-rst}\n.. autofunction:: echo\n```\n\n```{eval-rst}\n.. autofunction:: echo_via_pager\n```\n\n```{eval-rst}\n.. autofunction:: prompt\n```\n\n```{eval-rst}\n.. autofunction:: confirm\n```\n\n```{eval-rst}\n.. autofunction:: progressbar\n```\n\n```{eval-rst}\n.. autofunction:: clear\n```\n\n```{eval-rst}\n.. autofunction:: style\n```\n\n```{eval-rst}\n.. autofunction:: unstyle\n```\n\n```{eval-rst}\n.. autofunction:: secho\n```\n\n```{eval-rst}\n.. autofunction:: edit\n```\n\n```{eval-rst}\n.. autofunction:: launch\n```\n\n```{eval-rst}\n.. autofunction:: getchar\n```\n\n```{eval-rst}\n.. autofunction:: pause\n```\n\n```{eval-rst}\n.. autofunction:: get_binary_stream\n```\n\n```{eval-rst}\n.. autofunction:: get_text_stream\n```\n\n```{eval-rst}\n.. autofunction:: open_file\n```\n\n```{eval-rst}\n.. autofunction:: get_app_dir\n```\n\n```{eval-rst}\n.. autofunction:: format_filename\n```\n\n## Commands\n\n```{eval-rst}\n.. autoclass:: BaseCommand\n   :members:\n```\n\n```{eval-rst}\n.. autoclass:: Command\n   :members:\n```\n\n```{eval-rst}\n.. autoclass:: MultiCommand\n   :members:\n```\n\n```{eval-rst}\n.. autoclass:: Group\n   :members:\n```\n\n```{eval-rst}\n.. autoclass:: CommandCollection\n   :members:\n```\n\n## Parameters\n\n```{eval-rst}\n.. autoclass:: Parameter\n   :members:\n```\n\n```{eval-rst}\n.. autoclass:: Option\n```\n\n```{eval-rst}\n.. autoclass:: Argument\n```\n\n## Context\n\n```{eval-rst}\n.. autoclass:: Context\n   :members:\n```\n\n```{eval-rst}\n.. autofunction:: get_current_context\n```\n\n```{eval-rst}\n.. autoclass:: click.core.ParameterSource\n    :members:\n    :member-order: bysource\n```\n\n(click-api-types)=\n\n## Types\n\n```{eval-rst}\n.. autodata:: STRING\n```\n\n```{eval-rst}\n.. autodata:: INT\n```\n\n```{eval-rst}\n.. autodata:: FLOAT\n```\n\n```{eval-rst}\n.. autodata:: BOOL\n```\n\n```{eval-rst}\n.. autodata:: UUID\n```\n\n```{eval-rst}\n.. autodata:: UNPROCESSED\n```\n\n```{eval-rst}\n.. autoclass:: File\n```\n\n```{eval-rst}\n.. autoclass:: Path\n```\n\n```{eval-rst}\n.. autoclass:: Choice\n   :members:\n```\n\n```{eval-rst}\n.. autoclass:: IntRange\n```\n\n```{eval-rst}\n.. autoclass:: FloatRange\n```\n\n```{eval-rst}\n.. autoclass:: DateTime\n```\n\n```{eval-rst}\n.. autoclass:: Tuple\n```\n\n```{eval-rst}\n.. autoclass:: ParamType\n   :members:\n```\n\n## Exceptions\n\n```{eval-rst}\n.. autoexception:: ClickException\n```\n\n```{eval-rst}\n.. autoexception:: Abort\n```\n\n```{eval-rst}\n.. autoexception:: UsageError\n```\n\n```{eval-rst}\n.. autoexception:: BadParameter\n```\n\n```{eval-rst}\n.. autoexception:: FileError\n```\n\n```{eval-rst}\n.. autoexception:: NoSuchOption\n```\n\n```{eval-rst}\n.. autoexception:: BadOptionUsage\n```\n\n```{eval-rst}\n.. autoexception:: BadArgumentUsage\n```\n\n## Formatting\n\n```{eval-rst}\n.. autoclass:: HelpFormatter\n   :members:\n```\n\n```{eval-rst}\n.. autofunction:: wrap_text\n```\n\n## Parsing\n\n```{eval-rst}\n.. autoclass:: OptionParser\n   :members:\n\n```\n\n## Shell Completion\n\nSee {doc}`/shell-completion` for information about enabling and\ncustomizing Click's shell completion system.\n\n```{eval-rst}\n.. currentmodule:: click.shell_completion\n```\n\n```{eval-rst}\n.. autoclass:: CompletionItem\n```\n\n```{eval-rst}\n.. autoclass:: ShellComplete\n    :members:\n    :member-order: bysource\n```\n\n```{eval-rst}\n.. autofunction:: add_completion_class\n\n```\n\n(testing)=\n\n## Testing\n\n```{eval-rst}\n.. currentmodule:: click.testing\n```\n\n```{eval-rst}\n.. autoclass:: CliRunner\n   :members:\n```\n\n```{eval-rst}\n.. autoclass:: Result\n   :members:\n```\n"
  },
  {
    "path": "docs/arguments.rst",
    "content": ".. _arguments:\n\nArguments\n=========\n\n.. currentmodule:: click\n\nArguments are:\n\n*   Are positional in nature.\n*   Similar to a limited version of :ref:`options <options>` that can take an arbitrary number of inputs\n*   :ref:`Documented manually <documenting-arguments>`.\n\nUseful and often used kwargs are:\n\n*   ``default``: Passes a default.\n*   ``nargs``: Sets the number of arguments. Set to -1 to take an arbitrary number.\n\nBasic Arguments\n---------------\n\nA minimal :class:`click.Argument` solely takes one string argument: the name of the argument. This will assume the argument is required, has no default, and is of the type ``str``.\n\nExample:\n\n.. click:example::\n\n    @click.command()\n    @click.argument('filename')\n    def touch(filename: str):\n        \"\"\"Print FILENAME.\"\"\"\n        click.echo(filename)\n\nAnd from the command line:\n\n.. click:run::\n\n    invoke(touch, args=['foo.txt'])\n\n\nAn argument may be assigned a :ref:`parameter type <parameter-types>`. If no type is provided, the type of the default value is used. If no default value is provided, the type is assumed to be :data:`STRING`.\n\n.. admonition:: Note on Required Arguments\n\n   It is possible to make an argument required by setting ``required=True``.  It is not recommended since we think command line tools should gracefully degrade into becoming no ops.  We think this because command line tools are often invoked with wildcard inputs and they should not error out if the wildcard is empty.\n\nMultiple Arguments\n-----------------------------------\n\nTo set the number of argument use the ``nargs`` kwarg. It can be set to any positive integer and -1. Setting it to -1, makes the number of arguments arbitrary (which is called variadic) and can only be used once. The arguments are then packed as a tuple and passed to the function.\n\n.. click:example::\n\n    @click.command()\n    @click.argument('src', nargs=1)\n    @click.argument('dsts', nargs=-1)\n    def copy(src: str, dsts: tuple[str, ...]):\n        \"\"\"Move file SRC to DST.\"\"\"\n        for destination in dsts:\n            click.echo(f\"Copy {src} to folder {destination}\")\n\nAnd from the command line:\n\n.. click:run::\n\n    invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])\n\n.. admonition:: Note on Handling Files\n\n    This is not how you should handle files and files paths. This merely used as a simple example. See :ref:`handling-files` to learn more about how to handle files in parameters.\n\nArgument Escape Sequences\n---------------------------\n\nIf you want to process arguments that look like options, like a file named ``-foo.txt`` or ``--foo.txt`` , you must pass the ``--`` separator first. After you pass the ``--``, you may only pass arguments. This is a common feature for POSIX command line tools.\n\nExample usage:\n\n.. click:example::\n\n    @click.command()\n    @click.argument('files', nargs=-1, type=click.Path())\n    def touch(files):\n        \"\"\"Print all FILES file names.\"\"\"\n        for filename in files:\n            click.echo(filename)\n\nAnd from the command line:\n\n.. click:run::\n\n    invoke(touch, ['--', '-foo.txt', 'bar.txt'])\n\nIf you don't like the ``--`` marker, you can set ignore_unknown_options to True to avoid checking unknown options:\n\n.. click:example::\n\n    @click.command(context_settings={\"ignore_unknown_options\": True})\n    @click.argument('files', nargs=-1, type=click.Path())\n    def touch(files):\n        \"\"\"Print all FILES file names.\"\"\"\n        for filename in files:\n            click.echo(filename)\n\nAnd from the command line:\n\n.. click:run::\n\n    invoke(touch, ['-foo.txt', 'bar.txt'])\n\n\n.. _environment-variables:\n\nEnvironment Variables\n---------------------\n\nArguments can use environment variables. To do so, pass the name(s) of the environment variable(s) via `envvar` in ``click.argument``.\n\nChecking one environment variable:\n\n.. click:example::\n\n    @click.command()\n    @click.argument('src', envvar='SRC', type=click.File('r'))\n    def echo(src):\n        \"\"\"Print value of SRC environment variable.\"\"\"\n        click.echo(src.read())\n\nAnd from the command line:\n\n.. click:run::\n\n    with isolated_filesystem():\n        # Writing the file in the filesystem.\n        with open('hello.txt', 'w') as f:\n            f.write('Hello World!')\n        invoke(echo, env={'SRC': 'hello.txt'})\n\n\nChecking multiple environment variables:\n\n.. click:example::\n\n    @click.command()\n    @click.argument('src', envvar=['SRC', 'SRC_2'], type=click.File('r'))\n    def echo(src):\n        \"\"\"Print value of SRC environment variable.\"\"\"\n        click.echo(src.read())\n\nAnd from the command line:\n\n.. click:run::\n\n    with isolated_filesystem():\n        # Writing the file in the filesystem.\n        with open('hello.txt', 'w') as f:\n            f.write('Hello World from second variable!')\n        invoke(echo, env={'SRC_2': 'hello.txt'})\n"
  },
  {
    "path": "docs/changes.rst",
    "content": "Changes\n=======\n\n.. include:: ../CHANGES.rst\n"
  },
  {
    "path": "docs/click-concepts.rst",
    "content": "Click Concepts\n================\n\nThis section covers concepts about Click's design.\n\n.. contents::\n    :depth: 1\n    :local:\n\n.. _callback-evaluation-order:\n\nCallback Evaluation Order\n-------------------------\n\nClick works a bit differently than some other command line parsers in that\nit attempts to reconcile the order of arguments as defined by the\nprogrammer with the order of arguments as defined by the user before\ninvoking any callbacks.\n\nThis is an important concept to understand when porting complex\npatterns to Click from optparse or other systems.  A parameter\ncallback invocation in optparse happens as part of the parsing step,\nwhereas a callback invocation in Click happens after the parsing.\n\nThe main difference is that in optparse, callbacks are invoked with the raw\nvalue as it happens, whereas a callback in Click is invoked after the\nvalue has been fully converted.\n\nGenerally, the order of invocation is driven by the order in which the user\nprovides the arguments to the script; if there is an option called ``--foo``\nand an option called ``--bar`` and the user calls it as ``--bar\n--foo``, then the callback for ``bar`` will fire before the one for ``foo``.\n\nThere are three exceptions to this rule which are important to know:\n\nEagerness:\n    An option can be set to be \"eager\".  All eager parameters are\n    evaluated before all non-eager parameters, but again in the order as\n    they were provided on the command line by the user.\n\n    This is important for parameters that execute and exit like ``--help``\n    and ``--version``.  Both are eager parameters, but whatever parameter\n    comes first on the command line will win and exit the program.\n\nRepeated parameters:\n    If an option or argument is split up on the command line into multiple\n    places because it is repeated -- for instance, ``--exclude foo --include\n    baz --exclude bar`` -- the callback will fire based on the position of\n    the first option.  In this case, the callback will fire for\n    ``exclude`` and it will be passed both options (``foo`` and\n    ``bar``), then the callback for ``include`` will fire with ``baz``\n    only.\n\n    Note that even if a parameter does not allow multiple versions, Click\n    will still accept the position of the first, but it will ignore every\n    value except the last.  The reason for this is to allow composability\n    through shell aliases that set defaults.\n\nMissing parameters:\n    If a parameter is not defined on the command line, the callback will\n    still fire.  This is different from how it works in optparse where\n    undefined values do not fire the callback.  Missing parameters fire\n    their callbacks at the very end which makes it possible for them to\n    default to values from a parameter that came before.\n\nMost of the time you do not need to be concerned about any of this,\nbut it is important to know how it works for some advanced cases.\n"
  },
  {
    "path": "docs/command-line-reference.md",
    "content": "# General Command Line Topics\n\n```{currentmodule} click\n```\n\n```{contents}\n---\ndepth: 1\nlocal: true\n---\n```\n\n(exit-codes)=\n## Exit Codes\n\nWhen a command is executed from the command line, then an exit code is return. The exit code, also called exit status or exit status code, is a positive integer that tells you whether the command executed with or without errors.\n\n| Exit Code | Meaning                                         |\n|-----------|-------------------------------------------------|\n| 0         | Success — the command completed without errors. |\n| > 0       | Executed with errors                            |\n\nExit codes greater than zero mean are specific to the Operating System, Shell, and/or command.\n\nTo access the exit code, execute the command, then do the following depending:\n\n```{eval-rst}\n.. tabs::\n\n    .. group-tab:: Powershell\n\n        .. code-block:: powershell\n\n           > echo $LASTEXITCODE\n\n    .. group-tab:: Bash\n\n        .. code-block:: bash\n\n            $ echo $?\n\n    .. group-tab:: Command Prompt\n\n\n        .. code-block:: text\n\n            > echo %ERRORLEVEL%\n```\n\nFor Click specific behavior on exit codes, see {ref}`exception-handling-exit-codes`.\n"
  },
  {
    "path": "docs/commands-and-groups.rst",
    "content": "Basic Commands, Groups, Context\n================================\n\n.. currentmodule:: click\n\nCommands and Groups are the building blocks for Click applications. :class:`Command` wraps a function to make it into a cli command. :class:`Group` wraps Commands and Groups to make them into applications. :class:`Context` is how groups and commands communicate.\n\n.. contents::\n   :depth: 2\n   :local:\n\nCommands\n--------------------\n\nBasic Command Example\n^^^^^^^^^^^^^^^^^^^^^^^\nA simple command decorator takes no arguments.\n\n.. click:example::\n    @click.command()\n    @click.option('--count', default=1)\n    def hello(count):\n        for x in range(count):\n            click.echo(\"Hello!\")\n\n.. click:run::\n    invoke(hello, args=['--count', '2',])\n\nRenaming Commands\n^^^^^^^^^^^^^^^^^^^\nBy default the command is the function name with underscores replaced by dashes. To change this pass the  desired name into the first positional argument.\n\n.. click:example::\n    @click.command('say-hello')\n    @click.option('--count', default=1)\n    def hello(count):\n        for x in range(count):\n            click.echo(\"Hello!\")\n\n.. click:run::\n    invoke(hello, args=['--count', '2',])\n\nDeprecating Commands\n^^^^^^^^^^^^^^^^^^^^^^\nTo mark a command as deprecated pass in ``deprecated=True``\n\n.. click:example::\n    @click.command('say-hello', deprecated=True)\n    @click.option('--count', default=1)\n    def hello(count):\n        for x in range(count):\n            click.echo(\"Hello!\")\n\n.. click:run::\n    invoke(hello, args=['--count', '2',])\n\nGroups\n------------\n\nBasic Group Example\n^^^^^^^^^^^^^^^^^^^^^\nA group wraps one or more commands. After being wrapped, the commands are nested under that group. You can see that on the help pages and in the execution. By default, invoking the group with no command shows the help page.\n\n.. click:example::\n    @click.group()\n    def greeting():\n        click.echo('Starting greeting ...')\n\n    @greeting.command('say-hello')\n    @click.option('--count', default=1)\n    def hello(count):\n        for x in range(count):\n            click.echo(\"Hello!\")\n\nAt the top level:\n\n.. click:run::\n\n    invoke(greeting)\n\nAt the command level:\n\n.. click:run::\n\n    invoke(greeting, args=['say-hello'])\n    invoke(greeting, args=['say-hello', '--help'])\n\nAs you can see from the above example, the function wrapped by the group decorator executes unless it is interrupted (for example by calling the help).\n\nRenaming Groups\n^^^^^^^^^^^^^^^^^\nTo have a name other than the decorated function name as the group name, pass it in as the first positional argument.\n\n.. click:example::\n    @click.group('greet-someone')\n    def greeting():\n        click.echo('Starting greeting ...')\n\n    @greeting.command('say-hello')\n    @click.option('--count', default=1)\n    def hello(count):\n        for x in range(count):\n            click.echo(\"Hello!\")\n\n.. click:run::\n\n    invoke(greeting, args=['say-hello'])\n\nGroup Invocation Without Command\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nBy default, if a group is passed without a command, the group is not invoked and a command automatically passes ``--help``. To change this, pass ``invoke_without_command=True`` to the group. The context object also includes information about whether or not the group invocation would go to a command nested under it.\n\n.. click:example::\n\n    @click.group(invoke_without_command=True)\n    @click.pass_context\n    def cli(ctx):\n        if ctx.invoked_subcommand is None:\n            click.echo('I was invoked without subcommand')\n        else:\n            click.echo(f\"I am about to invoke {ctx.invoked_subcommand}\")\n\n    @cli.command()\n    def sync():\n        click.echo('The subcommand')\n\n.. click:run::\n\n    invoke(cli, prog_name='tool', args=[])\n    invoke(cli, prog_name='tool', args=['sync'])\n\n\n\nGroup Separation\n^^^^^^^^^^^^^^^^^^^\nCommand :ref:`parameters` attached to a command belong only to that command.\n\n.. click:example::\n    @click.group()\n    def greeting():\n        pass\n\n    @greeting.command()\n    @click.option('--count', default=1)\n    def hello(count):\n        for x in range(count):\n            click.echo(\"Hello!\")\n\n    @greeting.command()\n    @click.option('--count', default=1)\n    def goodbye(count):\n        for x in range(count):\n            click.echo(\"Goodbye!\")\n\n.. click:run::\n\n    invoke(greeting, args=['hello', '--count', '2'])\n    invoke(greeting, args=['goodbye', '--count', '2'])\n    invoke(greeting)\n\nAdditionally parameters for a given group belong only to that group and not to the commands under it. What this means is that options and arguments for a specific command have to be specified *after* the command name itself, but *before* any other command names.\n\nThis behavior is observable with the ``--help`` option. Suppose we have a group called ``tool`` containing a command called ``sub``.\n\n- ``tool --help`` returns the help for the whole program (listing subcommands).\n- ``tool sub --help`` returns the help for the ``sub`` subcommand.\n- But ``tool --help sub`` treats ``--help`` as an argument for the main program. Click then invokes the callback for ``--help``, which prints the help and aborts the program before click can process the subcommand.\n\nArbitrary Nesting\n^^^^^^^^^^^^^^^^^^^\n:class:`Commands <Command>` are attached to a :class:`Group`. Multiple groups can be attached to another group. Groups containing multiple groups can be attached to a group, and so on. To invoke a command nested under multiple groups, all the groups under which it is nested must be invoked.\n\n.. click:example::\n\n    @click.group()\n    def cli():\n        pass\n\n    # Not @click so that the group is registered now.\n    @cli.group()\n    def session():\n        click.echo('Starting session')\n\n    @session.command()\n    def initdb():\n        click.echo('Initialized the database')\n\n    @session.command()\n    def dropdb():\n        click.echo('Dropped the database')\n\n.. click:run::\n\n    invoke(cli, args=['session', 'initdb'])\n\nLazily Attaching Commands\n^^^^^^^^^^^^^^^^^^^^^^^^^^^\nMost examples so far have attached the commands to a group immediately, but commands may be registered later. This could be used to split commands into multiple Python modules. Regardless of how they are attached, the commands are invoked identically.\n\n.. click:example::\n\n    @click.group()\n    def cli():\n        pass\n\n    @cli.command()\n    def initdb():\n        click.echo('Initialized the database')\n\n    @click.command()\n    def dropdb():\n        click.echo('Dropped the database')\n\n    cli.add_command(dropdb)\n\n.. click:run::\n\n    invoke(cli, args=['initdb'])\n    invoke(cli, args=['dropdb'])\n\nContext Object\n-------------------\nThe :class:`Context` object is how commands and groups communicate.\n\nAuto Envvar Prefix\n^^^^^^^^^^^^^^^^^^^^\nAutomatically built environment variables are supported for options only. To enable this feature, the ``auto_envvar_prefix`` parameter needs to be passed to the script that is invoked.  Each command and parameter is then added as an uppercase underscore-separated variable.  If you have a subcommand\ncalled ``run`` taking an option called ``reload`` and the prefix is ``WEB``, then the variable is ``WEB_RUN_RELOAD``.\n\nExample usage:\n\n.. click:example::\n\n    @click.command()\n    @click.option('--username')\n    def greet(username):\n        click.echo(f'Hello {username}!')\n\n    if __name__ == '__main__':\n        greet(auto_envvar_prefix='GREETER')\n\nAnd from the command line:\n\n.. click:run::\n\n    invoke(greet, env={'GREETER_USERNAME': 'john'},\n           auto_envvar_prefix='GREETER')\n\nWhen using ``auto_envvar_prefix`` with command groups, the command name\nneeds to be included in the environment variable, between the prefix and\nthe parameter name, *i.e.* ``PREFIX_COMMAND_VARIABLE``. If you have a\nsubcommand called ``run-server`` taking an option called ``host`` and\nthe prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.\n\n.. click:example::\n\n   @click.group()\n   @click.option('--debug/--no-debug')\n   def cli(debug):\n       click.echo(f\"Debug mode is {'on' if debug else 'off'}\")\n\n   @cli.command()\n   @click.option('--username')\n   def greet(username):\n       click.echo(f\"Hello {username}!\")\n\n   if __name__ == '__main__':\n       cli(auto_envvar_prefix='GREETER')\n\n.. click:run::\n\n   invoke(cli, args=['greet',],\n          env={'GREETER_GREET_USERNAME': 'John', 'GREETER_DEBUG': 'false'},\n          auto_envvar_prefix='GREETER')\n\nGlobal Context Access\n^^^^^^^^^^^^^^^^^^^^^^\n\n.. versionadded:: 5.0\n\nStarting with Click 5.0 it is possible to access the current context from\nanywhere within the same thread through the use of the\n:func:`get_current_context` function which returns it.  This is primarily\nuseful for accessing the context bound object as well as some flags that\nare stored on it to customize the runtime behavior.  For instance the\n:func:`echo` function does this to infer the default value of the `color`\nflag.\n\nExample usage::\n\n    def get_current_command_name():\n        return click.get_current_context().info_name\n\nIt should be noted that this only works within the current thread.  If you\nspawn additional threads then those threads will not have the ability to\nrefer to the current context.  If you want to give another thread the\nability to refer to this context you need to use the context within the\nthread as a context manager::\n\n    def spawn_thread(ctx, func):\n        def wrapper():\n            with ctx:\n                func()\n        t = threading.Thread(target=wrapper)\n        t.start()\n        return t\n\nNow the thread function can access the context like the main thread would\ndo.  However if you do use this for threading you need to be very careful\nas the vast majority of the context is not thread safe!  You are only\nallowed to read from the context, but not to perform any modifications on\nit.\n\n\nDetecting the Source of a Parameter\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nIn some situations it's helpful to understand whether or not an option\nor parameter came from the command line, the environment, the default\nvalue, or :attr:`Context.default_map`. The\n:meth:`Context.get_parameter_source` method can be used to find this\nout. It will return a member of the :class:`~click.core.ParameterSource`\nenum.\n\n.. click:example::\n\n    @click.command()\n    @click.argument('port', nargs=1, default=8080, envvar=\"PORT\")\n    @click.pass_context\n    def cli(ctx, port):\n        source = ctx.get_parameter_source(\"port\")\n        click.echo(f\"Port came from {source.name}\")\n\n.. click:run::\n\n    invoke(cli, prog_name='cli', args=['8080'])\n    println()\n    invoke(cli, prog_name='cli', args=[], env={\"PORT\": \"8080\"})\n    println()\n    invoke(cli, prog_name='cli', args=[])\n    println()\n"
  },
  {
    "path": "docs/commands.rst",
    "content": "Advanced Groups and Context\n=============================\n\n.. currentmodule:: click\n\nIn addition to the capabilities covered in the previous section, Groups have more advanced capabilities that leverage the Context.\n\n.. contents::\n   :depth: 1\n   :local:\n\nCallback Invocation\n-------------------\n\nFor a regular command, the callback is executed whenever the command runs.\nIf the script is the only command, it will always fire (unless a parameter\ncallback prevents it.  This for instance happens if someone passes\n``--help`` to the script).\n\nFor groups, the situation looks different. In this case, the callback fires\nwhenever a subcommand fires.  What this means in practice is that an outer\ncommand runs when an inner command runs:\n\n.. click:example::\n\n    @click.group()\n    @click.option('--debug/--no-debug', default=False)\n    def cli(debug):\n        click.echo(f\"Debug mode is {'on' if debug else 'off'}\")\n\n    @cli.command()  # @cli, not @click!\n    def sync():\n        click.echo('Syncing')\n\nHere is what this looks like:\n\n.. click:run::\n\n    invoke(cli, prog_name='tool.py')\n    println()\n    invoke(cli, prog_name='tool.py', args=['--debug', 'sync'])\n\nNested Handling and Contexts\n----------------------------\n\nAs you can see from the earlier example, the basic command group accepts a\ndebug argument which is passed to its callback, but not to the sync\ncommand itself.  The sync command only accepts its own arguments.\n\nThis allows tools to act completely independent of each other, but how\ndoes one command talk to a nested one?  The answer to this is the\n:class:`Context`.\n\nEach time a command is invoked, a new context is created and linked with the\nparent context.  Normally, you can't see these contexts, but they are\nthere.  Contexts are passed to parameter callbacks together with the\nvalue automatically.  Commands can also ask for the context to be passed\nby marking themselves with the :func:`pass_context` decorator.  In that\ncase, the context is passed as first argument.\n\nThe context can also carry a program specified object that can be\nused for the program's purposes.  What this means is that you can build a\nscript like this:\n\n.. click:example::\n\n    @click.group()\n    @click.option('--debug/--no-debug', default=False)\n    @click.pass_context\n    def cli(ctx, debug):\n        # ensure that ctx.obj exists and is a dict (in case `cli()` is called\n        # by means other than the `if` block below)\n        ctx.ensure_object(dict)\n\n        ctx.obj['DEBUG'] = debug\n\n    @cli.command()\n    @click.pass_context\n    def sync(ctx):\n        click.echo(f\"Debug is {'on' if ctx.obj['DEBUG'] else 'off'}\")\n\n    if __name__ == '__main__':\n        cli(obj={})\n\nIf the object is provided, each context will pass the object onwards to\nits children, but at any level a context's object can be overridden.  To\nreach to a parent, ``context.parent`` can be used.\n\nIn addition to that, instead of passing an object down, nothing stops the\napplication from modifying global state.  For instance, you could just flip\na global ``DEBUG`` variable and be done with it.\n\nDecorating Commands\n-------------------\n\nAs you have seen in the earlier example, a decorator can change how a\ncommand is invoked.  What actually happens behind the scenes is that\ncallbacks are always invoked through the :meth:`Context.invoke` method\nwhich automatically invokes a command correctly (by either passing the\ncontext or not).\n\nThis is very useful when you want to write custom decorators.  For\ninstance, a common pattern would be to configure an object representing\nstate and then storing it on the context and then to use a custom\ndecorator to find the most recent object of this sort and pass it as first\nargument.\n\nFor instance, the :func:`pass_obj` decorator can be implemented like this:\n\n.. click:example::\n\n    from functools import update_wrapper\n\n    def pass_obj(f):\n        @click.pass_context\n        def new_func(ctx, *args, **kwargs):\n            return ctx.invoke(f, ctx.obj, *args, **kwargs)\n        return update_wrapper(new_func, f)\n\nThe :meth:`Context.invoke` command will automatically invoke the function\nin the correct way, so the function will either be called with ``f(ctx,\nobj)`` or ``f(obj)`` depending on whether or not it itself is decorated with\n:func:`pass_context`.\n\nThis is a very powerful concept that can be used to build very complex\nnested applications; see :ref:`complex-guide` for more information.\n\n.. _command-chaining:\n\nCommand Chaining\n----------------\n\nIt is useful to invoke more than one subcommand in one call. For example,\n``my-app validate build upload`` would invoke ``validate``, then ``build``, then\n``upload``. To implement this, pass ``chain=True`` when creating a group.\n\n.. click:example::\n\n    @click.group(chain=True)\n    def cli():\n        pass\n\n    @cli.command('validate')\n    def validate():\n        click.echo('validate')\n\n    @cli.command('build')\n    def build():\n        click.echo('build')\n\nYou can invoke it like this:\n\n.. click:run::\n\n    invoke(cli, prog_name='my-app', args=['validate', 'build'])\n\nWhen using chaining, there are a few restrictions:\n\n-   Only the last command may use ``nargs=-1`` on an argument, otherwise the\n    parser will not be able to find further commands.\n-   It is not possible to nest groups below a chain group.\n-   On the command line, options must be specified before arguments for each\n    command in the chain.\n-   The :attr:`Context.invoked_subcommand` attribute will be ``'*'`` because the\n    parser doesn't know the full list of commands that will run yet.\n\n.. _command-pipelines:\n\nCommand Pipelines\n------------------\n\nWhen using chaining, a common pattern is to have each command process the\nresult of the previous command.\n\nA straightforward way to do this is to use :func:`make_pass_decorator` to pass\na context object to each command, and store and read the data on that object.\n\n.. click:example::\n\n    pass_ns = click.make_pass_decorator(dict, ensure=True)\n\n    @click.group(chain=True)\n    @click.argument(\"name\")\n    @pass_ns\n    def cli(ns, name):\n        ns[\"name\"] = name\n\n    @cli.command\n    @pass_ns\n    def lower(ns):\n        ns[\"name\"] = ns[\"name\"].lower()\n\n    @cli.command\n    @pass_ns\n    def show(ns):\n        click.echo(ns[\"name\"])\n\n.. click:run::\n\n    invoke(cli, prog_name=\"process\", args=[\"Click\", \"show\", \"lower\", \"show\"])\n\nAnother way to do this is to collect data returned by each command, then process\nit at the end of the chain. Use the group's :meth:`~Group.result_callback`\ndecorator to register a function that is called after the chain is finished. It\nis passed the list of return values as well as any parameters registered on the\ngroup.\n\nA command can return anything, including a function. Here's an example of that,\nwhere each subcommand creates a function that processes the input, then the\nresult callback calls each function. The command takes a file, processes each\nline, then outputs it. If no subcommands are given, it outputs the contents\nof the file unchanged.\n\n.. code-block:: python\n\n    @click.group(chain=True, invoke_without_command=True)\n    @click.argument(\"fin\", type=click.File(\"r\"))\n    def cli(fin):\n        pass\n\n    @cli.result_callback()\n    def process_pipeline(processors, fin):\n        iterator = (x.rstrip(\"\\r\\n\") for x in input)\n\n        for processor in processors:\n            iterator = processor(iterator)\n\n        for item in iterator:\n            click.echo(item)\n\n    @cli.command(\"upper\")\n    def make_uppercase():\n        def processor(iterator):\n            for line in iterator:\n                yield line.upper()\n        return processor\n\n    @cli.command(\"lower\")\n    def make_lowercase():\n        def processor(iterator):\n            for line in iterator:\n                yield line.lower()\n        return processor\n\n    @cli.command(\"strip\")\n    def make_strip():\n        def processor(iterator):\n            for line in iterator:\n                yield line.strip()\n        return processor\n\nThat's a lot in one go, so let's go through it step by step.\n\n1.  The first thing is to make a :func:`group` that is chainable.  In\n    addition to that we also instruct Click to invoke even if no\n    subcommand is defined.  If this would not be done, then invoking an\n    empty pipeline would produce the help page instead of running the\n    result callbacks.\n2.  The next thing we do is to register a result callback on our group.\n    This callback will be invoked with an argument which is the list of\n    all return values of all subcommands and then the same keyword\n    parameters as our group itself.  This means we can access the input\n    file easily there without having to use the context object.\n3.  In this result callback we create an iterator of all the lines in the\n    input file and then pass this iterator through all the returned\n    callbacks from all subcommands and finally we print all lines to\n    stdout.\n\nAfter that point we can register as many subcommands as we want and each\nsubcommand can return a processor function to modify the stream of lines.\n\nOne important thing of note is that Click shuts down the context after\neach callback has been run.  This means that for instance file types\ncannot be accessed in the `processor` functions as the files will already\nbe closed there.  This limitation is unlikely to change because it would\nmake resource handling much more complicated.  For such it's recommended\nto not use the file type and manually open the file through\n:func:`open_file`.\n\nFor a more complex example that also improves upon handling of the pipelines,\nsee the `imagepipe example`_ in the Click repository. It implements a\npipeline based image editing tool that has a nice internal structure.\n\n.. _imagepipe example: https://github.com/pallets/click/tree/main/examples/imagepipe\n\n\nOverriding Defaults\n-------------------\n\nBy default, the default value for a parameter is pulled from the\n``default`` flag that is provided when it's defined, but that's not the\nonly place defaults can be loaded from.  The other place is the\n:attr:`Context.default_map` (a dictionary) on the context.  This allows\ndefaults to be loaded from a configuration file to override the regular\ndefaults.\n\nThis is useful if you plug in some commands from another package but\nyou're not satisfied with the defaults.\n\nThe default map can be nested arbitrarily for each subcommand:\n\n.. code-block:: python\n\n    default_map = {\n        \"debug\": True,  # default for a top level option\n        \"runserver\": {\"port\": 5000}  # default for a subcommand\n    }\n\nThe default map can be provided when the script is invoked, or\noverridden at any point by commands. For instance, a top-level command\ncould load the defaults from a configuration file.\n\nExample usage:\n\n.. click:example::\n\n    import click\n\n    @click.group()\n    def cli():\n        pass\n\n    @cli.command()\n    @click.option('--port', default=8000)\n    def runserver(port):\n        click.echo(f\"Serving on http://127.0.0.1:{port}/\")\n\n    if __name__ == '__main__':\n        cli(default_map={\n            'runserver': {\n                'port': 5000\n            }\n        })\n\nAnd in action:\n\n.. click:run::\n\n    invoke(cli, prog_name='cli', args=['runserver'], default_map={\n        'runserver': {\n            'port': 5000\n        }\n    })\n\nContext Defaults\n----------------\n\n.. versionadded:: 2.0\n\nStarting with Click 2.0 you can override defaults for contexts not just\nwhen calling your script, but also in the decorator that declares a\ncommand.  For instance given the previous example which defines a custom\n``default_map`` this can also be accomplished in the decorator now.\n\nThis example does the same as the previous example:\n\n.. click:example::\n\n    import click\n\n    CONTEXT_SETTINGS = dict(\n        default_map={'runserver': {'port': 5000}}\n    )\n\n    @click.group(context_settings=CONTEXT_SETTINGS)\n    def cli():\n        pass\n\n    @cli.command()\n    @click.option('--port', default=8000)\n    def runserver(port):\n        click.echo(f\"Serving on http://127.0.0.1:{port}/\")\n\n    if __name__ == '__main__':\n        cli()\n\nAnd again the example in action:\n\n.. click:run::\n\n    invoke(cli, prog_name='cli', args=['runserver'])\n\n\nCommand Return Values\n---------------------\n\n.. versionadded:: 3.0\n\nOne of the new introductions in Click 3.0 is the full support for return\nvalues from command callbacks.  This enables a whole range of features\nthat were previously hard to implement.\n\nIn essence any command callback can now return a value.  This return value\nis bubbled to certain receivers.  One usecase for this has already been\nshow in the example of :ref:`command-chaining` where it has been\ndemonstrated that chained groups can have callbacks that process\nall return values.\n\nWhen working with command return values in Click, this is what you need to\nknow:\n\n-   The return value of a command callback is generally returned from the\n    :meth:`Command.invoke` method.  The exception to this rule has to\n    do with :class:`Group`\\s:\n\n    *   In a group the return value is generally the return value of the\n        subcommand invoked.  The only exception to this rule is that the\n        return value is the return value of the group callback if it's\n        invoked without arguments and `invoke_without_command` is enabled.\n    *   If a group is set up for chaining then the return value is a list\n        of all subcommands' results.\n    *   Return values of groups can be processed through a\n        :attr:`Group.result_callback`.  This is invoked with the\n        list of all return values in chain mode, or the single return\n        value in case of non chained commands.\n\n-   The return value is bubbled through from the :meth:`Context.invoke`\n    and :meth:`Context.forward` methods.  This is useful in situations\n    where you internally want to call into another command.\n\n-   Click does not have any hard requirements for the return values and\n    does not use them itself.  This allows return values to be used for\n    custom decorators or workflows (like in the command chaining\n    example).\n\n-   When a Click script is invoked as command line application (through\n    :meth:`Command.main`) the return value is ignored unless the\n    `standalone_mode` is disabled in which case it's bubbled through.\n"
  },
  {
    "path": "docs/complex.md",
    "content": "(complex-guide)=\n\n# Complex Applications\n\n```{currentmodule} click\n```\n\nClick is designed to assist with the creation of complex and simple CLI tools\nalike.  However, the power of its design is the ability to arbitrarily nest\nsystems together.  For instance, if you have ever used Django, you will\nhave realized that it provides a command line utility, but so does Celery.\nWhen using Celery with Django, there are two tools that need to interact with\neach other and be cross-configured.\n\nIn a theoretical world of two separate Click command line utilities, they\ncould solve this problem by nesting one inside the other.  For instance, the\nweb framework could also load the commands for the message queue framework.\n\n```{contents}\n---\ndepth: 1\nlocal: true\n---\n```\n\n## Basic Concepts\n\nTo understand how this works, you need to understand two concepts: contexts\nand the calling convention.\n\n### Contexts\n\nWhenever a Click command is executed, a {class}`Context` object is created\nwhich holds state for this particular invocation.  It remembers parsed\nparameters, what command created it, which resources need to be cleaned up\nat the end of the function, and so forth.  It can also optionally hold an\napplication-defined object.\n\nContext objects build a linked list until they hit the top one.  Each context\nis linked to a parent context.  This allows a command to work below\nanother command and store its own information there without having to be\nafraid of altering up the state of the parent command.\n\nBecause the parent data is available, however, it is possible to navigate to\nit if needed.\n\nMost of the time, you do not see the context object, but when writing more\ncomplex applications it comes in handy.  This brings us to the next point.\n\n### Calling Convention\n\nWhen a Click command callback is executed, it's passed all the non-hidden\nparameters as keyword arguments.  Notably absent is the context.  However,\na callback can opt into being passed to the context object by marking itself\nwith {func}`pass_context`.\n\nSo how do you invoke a command callback if you don't know if it should\nreceive the context or not?  The answer is that the context itself\nprovides a helper function ({meth}`Context.invoke`) which can do this for\nyou.  It accepts the callback as first argument and then invokes the\nfunction correctly.\n\n## Building a Git Clone\n\nIn this example, we want to build a command line tool that resembles a\nversion control system.  Systems like Git usually provide one\nover-arching command that already accepts some parameters and\nconfiguration, and then have extra subcommands that do other things.\n\n### The Root Command\n\nAt the top level, we need a group that can hold all our commands.  In this\ncase, we use the basic {func}`click.group` which allows us to register\nother Click commands below it.\n\nFor this command, we also want to accept some parameters that configure the\nstate of our tool:\n\n```{eval-rst}\n.. click:example::\n\n    import os\n    import click\n\n\n    class Repo(object):\n        def __init__(self, home=None, debug=False):\n            self.home = os.path.abspath(home or '.')\n            self.debug = debug\n\n\n    @click.group()\n    @click.option('--repo-home', envvar='REPO_HOME', default='.repo')\n    @click.option('--debug/--no-debug', default=False,\n                  envvar='REPO_DEBUG')\n    @click.pass_context\n    def cli(ctx, repo_home, debug):\n        ctx.obj = Repo(repo_home, debug)\n```\n\nLet's understand what this does.  We create a group command which can\nhave subcommands.  When it is invoked, it will create an instance of a\n`Repo` class.  This holds the state for our command line tool.  In this\ncase, it just remembers some parameters, but at this point it could also\nstart loading configuration files and so on.\n\nThis state object is then remembered by the context as {attr}`~Context.obj`.\nThis is a special attribute where commands are supposed to remember what\nthey need to pass on to their children.\n\nIn order for this to work, we need to mark our function with\n{func}`pass_context`, because otherwise, the context object would be\nentirely hidden from us.\n\n### The First Child Command\n\nLet's add our first child command to it, the clone command:\n\n```python\n@cli.command()\n@click.argument('src')\n@click.argument('dest', required=False)\ndef clone(src, dest):\n    pass\n```\n\nSo now we have a clone command, but how do we get access to the repo?  As\nyou can imagine, one way is to use the {func}`pass_context` function which\nagain will make our callback also get the context passed on which we\nmemorized the repo.  However, there is a second version of this decorator\ncalled {func}`pass_obj` which will just pass the stored object, (in our case\nthe repo):\n\n```python\n@cli.command()\n@click.argument('src')\n@click.argument('dest', required=False)\n@click.pass_obj\ndef clone(repo, src, dest):\n    pass\n```\n\n### Interleaved Commands\n\nWhile not relevant for the particular program we want to build, there is\nalso quite good support for interleaving systems.  Imagine for instance that\nthere was a super cool plugin for our version control system that needed a\nlot of configuration and wanted to store its own configuration as\n{attr}`~Context.obj`.  If we would then attach another command below that,\nwe would all of a sudden get the plugin configuration instead of our repo\nobject.\n\nOne obvious way to remedy this is to store a reference to the repo in the\nplugin, but then a command needs to be aware that it's attached below such a\nplugin.\n\nThere is a much better system that can be built by taking advantage of the\nlinked nature of contexts.  We know that the plugin context is linked to the\ncontext that created our repo.  Because of that, we can start a search for\nthe last level where the object stored by the context was a repo.\n\nBuilt-in support for this is provided by the {func}`make_pass_decorator`\nfactory, which will create decorators for us that find objects (it\ninternally calls into {meth}`Context.find_object`).  In our case, we\nknow that we want to find the closest `Repo` object, so let's make a\ndecorator for this:\n\n```python\npass_repo = click.make_pass_decorator(Repo)\n```\n\nIf we now use `pass_repo` instead of `pass_obj`, we will always get a\nrepo instead of something else:\n\n```python\n@cli.command()\n@click.argument('src')\n@click.argument('dest', required=False)\n@pass_repo\ndef clone(repo, src, dest):\n    pass\n```\n\n### Ensuring Object Creation\n\nThe above example only works if there was an outer command that created a\n`Repo` object and stored it in the context.  For some more advanced use\ncases, this might become a problem.  The default behavior of\n{func}`make_pass_decorator` is to call {meth}`Context.find_object`\nwhich will find the object.  If it can't find the object,\n{meth}`make_pass_decorator` will raise an error.\nThe alternative behavior is to use {meth}`Context.ensure_object`\nwhich will find the object, and if it cannot find it, will create one and\nstore it in the innermost context.  This behavior can also be enabled for\n{func}`make_pass_decorator` by passing `ensure=True`:\n\n```python\npass_repo = click.make_pass_decorator(Repo, ensure=True)\n```\n\nIn this case, the innermost context gets an object created if it is\nmissing.  This might replace objects being placed there earlier.  In this\ncase, the command stays executable, even if the outer command does not run.\nFor this to work, the object type needs to have a constructor that accepts\nno arguments.\n\nAs such it runs standalone:\n\n```python\n@click.command()\n@pass_repo\ndef cp(repo):\n    click.echo(isinstance(repo, Repo))\n```\nAs you can see:\n\n```console\n$ cp\nTrue\n```\n\n## Lazily Loading Subcommands\n\nLarge CLIs and CLIs with slow imports may benefit from deferring the loading of\nsubcommands. The interfaces which support this mode of use are\n{meth}`Group.list_commands` and {meth}`Group.get_command`. A custom\n{class}`Group` subclass can implement a lazy loader by storing extra data such\nthat {meth}`Group.get_command` is responsible for running imports.\n\nSince the primary case for this is a {class}`Group` which loads its subcommands lazily,\nthe following example shows a lazy-group implementation.\n\n```{warning}\nLazy loading of python code can result in hard to track down bugs, circular imports\nin order-dependent codebases, and other surprising behaviors. It is recommended that\nthis technique only be used in concert with testing which will at least run the\n`--help` on each subcommand. That will guarantee that each subcommand can be loaded\nsuccessfully.\n```\n\n### Defining the Lazy Group\n\nThe following {class}`Group` subclass adds an attribute, `lazy_subcommands`, which\nstores a mapping from subcommand names to the information for importing them.\n\n\n```python\n# in lazy_group.py\nimport importlib\nimport click\n\nclass LazyGroup(click.Group):\n    def __init__(self, *args, lazy_subcommands=None, **kwargs):\n        super().__init__(*args, **kwargs)\n        # lazy_subcommands is a map of the form:\n        #\n        #   {command-name} -> {module-name}.{command-object-name}\n        #\n        self.lazy_subcommands = lazy_subcommands or {}\n\n    def list_commands(self, ctx):\n        base = super().list_commands(ctx)\n        lazy = sorted(self.lazy_subcommands.keys())\n        return base + lazy\n\n    def get_command(self, ctx, cmd_name):\n        if cmd_name in self.lazy_subcommands:\n            return self._lazy_load(cmd_name)\n        return super().get_command(ctx, cmd_name)\n\n    def _lazy_load(self, cmd_name):\n        # lazily loading a command, first get the module name and attribute name\n        import_path = self.lazy_subcommands[cmd_name]\n        modname, cmd_object_name = import_path.rsplit(\".\", 1)\n        # do the import\n        mod = importlib.import_module(modname)\n        # get the Command object from that module\n        cmd_object = getattr(mod, cmd_object_name)\n        # check the result to make debugging easier\n        if not isinstance(cmd_object, click.Command):\n            raise ValueError(\n                f\"Lazy loading of {import_path} failed by returning \"\n                \"a non-command object\"\n            )\n        return cmd_object\n```\n\n### Using LazyGroup To Define a CLI\n\nWith `LazyGroup` defined, it's now possible to write a group which lazily loads its\nsubcommands like so:\n\n```python\n# in main.py\nimport click\nfrom lazy_group import LazyGroup\n\n@click.group(\n    cls=LazyGroup,\n    lazy_subcommands={\"foo\": \"foo.cli\", \"bar\": \"bar.cli\"},\n    help=\"main CLI command for lazy example\",\n)\ndef cli():\n    pass\n```\n\n```python\n# in foo.py\nimport click\n\n@click.group(help=\"foo command for lazy example\")\ndef cli():\n    pass\n```\n\n```python\n# in bar.py\nimport click\nfrom lazy_group import LazyGroup\n\n@click.group(\n    cls=LazyGroup,\n    lazy_subcommands={\"baz\": \"baz.cli\"},\n    help=\"bar command for lazy example\",\n)\ndef cli():\n    pass\n```\n\n```python\n# in baz.py\nimport click\n\n@click.group(help=\"baz command for lazy example\")\ndef cli():\n    pass\n```\n\n### What triggers Lazy Loading?\n\nThere are several events which may trigger lazy loading by running the\n{meth}`Group.get_command` function.\nSome are intuititve, and some are less so.\n\nAll cases are described with respect to the above example, assuming the main program\nname is `cli`.\n\n1. Command resolution. If a user runs `cli bar baz`, this must first resolve `bar`,\n   and then resolve `baz`. Each subcommand resolution step does a lazy load.\n2. Helptext rendering. In order to get the short help description of subcommands,\n   `cli --help` will load `foo` and `bar`. Note that it will still not load\n   `baz`.\n3. Shell completion. In order to get the subcommands of a lazy command, `cli <TAB>`\n   will need to resolve the subcommands of `cli`. This process will trigger the lazy\n   loads.\n\n### Further Deferring Imports\n\nIt is possible to make the process even lazier, but it is generally more difficult the\nmore you want to defer work.\n\nFor example, subcommands could be represented as a custom {class}`Command` subclass\nwhich defers importing the command until it is invoked, but which provides\n{meth}`Command.get_short_help_str` in order to support completions and helptext.\nMore simply, commands can be constructed whose callback functions defer any actual work\nuntil after an import.\n\nThis command definition provides `foo`, but any of the work associated with importing\nthe \"real\" callback function is deferred until invocation time:\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option(\"-n\", type=int)\n    @click.option(\"-w\", type=str)\n    def foo(n, w):\n        from mylibrary import foo_concrete\n\n        foo_concrete(n, w)\n```\n\nBecause Click builds helptext and usage info from options, arguments, and command\nattributes, it has no awareness that the underlying function is in any way handling a\ndeferred import. Therefore, all Click-provided utilities and functionality will work\nas normal on such a command.\n"
  },
  {
    "path": "docs/conf.py",
    "content": "from pallets_sphinx_themes import get_version\nfrom pallets_sphinx_themes import ProjectLink\n\n# Project --------------------------------------------------------------\n\nproject = \"Click\"\ncopyright = \"2014 Pallets\"\nauthor = \"Pallets\"\nrelease, version = get_version(\"Click\")\n\n# General --------------------------------------------------------------\n\nmaster_doc = \"index\"\ndefault_role = \"code\"\nextensions = [\n    \"sphinx.ext.autodoc\",\n    \"sphinx.ext.extlinks\",\n    \"sphinx.ext.intersphinx\",\n    \"sphinx_tabs.tabs\",\n    \"sphinxcontrib.log_cabinet\",\n    \"pallets_sphinx_themes\",\n    \"myst_parser\",\n]\nautodoc_member_order = \"bysource\"\nautodoc_typehints = \"description\"\nautodoc_preserve_defaults = True\nextlinks = {\n    \"issue\": (\"https://github.com/pallets/click/issues/%s\", \"#%s\"),\n    \"pr\": (\"https://github.com/pallets/click/pull/%s\", \"#%s\"),\n}\nintersphinx_mapping = {\n    \"python\": (\"https://docs.python.org/3/\", None),\n}\n\n# HTML -----------------------------------------------------------------\n\nhtml_theme = \"click\"\nhtml_theme_options = {\"index_sidebar_logo\": False}\nhtml_context = {\n    \"project_links\": [\n        ProjectLink(\"Donate\", \"https://palletsprojects.com/donate\"),\n        ProjectLink(\"PyPI Releases\", \"https://pypi.org/project/click/\"),\n        ProjectLink(\"Source Code\", \"https://github.com/pallets/click/\"),\n        ProjectLink(\"Issue Tracker\", \"https://github.com/pallets/click/issues/\"),\n        ProjectLink(\"Chat\", \"https://discord.gg/pallets\"),\n    ]\n}\nhtml_sidebars = {\n    \"index\": [\"project.html\", \"localtoc.html\", \"searchbox.html\", \"ethicalads.html\"],\n    \"**\": [\"localtoc.html\", \"relations.html\", \"searchbox.html\", \"ethicalads.html\"],\n}\nsinglehtml_sidebars = {\"index\": [\"project.html\", \"localtoc.html\", \"ethicalads.html\"]}\nhtml_static_path = [\"_static\"]\nhtml_favicon = \"_static/click-icon.svg\"\nhtml_logo = \"_static/click-logo.svg\"\nhtml_title = f\"Click Documentation ({version})\"\nhtml_show_sourcelink = False\n"
  },
  {
    "path": "docs/contrib.md",
    "content": "(contrib)=\n\n# click-contrib\n\nAs the user number of Click grows, more and more major feature requests are\nmade. To users, it may seem reasonable to include those features with Click;\nhowever, many of them are experimental or aren't practical to support\ngenerically. Maintainers have to choose what is reasonable to maintain in Click\ncore.\n\nThe [click-contrib](https://github.com/click-contrib/) GitHub organization exists as a place to collect third-party\npackages that extend Click's features. It is also meant to ease the effort of\nsearching for such extensions.\n\nPlease note that the quality and stability of those packages may be different\nfrom Click itself. While published under a common organization, they are still\nseparate from Click and the Pallets maintainers.\n\n## Third-party projects\n\nOther projects that extend Click's features are available outside the\n[click-contrib](https://github.com/click-contrib/) organization.\n\nSome of the most popular and actively maintained are listed below:\n\n| Project                                                 | Description                                                                          | Popularity                                                                                             | Activity                                                                                                    |\n|---------------------------------------------------------|--------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------|\n| [Typer](https://github.com/fastapi/typer)               | Use Python type hints to create CLI apps.                                            | ![GitHub stars](https://img.shields.io/github/stars/fastapi/typer?label=%20&style=flat-square)         | ![Last commit](https://img.shields.io/github/last-commit/fastapi/typer?label=%20&style=flat-square)         |\n| [rich-click](https://github.com/ewels/rich-click)       | Format help output with Rich.                                                        | ![GitHub stars](https://img.shields.io/github/stars/ewels/rich-click?label=%20&style=flat-square)      | ![Last commit](https://img.shields.io/github/last-commit/ewels/rich-click?label=%20&style=flat-square)      |\n| [click-app](https://github.com/simonw/click-app)        | Cookiecutter template for creating new CLIs.                                         | ![GitHub stars](https://img.shields.io/github/stars/simonw/click-app?label=%20&style=flat-square)      | ![Last commit](https://img.shields.io/github/last-commit/simonw/click-app?label=%20&style=flat-square)      |\n| [Cloup](https://github.com/janluke/cloup)               | Adds option groups, constraints, command aliases, help themes, suggestions and more. | ![GitHub stars](https://img.shields.io/github/stars/janluke/cloup?label=%20&style=flat-square)         | ![Last commit](https://img.shields.io/github/last-commit/janluke/cloup?label=%20&style=flat-square)         |\n| [Click Extra](https://github.com/kdeldycke/click-extra) | Cloup + colorful `--help`, `--config`, `--show-params`, `--verbosity` options, etc.  | ![GitHub stars](https://img.shields.io/github/stars/kdeldycke/click-extra?label=%20&style=flat-square) | ![Last commit](https://img.shields.io/github/last-commit/kdeldycke/click-extra?label=%20&style=flat-square) |\n\n```{note}\nTo make it into the list above, a project:\n\n- must be actively maintained (at least one commit in the last year)\n- must have a reasonable number of stars (at least 20)\n\nIf you have a project that meets these criteria, please open a pull request\nto add it to the list.\n\nIf a project is no longer maintained or does not meet the criteria above,\nplease open a pull request to remove it from the list.\n```\n"
  },
  {
    "path": "docs/design-opinions.md",
    "content": "# CLI Design Opinions\n\n```{currentmodule} click\n```\nA penny for your thoughts...\n\n```{contents}\n:depth: 1\n:local: true\n```\n\n## Options over arguments\n{ref}`Positional arguments <arguments>` should be used sparingly, and if used should be required:\n- The more positional arguments there are, the more confusing the CLI invocation becomes to read. (This is true of Python too.)\n- Making some arguments optional, or arbitrary length, can make it harder to reason about. The parser handles this consistently by filling left to right, with an error if there is a non-optional unfilled after that. But that's not obvious to a user just looking at a command line.\n- A command should be doing one thing, and the arguments should be related directly to that.\n    - A group, where the argument is the sub-command name.\n    - A command acts on some files.\n    - A command looks at a source and acts on a destination.\n"
  },
  {
    "path": "docs/documentation.md",
    "content": "# Help Pages\n\n```{currentmodule} click\n```\n\nClick makes it very easy to document your command line tools. For most things Click automatically generates help pages for you. By design the text is customizable, but the layout is not.\n\n## Help Texts\n\nCommands and options accept help arguments. For commands, the docstring of the function is automatically used if provided.\n\nSimple example:\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.argument('name')\n    @click.option('--count', default=1, help='number of greetings')\n    def hello(name: str, count: int):\n        \"\"\"This script prints hello and a name one or more times.\"\"\"\n        for x in range(count):\n            if name:\n                click.echo(f\"Hello {name}!\")\n            else:\n                click.echo(\"Hello!\")\n\n.. click:run::\n    invoke(hello, args=['--help'])\n```\n\n## Command Short Help\n\nFor subcommands, a short help snippet is generated. By default, it's the first sentence of the docstring. If too long, then it will ellipsize what cannot be fit on a single line with `...`. The short help snippet can also be overridden with `short_help`:\n\n```{eval-rst}\n.. click:example::\n\n    import click\n\n    @click.group()\n    def cli():\n        \"\"\"A simple command line tool.\"\"\"\n\n    @cli.command('init', short_help='init the repo')\n    def init():\n        \"\"\"Initializes the repository.\"\"\"\n\n.. click:run::\n    invoke(cli, args=['--help'])\n```\n\n## Command Epilog Help\n\nThe help epilog is printed at the end of the help and is useful for showing example command usages or referencing additional help resources.\n\n```{eval-rst}\n.. click:example::\n\n    import click\n\n    @click.command(\n        epilog='See https://example.com for more details',\n        )\n    def init():\n        \"\"\"Initializes the repository.\"\"\"\n\n.. click:run::\n    invoke(init, args=['--help'])\n```\n\n(documenting-arguments)=\n\n## Documenting Arguments\n\n{class}`click.argument` does not take a `help` parameter. This follows the Unix Command Line Tools convention of using arguments only for necessary things and documenting them in the command help text\nby name. This should then be done via the docstring.\n\nA brief example:\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.argument('filename')\n    def touch(filename):\n        \"\"\"Print FILENAME.\"\"\"\n        click.echo(filename)\n\n.. click:run::\n    invoke(touch, args=['--help'])\n```\n\nOr more explicitly:\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.argument('filename')\n    def touch(filename):\n        \"\"\"Print FILENAME.\n\n        FILENAME is the name of the file to check.\n        \"\"\"\n        click.echo(filename)\n\n.. click:run::\n    invoke(touch, args=['--help'])\n```\n\n## Showing Defaults\n\nTo control the appearance of defaults pass `show_default`.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--n', default=1, show_default=False, help='number of dots')\n    def dots(n):\n        click.echo('.' * n)\n\n.. click:run::\n    invoke(dots, args=['--help'])\n```\n\nFor single option boolean flags, the default remains hidden if the default value is False, even if show default is set to true.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--n', default=1, show_default=True)\n    @click.option(\"--gr\", is_flag=True, show_default=True, default=False, help=\"Greet the world.\")\n    @click.option(\"--br\", is_flag=True, show_default=True, default=True, help=\"Add a thematic break\")\n    def dots(n, gr, br):\n        if gr:\n            click.echo('Hello world!')\n        click.echo('.' * n)\n        if br:\n            click.echo('-' * n)\n\n.. click:run::\n   invoke(dots, args=['--help'])\n```\n\n## Showing Environment Variables\n\nTo control the appearance of environment variables pass `show_envvar`.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--username', envvar='USERNAME', show_envvar=True)\n    def greet(username):\n        click.echo(f'Hello {username}!')\n\n.. click:run::\n    invoke(greet, args=['--help'])\n```\n\n## Click's Wrapping Behavior\n\nClick's default wrapping ignores single new lines and rewraps the text based on the width of the terminal to a maximum of 80 characters by default, but this can be modified with {attr}`~Context.max_content_width`. In the example notice how the second grouping of three lines is rewrapped into a single paragraph.\n\n```{eval-rst}\n.. click:example::\n\n    import click\n\n    @click.command()\n    def cli():\n        \"\"\"\n        This is a very long paragraph and as you\n        can see wrapped very early in the source text\n        but will be rewrapped to the terminal width in\n        the final output.\n\n        This is\n        a paragraph\n        that is compacted.\n        \"\"\"\n\n.. click:run::\n    invoke(cli, args=['--help'])\n```\n\n## Escaping Click's Wrapping\n\nSometimes Click's wrapping can be a problem, such as when showing code examples where new lines are significant. This behavior can be escaped on a per-paragraph basis by adding a line with only `\\b` . The `\\b` is removed from the rendered help text.\n\nExample:\n\n```{eval-rst}\n.. click:example::\n\n    import click\n\n    @click.command()\n    def cli():\n        \"\"\"First paragraph.\n\n        \\b\n        This is\n        a paragraph\n        without rewrapping.\n\n        And this is a paragraph\n        that will be rewrapped again.\n        \"\"\"\n\n.. click:run::\n    invoke(cli, args=['--help'])\n```\n\nTo change the rendering maximum width, pass `max_content_width` when calling the command.\n\n```bash\ncli(max_content_width=120)\n```\n\n## Truncating Help Texts\n\nClick gets {class}`Command` help text from the docstring. If you do not want to include part of the docstring, add the `\\f` escape marker to have Click truncate the help text after the marker.\n\nExample:\n\n```{eval-rst}\n.. click:example::\n\n    import click\n\n    @click.command()\n    def cli():\n        \"\"\"First paragraph.\n        \\f\n\n        Words to not be included.\n        \"\"\"\n\n.. click:run::\n    invoke(cli, args=['--help'])\n```\n\n(doc-meta-variables)=\n\n## Placeholder / Meta Variable\n\nThe default placeholder variable ([meta variable](https://en.wikipedia.org/wiki/Metasyntactic_variable#IETF_Requests_for_Comments)) in the help pages is the parameter name in uppercase with underscores. This can be changed for Commands and Parameters with the `options_metavar` and `metavar` kwargs.\n\n```{eval-rst}\n.. click:example::\n\n    # This controls entry on the usage line.\n    @click.command(options_metavar='[[options]]')\n    @click.option('--count', default=1, help='number of greetings',\n                  metavar='<int>')\n    @click.argument('name', metavar='<name>')\n    def hello(name: str, count: int) -> None:\n        \"\"\"This script prints 'hello <name>' a total of <count> times.\"\"\"\n        for x in range(count):\n            click.echo(f\"Hello {name}!\")\n\n# Example usage:\n\n.. click:run::\n    invoke(hello, args=['--help'])\n\n```\n\n## Help Parameter Customization\n\nHelp parameters are automatically added by Click for any command. The default is `--help` but can be overridden by the context setting {attr}`~Context.help_option_names`. Click also performs automatic conflict resolution on the default help parameter, so if a command itself implements a parameter named `help` then the default help will not be run.\n\nThis example changes the default parameters to `-h` and `--help`\ninstead of just `--help`:\n\n```{eval-rst}\n.. click:example::\n\n    import click\n\n    CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])\n\n    @click.command(context_settings=CONTEXT_SETTINGS)\n    def cli():\n        pass\n\n.. click:run::\n    invoke(cli, ['-h'])\n```\n"
  },
  {
    "path": "docs/entry-points.md",
    "content": "# Packaging Entry Points\n\n```{eval-rst}\n.. currentmodule:: click\n```\n\nIt's recommended to write command line utilities as installable packages with\nentry points instead of telling users to run ``python hello.py``.\n\nA distribution package is a ``.whl`` file you install with pip or another Python\ninstaller. You use a ``pyproject.toml`` file to describe the project and how it\nis built into a package. You might upload this package to PyPI, or distribute it\nto your users in another way.\n\nPython installers create executable scripts that will run a specified Python\nfunction. These are known as \"entry points\". The installer knows how to create\nan executable regardless of the operating system, so it will work on Linux,\nWindows, MacOS, etc.\n\n## Project Files\n\nTo install your app with an entry point, all you need is the script and a\n``pyproject.toml`` file. Here's an example project directory:\n\n```text\nhello-project/\n    src/\n        hello/\n            __init__.py\n            hello.py\n    pyproject.toml\n```\n\nContents of ``hello.py``:\n\n```{eval-rst}\n.. click:example::\n    import click\n\n    @click.command()\n    def cli():\n        \"\"\"Prints a greeting.\"\"\"\n        click.echo(\"Hello, World!\")\n```\n\nContents of ``pyproject.toml``:\n\n```toml\n[project]\nname = \"hello\"\nversion = \"1.0.0\"\ndescription = \"Hello CLI\"\nrequires-python = \">=3.11\"\ndependencies = [\n    \"click>=8.1\",\n]\n\n[project.scripts]\nhello = \"hello.hello:cli\"\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n```\n\nThe magic is in the ``project.scripts`` section. Each line identifies one executable\nscript. The first part before the equals sign (``=``) is the name of the script that\nshould be generated, the second part is the import path followed by a colon\n(``:``) with the function to call (the Click command).\n\n## Installation\n\nWhen your package is installed, the installer will create an executable script\nbased on the configuration. During development, you can install in editable\nmode using the ``-e`` option. Remember to use a virtual environment!\n\n```console\n$ python -m venv .venv\n$ . .venv/bin/activate\n$ pip install -e .\n```\n\nAfterwards, your command should be available:\n\n```console\n$ hello\nHello, World!\n```\n"
  },
  {
    "path": "docs/exceptions.md",
    "content": "(exception-handling-exit-codes)=\n\n# Exception Handling and Exit Codes\n\n```{eval-rst}\n.. currentmodule:: click\n```\n\nClick internally uses exceptions to signal various error conditions that\nthe user of the application might have caused. Primarily this is things\nlike incorrect usage.\n\n```{contents}\n:depth: 1\n:local:\n```\n\n## Where are Errors Handled?\n\nClick's main error handling is happening in {meth}`Command.main`. In\nthere it handles all subclasses of {exc}`ClickException` as well as the\nstandard {exc}`EOFError` and {exc}`KeyboardInterrupt` exceptions. The\nlatter are internally translated into an {exc}`Abort`.\n\nThe logic applied is the following:\n\n1. If an {exc}`EOFError` or {exc}`KeyboardInterrupt` happens, reraise it\n   as {exc}`Abort`.\n2. If a {exc}`ClickException` is raised, invoke the\n   {meth}`ClickException.show` method on it to display it and then exit\n   the program with {attr}`ClickException.exit_code`.\n3. If an {exc}`Abort` exception is raised print the string ``Aborted!``\n   to standard error and exit the program with exit code ``1``.\n4. If it goes through well, exit the program with exit code ``0``.\n\n## What if I Don't Want That?\n\nGenerally you always have the option to invoke the {meth}`Command.invoke`\nmethod yourself. For instance if you have a {class}`Command` you can\ninvoke it manually like this:\n\n```python\nctx = command.make_context(\"command-name\", [\"args\", \"go\", \"here\"])\nwith ctx:\n    result = command.invoke(ctx)\n```\n\nIn this case exceptions will not be handled at all and bubbled up as you\nwould expect.\n\nStarting with Click 3.0 you can also use the {meth}`Command.main` method\nbut disable the standalone mode which will do two things: disable\nexception handling and disable the implicit {func}`sys.exit` at the end.\n\nSo you can do something like this:\n\n```python\ncommand.main(\n    [\"command-name\", \"args\", \"go\", \"here\"],\n    standalone_mode=False,\n)\n```\n\n## Which Exceptions Exist?\n\nClick has two exception bases: {exc}`ClickException` which is raised for\nall exceptions that Click wants to signal to the user and {exc}`Abort`\nwhich is used to instruct Click to abort the execution.\n\nA {exc}`ClickException` has a {meth}`ClickException.show` method which\ncan render an error message to stderr or the given file object. If you\nwant to use the exception yourself for doing something check the API docs\nabout what else they provide.\n\nThe following common subclasses exist:\n\n- {exc}`UsageError` to inform the user that something went wrong.\n- {exc}`BadParameter` to inform the user that something went wrong with\n  a specific parameter. These are often handled internally in Click and\n  augmented with extra information if possible. For instance if those\n  are raised from a callback Click will automatically augment it with\n  the parameter name if possible.\n- {exc}`FileError` this is an error that is raised by the\n  {class}`FileType` if Click encounters issues opening the file.\n\n(help-page-exit-codes)=\n\n## Help Pages and Exit Codes\n\nTriggering the a help page intentionally (by passing in ``--help``)\nreturns exit code 0. If a help page is displayed due to incorrect user\ninput, the program returns exit code 2. See {ref}`exit-codes` for more\ngeneral information.\n\nFor clarity, here is an example.\n\n```{eval-rst}\n.. click:example::\n\n    @click.group('printer_group')\n    def printer_group():\n        pass\n\n    @printer_group.command('printer')\n    @click.option('--this')\n    def printer(this):\n        if this:\n            click.echo(this)\n\n.. click:run::\n    invoke(printer_group, args=['--help'])\n\nThe above invocation returns exit code 0.\n\n.. click:run::\n    invoke(printer_group, args=[])\n```\n\nThe above invocation returns exit code 2 since the user invoked the command incorrectly. However, since this is such a common error when first using a command, Click invokes the help page for the user. To see that `printer-group` is an invalid invocation, turn `no_args_is_help` off.\n\n```{eval-rst}\n.. click:example::\n\n    @click.group('printer_group', no_args_is_help=False)\n    def printer_group():\n        pass\n\n    @printer_group.command('printer')\n    @click.option('--this')\n    def printer(this):\n        if this:\n            click.echo(this)\n\n.. click:run::\n    invoke(printer_group, args=[])\n```\n"
  },
  {
    "path": "docs/extending-click.md",
    "content": "# Extending Click\n\n```{currentmodule} click\n```\n\nIn addition to common functionality that is implemented in the library itself, there are countless patterns that can be\nimplemented by extending Click. This page should give some insight into what can be accomplished.\n\n```{contents}\n:depth: 2\n:local: true\n```\n\n(custom-groups)=\n\n## Custom Groups\n\nYou can customize the behavior of a group beyond the arguments it accepts by subclassing {class}`click.Group`.\n\nThe most common methods to override are {meth}`~click.Group.get_command` and {meth}`~click.Group.list_commands`.\n\nThe following example implements a basic plugin system that loads commands from Python files in a folder. The command is\nlazily loaded to avoid slow startup.\n\n```python\nimport importlib.util\nimport os\nimport click\n\nclass PluginGroup(click.Group):\n    def __init__(self, name=None, plugin_folder=\"commands\", **kwargs):\n        super().__init__(name=name, **kwargs)\n        self.plugin_folder = plugin_folder\n\n    def list_commands(self, ctx):\n        rv = []\n\n        for filename in os.listdir(self.plugin_folder):\n            if filename.endswith(\".py\"):\n                rv.append(filename[:-3])\n\n        rv.sort()\n        return rv\n\n    def get_command(self, ctx, name):\n        path = os.path.join(self.plugin_folder, f\"{name}.py\")\n        spec = importlib.util.spec_from_file_location(name, path)\n        module = importlib.util.module_from_spec(spec)\n        spec.loader.exec_module(module)\n        return module.cli\n\ncli = PluginGroup(\n    plugin_folder=os.path.join(os.path.dirname(__file__), \"commands\")\n)\n\nif __name__ == \"__main__\":\n    cli()\n```\n\nCustom classes can also be used with decorators:\n\n```python\n@click.group(\n    cls=PluginGroup,\n    plugin_folder=os.path.join(os.path.dirname(__file__), \"commands\")\n)\ndef cli():\n    pass\n```\n\n(aliases)=\n\n## Command Aliases\n\nMany tools support aliases for commands. For example, you can configure `git` to accept `git ci` as alias for\n`git commit`. Other tools also support auto-discovery for aliases by automatically shortening them.\n\nIt's possible to customize {class}`Group` to provide this functionality. As explained in {ref}`custom-groups`, a group\nprovides two methods: {meth}`~Group.list_commands` and {meth}`~Group.get_command`. In this particular case, you only\nneed to override the latter as you generally don't want to enumerate the aliases on the help page in order to avoid\nconfusion.\n\nThe following example implements a subclass of {class}`Group` that accepts a prefix for a command. If there was a\ncommand called `push`, it would accept `pus` as an alias (so long as it was unique):\n\n```{eval-rst}\n.. click:example::\n\n    class AliasedGroup(click.Group):\n        def get_command(self, ctx, cmd_name):\n            rv = super().get_command(ctx, cmd_name)\n\n            if rv is not None:\n                return rv\n\n            matches = [\n                x for x in self.list_commands(ctx)\n                if x.startswith(cmd_name)\n            ]\n\n            if not matches:\n                return None\n\n            if len(matches) == 1:\n                return click.Group.get_command(self, ctx, matches[0])\n\n            ctx.fail(f\"Too many matches: {', '.join(sorted(matches))}\")\n\n        def resolve_command(self, ctx, args):\n            # always return the full command name\n            _, cmd, args = super().resolve_command(ctx, args)\n            return cmd.name, cmd, args\n```\n\nIt can be used like this:\n\n```python\n\n    @click.group(cls=AliasedGroup)\n    def cli():\n        pass\n\n    @cli.command\n    def push():\n        pass\n\n    @cli.command\n    def pop():\n        pass\n```\n\nSee the [alias example](https://github.com/pallets/click/tree/main/examples/aliases) in Click's repository for another example.\n"
  },
  {
    "path": "docs/faqs.md",
    "content": "# Frequently Asked Questions\n\n```{contents}\n:depth: 2\n:local: true\n```\n\n## General\n\n### Shell Variable Expansion On Windows\n\nI have a simple Click app :\n\n```\nimport click\n\n@click.command()\n@click.argument('message')\ndef main(message: str):\n    click.echo(message)\n\nif __name__ == '__main__':\n    main()\n\n```\n\nWhen you pass an environment variable in the argument, it expands it:\n\n```{code-block} powershell\n> Desktop python foo.py '$M0/.viola/2025-01-25-17-20-23-307878'\n> M:/home/ramrachum/.viola/2025-01-25-17-20-23-307878\n>\n```\nNote that I used single quotes above, so my shell is not expanding the environment variable, Click does. How do I get Click to not expand it?\n\n#### Answer\n\nIf you don't want Click to emulate (as best it can) unix expansion on Windows, pass windows_expand_args=False when calling the CLI.\nWindows command line doesn't do any *, ~, or $ENV expansion. It also doesn't distinguish between double quotes and single quotes (where the later means \"don't expand here\"). Click emulates the expansion so that the app behaves similarly on both platforms, but doesn't receive information about what quotes were used.\n"
  },
  {
    "path": "docs/handling-files.md",
    "content": "(handling-files)=\n\n# Handling Files\n\n```{currentmodule} click\n```\n\nClick has built in features to support file and file path handling. The examples use arguments but the same principle\napplies to options as well.\n\n(file-args)=\n\n## File Arguments\n\nClick supports working with files with the {class}`File` type. Some notable features are:\n\n- Support for `-` to mean a special file that refers to stdin when used for reading, and stdout when used for writing.\n  This is a common pattern for POSIX command line utilities.\n- Deals with `str` and `bytes` correctly for all versions of Python.\n\nExample:\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.argument('input', type=click.File('rb'))\n    @click.argument('output', type=click.File('wb'))\n    def inout(input, output):\n        \"\"\"Copy contents of INPUT to OUTPUT.\"\"\"\n        while True:\n            chunk = input.read(1024)\n            if not chunk:\n                break\n            output.write(chunk)\n\nAnd from the command line:\n\n.. click:run::\n\n    with isolated_filesystem():\n        invoke(inout, args=['-', 'hello.txt'], input=['hello'],\n               terminate_input=True)\n        invoke(inout, args=['hello.txt', '-'])\n```\n\n## File Path Arguments\n\nFor handling paths, the {class}`Path` type is better than a `str`. Some notable features are:\n\n- The `exists` argument will verify whether the path exists.\n- `readable`, `writable`, and `executable` can perform permission checks.\n- `file_okay` and `dir_okay` allow specifying whether files/directories are accepted.\n- Error messages are nicely formatted using {func}`format_filename` so any undecodable bytes will be printed nicely.\n\nSee {class}`Path` for all features.\n\nExample:\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.argument('filename', type=click.Path(exists=True))\n    def touch(filename):\n        \"\"\"Print FILENAME if the file exists.\"\"\"\n        click.echo(click.format_filename(filename))\n\nAnd from the command line:\n\n.. click:run::\n\n    with isolated_filesystem():\n        with open('hello.txt', 'w') as f:\n            f.write('Hello World!\\n')\n        invoke(touch, args=['hello.txt'])\n        println()\n        invoke(touch, args=['missing.txt'])\n```\n\n## File Opening Behaviors\n\nThe {class}`File` type attempts to be \"intelligent\" about when to open a file. Stdin/stdout and files opened for reading\nwill be opened immediately. This will give the user direct feedback when a file cannot be opened. Files opened for\nwriting will only be open on the first IO operation. This is done by automatically wrapping the file in a special\nwrapper.\n\nFile open behavior can be controlled by the boolean kwarg `lazy`. If a file is opened lazily:\n\n- A failure at first IO operation will happen by raising an {exc}`FileError`.\n- It can help minimize resource handling confusion. If a file is opened in lazy mode, it will call\n  {meth}`LazyFile.close_intelligently` to help figure out if the file needs closing or not. This is not needed for\n  parameters, but is necessary for manually prompting. For manual prompts with the {func}`prompt` function you do not\n  know if a stream like stdout was opened (which was already open before) or a real file was opened (that needs\n  closing).\n\nSince files opened for writing will typically empty the file, the lazy mode should only be disabled if the developer is\nabsolutely sure that this is intended behavior.\n\nIt is also possible to open files in atomic mode by passing `atomic=True`. In atomic mode, all writes go into a separate\nfile in the same folder, and upon completion, the file will be moved over to the original location. This is useful if a\nfile regularly read by other users is modified.\n"
  },
  {
    "path": "docs/index.rst",
    "content": ".. rst-class:: hide-header\n\nWelcome to Click\n================\n\n.. image:: _static/click-name.svg\n    :align: center\n    :height: 200px\n\nClick is a Python package for creating beautiful command line interfaces\nin a composable way with as little code as necessary.  It's the \"Command\nLine Interface Creation Kit\".  It's highly configurable but comes with\nsensible defaults out of the box.\n\nIt aims to make the process of writing command line tools quick and fun\nwhile also preventing any frustration caused by the inability to implement\nan intended CLI API.\n\nClick in three points:\n\n-   arbitrary nesting of commands\n-   automatic help page generation\n-   supports lazy loading of subcommands at runtime\n\nWhat does it look like?  Here is an example of a simple Click program:\n\n.. click:example::\n\n    import click\n\n    @click.command()\n    @click.option('--count', default=1, help='Number of greetings.')\n    @click.option('--name', prompt='Your name',\n                  help='The person to greet.')\n    def hello(count, name):\n        \"\"\"Simple program that greets NAME for a total of COUNT times.\"\"\"\n        for x in range(count):\n            click.echo(f\"Hello {name}!\")\n\n    if __name__ == '__main__':\n        hello()\n\nAnd what it looks like when run:\n\n.. click:run::\n\n    invoke(hello, ['--count=3'], prog_name='python hello.py', input='John\\n')\n\nIt automatically generates nicely formatted help pages:\n\n.. click:run::\n\n    invoke(hello, ['--help'], prog_name='python hello.py')\n\nYou can get the library directly from PyPI::\n\n    pip install click\n\nDocumentation\n==============\n\n.. toctree::\n   :maxdepth: 2\n\n   faqs\n\nTutorials\n------------\n.. toctree::\n   :maxdepth: 1\n\n   quickstart\n   virtualenv\n\nHow to Guides\n---------------\n.. toctree::\n   :maxdepth: 1\n\n   entry-points\n   setuptools\n   upgrade-guides\n   support-multiple-versions\n\nConceptual Guides\n-------------------\n.. toctree::\n   :maxdepth: 1\n\n   design-opinions\n   why\n   click-concepts\n\nGeneral Reference\n--------------------\n\n.. toctree::\n   :maxdepth: 1\n\n   parameters\n   parameter-types\n   options\n   option-decorators\n   arguments\n   commands-and-groups\n   commands\n   documentation\n   prompts\n   handling-files\n   advanced\n   complex\n   extending-click\n   testing\n   utils\n   shell-completion\n   exceptions\n   command-line-reference\n   unicode-support\n   wincmd\n\nAPI Reference\n-------------------\n\n.. toctree::\n   :maxdepth: 2\n\n   api\n\nAbout Project\n===============\n\n* This documentation is structured according to `Diataxis <https://diataxis.fr/>`_ and written with `MyST <https://myst-parser.readthedocs.io/en/latest/>`_\n\n* `Version Policy <https://palletsprojects.com/versions>`_\n\n* `Contributing <https://palletsprojects.com/contributing/>`_\n\n* `Donate <https://palletsprojects.com/donate>`_\n\n.. toctree::\n   :maxdepth: 1\n\n   contrib\n   license\n   changes\n"
  },
  {
    "path": "docs/license.md",
    "content": "# BSD-3-Clause License\n\n```{literalinclude} ../LICENSE.txt\n---\nlanguage: text\n---\n```\n"
  },
  {
    "path": "docs/option-decorators.md",
    "content": "# Options Shortcut Decorators\n\n```{currentmodule} click\n```\n\nFor convenience commonly used combinations of options arguments are available as their own decorators.\n\n```{contents}\n---\ndepth: 2\nlocal: true\n---\n```\n\n## Password Option\n\nClick supports hidden prompts and asking for confirmation. This is useful for password input:\n\n```{eval-rst}\n.. click:example::\n\n    import codecs\n\n    @click.command()\n    @click.option(\n        \"--password\", prompt=True, hide_input=True,\n        confirmation_prompt=True\n    )\n    def encode(password):\n        click.echo(f\"encoded: {codecs.encode(password, 'rot13')}\")\n\n.. click:run::\n\n    invoke(encode, input=['secret', 'secret'])\n```\n\nBecause this combination of parameters is quite common, this can also be\nreplaced with the {func}`password_option` decorator:\n\n```python\n    @click.command()\n    @click.password_option()\n    def encrypt(password):\n        click.echo(f\"encoded: to {codecs.encode(password, 'rot13')}\")\n```\n\n## Confirmation Option\n\nFor dangerous operations, it's very useful to be able to ask a user for confirmation. This can be done by adding a\nboolean `--yes` flag and asking for confirmation if the user did not provide it and to fail in a callback:\n\n```{eval-rst}\n.. click:example::\n\n    def abort_if_false(ctx, param, value):\n        if not value:\n            ctx.abort()\n\n    @click.command()\n    @click.option('--yes', is_flag=True, callback=abort_if_false,\n                  expose_value=False,\n                  prompt='Are you sure you want to drop the db?')\n    def dropdb():\n        click.echo('Dropped all tables!')\n\nAnd what it looks like on the command line:\n\n.. click:run::\n\n    invoke(dropdb, input=['n'])\n    invoke(dropdb, args=['--yes'])\n\nBecause this combination of parameters is quite common, this can also be\nreplaced with the :func:`confirmation_option` decorator:\n\n.. click:example::\n\n    @click.command()\n    @click.confirmation_option(prompt='Are you sure you want to drop the db?')\n    def dropdb():\n        click.echo('Dropped all tables!')\n```\n\n## Version Option\n\n{func}`version_option` adds a `--version` option which immediately prints the version number and exits the program.\n"
  },
  {
    "path": "docs/options.md",
    "content": "(options)=\n\n# Options\n\n```{eval-rst}\n.. currentmodule:: click\n```\n\nAdding options to commands can be accomplished with the {func}`option`\ndecorator. At runtime the decorator invokes the {class}`Option` class. Options in Click are distinct from {ref}`positional arguments <arguments>`.\n\nUseful and often used kwargs are:\n\n- `default`: Passes a default.\n- `help`: Sets help message.\n- `nargs`: Sets the number of arguments.\n- `required`: Makes option required.\n- `type`: Sets {ref}`parameter type <parameter-types>`\n\n```{contents}\n:depth: 2\n:local: true\n```\n\n## Option Decorator\n\nClick expects you to pass at least two positional arguments to the option decorator. They are option name and function argument name.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--string-to-echo', 'string_to_echo')\n    def echo(string_to_echo):\n        click.echo(string_to_echo)\n\n\n.. click:run::\n\n    invoke(echo, args=['--help'])\n```\n\nHowever, if you don't pass in the function argument name, then Click will try to infer it. A simple way to name your option is by taking the function argument, adding two dashes to the front and converting underscores to dashes. In this case, Click will infer the function argument name correctly so you can add only the option name.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--string-to-echo')\n    def echo(string_to_echo):\n        click.echo(string_to_echo)\n\n.. click:run::\n\n    invoke(echo, args=['--string-to-echo', 'Hi!'])\n```\n\nMore formally, Click will try to infer the function argument name by:\n\n1. If a positional argument name does not have a prefix, it is chosen.\n2. If a positional argument name starts with with two dashes, the first one given is chosen.\n3. The first positional argument prefixed with one dash is chosen otherwise.\n\nThe chosen positional argument is converted to lower case, up to two dashes are removed from the beginning, and other dashes are converted to underscores to get the function argument name.\n\n```{eval-rst}\n.. list-table:: Examples\n    :widths: 15 10\n    :header-rows: 1\n\n    * - Decorator Arguments\n      - Function Name\n    * - ``\"-f\", \"--foo-bar\"``\n      - foo_bar\n    * - ``\"-x\"``\n      - x\n    * - ``\"-f\", \"--filename\", \"dest\"``\n      - dest\n    * - ``\"--CamelCase\"``\n      - camelcase\n    * - ``\"-f\", \"-fb\"``\n      - f\n    * - ``\"--f\", \"--foo-bar\"``\n      - f\n    * - ``\"---f\"``\n      - _f\n```\n\n## Basic Example\n\nA simple {class}`click.Option` takes one argument. This will assume the argument is not required. If the decorated function takes an positional argument then None is passed it. This will also assume the type is `str`.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--text')\n    def print_this(text):\n        click.echo(text)\n\n\n.. click:run::\n\n    invoke(print_this, args=['--text=this'])\n\n    invoke(print_this, args=[])\n\n\n.. click:run::\n\n    invoke(print_this, args=['--help'])\n\n```\n\n## Setting a Default\n\nInstead of setting the `type`, you may set a default and Click will try to infer the type.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--n', default=1)\n    def dots(n):\n        click.echo('.' * n)\n\n.. click:run::\n\n    invoke(dots, args=['--help'])\n```\n\n## Multi Value Options\n\nTo make an option take multiple values, pass in `nargs`. Note you may pass in any positive integer, but not -1. The values are passed to the underlying function as a tuple.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--pos', nargs=2, type=float)\n    def findme(pos):\n        a, b = pos\n        click.echo(f\"{a} / {b}\")\n\n.. click:run::\n\n    invoke(findme, args=['--pos', '2.0', '3.0'])\n\n```\n\n(tuple-type)=\n\n## Multi Value Options as Tuples\n\n```{versionadded} 4.0\n```\n\nAs you can see that by using `nargs` set to a specific number each item in\nthe resulting tuple is of the same type. This might not be what you want.\nCommonly you might want to use different types for different indexes in\nthe tuple. For this you can directly specify a tuple as type:\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--item', type=(str, int))\n    def putitem(item):\n        name, id = item\n        click.echo(f\"name={name} id={id}\")\n\n\nAnd on the command line:\n\n.. click:run::\n\n    invoke(putitem, args=['--item', 'peter', '1338'])\n```\n\nBy using a tuple literal as type, `nargs` gets automatically set to the\nlength of the tuple and the {class}`click.Tuple` type is automatically\nused. The above example is thus equivalent to this:\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--item', nargs=2, type=click.Tuple([str, int]))\n    def putitem(item):\n        name, id = item\n        click.echo(f\"name={name} id={id}\")\n```\n\n(multiple-options)=\n\n## Multiple Options\n\nThe multiple options format allows options to take an arbitrary number of arguments (which is called variadic). The arguments are passed to the underlying function as a tuple. If set, the default must be a list or tuple. Setting a string as a default will be interpreted as list of characters.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--message', '-m', multiple=True)\n    def commit(message):\n        click.echo(message)\n        for m in message:\n            click.echo(m)\n\n.. click:run::\n\n    invoke(commit, args=['-m', 'foo', '-m', 'bar', '-m', 'here'])\n```\n\n## Counting\n\nTo count the occurrence of an option pass in `count=True`. If the option is not passed in, then the count is 0. Counting is commonly used for verbosity.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('-v', '--verbose', count=True)\n    def log(verbose):\n        click.echo(f\"Verbosity: {verbose}\")\n\n.. click:run::\n\n    invoke(log, args=[])\n    invoke(log, args=['-vvv'])\n```\n\n(option-boolean-flag)=\n\n## Boolean\n\nBoolean options (boolean flags) take the value True or False. The simplest case sets the default value to `False` if the flag is not passed, and `True` if it is.\n\n```{eval-rst}\n.. click:example::\n\n    import sys\n\n    @click.command()\n    @click.option('--shout', is_flag=True)\n    def info(shout):\n        rv = sys.platform\n        if shout:\n            rv = rv.upper() + '!!!!111'\n        click.echo(rv)\n\n\n.. click:run::\n\n    invoke(info)\n    invoke(info, args=['--shout'])\n\n```\n\nTo implement this more explicitly, pass in on-option `/` off-option. Click will automatically set `is_flag=True`.\n\n```{eval-rst}\n.. click:example::\n\n    import sys\n\n    @click.command()\n    @click.option('--shout/--no-shout', default=False)\n    def info(shout):\n        rv = sys.platform\n        if shout:\n            rv = rv.upper() + '!!!!111'\n        click.echo(rv)\n\n.. click:run::\n\n    invoke(info)\n    invoke(info, args=['--shout'])\n    invoke(info, args=['--no-shout'])\n```\n\nUse cases for this more explicit pattern include:\n\n* The default can be dynamic so the user can explicitly specify the option with either on or off option, or pass in no option to use the dynamic default.\n* Shell scripts sometimes want to be explicit even when it's the default\n* Shell aliases can set a flag, then an invocation can add a negation of the flag\n\nIf a forward slash(`/`) is contained in your option name already, you can split the parameters using `;`. In Windows `/` is commonly used as the prefix character.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('/debug;/no-debug')\n    def log(debug):\n        click.echo(f\"debug={debug}\")\n```\n\n```{versionchanged} 6.0\n```\n\nIf you want to define an alias for the second option only, then you will need to use leading whitespace to disambiguate the format string.\n\n```{eval-rst}\n.. click:example::\n\n    import sys\n\n    @click.command()\n    @click.option('--shout/--no-shout', ' /-N', default=False)\n    def info(shout):\n        rv = sys.platform\n        if shout:\n            rv = rv.upper() + '!!!!111'\n        click.echo(rv)\n\n.. click:run::\n\n    invoke(info, args=['--help'])\n```\n\n## Flag Value\n\nTo have an flag pass a value to the underlying function set `flag_value`. This automatically sets `is_flag=True`. To mark the flag as default, set `default=True`. Setting flag values can be used to create patterns like this:\n\n```{eval-rst}\n.. click:example::\n\n    import sys\n\n    @click.command()\n    @click.option('--upper', 'transformation', flag_value='upper', default=True)\n    @click.option('--lower', 'transformation', flag_value='lower')\n    def info(transformation):\n        click.echo(getattr(sys.platform, transformation)())\n\n.. click:run::\n\n    invoke(info, args=['--help'])\n    invoke(info, args=['--upper'])\n    invoke(info, args=['--lower'])\n    invoke(info)\n```\n\n````{note}\nThe `default` value is given to the underlying function as-is. So if you set `default=None`, the value passed to the function is the `None` Python value. Same for any other type.\n\nBut there is a special case for flags. If a flag has a `flag_value`, then setting `default=True` is interpreted as *the flag should be activated by default*. So instead of the underlying function receiving the `True` Python value, it will receive the `flag_value`.\n\nWhich means, in example above, this option:\n\n```python\n@click.option('--upper', 'transformation', flag_value='upper', default=True)\n```\n\nis equivalent to:\n\n```python\n@click.option('--upper', 'transformation', flag_value='upper', default='upper')\n```\n\nBecause the two are equivalent, it is recommended to always use the second form, and set `default` to the actual value you want to pass. And not use the special `True` case. This makes the code more explicit and predictable.\n````\n\n## Values from Environment Variables\n\nTo pass in a value in from a specific environment variable use `envvar`.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--username', envvar='USERNAME')\n    def greet(username):\n       click.echo(f\"Hello {username}!\")\n\n.. click:run::\n\n    invoke(greet, env={'USERNAME': 'john'})\n```\n\nIf a list is passed to `envvar`, the first environment variable found is picked.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--username', envvar=['ALT_USERNAME', 'USERNAME'])\n    def greet(username):\n       click.echo(f\"Hello {username}!\")\n\n.. click:run::\n\n    invoke(greet, env={'ALT_USERNAME': 'Bill', 'USERNAME': 'john'})\n\n```\n\nVariable names are:\n - [Case-insensitive on Windows but not on other platforms](https://github.com/python/cpython/blob/aa9eb5f757ceff461e6e996f12c89e5d9b583b01/Lib/os.py#L777-L789).\n - Not stripped of whitespaces and should match the exact name provided to the `envvar` argument.\n\nFor flag options, there is two concepts to consider: the activation of the flag driven by the environment variable, and the value of the flag if it is activated.\n\nThe environment variable need to be interpreted, because values read from them are always strings. We need to transform these strings into boolean values that will determine if the flag is activated or not.\n\nHere are the rules used to parse environment variable values for flag options:\n   - `true`, `1`, `yes`, `on`, `t`, `y` are interpreted as activating the flag\n   - `false`, `0`, `no`, `off`, `f`, `n` are interpreted as deactivating the flag\n   - The presence of the environment variable without value is interpreted as deactivating the flag\n   - Empty strings are interpreted as deactivating the flag\n   - Values are case-insensitive, so the `True`, `TRUE`, `tRuE` strings are all activating the flag\n   - Values are stripped of leading and trailing whitespaces before being interpreted, so the `\" True \"` string is transformed to `\"true\"` and so activates the flag\n   - If the flag option has a `flag_value` argument, passing that value in the environment variable will activate the flag, in addition to all the cases described above\n   - Any other value is interpreted as deactivating the flag\n\n```{caution}\nFor boolean flags with a pair of values, the only recognized environment variable is the one provided to the `envvar` argument.\n\nSo an option defined as `--flag\\--no-flag`, with a `envvar=\"FLAG\"` parameter, there is no magical `NO_FLAG=<anything>` variable that is recognized. Only the `FLAG=<anything>` environment variable is recognized.\n```\n\nOnce the status of the flag has been determine to be activated or not, the `flag_value` is used as the value of the flag if it is activated. If the flag is not activated, the value of the flag is set to `None` by default.\n\n## Multiple Options from Environment Values\n\nAs options can accept multiple values, pulling in such values from\nenvironment variables (which are strings) is a bit more complex. The way\nClick solves this is by leaving it up to the type to customize this\nbehavior. For both `multiple` and `nargs` with values other than\n`1`, Click will invoke the {meth}`ParamType.split_envvar_value` method to\nperform the splitting.\n\nThe default implementation for all types is to split on whitespace. The\nexceptions to this rule are the {class}`File` and {class}`Path` types\nwhich both split according to the operating system's path splitting rules.\nOn Unix systems like Linux and OS X, the splitting happens on\nevery colon (`:`), and for Windows, splitting on every semicolon (`;`).\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('paths', '--path', envvar='PATHS', multiple=True,\n                  type=click.Path())\n    def perform(paths):\n        for path in paths:\n            click.echo(path)\n\n    if __name__ == '__main__':\n        perform()\n\n.. click:run::\n\n    import os\n    invoke(perform, env={\"PATHS\": f\"./foo/bar{os.path.pathsep}./test\"})\n```\n\n## Other Prefix Characters\n\nClick can deal with prefix characters besides `-` for options. Click can use\n`/`, `+` as well as others. Note that alternative prefix characters are generally used very sparingly if at all within POSIX.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('+w/-w')\n    def chmod(w):\n        click.echo(f\"writable={w}\")\n\n.. click:run::\n\n    invoke(chmod, args=['+w'])\n    invoke(chmod, args=['-w'])\n```\n\nThere are special considerations for using `/` as prefix character, see {ref}`option-boolean-flag` for more.\n\n(optional-value)=\n\n## Optional Value\n\nProviding the value to an option can be made optional, in which case\nproviding only the option's flag without a value will either show a\nprompt or use its `flag_value`.\n\nSetting `is_flag=False, flag_value=value` tells Click that the option\ncan still be passed a value, but if only the flag is given, the\nvalue will be `flag_value`.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option(\"--name\", is_flag=False, flag_value=\"Flag\", default=\"Default\")\n    def hello(name):\n        click.echo(f\"Hello, {name}!\")\n\n.. click:run::\n\n    invoke(hello, args=[])\n    invoke(hello, args=[\"--name\", \"Value\"])\n    invoke(hello, args=[\"--name\"])\n```\n"
  },
  {
    "path": "docs/parameter-types.md",
    "content": "(parameter-types)=\n\n# Parameter Types\n\n```{currentmodule} click\n```\n\nWhen the parameter type is set using `type`, Click will leverage the type to make your life easier, for example adding\ndata to your help pages. Most examples are done with options, but types are available to options and arguments.\n\n```{contents}\n---\ndepth: 2\nlocal: true\n---\n```\n\n## Built-in Types Examples\n\n(choice-opts)=\n\n### Choice\n\nSometimes, you want to have a parameter be a choice of a list of values. In that case you can use {class}`Choice` type.\nIt can be instantiated with a list of valid values. The originally passed choice will be returned, not the str passed on\nthe command line. Token normalization functions and `case_sensitive=False` can cause the two to be different but still\nmatch. {meth}`Choice.normalize_choice` for more info.\n\n\nExample:\n\n```{eval-rst}\n.. click:example::\n\n    import enum\n\n    class HashType(enum.Enum):\n        MD5 = enum.auto()\n        SHA1 = enum.auto()\n\n    @click.command()\n    @click.option('--hash-type',\n                  type=click.Choice(HashType, case_sensitive=False))\n    def digest(hash_type: HashType):\n        click.echo(hash_type)\n\nWhat it looks like:\n\n.. click:run::\n\n    invoke(digest, args=['--hash-type=MD5'])\n    println()\n    invoke(digest, args=['--hash-type=md5'])\n    println()\n    invoke(digest, args=['--hash-type=foo'])\n    println()\n    invoke(digest, args=['--help'])\n```\n\nAny iterable may be passed to {class}`Choice`. If an `Enum` is passed, the names of the enum members will be used as\nvalid choices.\n\nChoices work with options that have `multiple=True`. If a `default` value is given with `multiple=True`, it should be a\nlist or tuple of valid choices.\n\nChoices should be unique after normalization, see {meth}`Choice.normalize_choice` for more info.\n\n```{versionchanged} 7.1\nThe resulting value from an option will always be one of the originally passed choices\nregardless of `case_sensitive`.\n```\n\n(ranges)=\n\n### Int and Float Ranges\n\nThe {class}`IntRange` type extends the {data}`INT` type to ensure the value is contained in the given range. The\n{class}`FloatRange` type does the same for {data}`FLOAT`.\n\nIf `min` or `max` is omitted, that side is *unbounded*. Any value in that direction is accepted. By default, both bounds\nare *closed*, which means the boundary value is included in the accepted range. `min_open` and `max_open` can be used to\nexclude that boundary from the range.\n\nIf `clamp` mode is enabled, a value that is outside the range is set to the boundary instead of failing. For example,\nthe range `0, 5` would return `5` for the value `10`, or `0` for the value `-1`. When using {class}`FloatRange`, `clamp`\ncan only be enabled if both bounds are *closed* (the default).\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option(\"--count\", type=click.IntRange(0, 20, clamp=True))\n    @click.option(\"--digit\", type=click.IntRange(0, 9))\n    def repeat(count, digit):\n        click.echo(str(digit) * count)\n\n.. click:run::\n\n    invoke(repeat, args=['--count=100', '--digit=5'])\n    invoke(repeat, args=['--count=6', '--digit=12'])\n```\n\n## Built-in Types Listing\n\nThe supported parameter {ref}`click-api-types` are\n\n- `str` / {data}`click.STRING`: The default parameter type which indicates unicode strings.\n\n- `int` / {data}`click.INT`: A parameter that only accepts integers.\n\n- `float` / {data}`click.FLOAT`: A parameter that only accepts floating point values.\n\n- `bool` / {data}`click.BOOL`: A parameter that accepts boolean values. This is automatically used for boolean flags.\n  The string values \"1\", \"true\", \"t\", \"yes\", \"y\", and \"on\" convert to `True`. \"0\", \"false\", \"f\", \"no\", \"n\", and \"off\"\n  convert to `False`.\n\n- {data}`click.UUID`: A parameter that accepts UUID values. This is not automatically guessed but represented as\n  {class}`uuid.UUID`.\n\n```{eval-rst}\n*   .. autoclass:: Choice\n       :noindex:\n```\n\n```{eval-rst}\n*   .. autoclass:: DateTime\n       :noindex:\n```\n\n```{eval-rst}\n*   .. autoclass:: File\n       :noindex:\n```\n\n```{eval-rst}\n*   .. autoclass:: FloatRange\n       :noindex:\n```\n\n```{eval-rst}\n*   .. autoclass:: IntRange\n       :noindex:\n```\n\n```{eval-rst}\n*   .. autoclass:: Path\n       :noindex:\n```\n\n## How to Implement Custom Types\n\nTo implement a custom type, you need to subclass the {class}`ParamType` class. For simple cases, passing a Python\nfunction that fails with a `ValueError` is also supported, though discouraged. Override the {meth}`~ParamType.convert`\nmethod to convert the value from a string to the correct type.\n\nThe following code implements an integer type that accepts hex and octal numbers in addition to normal integers, and\nconverts them into regular integers.\n\n```python\nimport click\n\nclass BasedIntParamType(click.ParamType):\n    name = \"integer\"\n\n    def convert(self, value, param, ctx):\n        if isinstance(value, int):\n            return value\n\n        try:\n            if value[:2].lower() == \"0x\":\n                return int(value[2:], 16)\n            elif value[:1] == \"0\":\n                return int(value, 8)\n            return int(value, 10)\n        except ValueError:\n            self.fail(f\"{value!r} is not a valid integer\", param, ctx)\n\nBASED_INT = BasedIntParamType()\n```\n\nThe {attr}`~ParamType.name` attribute is optional and is used for documentation. Call {meth}`~ParamType.fail` if\nconversion fails. The `param` and `ctx` arguments may be `None` in some cases such as prompts.\n\nValues from user input or the command line will be strings, but default values and Python arguments may already be the\ncorrect type. The custom type should check at the top if the value is already valid and pass it through to support those\ncases.\n"
  },
  {
    "path": "docs/parameters.md",
    "content": "(parameters)=\n\n# Parameters\n\n```{currentmodule} click\n```\n\nClick supports only two principle types of parameters for scripts (by design): options and arguments.\n\n## Options\n\n- Are optional.\n- Recommended to use for everything except subcommands, urls, or files.\n- Can take a fixed number of arguments. The default is 1. They may be specified multiple times using {ref}`multiple-options`.\n- Are fully documented by the help page.\n- Have automatic prompting for missing input.\n- Can act as flags (boolean or otherwise).\n- Can be pulled from environment variables.\n\n## Arguments\n\n- Are optional with in reason, but not entirely so.\n- Recommended to use for subcommands, urls, or files.\n- Can take an arbitrary number of arguments.\n- Are not fully documented by the help page since they may be too specific to be automatically documented. For more see {ref}`documenting-arguments`.\n- Can be pulled from environment variables but only explicitly named ones. For more see {ref}`environment-variables`.\n\nOn each principle type you can specify {ref}`parameter-types`. Specifying these types helps Click add details to your help pages and help with the handling of those types.\n\n(parameter-names)=\n\n## Parameter Names\n\nParameters (options and arguments) have a name that will be used as\nthe Python argument name when calling the decorated function with\nvalues.\n\nIn the example, the argument's name is `filename`. The name must match the python arg name. To provide a different name for use in help text, see {ref}`doc-meta-variables`.\nThe option's names are `-t` and `--times`. More names are available for options and are covered in {ref}`options`.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.argument('filename')\n    @click.option('-t', '--times', type=int)\n    def multi_echo(filename, times):\n        \"\"\"Print value filename multiple times.\"\"\"\n        for x in range(times):\n            click.echo(filename)\n\n.. click:run::\n\n    invoke(multi_echo, ['--times=3', 'index.txt'], prog_name='multi_echo')\n```\n"
  },
  {
    "path": "docs/prompts.md",
    "content": "# User Input Prompts\n\n```{currentmodule} click\n```\n\nClick supports prompts in two different places. The first is automated prompts when the parameter handling happens, and\nthe second is to ask for prompts at a later point independently.\n\nThis can be accomplished with the {func}`prompt` function, which asks for valid input according to a type, or the\n{func}`confirm` function, which asks for confirmation (yes/no).\n\n```{contents}\n---\ndepth: 2\nlocal: true\n---\n```\n\n(option-prompting)=\n\n## Option Prompts\n\nOption prompts are integrated into the option interface. Internally, it automatically calls either {func}`prompt` or\n{func}`confirm` as necessary.\n\nIn some cases, you want parameters that can be provided from the command line, but if not provided, ask for user input\ninstead. This can be implemented with Click by defining a prompt string.\n\nExample:\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--name', prompt=True)\n    def hello(name):\n        click.echo(f\"Hello {name}!\")\n\nAnd what it looks like:\n\n.. click:run::\n\n    invoke(hello, args=['--name=John'])\n    invoke(hello, input=['John'])\n```\n\nIf you are not happy with the default prompt string, you can ask for\na different one:\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--name', prompt='Your name please')\n    def hello(name):\n        click.echo(f\"Hello {name}!\")\n\nWhat it looks like:\n\n.. click:run::\n\n    invoke(hello, input=['John'])\n```\n\nIt is advised that prompt not be used in conjunction with the multiple flag set to True. Instead, prompt in the function\ninteractively.\n\nBy default, the user will be prompted for an input if one was not passed through the command line. To turn this behavior\noff, see {ref}`optional-value`.\n\n## Input Prompts\n\nTo manually ask for user input, you can use the {func}`prompt` function. By default, it accepts any Unicode string, but\nyou can ask for any other type. For instance, you can ask for a valid integer:\n\n```python\nvalue = click.prompt('Please enter a valid integer', type=int)\n```\n\nAdditionally, the type will be determined automatically if a default value is provided. For instance, the following will\nonly accept floats:\n\n```python\nvalue = click.prompt('Please enter a number', default=42.0)\n```\n\n## Optional Prompts\n\nIf the option has `prompt` enabled, then setting `prompt_required=False` tells Click to only show the prompt if the\noption's flag is given, instead of if the option is not provided at all.\n\n```{eval-rst}\n.. click:example::\n\n    @click.command()\n    @click.option('--name', prompt=True, prompt_required=False, default=\"Default\")\n    def hello(name):\n        click.echo(f\"Hello {name}!\")\n\n.. click:run::\n\n    invoke(hello)\n    invoke(hello, args=[\"--name\", \"Value\"])\n    invoke(hello, args=[\"--name\"], input=\"Prompt\")\n```\n\nIf `required=True`, then the option will still prompt if it is not given, but it will also prompt if only the flag is\ngiven.\n\n## Confirmation Prompts\n\nTo ask if a user wants to continue with an action, the {func}`confirm` function comes in handy. By default, it returns\nthe result of the prompt as a boolean value:\n\n```python\nif click.confirm('Do you want to continue?'):\n    click.echo('Well done!')\n```\n\nThere is also the option to make the function automatically abort the execution of the program if it does not return\n`True`:\n\n```python\nclick.confirm('Do you want to continue?', abort=True)\n```\n\n## Dynamic Defaults for Prompts\n\nThe `auto_envvar_prefix` and `default_map` options for the context allow the program to read option values from the\nenvironment or a configuration file. However, this overrides the prompting mechanism, so that the user does not get the\noption to change the value interactively.\n\nIf you want to let the user configure the default value, but still be prompted if the option isn't specified on the\ncommand line, you can do so by supplying a callable as the default value. For example, to get a default from the\nenvironment:\n\n```python\nimport os\n\n@click.command()\n@click.option(\n    \"--username\", prompt=True,\n    default=lambda: os.environ.get(\"USER\", \"\")\n)\ndef hello(username):\n    click.echo(f\"Hello, {username}!\")\n```\n\nTo describe what the default value will be, set it in ``show_default``.\n\n```{eval-rst}\n.. click:example::\n\n    import os\n\n    @click.command()\n    @click.option(\n        \"--username\", prompt=True,\n        default=lambda: os.environ.get(\"USER\", \"\"),\n        show_default=\"current user\"\n    )\n    def hello(username):\n        click.echo(f\"Hello, {username}!\")\n\n.. click:run::\n\n   invoke(hello, args=[\"--help\"])\n```\n"
  },
  {
    "path": "docs/quickstart.md",
    "content": "# Quickstart\n\n```{currentmodule} click\n```\n\n## Install\n\nInstall from PyPI:\n\n```console\npip install click\n```\n\nInstalling into a virtual environment is highly recommended. We suggest {ref}`virtualenv-heading`.\n\n## Examples\n\nSome standalone examples of Click applications are packaged with Click. They are available in the\n[examples folder](https://github.com/pallets/click/tree/main/examples) of the repo.\n\n- [inout](https://github.com/pallets/click/tree/main/examples/inout) : A very simple example of an application that can\n  read from files and write to files and also accept input from stdin or write to stdout.\n- [validation](https://github.com/pallets/click/tree/main/examples/validation) : A simple example of an application that\n  performs custom validation of parameters in different ways.\n- [naval](https://github.com/pallets/click/tree/main/examples/naval) : Port of the [docopt](http://docopt.org/) naval\n  example.\n- [colors](https://github.com/pallets/click/tree/main/examples/colors) : A simple example that colorizes text. Uses\n  colorama on Windows.\n- [aliases](https://github.com/pallets/click/tree/main/examples/aliases) : An advanced example that implements\n  {ref}`aliases`.\n- [imagepipe](https://github.com/pallets/click/tree/main/examples/imagepipe) : A complex example that implements some\n  {ref}`command-pipelines` . It chains together image processing instructions. Requires pillow.\n- [repo](https://github.com/pallets/click/tree/main/examples/repo) : An advanced example that implements a\n  Git-/Mercurial-like command line interface.\n- [complex](https://github.com/pallets/click/tree/main/examples/complex) : A very advanced example that implements\n  loading subcommands dynamically from a plugin folder.\n- [termui](https://github.com/pallets/click/tree/main/examples/termui) : A simple example that showcases terminal UI\n  helpers provided by click.\n\n## Basic Concepts - Creating a Command\n\nClick is based on declaring commands through decorators. Internally, there is a non-decorator interface for advanced use\ncases, but it's discouraged for high-level usage.\n\nA function becomes a Click command line tool by decorating it through {func}`command`. At its simplest, just\ndecorating a function with this decorator will make it into a callable script:\n\n\n```{eval-rst}\n.. click:example::\n    import click\n\n    @click.command()\n    def hello():\n        click.echo('Hello World!')\n\nWhat's happening is that the decorator converts the function into a :class:`Command` which then can be invoked:\n\n.. click:example::\n    if __name__ == '__main__':\n        hello()\n\nAnd what it looks like:\n\n.. click:run::\n    invoke(hello, args=[], prog_name='python hello.py')\n\nAnd the corresponding help page:\n\n.. click:run::\n    invoke(hello, args=['--help'], prog_name='python hello.py')\n```\n\n## Echoing\n\nWhy does this example use {func}`echo` instead of the regular {func}`print` function? The answer to this question is\nthat Click attempts to support different environments consistently and to be very robust even when the environment is\nmisconfigured. Click wants to be functional at least on a basic level even if everything is completely broken.\n\nWhat this means is that the {func}`echo` function applies some error correction in case the terminal is misconfigured\ninstead of dying with a {exc}`UnicodeError`.\n\nThe echo function also supports color and other styles in output. It will automatically remove styles if the output\nstream is a file. On Windows, colorama is automatically installed and used. See {ref}`ansi-colors`.\n\nIf you don't need this, you can also use the `print()` construct / function.\n\n## Nesting Commands\n\nCommands can be attached to other commands of type {class}`Group`. This allows arbitrary nesting of scripts. As an\nexample here is a script that implements two commands for managing databases:\n\n```{eval-rst}\n.. click:example::\n    @click.group()\n    def cli():\n        pass\n\n    @click.command()\n    def initdb():\n        click.echo('Initialized the database')\n\n    @click.command()\n    def dropdb():\n        click.echo('Dropped the database')\n\n    cli.add_command(initdb)\n    cli.add_command(dropdb)\n```\n\nAs you can see, the {func}`group` decorator works like the {func}`command` decorator, but creates a {class}`Group`\nobject instead which can be given multiple subcommands that can be attached with {meth}`Group.add_command`.\n\nFor simple scripts, it's also possible to automatically attach and create a command by using the {meth}`Group.command`\ndecorator instead. The above script can instead be written like this:\n\n```{eval-rst}\n.. click:example::\n    @click.group()\n    def cli():\n        pass\n\n    @cli.command()\n    def initdb():\n        click.echo('Initialized the database')\n\n    @cli.command()\n    def dropdb():\n        click.echo('Dropped the database')\n\nYou would then invoke the :class:`Group` in your entry points or other invocations:\n\n.. click:example::\n    if __name__ == '__main__':\n        cli()\n```\n\n## Registering Commands Later\n\nInstead of using the `@group.command()` decorator, commands can be decorated with the plain `@command()` decorator\nand registered with a group later with `group.add_command()`. This could be used to split commands into multiple Python\nmodules.\n\n```{code-block} python\n    @click.command()\n    def greet():\n        click.echo(\"Hello, World!\")\n```\n\n```{code-block} python\n    @click.group()\n    def group():\n        pass\n\n    group.add_command(greet)\n```\n\n## Adding Parameters\n\nTo add parameters, use the {func}`option` and {func}`argument` decorators:\n\n```{eval-rst}\n.. click:example::\n    @click.command()\n    @click.option('--count', default=1, help='number of greetings')\n    @click.argument('name')\n    def hello(count, name):\n        for x in range(count):\n            click.echo(f\"Hello {name}!\")\n\nWhat it looks like:\n\n.. click:run::\n    invoke(hello, args=['--help'], prog_name='python hello.py')\n```\n\n## Switching to Entry Points\n\nIn the code you wrote so far there is a block at the end of the file which looks like this:\n`if __name__ == '__main__':`. This is traditionally how a standalone Python file looks like. With Click you can continue\ndoing that, but a better way is to package your app with an entry point.\n\nThere are two main (and many more) reasons for this:\n\nThe first one is that installers automatically generate executable wrappers for Windows so your command line utilities\nwork on Windows too.\n\nThe second reason is that entry point scripts work with virtualenv on Unix without the virtualenv having to be\nactivated. This is a very useful concept which allows you to bundle your scripts with all requirements into a\nvirtualenv.\n\nClick is perfectly equipped to work with that and in fact the rest of the documentation will assume that you are writing\napplications as distributed packages.\n\nLook at the {doc}`entry-points` chapter before reading the rest as the examples assume that you will be using entry\npoints.\n"
  },
  {
    "path": "docs/setuptools.md",
    "content": "---\norphan: true\n---\n\n# Setuptools Integration\n\nMoved to {doc}`entry-points`.\n"
  },
  {
    "path": "docs/shell-completion.md",
    "content": "(shell-completion)=\n\n# Shell Completion\n\n```{currentmodule} click.shell_completion\n```\n\nClick provides tab completion support for Bash (version 4.4 and up), Zsh, and Fish. It is possible to add support for\nother shells too, and suggestions can be customized at multiple levels.\n\nShell completion suggests command names, option names, and values for choice, file, and path parameter types. Options\nare only listed if at least a dash has been entered. Hidden commands and options are not shown.\n\n```console\n$ repo <TAB><TAB>\nclone  commit  copy  delete  setuser\n$ repo clone -<TAB><TAB>\n--deep  --help  --rev  --shallow  -r\n```\n\n## Enabling Completion\n\nCompletion is only available if a script is installed and invoked through an entry point, not through the `python`\ncommand. See {doc}`entry-points`. Once the executable is installed, calling it with a special environment variable will\nput Click in completion mode.\n\nTo enable shell completion, the user needs to register a special function with their shell. The exact script varies\ndepending on the shell you are using. Click will output it when called with `_{FOO_BAR}_COMPLETE` set to\n`{shell}_source`. `{FOO_BAR}` is the executable name in uppercase with dashes replaced by underscores. It is\nconventional but not strictly required for environment variable names to be in upper case. This convention helps\ndistinguish environment variables from regular shell variables and commands, making scripts and configuration files more\nreadable and easier to maintain. The built-in shells are `bash`, `zsh`, and `fish`.\n\nProvide your users with the following instructions customized to your program name. This uses `foo-bar` as an example.\n\n```{eval-rst}\n.. tabs::\n\n    .. group-tab:: Bash\n\n        Add this to ``~/.bashrc``:\n\n        .. code-block:: bash\n\n            eval \"$(_FOO_BAR_COMPLETE=bash_source foo-bar)\"\n\n    .. group-tab:: Zsh\n\n        Add this to ``~/.zshrc``:\n\n        .. code-block:: zsh\n\n            eval \"$(_FOO_BAR_COMPLETE=zsh_source foo-bar)\"\n\n    .. group-tab:: Fish\n\n        Add this to ``~/.config/fish/completions/foo-bar.fish``:\n\n        .. code-block:: fish\n\n            _FOO_BAR_COMPLETE=fish_source foo-bar | source\n\n        This is the same file used for the activation script method\n        below. For Fish it's probably always easier to use that method.\n```\n\nUsing `eval` means that the command is invoked and evaluated every time a shell is started, which can delay shell\nresponsiveness. To speed it up, write the generated script to a file, then source that. You can generate the files ahead\nof time and distribute them with your program to save your users a step.\n\n```{eval-rst}\n.. tabs::\n\n    .. group-tab:: Bash\n\n        Save the script somewhere.\n\n        .. code-block:: bash\n\n            _FOO_BAR_COMPLETE=bash_source foo-bar > ~/.foo-bar-complete.bash\n\n        Source the file in ``~/.bashrc``.\n\n        .. code-block:: bash\n\n            . ~/.foo-bar-complete.bash\n\n    .. group-tab:: Zsh\n\n        Save the script somewhere.\n\n        .. code-block:: bash\n\n            _FOO_BAR_COMPLETE=zsh_source foo-bar > ~/.foo-bar-complete.zsh\n\n        Source the file in ``~/.zshrc``.\n\n        .. code-block:: bash\n\n            . ~/.foo-bar-complete.zsh\n\n    .. group-tab:: Fish\n\n        Save the script to ``~/.config/fish/completions/foo-bar.fish``:\n\n        .. code-block:: fish\n\n            _FOO_BAR_COMPLETE=fish_source foo-bar > ~/.config/fish/completions/foo-bar.fish\n```\n\nAfter modifying the shell config, you need to start a new shell in order for the changes to be loaded.\n\n## Custom Type Completion\n\nWhen creating a custom {class}`~click.ParamType`, override its {meth}`~click.ParamType.shell_complete` method to provide\nshell completion for parameters with the type. The method must return a list of {class}`~CompletionItem` objects.\nBesides the value, these objects hold metadata that shell support might use. The built-in implementations use `type` to\nindicate special handling for paths, and `help` for shells that support showing a help string next to a suggestion.\n\nIn this example, the type will suggest environment variables that start with the incomplete value.\n\n```python\nclass EnvVarType(ParamType):\n    name = \"envvar\"\n\n    def shell_complete(self, ctx, param, incomplete):\n        return [\n            CompletionItem(name)\n            for name in os.environ if name.startswith(incomplete)\n        ]\n\n@click.command()\n@click.option(\"--ev\", type=EnvVarType())\ndef cli(ev):\n    click.echo(os.environ[ev])\n```\n\n## Overriding Value Completion\n\nValue completions for a parameter can be customized without a custom type by providing a `shell_complete` function. The\nfunction is used instead of any completion provided by the type. It is passed 3 positional arguments:\n\n- `ctx` - The current command context.\n- `param` - The current parameter requesting completion.\n- `incomplete` - The partial word that is being completed. May be an empty string if no characters have been entered\n  yet.\n\nIt must return a list of {class}`CompletionItem` objects, or as a shortcut it can return a list of strings.\n\nIn this example, the command will suggest environment variables that start with the incomplete value.\n\n```python\ndef complete_env_vars(ctx, param, incomplete):\n    return [k for k in os.environ if k.startswith(incomplete)]\n\n@click.command()\n@click.argument(\"name\", shell_complete=complete_env_vars)\ndef cli(name):\n    click.echo(f\"Name: {name}\")\n    click.echo(f\"Value: {os.environ[name]}\")\n```\n\n## Adding Support for a Shell\n\nSupport can be added for shells that do not come built in. Be sure to check PyPI to see if there's already a package\nthat adds support for your shell. This topic is very technical, you'll want to look at Click's source to study the\nbuilt-in implementations.\n\nShell support is provided by subclasses of {class}`ShellComplete` registered with {func}`add_completion_class`. When\nClick is invoked in completion mode, it calls {meth}`~ShellComplete.source` to output the completion script, or\n{meth}`~ShellComplete.complete` to output completions. The base class provides default implementations that require\nimplementing some smaller parts.\n\nFirst, you'll need to figure out how your shell's completion system works and write a script to integrate it with Click.\nIt must invoke your program with the environment variable `_{FOO_BAR}_COMPLETE` set to `{shell}_complete` and pass the\ncomplete args and incomplete value. How it passes those values, and the format of the completion response from Click is\nup to you.\n\nIn your subclass, set {attr}`ShellComplete.source_template` to the completion script. The default implementation will\nperform `%` formatting with the following variables:\n\n- `complete_func` - A safe name for the completion function defined in the script.\n- `complete_var` - The environment variable name for passing the `{shell}_complete` instruction.\n- `foo_bar` - The name of the executable being completed.\n\nThe example code is for a made up shell \"My Shell\" or \"mysh\" for short.\n\n```python\nfrom click.shell_completion import add_completion_class\nfrom click.shell_completion import ShellComplete\n\n_mysh_source = \"\"\"\\\n%(complete_func)s {\n    response=$(%(complete_var)s=mysh_complete %(foo_bar)s)\n    # parse response and set completions somehow\n}\ncall-on-complete %(foo_bar)s %(complete_func)s\n\"\"\"\n\n@add_completion_class\nclass MyshComplete(ShellComplete):\n    name = \"mysh\"\n    source_template = _mysh_source\n```\n\nNext, implement {meth}`~ShellComplete.get_completion_args`. This must get, parse, and return the complete args and\nincomplete value from the completion script. For example, for the Bash implementation the `COMP_WORDS` env var contains\nthe command line args as a string, and the `COMP_CWORD` env var contains the index of the incomplete arg. The method\nmust return a `(args, incomplete)` tuple.\n\n```python\nimport os\nfrom click.parser import split_arg_string\n\nclass MyshComplete(ShellComplete):\n    ...\n\n    def get_completion_args(self):\n        args = split_arg_string(os.environ[\"COMP_WORDS\"])\n\n        if os.environ[\"COMP_PARTIAL\"] == \"1\":\n            incomplete = args.pop()\n            return args, incomplete\n\n        return args, \"\"\n```\n\nFinally, implement {meth}`~ShellComplete.format_completion`. This is called to format each {class}`CompletionItem` into a string. For example, the Bash implementation returns `f\"{item.type},{item.value}` (it doesn't support help strings), and the Zsh implementation returns each part separated by a newline, replacing empty help with a `_` placeholder. This format is entirely up to what you parse with your completion script.\n\nThe `type` value is usually `plain`, but it can be another value that the completion script can switch on. For example,\n`file` or `dir` can tell the shell to handle path completion, since the shell is better at that than Click.\n\n```python\nclass MyshComplete(ShellComplete):\n    ...\n\n    def format_completion(self, item):\n        return f\"{item.type}\\t{item.value}\"\n```\n\nWith those three things implemented, the new shell support is ready. In case those weren't sufficient, there are more\nparts that can be overridden, but that probably isn't necessary.\n\nThe activation instructions will again depend on how your shell works. Use the following to generate the completion\nscript, then load it into the shell somehow.\n\n```console\n_FOO_BAR_COMPLETE=mysh_source foo-bar\n```\n"
  },
  {
    "path": "docs/support-multiple-versions.md",
    "content": "# Supporting Multiple Versions\n\nIf you are a library maintainer, you may want to support multiple versions of\nClick. See the Pallets [version policy] for information about our version\nnumbers and support policy.\n\n[version policy]: https://palletsprojects.com/versions\n\nMost features of Click are stable across releases, and don't require special\nhandling. However, feature releases may deprecate and change APIs. Occasionally,\na change will require special handling.\n\n## Use Feature Detection\n\nPrefer using feature detection. Looking at the version can be tempting, but is\noften more brittle or results in more complicated code. Try to use `if` or `try`\nblocks to decide whether to use a new or old pattern.\n\nIf you do need to look at the version, use {func}`importlib.metadata.version`,\nthe standardized way to get versions for any installed Python package.\n\n## Changes in 8.2\n\n### `ParamType` methods require `ctx`\n\nIn 8.2, several methods of `ParamType` now have a `ctx: click.Context`\nargument. Because this changes the signature of the methods from 8.1, it's not\nobvious how to support both when subclassing or calling.\n\nThis example uses `ParamType.get_metavar`, and the same technique should be\napplicable to other methods such as `get_missing_message`.\n\nUpdate your methods overrides to take the new `ctx` argument. Use the\nfollowing decorator to wrap each method. In 8.1, it will get the context where\npossible and pass it using the 8.2 signature.\n\n```python\nimport functools\nimport typing as t\nimport click\n\nF = t.TypeVar(\"F\", bound=t.Callable[..., t.Any])\n\ndef add_ctx_arg(f: F) -> F:\n    @functools.wraps(f)\n    def wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any:\n        if \"ctx\" not in kwargs:\n            kwargs[\"ctx\"] = click.get_current_context(silent=True)\n\n        return f(*args, **kwargs)\n\n    return wrapper  # type: ignore[return-value]\n```\n\nHere's an example ``ParamType`` subclass which uses this:\n\n```python\nclass CommaDelimitedString(click.ParamType):\n    @add_ctx_arg\n    def get_metavar(self, param: click.Parameter, ctx: click.Context | None) -> str:\n        return \"TEXT,TEXT,...\"\n```\n"
  },
  {
    "path": "docs/testing.md",
    "content": "# Testing Click Applications\n\n```{eval-rst}\n.. currentmodule:: click.testing\n```\n\nClick provides the {ref}`click.testing <testing>` module to help you invoke command line applications and check their behavior.\n\nThese tools should only be used for testing since they change\nthe entire interpreter state for simplicity. They are not thread-safe!\n\nThe examples use [pytest](https://docs.pytest.org/en/stable/) style tests.\n\n```{contents}\n:depth: 1\n:local: true\n```\n\n## Basic Example\n\nThe key pieces are:\n  - {class}`CliRunner` - used to invoke commands as command line scripts.\n  - {class}`Result` - returned from {meth}`CliRunner.invoke`. Captures output data, exit code, optional exception, and captures the output as bytes and binary data.\n\n```{code-block} python\n:caption: hello.py\n\nimport click\n\n@click.command()\n@click.argument('name')\ndef hello(name):\n   click.echo(f'Hello {name}!')\n```\n\n```{code-block} python\n:caption: test_hello.py\n\nfrom click.testing import CliRunner\nfrom hello import hello\n\ndef test_hello_world():\n  runner = CliRunner()\n  result = runner.invoke(hello, ['Peter'])\n  assert result.exit_code == 0\n  assert result.output == 'Hello Peter!\\n'\n```\n\n## Subcommands\n\nA subcommand name must be specified in the `args` parameter {meth}`CliRunner.invoke`:\n\n```{code-block} python\n:caption: sync.py\n\nimport click\n\n@click.group()\n@click.option('--debug/--no-debug', default=False)\ndef cli(debug):\n   click.echo(f\"Debug mode is {'on' if debug else 'off'}\")\n\n@cli.command()\ndef sync():\n   click.echo('Syncing')\n```\n\n```{code-block} python\n:caption: test_sync.py\n\nfrom click.testing import CliRunner\nfrom sync import cli\n\ndef test_sync():\n  runner = CliRunner()\n  result = runner.invoke(cli, ['--debug', 'sync'])\n  assert result.exit_code == 0\n  assert 'Debug mode is on' in result.output\n  assert 'Syncing' in result.output\n```\n\n## Context Settings\n\nAdditional keyword arguments passed to {meth}`CliRunner.invoke` will be used to construct the initial {class}`Context object <click.Context>`.\nFor example, setting a fixed terminal width equal to 60:\n\n```{code-block} python\n:caption: sync.py\n\nimport click\n\n@click.group()\ndef cli():\n   pass\n\n@cli.command()\ndef sync():\n   click.echo('Syncing')\n```\n\n```{code-block} python\n:caption: test_sync.py\n\nfrom click.testing import CliRunner\nfrom sync import cli\n\ndef test_sync():\n  runner = CliRunner()\n  result = runner.invoke(cli, ['sync'], terminal_width=60)\n  assert result.exit_code == 0\n  assert 'Debug mode is on' in result.output\n  assert 'Syncing' in result.output\n```\n\n## File System Isolation\n\nThe {meth}`CliRunner.isolated_filesystem` context manager sets the current working directory to a new, empty folder.\n\n```{code-block} python\n:caption: cat.py\n\nimport click\n\n@click.command()\n@click.argument('f', type=click.File())\ndef cat(f):\n   click.echo(f.read())\n```\n\n```{code-block} python\n:caption: test_cat.py\n\nfrom click.testing import CliRunner\nfrom cat import cat\n\ndef test_cat():\n   runner = CliRunner()\n   with runner.isolated_filesystem():\n      with open('hello.txt', 'w') as f:\n          f.write('Hello World!')\n\n      result = runner.invoke(cat, ['hello.txt'])\n      assert result.exit_code == 0\n      assert result.output == 'Hello World!\\n'\n```\n\nPass in a path to control where the temporary directory is created.\nIn this case, the directory will not be removed by Click. Its useful\nto integrate with a framework like Pytest that manages temporary files.\n\n```{code-block} python\n:caption: test_cat.py\n\nfrom click.testing import CliRunner\nfrom cat import cat\n\ndef test_cat_with_path_specified():\n   runner = CliRunner()\n   with runner.isolated_filesystem('~/test_folder'):\n      with open('hello.txt', 'w') as f:\n          f.write('Hello World!')\n\n      result = runner.invoke(cat, ['hello.txt'])\n      assert result.exit_code == 0\n      assert result.output == 'Hello World!\\n'\n```\n\n## Input Streams\n\nThe test wrapper can provide input data for the input stream (stdin). This is very useful for testing prompts.\n\n```{code-block} python\n:caption: prompt.py\n\nimport click\n\n@click.command()\n@click.option('--foo', prompt=True)\ndef prompt(foo):\n   click.echo(f\"foo={foo}\")\n```\n\n```{code-block} python\n:caption: test_prompt.py\n\nfrom click.testing import CliRunner\nfrom prompt import prompt\n\ndef test_prompts():\n   runner = CliRunner()\n   result = runner.invoke(prompt, input='wau wau\\n')\n   assert not result.exception\n   assert result.output == 'Foo: wau wau\\nfoo=wau wau\\n'\n```\n\nPrompts will be emulated so they write the input data to\nthe output stream as well. If hidden input is expected then this\ndoes not happen.\n"
  },
  {
    "path": "docs/unicode-support.md",
    "content": "# Unicode Support\n\n```{currentmodule} click\n```\n\nClick has to take extra care to support Unicode text in different environments.\n\n- The command line in Unix is traditionally bytes, not Unicode. While there are encoding hints, there are some\n  situations where this can break. The most common one is SSH connections to machines with different locales.\n\n  Misconfigured environments can cause a wide range of Unicode problems due to the lack of support for roundtripping\n  surrogate escapes. This will not be fixed in Click itself!\n\n- Standard input and output is opened in text mode by default. Click has to reopen the stream in binary mode in certain\n  situations. Because there is no standard way to do this, it might not always work. Primarily this can become a problem\n  when testing command-line applications.\n\n  This is not supported:\n\n  ```python\n  sys.stdin = io.StringIO('Input here')\n  sys.stdout = io.StringIO()\n  ```\n\n  Instead you need to do this:\n\n  ```python\n  input = 'Input here'\n  in_stream = io.BytesIO(input.encode('utf-8'))\n  sys.stdin = io.TextIOWrapper(in_stream, encoding='utf-8')\n  out_stream = io.BytesIO()\n  sys.stdout = io.TextIOWrapper(out_stream, encoding='utf-8')\n  ```\n\n  Remember in that case, you need to use `out_stream.getvalue()` and not `sys.stdout.getvalue()` if you want to access\n  the buffer contents as the wrapper will not forward that method.\n\n- `sys.stdin`, `sys.stdout` and `sys.stderr` are by default text-based. When Click needs a binary stream, it attempts to\n  discover the underlying binary stream.\n\n- `sys.argv` is always text. This means that the native type for input values to the types in Click is Unicode, not\n  bytes.\n\n  This causes problems if the terminal is incorrectly set and Python does not figure out the encoding. In that case, the\n  Unicode string will contain error bytes encoded as surrogate escapes.\n\n- When dealing with files, Click will always use the Unicode file system API by using the operating system's reported or\n  guessed filesystem encoding. Surrogates are supported for filenames, so it should be possible to open files through\n  the {func}`File` type even if the environment is misconfigured.\n\n## Surrogate Handling\n\nClick does all the Unicode handling in the standard library and is subject to its behavior. Unicode requires extra care.\nThe reason for this is that the encoding detection is done in the interpreter, and on Linux and certain other operating\nsystems, its encoding handling is problematic.\n\nThe biggest source of frustration is that Click scripts invoked by init systems, deployment tools, or cron jobs will\nrefuse to work unless a Unicode locale is exported.\n\nIf Click encounters such an environment it will prevent further execution to force you to set a locale. This is done\nbecause Click cannot know about the state of the system once it's invoked and restore the values before Python's Unicode\nhandling kicked in.\n\nIf you see something like this error:\n\n```console\nTraceback (most recent call last):\n  ...\nRuntimeError: Click will abort further execution because Python was\n  configured to use ASCII as encoding for the environment. Consult\n  https://click.palletsprojects.com/unicode-support/ for mitigation\n  steps.\n```\n\nYou are dealing with an environment where Python thinks you are restricted to ASCII data. The solution to these problems\nis different depending on which locale your computer is running in.\n\nFor instance, if you have a German Linux machine, you can fix the problem by exporting the locale to `de_DE.utf-8`:\n\n```console\nexport LC_ALL=de_DE.utf-8\nexport LANG=de_DE.utf-8\n```\n\nIf you are on a US machine, `en_US.utf-8` is the encoding of choice. On some newer Linux systems, you could also try\n`C.UTF-8` as the locale:\n\n```console\nexport LC_ALL=C.UTF-8\nexport LANG=C.UTF-8\n```\n\nOn some systems it was reported that `UTF-8` has to be written as `UTF8` and vice versa. To see which locales are\nsupported you can invoke `locale -a`.\n\nYou need to export the values before you invoke your Python script.\n\nIn Python 3.7 and later you will no longer get a `RuntimeError` in many cases thanks to {pep}`538` and {pep}`540`, which\nchanged the default assumption in unconfigured environments. This doesn't change the general issue that your locale may\nbe misconfigured.\n"
  },
  {
    "path": "docs/upgrade-guides.md",
    "content": "# Upgrade Guides\n\n```{contents}\n:depth: 1\n:local: true\n```\n\n## Upgrading 8.3.X to 9.0\n**This is under active construction and will not be finalized until 9.0.0 is released.**\n\nThis guide assumes the user is on version 8.3.X.\n\n### Deprecations\n\nFor each deprecation, provide a brief explanation, and direct users to new function / class if available.\n- TBD\n\n### Removals with prior deprecation\n\nFor each removal, provide a brief explanation, and direct users to new function / class if available. If possible, deprecate and remove in 10.0.0, rather than removing outright.\n- TBD\n\n### Removals with no prior deprecation\n\nThe changes were not able to be deprecated prior to removal. Explain clearly why then were not able to be deprecated first.\n- TBD\n\n### Changes\n\n- TBD\n\n### Fixes\n\n- TBD\n"
  },
  {
    "path": "docs/utils.md",
    "content": "# Utilities\n\n```{currentmodule} click\n```\n\nBesides the functionality that Click provides to interface with argument parsing and handling, it also provides a bunch\nof addon functionality that is useful for writing command line utilities.\n\n## Printing to Stdout\n\nThe most obvious helper is the {func}`echo` function, which in many ways works like the Python `print` statement or\nfunction. The main difference is that it works the same in many different terminal environments.\n\nExample:\n\n```python\nimport click\n\nclick.echo('Hello World!')\n```\n\nIt can output both text and binary data. It will emit a trailing newline by default, which needs to be suppressed by\npassing `nl=False`:\n\n```python\nclick.echo(b'\\xe2\\x98\\x83', nl=False)\n```\n\nLast but not least {func}`echo` uses click's intelligent internal output streams to stdout and stderr which support\nunicode output on the Windows console. This means for as long as you are using `click.echo` you can output unicode\ncharacters (there are some limitations on the default font with regards to which characters can be displayed).\n\n```{versionadded} 6.0\n```\n\nClick emulates output streams on Windows to support unicode to the Windows console through separate APIs. For more\ninformation see {doc}`wincmd`.\n\n```{versionadded} 3.0\n```\n\nYou can also easily print to standard error by passing `err=True`:\n\n```python\nclick.echo('Hello World!', err=True)\n```\n\n(ansi-colors)=\n\n## ANSI Colors\n\n```{versionadded} 2.0\n```\n\nThe {func}`echo` function supports ANSI colors and styles. On Windows this uses [colorama](https://pypi.org/project/colorama/).\n\nPrimarily this means that:\n\n- Click's {func}`echo` function will automatically strip ANSI color codes if the stream is not connected to a terminal.\n- the {func}`echo` function will transparently connect to the terminal on Windows and translate ANSI codes to terminal\n  API calls. This means that colors will work on Windows the same way they do on other operating systems.\n\nOn Windows, Click uses colorama without calling `colorama.init()`. You can still call that in your code, but it's not\nrequired for Click.\n\nFor styling a string, the {func}`style` function can be used:\n\n```python\nimport click\n\nclick.echo(click.style('Hello World!', fg='green'))\nclick.echo(click.style('Some more text', bg='blue', fg='white'))\nclick.echo(click.style('ATTENTION', blink=True, bold=True))\n```\n\nThe combination of {func}`echo` and {func}`style` is also available in a single function called {func}`secho`:\n\n```python\nclick.secho('Hello World!', fg='green')\nclick.secho('Some more text', bg='blue', fg='white')\nclick.secho('ATTENTION', blink=True, bold=True)\n```\n\n## Pager Support\n\nIn some situations, you might want to show long texts on the terminal and let a user scroll through it. This can be\nachieved by using the {func}`echo_via_pager` function which works similarly to the {func}`echo` function, but always\nwrites to stdout and, if possible, through a pager.\n\nExample:\n\n```{eval-rst}\n.. click:example::\n    @click.command()\n    def less():\n        click.echo_via_pager(\"\\n\".join(f\"Line {idx}\" for idx in range(200)))\n```\n\nIf you want to use the pager for a lot of text, especially if generating everything in advance would take a lot of time,\nyou can pass a generator (or generator function) instead of a string:\n\n```{eval-rst}\n.. click:example::\n    def _generate_output():\n        for idx in range(50000):\n            yield f\"Line {idx}\\n\"\n\n    @click.command()\n    def less():\n        click.echo_via_pager(_generate_output())\n```\n\n## Screen Clearing\n\n```{versionadded} 2.0\n```\n\nTo clear the terminal screen, you can use the {func}`clear` function that is provided starting with Click 2.0. It does\nwhat the name suggests: it clears the entire visible screen in a platform-agnostic way:\n\n```python\nimport click\nclick.clear()\n```\n\n## Getting Characters from Terminal\n\n```{versionadded} 2.0\n```\n\nNormally, when reading input from the terminal, you would read from standard input. However, this is buffered input and\nwill not show up until the line has been terminated. In certain circumstances, you might not want to do that and instead\nread individual characters as they are being written.\n\nFor this, Click provides the {func}`getchar` function which reads a single character from the terminal buffer and\nreturns it as a Unicode character.\n\nNote that this function will always read from the terminal, even if stdin is instead a pipe.\n\nExample:\n\n```python\nimport click\n\nclick.echo('Continue? [yn] ', nl=False)\nc = click.getchar()\nclick.echo()\nif c == 'y':\n    click.echo('We will go on')\nelif c == 'n':\n    click.echo('Abort!')\nelse:\n    click.echo('Invalid input :(')\n```\n\nNote that this reads raw input, which means that things like arrow keys will show up in the platform's native escape\nformat. The only characters translated are `^C` and `^D` which are converted into keyboard interrupts and end of file\nexceptions respectively. This is done because otherwise, it's too easy to forget about that and to create scripts that\ncannot be properly exited.\n\n## Waiting for Key Press\n\n```{versionadded} 2.0\n```\n\nSometimes, it's useful to pause until the user presses any key on the keyboard. This is especially useful on Windows\nwhere `cmd.exe` will close the window at the end of the command execution by default, instead of waiting.\n\nIn click, this can be accomplished with the {func}`pause` function. This function will print a quick message to the\nterminal (which can be customized) and wait for the user to press a key. In addition to that, it will also become a NOP\n(no operation instruction) if the script is not run interactively.\n\nExample:\n\n```python\nimport click\nclick.pause()\n```\n\n## Launching Editors\n\n```{versionadded} 2.0\n```\n\nClick supports launching editors automatically through {func}`edit`. This is very useful for asking users for multi-line\ninput. It will automatically open the user's defined editor or fall back to a sensible default. If the user closes the\neditor without saving, the return value will be `None`, otherwise the entered text.\n\nExample usage:\n\n```python\nimport click\n\ndef get_commit_message():\n    MARKER = '# Everything below is ignored\\n'\n    message = click.edit('\\n\\n' + MARKER)\n    if message is not None:\n        return message.split(MARKER, 1)[0].rstrip('\\n')\n```\n\nAlternatively, the function can also be used to launch editors for files by a specific filename. In this case, the\nreturn value is always `None`.\n\nExample usage:\n\n```python\nimport click\nclick.edit(filename='/etc/passwd')\n```\n\n## Launching Applications\n\n```{versionadded} 2.0\n```\n\nClick supports launching applications through {func}`launch`. This can be used to open the default application\nassociated with a URL or filetype. This can be used to launch web browsers or picture viewers, for instance. In addition\nto this, it can also launch the file manager and automatically select the provided file.\n\nExample usage:\n\n```python\nclick.launch(\"https://click.palletsprojects.com/\")\nclick.launch(\"/my/downloaded/file.txt\", locate=True)\n```\n\n## Printing Filenames\n\nBecause filenames might not be Unicode, formatting them can be a bit tricky.\n\nThe way this works with click is through the {func}`format_filename` function. It does a best-effort conversion of the\nfilename to Unicode and will never fail. This makes it possible to use these filenames in the context of a full Unicode\nstring.\n\nExample:\n\n```python\nclick.echo(f\"Path: {click.format_filename(b'foo.txt')}\")\n```\n\n## Standard Streams\n\nFor command line utilities, it's very important to get access to input and output streams reliably. Python generally\nprovides access to these streams through `sys.stdout` and friends, but unfortunately, there are API differences between\n2.x and 3.x, especially with regards to how these streams respond to Unicode and binary data.\n\nBecause of this, click provides the {func}`get_binary_stream` and {func}`get_text_stream` functions, which produce\nconsistent results with different Python versions and for a wide variety of terminal configurations.\n\nThe end result is that these functions will always return a functional stream object (except in very odd cases; see\n{doc}`/unicode-support`).\n\nExample:\n\n```python\nimport click\n\nstdin_text = click.get_text_stream('stdin')\nstdout_binary = click.get_binary_stream('stdout')\n```\n\n```{versionadded} 6.0\n```\n\nClick now emulates output streams on Windows to support unicode to the Windows console through separate APIs. For more\ninformation see {doc}`wincmd`.\n\n## Intelligent File Opening\n\n```{versionadded} 3.0\n```\n\nStarting with Click 3.0 the logic for opening files from the {func}`File` type is exposed through the {func}`open_file`\nfunction. It can intelligently open stdin/stdout as well as any other file.\n\nExample:\n\n```python\nimport click\n\nstdout = click.open_file('-', 'w')\ntest_file = click.open_file('test.txt', 'w')\n```\n\nIf stdin or stdout are returned, the return value is wrapped in a special file where the context manager will prevent\nthe closing of the file. This makes the handling of standard streams transparent and you can always use it like this:\n\n```python\nwith click.open_file(filename, 'w') as f:\n    f.write('Hello World!\\n')\n```\n\n## Finding Application Folders\n\n```{versionadded} 2.0\n```\n\nVery often, you want to open a configuration file that belongs to your application. However, different operating systems\nstore these configuration files in different locations depending on their standards. Click provides a\n{func}`get_app_dir` function which returns the most appropriate location for per-user config files for your application\ndepending on the OS.\n\nExample usage:\n\n```python\nimport os\nimport click\nimport ConfigParser\n\nAPP_NAME = 'My Application'\ndef read_config():\ncfg = os.path.join(click.get_app_dir(APP_NAME), 'config.ini')\nparser = ConfigParser.RawConfigParser()\nparser.read([cfg])\nrv = {}\nfor section in parser.sections():\n    for key, value in parser.items(section):\n        rv[f\"{section}.{key}\"] = value\nreturn rv\n```\n\n## Showing Progress Bars\n\nSometimes, you have command line scripts that need to process a lot of data, but you want to quickly show the user some\nprogress about how long that will take. Click supports simple progress bar rendering for that through the\n{func}`progressbar` function.\n\n```{note} If you find that you have requirements beyond what Click's progress bar supports, try using [tqdm](https://tqdm.github.io/).\n```\n\nThe basic usage is very simple: the idea is that you have an iterable that you want to operate on. For each item in the\niterable it might take some time to do processing. So say you have a loop like this:\n\n```python\nfor user in all_the_users_to_process:\n    modify_the_user(user)\n```\n\nTo hook this up with an automatically updating progress bar, all you need to do is to change the code to this:\n\n```python\nimport click\n\nwith click.progressbar(all_the_users_to_process) as bar:\n    for user in bar:\n        modify_the_user(user)\n```\n\nClick will then automatically print a progress bar to the terminal and calculate the remaining time for you. The\ncalculation of remaining time requires that the iterable has a length. If it does not have a length but you know the\nlength, you can explicitly provide it:\n\n```python\nwith click.progressbar(all_the_users_to_process,\n                       length=number_of_users) as bar:\n    for user in bar:\n        modify_the_user(user)\n```\n\nNote that {func}`progressbar` updates the bar *after* each iteration of the loop. So code like this will render\ncorrectly:\n\n```python\nimport time\n\nwith click.progressbar([1, 2, 3]) as bar:\n    for x in bar:\n        print(f\"sleep({x})...\")\n        time.sleep(x)\n```\n\nAnother useful feature is to associate a label with the progress bar which will be shown preceding the progress bar:\n\n```python\nwith click.progressbar(all_the_users_to_process,\n                       label='Modifying user accounts',\n                       length=number_of_users) as bar:\n    for user in bar:\n        modify_the_user(user)\n```\n\nSometimes, one may need to iterate over an external iterator, and advance the progress bar irregularly. To do so, you\nneed to specify the length (and no iterable), and use the update method on the context return value instead of iterating\ndirectly over it:\n\n```python\nwith click.progressbar(length=total_size,\n                       label='Unzipping archive') as bar:\n    for archive in zip_file:\n        archive.extract()\n        bar.update(archive.size)\n```\n"
  },
  {
    "path": "docs/virtualenv.md",
    "content": "(virtualenv-heading)=\n\n# Virtualenv\n\n## Why Use Virtualenv?\n\nYou should use [Virtualenv](https://virtualenv.pypa.io/en/latest/) because:\n\n- It allows you to install multiple versions of the same dependency.\n- If you have an operating system version of Python, it prevents you from changing its dependencies and potentially\n  messing up your os.\n\n## How to Use Virtualenv\n\nCreate your project folder, then a virtualenv within it:\n\n```console\n$ mkdir myproject\n$ cd myproject\n$ python3 -m venv .venv\n```\n\nNow, whenever you want to work on a project, you only have to activate the corresponding environment.\n\n\n```{eval-rst}\n.. tabs::\n\n    .. group-tab:: OSX/Linux\n\n        .. code-block:: text\n\n            $ . .venv/bin/activate\n            (venv) $\n\n    .. group-tab:: Windows\n\n        .. code-block:: text\n\n            > .venv\\scripts\\activate\n            (venv) >\n```\n\nYou are now using your virtualenv (notice how the prompt of your shell has changed to show the active environment).\n\nTo install packages in the virtual environment:\n\n```console\n$ pip install click\n```\n\nAnd if you want to stop using the virtualenv, use the following command:\n\n```console\n$ deactivate\n```\n\nAfter doing this, the prompt of your shell should be as familiar as before.\n"
  },
  {
    "path": "docs/why.md",
    "content": "# Why Click?\n\nThere are so many libraries out there for writing command line utilities; why does Click exist?\n\nThis question is easy to answer: because there is not a single command line utility for Python out there which ticks the\nfollowing boxes:\n\n- Is lazily composable without restrictions.\n- Supports implementation of Unix/POSIX command line conventions.\n- Supports loading values from environment variables out of the box.\n- Support for prompting of custom values.\n- Is fully nestable and composable.\n- Supports file handling out of the box.\n- Comes with useful common helpers (getting terminal dimensions, ANSI colors, fetching direct keyboard input, screen\n  clearing, finding config paths, launching apps and editors, etc.).\n\nThere are many alternatives to Click; the obvious ones are `optparse` and `argparse` from the standard library. Have a\nlook to see if something else resonates with you.\n\nClick actually implements its own parsing of arguments and does not use `optparse` or `argparse` following the\n`optparse` parsing behavior. The reason it's not based on `argparse` is that `argparse` does not allow proper nesting of\ncommands by design and has some deficiencies when it comes to POSIX compliant argument handling.\n\nClick is designed to be fun and customizable but not overly flexible. For instance, the customizability of help pages is\nconstrained. This constraint is intentional because Click promises multiple Click instances will continue to function as\nintended when strung together.\n\nToo much customizability would break this promise.\n\nClick was written to support the [Flask](https://palletsprojects.com/p/flask/) microframework ecosystem because no tool\ncould provide it with the functionality it needed.\n\nTo get an understanding of what Click is all about, I strongly recommend looking at the {ref}`complex-guide` chapter.\n\n## Why not Argparse?\n\nClick is internally based on `optparse` instead of `argparse`. This is an implementation detail that a user does not\nhave to be concerned with. Click is not based on `argparse` because it has some behaviors that make handling arbitrary\ncommand line interfaces hard:\n\n- `argparse` has built-in behavior to guess if something is an argument or an option. This becomes a problem when\n  dealing with incomplete command lines; the behaviour becomes unpredictable without full knowledge of a command line.\n  This goes against Click's ambitions of dispatching to subparsers.\n- `argparse` does not support disabling interspersed arguments. Without this feature, it's not possible to safely\n  implement Click's nested parsing.\n\n## Why not Docopt etc.?\n\nDocopt, and many tools like it, are cool in how they work, but very few of these tools deal with nesting of commands and\ncomposability in a way like Click. To the best of the developer's knowledge, Click is the first Python library that aims\nto create a level of composability of applications that goes beyond what the system itself supports.\n\nDocopt, for instance, acts by parsing your help pages and then parsing according to those rules. The side effect of this\nis that docopt is quite rigid in how it handles the command line interface. The upside of docopt is that it gives you\nstrong control over your help page; the downside is that due to this it cannot rewrap your output for the current\nterminal width, and it makes translations hard. On top of that, docopt is restricted to basic parsing. It does not\nhandle argument dispatching and callback invocation or types. This means there is a lot of code that needs to be written\nin addition to the basic help page to handle the parsing results.\n\nMost of all, however, it makes composability hard. While docopt does support dispatching to subcommands, it, for\ninstance, does not directly support any kind of automatic subcommand enumeration based on what's available or it does\nnot enforce subcommands to work in a consistent way.\n\nThis is fine, but it's different from how Click wants to work. Click aims to support fully composable command line user\ninterfaces by doing the following:\n\n- Click does not just parse, it also dispatches to the appropriate code.\n- Click has a strong concept of an invocation context that allows subcommands to respond to data from the parent\n  command.\n- Click has strong information available for all parameters and commands, so it can generate unified help pages for the\n  full CLI and assist the user in converting the input data as necessary.\n- Click has a strong understanding of what types are, and it can give the user consistent error messages if something\n  goes wrong. A subcommand written by a different developer will not suddenly die with a different error message because\n  it's manually handled.\n- Click has enough meta information available for its whole program to evolve over time and improve the user experience\n  without forcing developers to adjust their programs. For instance, if Click decides to change how help pages are\n  formatted, all Click programs will automatically benefit from this.\n\nThe aim of Click is to make composable systems. Whereas, the aim of docopt is to build the most beautiful and\nhand-crafted command line interfaces. These two goals conflict with one another in subtle ways. Click actively prevents\npeople from implementing certain patterns in order to achieve unified command line interfaces. For instance, as a\ndeveloper, you are given very little choice in formatting your help pages.\n\n## Why Hardcoded Behaviors?\n\nThe other question is why Click goes away from optparse and hardcodes certain behaviors instead of staying configurable.\nThere are multiple reasons for this. The biggest one is that too much configurability makes it hard to achieve a\nconsistent command line experience.\n\nThe best example for this is optparse's `callback` functionality for accepting an arbitrary number of arguments. Due to\nsyntactical ambiguities on the command line, there is no way to implement fully variadic arguments. There are always\ntradeoffs that need to be made and in case of `argparse` these tradeoffs have been critical enough, that a system like\nClick cannot even be implemented on top of it.\n\nIn this particular case, Click attempts to stay with a handful of accepted paradigms for building command line\ninterfaces that can be well documented and tested.\n\n## Why No Auto Correction?\n\nThe question came up why Click does not auto correct parameters given that even optparse and `argparse` support\nautomatic expansion of long arguments. The reason for this is that it's a liability for backwards compatibility. If\npeople start relying on automatically modified parameters and someone adds a new parameter in the future, the script\nmight stop working. These kinds of problems are hard to find, so Click does not attempt to be magical about this.\n\nThis sort of behavior however can be implemented on a higher level to support things such as explicit aliases. For more\ninformation see {ref}`aliases`.\n"
  },
  {
    "path": "docs/wincmd.md",
    "content": "# Windows Console Notes\n\n```{versionadded} 6.0\n```\n\nClick emulates output streams on Windows to support unicode to the Windows console through separate APIs and we perform\ndifferent decoding of parameters.\n\nHere is a brief overview of how this works and what it means to you.\n\n## Unicode Arguments\n\nClick internally is generally based on the concept that any argument can come in as either byte string or unicode string\nand conversion is performed to the type expected value as late as possible. This has some advantages as it allows us to\naccept the data in the most appropriate form for the operating system and Python version.\n\nThis caused some problems on Windows where initially the wrong encoding was used and garbage ended up in your input\ndata. We not only fixed the encoding part, but we also now extract unicode parameters from `sys.argv`.\n\nThere is also another limitation with this: if `sys.argv` was modified prior to invoking a click handler, we have to\nfall back to the regular byte input in which case not all unicode values are available but only a subset of the codepage\nused for parameters.\n\n## Unicode Output and Input\n\nUnicode output and input on Windows is implemented through the concept of a dispatching text stream. What this means is\nthat when click first needs a text output (or input) stream on windows it goes through a few checks to figure out of a\nwindows console is connected or not. If no Windows console is present then the text output stream is returned as such\nand the encoding for that stream is set to `utf-8` like on all platforms.\n\nHowever if a console is connected the stream will instead be emulated and use the cmd.exe unicode APIs to output text\ninformation. In this case the stream will also use `utf-16-le` as internal encoding. However there is some hackery going\non that the underlying raw IO buffer is still bypassing the unicode APIs and byte output through an indirection is still\npossible.\n\n- This unicode support is limited to `click.echo`, `click.prompt` as well as `click.get_text_stream`.\n- Depending on if unicode values or byte strings are passed the control flow goes completely different places internally\n  which can have some odd artifacts if data partially ends up being buffered. Click attempts to protect against that by\n  manually always flushing but if you are mixing and matching different string types to `stdout` or `stderr` you will\n  need to manually flush.\n- The raw output stream is set to binary mode, which is a global operation on Windows, so `print` calls will be\n  affected. Prefer `click.echo` over `print`.\n- On Windows 7 and below, there is a limitation where at most 64k characters can be written in one call in binary mode.\n  In this situation, `sys.stdout` and `sys.stderr` are replaced with wrappers that work around the limitation.\n\nAnother important thing to note is that the Windows console's default fonts do not support a lot of characters which\nmeans that you are mostly limited to international letters but no emojis or special characters.\n"
  },
  {
    "path": "examples/README",
    "content": "Click Examples\n\n  This folder contains various Click examples.  Note that\n  all of these are not runnable by themselves but should be\n  installed into a virtualenv.\n"
  },
  {
    "path": "examples/aliases/README",
    "content": "$ aliases_\n\n  aliases is a fairly advanced example that shows how\n  to implement command aliases with Click.  It uses a\n  subclass of the default group to customize how commands\n  are located.\n\n  It supports both aliases read from a config file as well\n  as automatic abbreviations.\n\n  The aliases from the config are read from the aliases.ini\n  file.  Try `aliases st` and `aliases ci`!\n\nUsage:\n\n  $ pip install --editable .\n  $ aliases --help\n"
  },
  {
    "path": "examples/aliases/aliases.ini",
    "content": "[aliases]\nci=commit\n"
  },
  {
    "path": "examples/aliases/aliases.py",
    "content": "import configparser\nimport os\n\nimport click\n\n\nclass Config:\n    \"\"\"The config in this example only holds aliases.\"\"\"\n\n    def __init__(self):\n        self.path = os.getcwd()\n        self.aliases = {}\n\n    def add_alias(self, alias, cmd):\n        self.aliases.update({alias: cmd})\n\n    def read_config(self, filename):\n        parser = configparser.RawConfigParser()\n        parser.read([filename])\n        try:\n            self.aliases.update(parser.items(\"aliases\"))\n        except configparser.NoSectionError:\n            pass\n\n    def write_config(self, filename):\n        parser = configparser.RawConfigParser()\n        parser.add_section(\"aliases\")\n        for key, value in self.aliases.items():\n            parser.set(\"aliases\", key, value)\n        with open(filename, \"wb\") as file:\n            parser.write(file)\n\n\npass_config = click.make_pass_decorator(Config, ensure=True)\n\n\nclass AliasedGroup(click.Group):\n    \"\"\"This subclass of a group supports looking up aliases in a config\n    file and with a bit of magic.\n    \"\"\"\n\n    def get_command(self, ctx, cmd_name):\n        # Step one: bulitin commands as normal\n        rv = click.Group.get_command(self, ctx, cmd_name)\n        if rv is not None:\n            return rv\n\n        # Step two: find the config object and ensure it's there.  This\n        # will create the config object is missing.\n        cfg = ctx.ensure_object(Config)\n\n        # Step three: look up an explicit command alias in the config\n        if cmd_name in cfg.aliases:\n            actual_cmd = cfg.aliases[cmd_name]\n            return click.Group.get_command(self, ctx, actual_cmd)\n\n        # Alternative option: if we did not find an explicit alias we\n        # allow automatic abbreviation of the command.  \"status\" for\n        # instance will match \"st\".  We only allow that however if\n        # there is only one command.\n        matches = [\n            x for x in self.list_commands(ctx) if x.lower().startswith(cmd_name.lower())\n        ]\n        if not matches:\n            return None\n        elif len(matches) == 1:\n            return click.Group.get_command(self, ctx, matches[0])\n        ctx.fail(f\"Too many matches: {', '.join(sorted(matches))}\")\n\n    def resolve_command(self, ctx, args):\n        # always return the command's name, not the alias\n        _, cmd, args = super().resolve_command(ctx, args)\n        return cmd.name, cmd, args\n\n\ndef read_config(ctx, param, value):\n    \"\"\"Callback that is used whenever --config is passed.  We use this to\n    always load the correct config.  This means that the config is loaded\n    even if the group itself never executes so our aliases stay always\n    available.\n    \"\"\"\n    cfg = ctx.ensure_object(Config)\n    if value is None:\n        value = os.path.join(os.path.dirname(__file__), \"aliases.ini\")\n    cfg.read_config(value)\n    return value\n\n\n@click.command(cls=AliasedGroup)\n@click.option(\n    \"--config\",\n    type=click.Path(exists=True, dir_okay=False),\n    callback=read_config,\n    expose_value=False,\n    help=\"The config file to use instead of the default.\",\n)\ndef cli():\n    \"\"\"An example application that supports aliases.\"\"\"\n\n\n@cli.command()\ndef push():\n    \"\"\"Pushes changes.\"\"\"\n    click.echo(\"Push\")\n\n\n@cli.command()\ndef pull():\n    \"\"\"Pulls changes.\"\"\"\n    click.echo(\"Pull\")\n\n\n@cli.command()\ndef clone():\n    \"\"\"Clones a repository.\"\"\"\n    click.echo(\"Clone\")\n\n\n@cli.command()\ndef commit():\n    \"\"\"Commits pending changes.\"\"\"\n    click.echo(\"Commit\")\n\n\n@cli.command()\n@pass_config\ndef status(config):\n    \"\"\"Shows the status.\"\"\"\n    click.echo(f\"Status for {config.path}\")\n\n\n@cli.command()\n@pass_config\n@click.argument(\"alias_\", metavar=\"ALIAS\", type=click.STRING)\n@click.argument(\"cmd\", type=click.STRING)\n@click.option(\n    \"--config_file\", type=click.Path(exists=True, dir_okay=False), default=\"aliases.ini\"\n)\ndef alias(config, alias_, cmd, config_file):\n    \"\"\"Adds an alias to the specified configuration file.\"\"\"\n    config.add_alias(alias_, cmd)\n    config.write_config(config_file)\n    click.echo(f\"Added '{alias_}' as alias for '{cmd}'\")\n"
  },
  {
    "path": "examples/aliases/pyproject.toml",
    "content": "[project]\nname = \"click-example-aliases\"\nversion = \"1.0.0\"\ndescription = \"Click aliases example\"\nrequires-python = \">=3.10\"\ndependencies = [\n    \"click>=8.1\",\n]\n\n[project.scripts]\naliases = \"aliases:cli\"\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"aliases\"\n"
  },
  {
    "path": "examples/colors/README",
    "content": "$ colors_\n\n  colors is a simple example that shows how you can\n  colorize text.\n\n  Uses colorama on Windows.\n\nUsage:\n\n  $ pip install --editable .\n  $ colors\n"
  },
  {
    "path": "examples/colors/colors.py",
    "content": "import click\n\n\nall_colors = (\n    \"black\",\n    \"red\",\n    \"green\",\n    \"yellow\",\n    \"blue\",\n    \"magenta\",\n    \"cyan\",\n    \"white\",\n    \"bright_black\",\n    \"bright_red\",\n    \"bright_green\",\n    \"bright_yellow\",\n    \"bright_blue\",\n    \"bright_magenta\",\n    \"bright_cyan\",\n    \"bright_white\",\n)\n\n\n@click.command()\ndef cli():\n    \"\"\"This script prints some colors. It will also automatically remove\n    all ANSI styles if data is piped into a file.\n\n    Give it a try!\n    \"\"\"\n    for color in all_colors:\n        click.echo(click.style(f\"I am colored {color}\", fg=color))\n    for color in all_colors:\n        click.echo(click.style(f\"I am colored {color} and bold\", fg=color, bold=True))\n    for color in all_colors:\n        click.echo(click.style(f\"I am reverse colored {color}\", fg=color, reverse=True))\n\n    click.echo(click.style(\"I am blinking\", blink=True))\n    click.echo(click.style(\"I am underlined\", underline=True))\n"
  },
  {
    "path": "examples/colors/pyproject.toml",
    "content": "[project]\nname = \"click-example-colors\"\nversion = \"1.0.0\"\ndescription = \"Click colors example\"\nrequires-python = \">=3.10\"\ndependencies = [\n    \"click>=8.1\",\n]\n\n[project.scripts]\ncolors = \"colors:cli\"\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"colors\"\n"
  },
  {
    "path": "examples/completion/README",
    "content": "$ completion\n============\n\nDemonstrates Click's shell completion support.\n\n.. code-block:: bash\n\n    pip install --editable .\n\nFor Bash:\n\n.. code-block:: bash\n\n    eval \"$(_COMPLETION_COMPLETE=bash_source completion)\"\n\nFor Zsh:\n\n.. code-block:: zsh\n\n    eval \"$(_COMPLETION_COMPLETE=zsh_source completion)\"\n\nFor Fish:\n\n.. code-block:: fish\n\n    eval (env _COMPLETION_COMPLETE=fish_source completion)\n\nNow press tab (maybe twice) after typing something to see completions.\n\n.. code-block:: python\n\n    $ completion <TAB> <TAB>\n    $ completion gr <TAB> <TAB>\n"
  },
  {
    "path": "examples/completion/completion.py",
    "content": "import os\n\nimport click\nfrom click.shell_completion import CompletionItem\n\n\n@click.group()\ndef cli():\n    pass\n\n\n@cli.command()\n@click.option(\"--dir\", type=click.Path(file_okay=False))\ndef ls(dir):\n    click.echo(\"\\n\".join(os.listdir(dir)))\n\n\ndef get_env_vars(ctx, param, incomplete):\n    # Returning a list of values is a shortcut to returning a list of\n    # CompletionItem(value).\n    return [k for k in os.environ if incomplete in k]\n\n\n@cli.command(help=\"A command to print environment variables\")\n@click.argument(\"envvar\", shell_complete=get_env_vars)\ndef show_env(envvar):\n    click.echo(f\"Environment variable: {envvar}\")\n    click.echo(f\"Value: {os.environ[envvar]}\")\n\n\n@cli.group(help=\"A group that holds a subcommand\")\ndef group():\n    pass\n\n\ndef list_users(ctx, param, incomplete):\n    # You can generate completions with help strings by returning a list\n    # of CompletionItem. You can match on whatever you want, including\n    # the help.\n    items = [(\"bob\", \"butcher\"), (\"alice\", \"baker\"), (\"jerry\", \"candlestick maker\")]\n    out = []\n\n    for value, help in items:\n        if incomplete in value or incomplete in help:\n            out.append(CompletionItem(value, help=help))\n\n    return out\n\n\n@group.command(help=\"Choose a user\")\n@click.argument(\"user\", shell_complete=list_users)\ndef select_user(user):\n    click.echo(f\"Chosen user is {user}\")\n\n\ncli.add_command(group)\n"
  },
  {
    "path": "examples/completion/pyproject.toml",
    "content": "[project]\nname = \"click-example-completion\"\nversion = \"1.0.0\"\ndescription = \"Click completion example\"\nrequires-python = \">=3.10\"\ndependencies = [\n    \"click>=8.1\",\n]\n\n[project.scripts]\ncompletion = \"completion:cli\"\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"completion\"\n"
  },
  {
    "path": "examples/complex/README",
    "content": "$ complex_\n\n  complex is an example of building very complex cli\n  applications that load subcommands dynamically from\n  a plugin folder and other things.\n\n  All the commands are implemented as plugins in the\n  `complex.commands` package.  If a python module is\n  placed named \"cmd_foo\" it will show up as \"foo\"\n  command and the `cli` object within it will be\n  loaded as nested Click command.\n\nUsage:\n\n  $ pip install --editable .\n  $ complex --help\n"
  },
  {
    "path": "examples/complex/complex/__init__.py",
    "content": ""
  },
  {
    "path": "examples/complex/complex/cli.py",
    "content": "import os\nimport sys\n\nimport click\n\n\nCONTEXT_SETTINGS = dict(auto_envvar_prefix=\"COMPLEX\")\n\n\nclass Environment:\n    def __init__(self):\n        self.verbose = False\n        self.home = os.getcwd()\n\n    def log(self, msg, *args):\n        \"\"\"Logs a message to stderr.\"\"\"\n        if args:\n            msg %= args\n        click.echo(msg, file=sys.stderr)\n\n    def vlog(self, msg, *args):\n        \"\"\"Logs a message to stderr only if verbose is enabled.\"\"\"\n        if self.verbose:\n            self.log(msg, *args)\n\n\npass_environment = click.make_pass_decorator(Environment, ensure=True)\ncmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), \"commands\"))\n\n\nclass ComplexCLI(click.Group):\n    def list_commands(self, ctx):\n        rv = []\n        for filename in os.listdir(cmd_folder):\n            if filename.endswith(\".py\") and filename.startswith(\"cmd_\"):\n                rv.append(filename[4:-3])\n        rv.sort()\n        return rv\n\n    def get_command(self, ctx, name):\n        try:\n            mod = __import__(f\"complex.commands.cmd_{name}\", None, None, [\"cli\"])\n        except ImportError:\n            return\n        return mod.cli\n\n\n@click.command(cls=ComplexCLI, context_settings=CONTEXT_SETTINGS)\n@click.option(\n    \"--home\",\n    type=click.Path(exists=True, file_okay=False, resolve_path=True),\n    help=\"Changes the folder to operate on.\",\n)\n@click.option(\"-v\", \"--verbose\", is_flag=True, help=\"Enables verbose mode.\")\n@pass_environment\ndef cli(ctx, verbose, home):\n    \"\"\"A complex command line interface.\"\"\"\n    ctx.verbose = verbose\n    if home is not None:\n        ctx.home = home\n"
  },
  {
    "path": "examples/complex/complex/commands/__init__.py",
    "content": ""
  },
  {
    "path": "examples/complex/complex/commands/cmd_init.py",
    "content": "from complex.cli import pass_environment\n\nimport click\n\n\n@click.command(\"init\", short_help=\"Initializes a repo.\")\n@click.argument(\"path\", required=False, type=click.Path(resolve_path=True))\n@pass_environment\ndef cli(ctx, path):\n    \"\"\"Initializes a repository.\"\"\"\n    if path is None:\n        path = ctx.home\n    ctx.log(f\"Initialized the repository in {click.format_filename(path)}\")\n"
  },
  {
    "path": "examples/complex/complex/commands/cmd_status.py",
    "content": "from complex.cli import pass_environment\n\nimport click\n\n\n@click.command(\"status\", short_help=\"Shows file changes.\")\n@pass_environment\ndef cli(ctx):\n    \"\"\"Shows file changes in the current working directory.\"\"\"\n    ctx.log(\"Changed files: none\")\n    ctx.vlog(\"bla bla bla, debug info\")\n"
  },
  {
    "path": "examples/complex/pyproject.toml",
    "content": "[project]\nname = \"click-example-complex\"\nversion = \"1.0.0\"\ndescription = \"Click complex example\"\nrequires-python = \">=3.10\"\ndependencies = [\n    \"click>=8.1\",\n]\n\n[project.scripts]\ncomplex = \"complex.cli:cli\"\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"complex\"\n"
  },
  {
    "path": "examples/imagepipe/.gitignore",
    "content": "processed-*\n"
  },
  {
    "path": "examples/imagepipe/README",
    "content": "$ imagepipe_\n\n  imagepipe is an example application that implements some\n  commands that chain image processing instructions\n  together.\n\n  This requires pillow.\n\nUsage:\n\n  $ pip install --editable .\n  $ imagepipe open -i example01.jpg resize -w 128 display\n  $ imagepipe open -i example02.jpg blur save\n"
  },
  {
    "path": "examples/imagepipe/imagepipe.py",
    "content": "from functools import update_wrapper\n\nfrom PIL import Image\nfrom PIL import ImageEnhance\nfrom PIL import ImageFilter\n\nimport click\n\n\n@click.group(chain=True)\ndef cli():\n    \"\"\"This script processes a bunch of images through pillow in a unix\n    pipe.  One commands feeds into the next.\n\n    Example:\n\n    \\b\n        imagepipe open -i example01.jpg resize -w 128 display\n        imagepipe open -i example02.jpg blur save\n    \"\"\"\n\n\n@cli.result_callback()\ndef process_commands(processors):\n    \"\"\"This result callback is invoked with an iterable of all the chained\n    subcommands.  As in this example each subcommand returns a function\n    we can chain them together to feed one into the other, similar to how\n    a pipe on unix works.\n    \"\"\"\n    # Start with an empty iterable.\n    stream = ()\n\n    # Pipe it through all stream processors.\n    for processor in processors:\n        stream = processor(stream)\n\n    # Evaluate the stream and throw away the items.\n    for _ in stream:\n        pass\n\n\ndef processor(f):\n    \"\"\"Helper decorator to rewrite a function so that it returns another\n    function from it.\n    \"\"\"\n\n    def new_func(*args, **kwargs):\n        def processor(stream):\n            return f(stream, *args, **kwargs)\n\n        return processor\n\n    return update_wrapper(new_func, f)\n\n\ndef generator(f):\n    \"\"\"Similar to the :func:`processor` but passes through old values\n    unchanged and does not pass through the values as parameter.\n    \"\"\"\n\n    @processor\n    def new_func(stream, *args, **kwargs):\n        yield from stream\n        yield from f(*args, **kwargs)\n\n    return update_wrapper(new_func, f)\n\n\ndef copy_filename(new, old):\n    new.filename = old.filename\n    return new\n\n\n@cli.command(\"open\")\n@click.option(\n    \"-i\",\n    \"--image\",\n    \"images\",\n    type=click.Path(),\n    multiple=True,\n    help=\"The image file to open.\",\n)\n@generator\ndef open_cmd(images):\n    \"\"\"Loads one or multiple images for processing.  The input parameter\n    can be specified multiple times to load more than one image.\n    \"\"\"\n    for image in images:\n        try:\n            click.echo(f\"Opening '{image}'\")\n            if image == \"-\":\n                img = Image.open(click.get_binary_stdin())\n                img.filename = \"-\"\n            else:\n                img = Image.open(image)\n            yield img\n        except Exception as e:\n            click.echo(f\"Could not open image '{image}': {e}\", err=True)\n\n\n@cli.command(\"save\")\n@click.option(\n    \"--filename\",\n    default=\"processed-{:04}.png\",\n    type=click.Path(),\n    help=\"The format for the filename.\",\n    show_default=True,\n)\n@processor\ndef save_cmd(images, filename):\n    \"\"\"Saves all processed images to a series of files.\"\"\"\n    for idx, image in enumerate(images):\n        try:\n            fn = filename.format(idx + 1)\n            click.echo(f\"Saving '{image.filename}' as '{fn}'\")\n            yield image.save(fn)\n        except Exception as e:\n            click.echo(f\"Could not save image '{image.filename}': {e}\", err=True)\n\n\n@cli.command(\"display\")\n@processor\ndef display_cmd(images):\n    \"\"\"Opens all images in an image viewer.\"\"\"\n    for image in images:\n        click.echo(f\"Displaying '{image.filename}'\")\n        image.show()\n        yield image\n\n\n@cli.command(\"resize\")\n@click.option(\"-w\", \"--width\", type=int, help=\"The new width of the image.\")\n@click.option(\"-h\", \"--height\", type=int, help=\"The new height of the image.\")\n@processor\ndef resize_cmd(images, width, height):\n    \"\"\"Resizes an image by fitting it into the box without changing\n    the aspect ratio.\n    \"\"\"\n    for image in images:\n        w, h = (width or image.size[0], height or image.size[1])\n        click.echo(f\"Resizing '{image.filename}' to {w}x{h}\")\n        image.thumbnail((w, h))\n        yield image\n\n\n@cli.command(\"crop\")\n@click.option(\n    \"-b\", \"--border\", type=int, help=\"Crop the image from all sides by this amount.\"\n)\n@processor\ndef crop_cmd(images, border):\n    \"\"\"Crops an image from all edges.\"\"\"\n    for image in images:\n        box = [0, 0, image.size[0], image.size[1]]\n\n        if border is not None:\n            for idx, val in enumerate(box):\n                box[idx] = max(0, val - border)\n            click.echo(f\"Cropping '{image.filename}' by {border}px\")\n            yield copy_filename(image.crop(box), image)\n        else:\n            yield image\n\n\ndef convert_rotation(ctx, param, value):\n    if value is None:\n        return\n    value = value.lower()\n    if value in (\"90\", \"r\", \"right\"):\n        return (Image.ROTATE_90, 90)\n    if value in (\"180\", \"-180\"):\n        return (Image.ROTATE_180, 180)\n    if value in (\"-90\", \"270\", \"l\", \"left\"):\n        return (Image.ROTATE_270, 270)\n    raise click.BadParameter(f\"invalid rotation '{value}'\")\n\n\ndef convert_flip(ctx, param, value):\n    if value is None:\n        return\n    value = value.lower()\n    if value in (\"lr\", \"leftright\"):\n        return (Image.FLIP_LEFT_RIGHT, \"left to right\")\n    if value in (\"tb\", \"topbottom\", \"upsidedown\", \"ud\"):\n        return (Image.FLIP_LEFT_RIGHT, \"top to bottom\")\n    raise click.BadParameter(f\"invalid flip '{value}'\")\n\n\n@cli.command(\"transpose\")\n@click.option(\n    \"-r\", \"--rotate\", callback=convert_rotation, help=\"Rotates the image (in degrees)\"\n)\n@click.option(\"-f\", \"--flip\", callback=convert_flip, help=\"Flips the image  [LR / TB]\")\n@processor\ndef transpose_cmd(images, rotate, flip):\n    \"\"\"Transposes an image by either rotating or flipping it.\"\"\"\n    for image in images:\n        if rotate is not None:\n            mode, degrees = rotate\n            click.echo(f\"Rotate '{image.filename}' by {degrees}deg\")\n            image = copy_filename(image.transpose(mode), image)\n        if flip is not None:\n            mode, direction = flip\n            click.echo(f\"Flip '{image.filename}' {direction}\")\n            image = copy_filename(image.transpose(mode), image)\n        yield image\n\n\n@cli.command(\"blur\")\n@click.option(\"-r\", \"--radius\", default=2, show_default=True, help=\"The blur radius.\")\n@processor\ndef blur_cmd(images, radius):\n    \"\"\"Applies gaussian blur.\"\"\"\n    blur = ImageFilter.GaussianBlur(radius)\n    for image in images:\n        click.echo(f\"Blurring '{image.filename}' by {radius}px\")\n        yield copy_filename(image.filter(blur), image)\n\n\n@cli.command(\"smoothen\")\n@click.option(\n    \"-i\",\n    \"--iterations\",\n    default=1,\n    show_default=True,\n    help=\"How many iterations of the smoothen filter to run.\",\n)\n@processor\ndef smoothen_cmd(images, iterations):\n    \"\"\"Applies a smoothening filter.\"\"\"\n    for image in images:\n        click.echo(\n            f\"Smoothening {image.filename!r} {iterations}\"\n            f\" time{'s' if iterations != 1 else ''}\"\n        )\n        for _ in range(iterations):\n            image = copy_filename(image.filter(ImageFilter.BLUR), image)\n        yield image\n\n\n@cli.command(\"emboss\")\n@processor\ndef emboss_cmd(images):\n    \"\"\"Embosses an image.\"\"\"\n    for image in images:\n        click.echo(f\"Embossing '{image.filename}'\")\n        yield copy_filename(image.filter(ImageFilter.EMBOSS), image)\n\n\n@cli.command(\"sharpen\")\n@click.option(\n    \"-f\", \"--factor\", default=2.0, help=\"Sharpens the image.\", show_default=True\n)\n@processor\ndef sharpen_cmd(images, factor):\n    \"\"\"Sharpens an image.\"\"\"\n    for image in images:\n        click.echo(f\"Sharpen '{image.filename}' by {factor}\")\n        enhancer = ImageEnhance.Sharpness(image)\n        yield copy_filename(enhancer.enhance(max(1.0, factor)), image)\n\n\n@cli.command(\"paste\")\n@click.option(\"-l\", \"--left\", default=0, help=\"Offset from left.\")\n@click.option(\"-r\", \"--right\", default=0, help=\"Offset from right.\")\n@processor\ndef paste_cmd(images, left, right):\n    \"\"\"Pastes the second image on the first image and leaves the rest\n    unchanged.\n    \"\"\"\n    imageiter = iter(images)\n    image = next(imageiter, None)\n    to_paste = next(imageiter, None)\n\n    if to_paste is None:\n        if image is not None:\n            yield image\n        return\n\n    click.echo(f\"Paste '{to_paste.filename}' on '{image.filename}'\")\n    mask = None\n    if to_paste.mode == \"RGBA\" or \"transparency\" in to_paste.info:\n        mask = to_paste\n    image.paste(to_paste, (left, right), mask)\n    image.filename += f\"+{to_paste.filename}\"\n    yield image\n\n    yield from imageiter\n"
  },
  {
    "path": "examples/imagepipe/pyproject.toml",
    "content": "[project]\nname = \"click-example-imagepipe\"\nversion = \"1.0.0\"\ndescription = \"Click imagepipe example\"\nrequires-python = \">=3.10\"\ndependencies = [\n    \"click>=8.1\",\n    \"pillow\",\n]\n\n[project.scripts]\nimagepipe = \"imagepipe:cli\"\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"imagepipe\"\n"
  },
  {
    "path": "examples/inout/README",
    "content": "$ inout_\n\n  inout is a simple example of an application that\n  can read from files and write to files but also\n  accept input from stdin or write to stdout.\n\nUsage:\n\n  $ pip install --editable .\n  $ inout input_file.txt output_file.txt\n"
  },
  {
    "path": "examples/inout/inout.py",
    "content": "import click\n\n\n@click.command()\n@click.argument(\"input\", type=click.File(\"rb\"), nargs=-1)\n@click.argument(\"output\", type=click.File(\"wb\"))\ndef cli(input, output):\n    \"\"\"This script works similar to the Unix `cat` command but it writes\n    into a specific file (which could be the standard output as denoted by\n    the ``-`` sign).\n\n    \\b\n    Copy stdin to stdout:\n        inout - -\n\n    \\b\n    Copy foo.txt and bar.txt to stdout:\n        inout foo.txt bar.txt -\n\n    \\b\n    Write stdin into the file foo.txt\n        inout - foo.txt\n    \"\"\"\n    for f in input:\n        while True:\n            chunk = f.read(1024)\n            if not chunk:\n                break\n            output.write(chunk)\n            output.flush()\n"
  },
  {
    "path": "examples/inout/pyproject.toml",
    "content": "[project]\nname = \"click-example-inout\"\nversion = \"1.0.0\"\ndescription = \"Click inout example\"\nrequires-python = \">=3.10\"\ndependencies = [\n    \"click>=8.1\",\n]\n\n[project.scripts]\ninout = \"inout:cli\"\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"inout\"\n"
  },
  {
    "path": "examples/naval/README",
    "content": "$ naval_\n\n  naval is a simple example of an application that\n  is ported from the docopt example of the same name.\n\n  Unlike the original this one also runs some code and\n  prints messages and it's command line interface was\n  changed slightly to make more sense with established\n  POSIX semantics.\n\nUsage:\n\n  $ pip install --editable .\n  $ naval --help\n"
  },
  {
    "path": "examples/naval/naval.py",
    "content": "import click\n\n\n@click.group()\n@click.version_option()\ndef cli():\n    \"\"\"Naval Fate.\n\n    This is the docopt example adopted to Click but with some actual\n    commands implemented and not just the empty parsing which really\n    is not all that interesting.\n    \"\"\"\n\n\n@cli.group()\ndef ship():\n    \"\"\"Manages ships.\"\"\"\n\n\n@ship.command(\"new\")\n@click.argument(\"name\")\ndef ship_new(name):\n    \"\"\"Creates a new ship.\"\"\"\n    click.echo(f\"Created ship {name}\")\n\n\n@ship.command(\"move\")\n@click.argument(\"ship\")\n@click.argument(\"x\", type=float)\n@click.argument(\"y\", type=float)\n@click.option(\"--speed\", metavar=\"KN\", default=10, help=\"Speed in knots.\")\ndef ship_move(ship, x, y, speed):\n    \"\"\"Moves SHIP to the new location X,Y.\"\"\"\n    click.echo(f\"Moving ship {ship} to {x},{y} with speed {speed}\")\n\n\n@ship.command(\"shoot\")\n@click.argument(\"ship\")\n@click.argument(\"x\", type=float)\n@click.argument(\"y\", type=float)\ndef ship_shoot(ship, x, y):\n    \"\"\"Makes SHIP fire to X,Y.\"\"\"\n    click.echo(f\"Ship {ship} fires to {x},{y}\")\n\n\n@cli.group(\"mine\")\ndef mine():\n    \"\"\"Manages mines.\"\"\"\n\n\n@mine.command(\"set\")\n@click.argument(\"x\", type=float)\n@click.argument(\"y\", type=float)\n@click.option(\n    \"ty\",\n    \"--moored\",\n    flag_value=\"moored\",\n    default=True,\n    help=\"Moored (anchored) mine. Default.\",\n)\n@click.option(\"ty\", \"--drifting\", flag_value=\"drifting\", help=\"Drifting mine.\")\ndef mine_set(x, y, ty):\n    \"\"\"Sets a mine at a specific coordinate.\"\"\"\n    click.echo(f\"Set {ty} mine at {x},{y}\")\n\n\n@mine.command(\"remove\")\n@click.argument(\"x\", type=float)\n@click.argument(\"y\", type=float)\ndef mine_remove(x, y):\n    \"\"\"Removes a mine at a specific coordinate.\"\"\"\n    click.echo(f\"Removed mine at {x},{y}\")\n"
  },
  {
    "path": "examples/naval/pyproject.toml",
    "content": "[project]\nname = \"click-example-naval\"\nversion = \"1.0.0\"\ndescription = \"Click naval example\"\nrequires-python = \">=3.10\"\ndependencies = [\n    \"click>=8.1\",\n]\n\n[project.scripts]\nnaval = \"naval:cli\"\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"naval\"\n"
  },
  {
    "path": "examples/repo/README",
    "content": "$ repo_\n\n  repo is a simple example of an application that looks\n  and works similar to hg or git.\n\nUsage:\n\n  $ pip install --editable .\n  $ repo --help\n"
  },
  {
    "path": "examples/repo/pyproject.toml",
    "content": "[project]\nname = \"click-example-repo\"\nversion = \"1.0.0\"\ndescription = \"Click repo example\"\nrequires-python = \">=3.10\"\ndependencies = [\n    \"click>=8.1\",\n]\n\n[project.scripts]\nrepo = \"repo:cli\"\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"repo\"\n"
  },
  {
    "path": "examples/repo/repo.py",
    "content": "import os\nimport posixpath\nimport sys\n\nimport click\n\n\nclass Repo:\n    def __init__(self, home):\n        self.home = home\n        self.config = {}\n        self.verbose = False\n\n    def set_config(self, key, value):\n        self.config[key] = value\n        if self.verbose:\n            click.echo(f\"  config[{key}] = {value}\", file=sys.stderr)\n\n    def __repr__(self):\n        return f\"<Repo {self.home}>\"\n\n\npass_repo = click.make_pass_decorator(Repo)\n\n\n@click.group()\n@click.option(\n    \"--repo-home\",\n    envvar=\"REPO_HOME\",\n    default=\".repo\",\n    metavar=\"PATH\",\n    help=\"Changes the repository folder location.\",\n)\n@click.option(\n    \"--config\",\n    nargs=2,\n    multiple=True,\n    metavar=\"KEY VALUE\",\n    help=\"Overrides a config key/value pair.\",\n)\n@click.option(\"--verbose\", \"-v\", is_flag=True, help=\"Enables verbose mode.\")\n@click.version_option(\"1.0\")\n@click.pass_context\ndef cli(ctx, repo_home, config, verbose):\n    \"\"\"Repo is a command line tool that showcases how to build complex\n    command line interfaces with Click.\n\n    This tool is supposed to look like a distributed version control\n    system to show how something like this can be structured.\n    \"\"\"\n    # Create a repo object and remember it as as the context object.  From\n    # this point onwards other commands can refer to it by using the\n    # @pass_repo decorator.\n    ctx.obj = Repo(os.path.abspath(repo_home))\n    ctx.obj.verbose = verbose\n    for key, value in config:\n        ctx.obj.set_config(key, value)\n\n\n@cli.command()\n@click.argument(\"src\")\n@click.argument(\"dest\", required=False)\n@click.option(\n    \"--shallow/--deep\",\n    default=False,\n    help=\"Makes a checkout shallow or deep.  Deep by default.\",\n)\n@click.option(\n    \"--rev\", \"-r\", default=\"HEAD\", help=\"Clone a specific revision instead of HEAD.\"\n)\n@pass_repo\ndef clone(repo, src, dest, shallow, rev):\n    \"\"\"Clones a repository.\n\n    This will clone the repository at SRC into the folder DEST.  If DEST\n    is not provided this will automatically use the last path component\n    of SRC and create that folder.\n    \"\"\"\n    if dest is None:\n        dest = posixpath.split(src)[-1] or \".\"\n    click.echo(f\"Cloning repo {src} to {os.path.basename(dest)}\")\n    repo.home = dest\n    if shallow:\n        click.echo(\"Making shallow checkout\")\n    click.echo(f\"Checking out revision {rev}\")\n\n\n@cli.command()\n@click.confirmation_option()\n@pass_repo\ndef delete(repo):\n    \"\"\"Deletes a repository.\n\n    This will throw away the current repository.\n    \"\"\"\n    click.echo(f\"Destroying repo {repo.home}\")\n    click.echo(\"Deleted!\")\n\n\n@cli.command()\n@click.option(\"--username\", prompt=True, help=\"The developer's shown username.\")\n@click.option(\"--email\", prompt=\"E-Mail\", help=\"The developer's email address\")\n@click.password_option(help=\"The login password.\")\n@pass_repo\ndef setuser(repo, username, email, password):\n    \"\"\"Sets the user credentials.\n\n    This will override the current user config.\n    \"\"\"\n    repo.set_config(\"username\", username)\n    repo.set_config(\"email\", email)\n    repo.set_config(\"password\", \"*\" * len(password))\n    click.echo(\"Changed credentials.\")\n\n\n@cli.command()\n@click.option(\n    \"--message\",\n    \"-m\",\n    multiple=True,\n    help=\"The commit message.  If provided multiple times each\"\n    \" argument gets converted into a new line.\",\n)\n@click.argument(\"files\", nargs=-1, type=click.Path())\n@pass_repo\ndef commit(repo, files, message):\n    \"\"\"Commits outstanding changes.\n\n    Commit changes to the given files into the repository.  You will need to\n    \"repo push\" to push up your changes to other repositories.\n\n    If a list of files is omitted, all changes reported by \"repo status\"\n    will be committed.\n    \"\"\"\n    if not message:\n        marker = \"# Files to be committed:\"\n        hint = [\"\", \"\", marker, \"#\"]\n        for file in files:\n            hint.append(f\"#   U {file}\")\n        message = click.edit(\"\\n\".join(hint))\n        if message is None:\n            click.echo(\"Aborted!\")\n            return\n        msg = message.split(marker)[0].rstrip()\n        if not msg:\n            click.echo(\"Aborted! Empty commit message\")\n            return\n    else:\n        msg = \"\\n\".join(message)\n    click.echo(f\"Files to be committed: {files}\")\n    click.echo(f\"Commit message:\\n{msg}\")\n\n\n@cli.command(short_help=\"Copies files.\")\n@click.option(\n    \"--force\", is_flag=True, help=\"forcibly copy over an existing managed file\"\n)\n@click.argument(\"src\", nargs=-1, type=click.Path())\n@click.argument(\"dst\", type=click.Path())\n@pass_repo\ndef copy(repo, src, dst, force):\n    \"\"\"Copies one or multiple files to a new location.  This copies all\n    files from SRC to DST.\n    \"\"\"\n    for fn in src:\n        click.echo(f\"Copy from {fn} -> {dst}\")\n"
  },
  {
    "path": "examples/termui/README",
    "content": "$ termui_\n\n  termui showcases the different terminal UI helpers that\n  Click provides.\n\nUsage:\n\n  $ pip install --editable .\n  $ termui --help\n"
  },
  {
    "path": "examples/termui/pyproject.toml",
    "content": "[project]\nname = \"click-example-termui\"\nversion = \"1.0.0\"\ndescription = \"Click termui example\"\nrequires-python = \">=3.10\"\ndependencies = [\n    \"click>=8.1\",\n]\n\n[project.scripts]\ntermui = \"termui:cli\"\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"termui\"\n"
  },
  {
    "path": "examples/termui/termui.py",
    "content": "import math\nimport random\nimport time\n\nimport click\n\n\n@click.group()\ndef cli():\n    \"\"\"This script showcases different terminal UI helpers in Click.\"\"\"\n    pass\n\n\n@cli.command()\ndef colordemo():\n    \"\"\"Demonstrates ANSI color support.\"\"\"\n    for color in \"red\", \"green\", \"blue\":\n        click.echo(click.style(f\"I am colored {color}\", fg=color))\n        click.echo(click.style(f\"I am background colored {color}\", bg=color))\n\n\n@cli.command()\ndef pager():\n    \"\"\"Demonstrates using the pager.\"\"\"\n    lines = []\n    for x in range(200):\n        lines.append(f\"{click.style(str(x), fg='green')}. Hello World!\")\n    click.echo_via_pager(\"\\n\".join(lines))\n\n\n@cli.command()\n@click.option(\n    \"--count\",\n    default=8000,\n    type=click.IntRange(1, 100000),\n    help=\"The number of items to process.\",\n)\ndef progress(count):\n    \"\"\"Demonstrates the progress bar.\"\"\"\n    items = range(count)\n\n    def process_slowly(item):\n        time.sleep(0.002 * random.random())\n\n    def filter(items):\n        for item in items:\n            if random.random() > 0.3:\n                yield item\n\n    with click.progressbar(\n        items, label=\"Processing accounts\", fill_char=click.style(\"#\", fg=\"green\")\n    ) as bar:\n        for item in bar:\n            process_slowly(item)\n\n    def show_item(item):\n        if item is not None:\n            return f\"Item #{item}\"\n\n    with click.progressbar(\n        filter(items),\n        label=\"Committing transaction\",\n        fill_char=click.style(\"#\", fg=\"yellow\"),\n        item_show_func=show_item,\n    ) as bar:\n        for item in bar:\n            process_slowly(item)\n\n    with click.progressbar(\n        length=count,\n        label=\"Counting\",\n        bar_template=\"%(label)s  %(bar)s | %(info)s\",\n        fill_char=click.style(\"█\", fg=\"cyan\"),\n        empty_char=\" \",\n    ) as bar:\n        for item in bar:\n            process_slowly(item)\n\n    with click.progressbar(\n        length=count,\n        width=0,\n        show_percent=False,\n        show_eta=False,\n        fill_char=click.style(\"#\", fg=\"magenta\"),\n    ) as bar:\n        for item in bar:\n            process_slowly(item)\n\n    # 'Non-linear progress bar'\n    steps = [math.exp(x * 1.0 / 20) - 1 for x in range(20)]\n    count = int(sum(steps))\n    with click.progressbar(\n        length=count,\n        show_percent=False,\n        label=\"Slowing progress bar\",\n        fill_char=click.style(\"█\", fg=\"green\"),\n    ) as bar:\n        for item in steps:\n            time.sleep(item)\n            bar.update(item)\n\n\n@cli.command()\n@click.argument(\"url\")\ndef open(url):\n    \"\"\"Opens a file or URL In the default application.\"\"\"\n    click.launch(url)\n\n\n@cli.command()\n@click.argument(\"url\")\ndef locate(url):\n    \"\"\"Opens a file or URL In the default application.\"\"\"\n    click.launch(url, locate=True)\n\n\n@cli.command()\ndef edit():\n    \"\"\"Opens an editor with some text in it.\"\"\"\n    MARKER = \"# Everything below is ignored\\n\"\n    message = click.edit(f\"\\n\\n{MARKER}\")\n    if message is not None:\n        msg = message.split(MARKER, 1)[0].rstrip(\"\\n\")\n        if not msg:\n            click.echo(\"Empty message!\")\n        else:\n            click.echo(f\"Message:\\n{msg}\")\n    else:\n        click.echo(\"You did not enter anything!\")\n\n\n@cli.command()\ndef clear():\n    \"\"\"Clears the entire screen.\"\"\"\n    click.clear()\n\n\n@cli.command()\ndef pause():\n    \"\"\"Waits for the user to press a button.\"\"\"\n    click.pause()\n\n\n@cli.command()\ndef menu():\n    \"\"\"Shows a simple menu.\"\"\"\n    menu = \"main\"\n    while True:\n        if menu == \"main\":\n            click.echo(\"Main menu:\")\n            click.echo(\"  d: debug menu\")\n            click.echo(\"  q: quit\")\n            char = click.getchar()\n            if char == \"d\":\n                menu = \"debug\"\n            elif char == \"q\":\n                menu = \"quit\"\n            else:\n                click.echo(\"Invalid input\")\n        elif menu == \"debug\":\n            click.echo(\"Debug menu\")\n            click.echo(\"  b: back\")\n            char = click.getchar()\n            if char == \"b\":\n                menu = \"main\"\n            else:\n                click.echo(\"Invalid input\")\n        elif menu == \"quit\":\n            return\n"
  },
  {
    "path": "examples/validation/README",
    "content": "$ validation_\n\n  validation is a simple example of an application that\n  performs custom validation of parameters in different\n  ways.\n\n  This example requires Click 2.0 or higher.\n\nUsage:\n\n  $ pip install --editable .\n  $ validation --help\n"
  },
  {
    "path": "examples/validation/pyproject.toml",
    "content": "[project]\nname = \"click-example-validation\"\nversion = \"1.0.0\"\ndescription = \"Click validation example\"\nrequires-python = \">=3.10\"\ndependencies = [\n    \"click>=8.1\",\n]\n\n[project.scripts]\nvalidation = \"validation:cli\"\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"validation\"\n"
  },
  {
    "path": "examples/validation/validation.py",
    "content": "from urllib import parse as urlparse\n\nimport click\n\n\ndef validate_count(ctx, param, value):\n    if value < 0 or value % 2 != 0:\n        raise click.BadParameter(\"Should be a positive, even integer.\")\n    return value\n\n\nclass URL(click.ParamType):\n    name = \"url\"\n\n    def convert(self, value, param, ctx):\n        if not isinstance(value, tuple):\n            value = urlparse.urlparse(value)\n            if value.scheme not in (\"http\", \"https\"):\n                self.fail(\n                    f\"invalid URL scheme ({value.scheme}). Only HTTP URLs are allowed\",\n                    param,\n                    ctx,\n                )\n        return value\n\n\n@click.command()\n@click.option(\n    \"--count\", default=2, callback=validate_count, help=\"A positive even number.\"\n)\n@click.option(\"--foo\", help=\"A mysterious parameter.\")\n@click.option(\"--url\", help=\"A URL\", type=URL())\n@click.version_option()\ndef cli(count, foo, url):\n    \"\"\"Validation.\n\n    This example validates parameters in different ways.  It does it\n    through callbacks, through a custom type as well as by validating\n    manually in the function.\n    \"\"\"\n    if foo is not None and foo != \"wat\":\n        raise click.BadParameter(\n            'If a value is provided it needs to be the value \"wat\".',\n            param_hint=[\"--foo\"],\n        )\n    click.echo(f\"count: {count}\")\n    click.echo(f\"foo: {foo}\")\n    click.echo(f\"url: {url!r}\")\n"
  },
  {
    "path": "pyproject.toml",
    "content": "[project]\nname = \"click\"\nversion = \"8.3.dev\"\ndescription = \"Composable command line interface toolkit\"\nreadme = \"README.md\"\nlicense = \"BSD-3-Clause\"\nlicense-files = [\"LICENSE.txt\"]\nmaintainers = [{name = \"Pallets\", email = \"contact@palletsprojects.com\"}]\nclassifiers = [\n    \"Development Status :: 5 - Production/Stable\",\n    \"Intended Audience :: Developers\",\n    \"Operating System :: OS Independent\",\n    \"Programming Language :: Python\",\n    \"Typing :: Typed\",\n]\nrequires-python = \">=3.10\"\ndependencies = [\n    \"colorama; platform_system == 'Windows'\",\n]\n\n[project.urls]\nDonate = \"https://palletsprojects.com/donate\"\nDocumentation = \"https://click.palletsprojects.com/\"\nChanges = \"https://click.palletsprojects.com/page/changes/\"\nSource = \"https://github.com/pallets/click/\"\nChat = \"https://discord.gg/pallets\"\n\n[dependency-groups]\ndev = [\n    \"ruff\",\n    \"tox\",\n    \"tox-uv\",\n]\ndocs = [\n    \"myst-parser\",\n    \"pallets-sphinx-themes\",\n    \"sphinx\",\n    \"sphinx-tabs\",\n    \"sphinxcontrib-log-cabinet\",\n]\ndocs-auto = [\n    \"sphinx-autobuild\",\n]\ngha-update = [\n    \"gha-update ; python_full_version >= '3.12'\",\n]\npre-commit = [\n    \"pre-commit\",\n    \"pre-commit-uv\",\n]\ntests = [\n    \"pytest\",\n]\ntyping = [\n    \"mypy\",\n    \"pyright\",\n    \"pytest\",\n]\n\n[build-system]\nrequires = [\"flit_core>=3.11,<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"click\"\n\n[tool.flit.sdist]\ninclude = [\n    \"docs/\",\n    \"tests/\",\n    \"CHANGES.rst\",\n    \"uv.lock\"\n]\nexclude = [\n    \"docs/_build/\",\n]\n\n[tool.uv]\ndefault-groups = [\"dev\", \"pre-commit\", \"tests\", \"typing\"]\n\n[tool.pytest.ini_options]\ntestpaths = [\"tests\"]\nfilterwarnings = [\n    \"error\",\n]\n\n[tool.coverage.run]\nbranch = true\nsource = [\"click\", \"tests\"]\n\n[tool.coverage.paths]\nsource = [\"src\", \"*/site-packages\"]\n\n[tool.coverage.report]\nexclude_also = [\n    \"if t.TYPE_CHECKING\",\n    \"raise NotImplementedError\",\n    \": \\\\.{3}\",\n]\n\n[tool.mypy]\npython_version = \"3.10\"\nfiles = [\"src\", \"tests/typing\"]\nshow_error_codes = true\npretty = true\nstrict = true\n\n[[tool.mypy.overrides]]\nmodule = [\n    \"colorama.*\",\n]\nignore_missing_imports = true\n\n[tool.pyright]\npythonVersion = \"3.10\"\ninclude = [\"src\", \"tests/typing\"]\ntypeCheckingMode = \"basic\"\n\n[tool.ruff]\nextend-exclude = [\"examples/\"]\nsrc = [\"src\"]\nfix = true\nshow-fixes = true\noutput-format = \"full\"\n\n[tool.ruff.lint]\nselect = [\n    \"B\",  # flake8-bugbear\n    \"E\",  # pycodestyle error\n    \"F\",  # pyflakes\n    \"I\",  # isort\n    \"UP\",  # pyupgrade\n    \"W\",  # pycodestyle warning\n]\n\n[tool.ruff.lint.isort]\nforce-single-line = true\norder-by-type = false\n\n[tool.tox]\nenv_list = [\n    \"py3.14\", \"py3.13\", \"py3.12\", \"py3.11\", \"py3.10\",\n    \"py3.14t\",\n    \"pypy3.11\",\n    \"style\",\n    \"typing\",\n    \"docs\",\n]\n\n[tool.tox.env_run_base]\ndescription = \"pytest on latest dependency versions\"\nrunner = \"uv-venv-lock-runner\"\npackage = \"wheel\"\nwheel_build_env = \".pkg\"\nconstrain_package_deps = true\nuse_frozen_constraints = true\ndependency_groups = [\"tests\"]\ncommands = [[\n    \"pytest\", \"-v\", \"--tb=short\", \"--basetemp={env_tmp_dir}\",\n    {replace = \"posargs\", default = [], extend = true},\n]]\n\n[tool.tox.env.style]\ndescription = \"run all pre-commit hooks on all files\"\ndependency_groups = [\"pre-commit\"]\nskip_install = true\ncommands = [[\"pre-commit\", \"run\", \"--all-files\"]]\n\n[tool.tox.env.typing]\ndescription = \"run static type checkers\"\ndependency_groups = [\"typing\"]\ncommands = [\n    [\"mypy\"],\n    [\"pyright\", \"--ignoreexternal\", \"--verifytypes\", \"click\"],\n]\n\n[tool.tox.env.docs]\ndescription = \"build docs\"\ndependency_groups = [\"docs\"]\ncommands = [[\"sphinx-build\", \"-E\", \"-W\", \"-b\", \"dirhtml\", \"docs\", \"docs/_build/dirhtml\"]]\n\n[tool.tox.env.docs-auto]\ndescription = \"continuously rebuild docs and start a local server\"\ndependency_groups = [\"docs\", \"docs-auto\"]\ncommands = [[\"sphinx-autobuild\", \"-W\", \"-b\", \"dirhtml\", \"--watch\", \"src\", \"docs\", \"docs/_build/dirhtml\"]]\n\n[tool.tox.env.update-actions]\ndescription = \"update GitHub Actions pins\"\nlabels = [\"update\"]\ndependency_groups = [\"gha-update\"]\nskip_install = true\ncommands = [[\"gha-update\"]]\n\n[tool.tox.env.update-pre_commit]\ndescription = \"update pre-commit pins\"\nlabels = [\"update\"]\ndependency_groups = [\"pre-commit\"]\nskip_install = true\ncommands = [[\"pre-commit\", \"autoupdate\", \"--freeze\", \"-j4\"]]\n\n[tool.tox.env.update-requirements]\ndescription = \"update uv lock\"\nlabels = [\"update\"]\ndependency_groups = []\nno_default_groups = true\nskip_install = true\ncommands = [[\"uv\", \"lock\", {replace = \"posargs\", default = [\"-U\"], extend = true}]]\n"
  },
  {
    "path": "src/click/__init__.py",
    "content": "\"\"\"\nClick is a simple Python module inspired by the stdlib optparse to make\nwriting command line scripts fun. Unlike other modules, it's based\naround a simple API that does not come with too much magic and is\ncomposable.\n\"\"\"\n\nfrom __future__ import annotations\n\nfrom .core import Argument as Argument\nfrom .core import Command as Command\nfrom .core import CommandCollection as CommandCollection\nfrom .core import Context as Context\nfrom .core import Group as Group\nfrom .core import Option as Option\nfrom .core import Parameter as Parameter\nfrom .decorators import argument as argument\nfrom .decorators import command as command\nfrom .decorators import confirmation_option as confirmation_option\nfrom .decorators import group as group\nfrom .decorators import help_option as help_option\nfrom .decorators import make_pass_decorator as make_pass_decorator\nfrom .decorators import option as option\nfrom .decorators import pass_context as pass_context\nfrom .decorators import pass_obj as pass_obj\nfrom .decorators import password_option as password_option\nfrom .decorators import version_option as version_option\nfrom .exceptions import Abort as Abort\nfrom .exceptions import BadArgumentUsage as BadArgumentUsage\nfrom .exceptions import BadOptionUsage as BadOptionUsage\nfrom .exceptions import BadParameter as BadParameter\nfrom .exceptions import ClickException as ClickException\nfrom .exceptions import FileError as FileError\nfrom .exceptions import MissingParameter as MissingParameter\nfrom .exceptions import NoSuchOption as NoSuchOption\nfrom .exceptions import UsageError as UsageError\nfrom .formatting import HelpFormatter as HelpFormatter\nfrom .formatting import wrap_text as wrap_text\nfrom .globals import get_current_context as get_current_context\nfrom .termui import clear as clear\nfrom .termui import confirm as confirm\nfrom .termui import echo_via_pager as echo_via_pager\nfrom .termui import edit as edit\nfrom .termui import getchar as getchar\nfrom .termui import launch as launch\nfrom .termui import pause as pause\nfrom .termui import progressbar as progressbar\nfrom .termui import prompt as prompt\nfrom .termui import secho as secho\nfrom .termui import style as style\nfrom .termui import unstyle as unstyle\nfrom .types import BOOL as BOOL\nfrom .types import Choice as Choice\nfrom .types import DateTime as DateTime\nfrom .types import File as File\nfrom .types import FLOAT as FLOAT\nfrom .types import FloatRange as FloatRange\nfrom .types import INT as INT\nfrom .types import IntRange as IntRange\nfrom .types import ParamType as ParamType\nfrom .types import Path as Path\nfrom .types import STRING as STRING\nfrom .types import Tuple as Tuple\nfrom .types import UNPROCESSED as UNPROCESSED\nfrom .types import UUID as UUID\nfrom .utils import echo as echo\nfrom .utils import format_filename as format_filename\nfrom .utils import get_app_dir as get_app_dir\nfrom .utils import get_binary_stream as get_binary_stream\nfrom .utils import get_text_stream as get_text_stream\nfrom .utils import open_file as open_file\n\n\ndef __getattr__(name: str) -> object:\n    import warnings\n\n    if name == \"BaseCommand\":\n        from .core import _BaseCommand\n\n        warnings.warn(\n            \"'BaseCommand' is deprecated and will be removed in Click 9.0. Use\"\n            \" 'Command' instead.\",\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return _BaseCommand\n\n    if name == \"MultiCommand\":\n        from .core import _MultiCommand\n\n        warnings.warn(\n            \"'MultiCommand' is deprecated and will be removed in Click 9.0. Use\"\n            \" 'Group' instead.\",\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return _MultiCommand\n\n    if name == \"OptionParser\":\n        from .parser import _OptionParser\n\n        warnings.warn(\n            \"'OptionParser' is deprecated and will be removed in Click 9.0. The\"\n            \" old parser is available in 'optparse'.\",\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return _OptionParser\n\n    if name == \"__version__\":\n        import importlib.metadata\n        import warnings\n\n        warnings.warn(\n            \"The '__version__' attribute is deprecated and will be removed in\"\n            \" Click 9.1. Use feature detection or\"\n            \" 'importlib.metadata.version(\\\"click\\\")' instead.\",\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return importlib.metadata.version(\"click\")\n\n    raise AttributeError(name)\n"
  },
  {
    "path": "src/click/_compat.py",
    "content": "from __future__ import annotations\n\nimport codecs\nimport collections.abc as cabc\nimport io\nimport os\nimport re\nimport sys\nimport typing as t\nfrom types import TracebackType\nfrom weakref import WeakKeyDictionary\n\nCYGWIN = sys.platform.startswith(\"cygwin\")\nWIN = sys.platform.startswith(\"win\")\nauto_wrap_for_ansi: t.Callable[[t.TextIO], t.TextIO] | None = None\n_ansi_re = re.compile(r\"\\033\\[[;?0-9]*[a-zA-Z]\")\n\n\ndef _make_text_stream(\n    stream: t.BinaryIO,\n    encoding: str | None,\n    errors: str | None,\n    force_readable: bool = False,\n    force_writable: bool = False,\n) -> t.TextIO:\n    if encoding is None:\n        encoding = get_best_encoding(stream)\n    if errors is None:\n        errors = \"replace\"\n    return _NonClosingTextIOWrapper(\n        stream,\n        encoding,\n        errors,\n        line_buffering=True,\n        force_readable=force_readable,\n        force_writable=force_writable,\n    )\n\n\ndef is_ascii_encoding(encoding: str) -> bool:\n    \"\"\"Checks if a given encoding is ascii.\"\"\"\n    try:\n        return codecs.lookup(encoding).name == \"ascii\"\n    except LookupError:\n        return False\n\n\ndef get_best_encoding(stream: t.IO[t.Any]) -> str:\n    \"\"\"Returns the default stream encoding if not found.\"\"\"\n    rv = getattr(stream, \"encoding\", None) or sys.getdefaultencoding()\n    if is_ascii_encoding(rv):\n        return \"utf-8\"\n    return rv\n\n\nclass _NonClosingTextIOWrapper(io.TextIOWrapper):\n    def __init__(\n        self,\n        stream: t.BinaryIO,\n        encoding: str | None,\n        errors: str | None,\n        force_readable: bool = False,\n        force_writable: bool = False,\n        **extra: t.Any,\n    ) -> None:\n        self._stream = stream = t.cast(\n            t.BinaryIO, _FixupStream(stream, force_readable, force_writable)\n        )\n        super().__init__(stream, encoding, errors, **extra)\n\n    def __del__(self) -> None:\n        try:\n            self.detach()\n        except Exception:\n            pass\n\n    def isatty(self) -> bool:\n        # https://bitbucket.org/pypy/pypy/issue/1803\n        return self._stream.isatty()\n\n\nclass _FixupStream:\n    \"\"\"The new io interface needs more from streams than streams\n    traditionally implement.  As such, this fix-up code is necessary in\n    some circumstances.\n\n    The forcing of readable and writable flags are there because some tools\n    put badly patched objects on sys (one such offender are certain version\n    of jupyter notebook).\n    \"\"\"\n\n    def __init__(\n        self,\n        stream: t.BinaryIO,\n        force_readable: bool = False,\n        force_writable: bool = False,\n    ):\n        self._stream = stream\n        self._force_readable = force_readable\n        self._force_writable = force_writable\n\n    def __getattr__(self, name: str) -> t.Any:\n        return getattr(self._stream, name)\n\n    def read1(self, size: int) -> bytes:\n        f = getattr(self._stream, \"read1\", None)\n\n        if f is not None:\n            return t.cast(bytes, f(size))\n\n        return self._stream.read(size)\n\n    def readable(self) -> bool:\n        if self._force_readable:\n            return True\n        x = getattr(self._stream, \"readable\", None)\n        if x is not None:\n            return t.cast(bool, x())\n        try:\n            self._stream.read(0)\n        except Exception:\n            return False\n        return True\n\n    def writable(self) -> bool:\n        if self._force_writable:\n            return True\n        x = getattr(self._stream, \"writable\", None)\n        if x is not None:\n            return t.cast(bool, x())\n        try:\n            self._stream.write(b\"\")\n        except Exception:\n            try:\n                self._stream.write(b\"\")\n            except Exception:\n                return False\n        return True\n\n    def seekable(self) -> bool:\n        x = getattr(self._stream, \"seekable\", None)\n        if x is not None:\n            return t.cast(bool, x())\n        try:\n            self._stream.seek(self._stream.tell())\n        except Exception:\n            return False\n        return True\n\n\ndef _is_binary_reader(stream: t.IO[t.Any], default: bool = False) -> bool:\n    try:\n        return isinstance(stream.read(0), bytes)\n    except Exception:\n        return default\n        # This happens in some cases where the stream was already\n        # closed.  In this case, we assume the default.\n\n\ndef _is_binary_writer(stream: t.IO[t.Any], default: bool = False) -> bool:\n    try:\n        stream.write(b\"\")\n    except Exception:\n        try:\n            stream.write(\"\")\n            return False\n        except Exception:\n            pass\n        return default\n    return True\n\n\ndef _find_binary_reader(stream: t.IO[t.Any]) -> t.BinaryIO | None:\n    # We need to figure out if the given stream is already binary.\n    # This can happen because the official docs recommend detaching\n    # the streams to get binary streams.  Some code might do this, so\n    # we need to deal with this case explicitly.\n    if _is_binary_reader(stream, False):\n        return t.cast(t.BinaryIO, stream)\n\n    buf = getattr(stream, \"buffer\", None)\n\n    # Same situation here; this time we assume that the buffer is\n    # actually binary in case it's closed.\n    if buf is not None and _is_binary_reader(buf, True):\n        return t.cast(t.BinaryIO, buf)\n\n    return None\n\n\ndef _find_binary_writer(stream: t.IO[t.Any]) -> t.BinaryIO | None:\n    # We need to figure out if the given stream is already binary.\n    # This can happen because the official docs recommend detaching\n    # the streams to get binary streams.  Some code might do this, so\n    # we need to deal with this case explicitly.\n    if _is_binary_writer(stream, False):\n        return t.cast(t.BinaryIO, stream)\n\n    buf = getattr(stream, \"buffer\", None)\n\n    # Same situation here; this time we assume that the buffer is\n    # actually binary in case it's closed.\n    if buf is not None and _is_binary_writer(buf, True):\n        return t.cast(t.BinaryIO, buf)\n\n    return None\n\n\ndef _stream_is_misconfigured(stream: t.TextIO) -> bool:\n    \"\"\"A stream is misconfigured if its encoding is ASCII.\"\"\"\n    # If the stream does not have an encoding set, we assume it's set\n    # to ASCII.  This appears to happen in certain unittest\n    # environments.  It's not quite clear what the correct behavior is\n    # but this at least will force Click to recover somehow.\n    return is_ascii_encoding(getattr(stream, \"encoding\", None) or \"ascii\")\n\n\ndef _is_compat_stream_attr(stream: t.TextIO, attr: str, value: str | None) -> bool:\n    \"\"\"A stream attribute is compatible if it is equal to the\n    desired value or the desired value is unset and the attribute\n    has a value.\n    \"\"\"\n    stream_value = getattr(stream, attr, None)\n    return stream_value == value or (value is None and stream_value is not None)\n\n\ndef _is_compatible_text_stream(\n    stream: t.TextIO, encoding: str | None, errors: str | None\n) -> bool:\n    \"\"\"Check if a stream's encoding and errors attributes are\n    compatible with the desired values.\n    \"\"\"\n    return _is_compat_stream_attr(\n        stream, \"encoding\", encoding\n    ) and _is_compat_stream_attr(stream, \"errors\", errors)\n\n\ndef _force_correct_text_stream(\n    text_stream: t.IO[t.Any],\n    encoding: str | None,\n    errors: str | None,\n    is_binary: t.Callable[[t.IO[t.Any], bool], bool],\n    find_binary: t.Callable[[t.IO[t.Any]], t.BinaryIO | None],\n    force_readable: bool = False,\n    force_writable: bool = False,\n) -> t.TextIO:\n    if is_binary(text_stream, False):\n        binary_reader = t.cast(t.BinaryIO, text_stream)\n    else:\n        text_stream = t.cast(t.TextIO, text_stream)\n        # If the stream looks compatible, and won't default to a\n        # misconfigured ascii encoding, return it as-is.\n        if _is_compatible_text_stream(text_stream, encoding, errors) and not (\n            encoding is None and _stream_is_misconfigured(text_stream)\n        ):\n            return text_stream\n\n        # Otherwise, get the underlying binary reader.\n        possible_binary_reader = find_binary(text_stream)\n\n        # If that's not possible, silently use the original reader\n        # and get mojibake instead of exceptions.\n        if possible_binary_reader is None:\n            return text_stream\n\n        binary_reader = possible_binary_reader\n\n    # Default errors to replace instead of strict in order to get\n    # something that works.\n    if errors is None:\n        errors = \"replace\"\n\n    # Wrap the binary stream in a text stream with the correct\n    # encoding parameters.\n    return _make_text_stream(\n        binary_reader,\n        encoding,\n        errors,\n        force_readable=force_readable,\n        force_writable=force_writable,\n    )\n\n\ndef _force_correct_text_reader(\n    text_reader: t.IO[t.Any],\n    encoding: str | None,\n    errors: str | None,\n    force_readable: bool = False,\n) -> t.TextIO:\n    return _force_correct_text_stream(\n        text_reader,\n        encoding,\n        errors,\n        _is_binary_reader,\n        _find_binary_reader,\n        force_readable=force_readable,\n    )\n\n\ndef _force_correct_text_writer(\n    text_writer: t.IO[t.Any],\n    encoding: str | None,\n    errors: str | None,\n    force_writable: bool = False,\n) -> t.TextIO:\n    return _force_correct_text_stream(\n        text_writer,\n        encoding,\n        errors,\n        _is_binary_writer,\n        _find_binary_writer,\n        force_writable=force_writable,\n    )\n\n\ndef get_binary_stdin() -> t.BinaryIO:\n    reader = _find_binary_reader(sys.stdin)\n    if reader is None:\n        raise RuntimeError(\"Was not able to determine binary stream for sys.stdin.\")\n    return reader\n\n\ndef get_binary_stdout() -> t.BinaryIO:\n    writer = _find_binary_writer(sys.stdout)\n    if writer is None:\n        raise RuntimeError(\"Was not able to determine binary stream for sys.stdout.\")\n    return writer\n\n\ndef get_binary_stderr() -> t.BinaryIO:\n    writer = _find_binary_writer(sys.stderr)\n    if writer is None:\n        raise RuntimeError(\"Was not able to determine binary stream for sys.stderr.\")\n    return writer\n\n\ndef get_text_stdin(encoding: str | None = None, errors: str | None = None) -> t.TextIO:\n    rv = _get_windows_console_stream(sys.stdin, encoding, errors)\n    if rv is not None:\n        return rv\n    return _force_correct_text_reader(sys.stdin, encoding, errors, force_readable=True)\n\n\ndef get_text_stdout(encoding: str | None = None, errors: str | None = None) -> t.TextIO:\n    rv = _get_windows_console_stream(sys.stdout, encoding, errors)\n    if rv is not None:\n        return rv\n    return _force_correct_text_writer(sys.stdout, encoding, errors, force_writable=True)\n\n\ndef get_text_stderr(encoding: str | None = None, errors: str | None = None) -> t.TextIO:\n    rv = _get_windows_console_stream(sys.stderr, encoding, errors)\n    if rv is not None:\n        return rv\n    return _force_correct_text_writer(sys.stderr, encoding, errors, force_writable=True)\n\n\ndef _wrap_io_open(\n    file: str | os.PathLike[str] | int,\n    mode: str,\n    encoding: str | None,\n    errors: str | None,\n) -> t.IO[t.Any]:\n    \"\"\"Handles not passing ``encoding`` and ``errors`` in binary mode.\"\"\"\n    if \"b\" in mode:\n        return open(file, mode)\n\n    return open(file, mode, encoding=encoding, errors=errors)\n\n\ndef open_stream(\n    filename: str | os.PathLike[str],\n    mode: str = \"r\",\n    encoding: str | None = None,\n    errors: str | None = \"strict\",\n    atomic: bool = False,\n) -> tuple[t.IO[t.Any], bool]:\n    binary = \"b\" in mode\n    filename = os.fspath(filename)\n\n    # Standard streams first. These are simple because they ignore the\n    # atomic flag. Use fsdecode to handle Path(\"-\").\n    if os.fsdecode(filename) == \"-\":\n        if any(m in mode for m in [\"w\", \"a\", \"x\"]):\n            if binary:\n                return get_binary_stdout(), False\n            return get_text_stdout(encoding=encoding, errors=errors), False\n        if binary:\n            return get_binary_stdin(), False\n        return get_text_stdin(encoding=encoding, errors=errors), False\n\n    # Non-atomic writes directly go out through the regular open functions.\n    if not atomic:\n        return _wrap_io_open(filename, mode, encoding, errors), True\n\n    # Some usability stuff for atomic writes\n    if \"a\" in mode:\n        raise ValueError(\n            \"Appending to an existing file is not supported, because that\"\n            \" would involve an expensive `copy`-operation to a temporary\"\n            \" file. Open the file in normal `w`-mode and copy explicitly\"\n            \" if that's what you're after.\"\n        )\n    if \"x\" in mode:\n        raise ValueError(\"Use the `overwrite`-parameter instead.\")\n    if \"w\" not in mode:\n        raise ValueError(\"Atomic writes only make sense with `w`-mode.\")\n\n    # Atomic writes are more complicated.  They work by opening a file\n    # as a proxy in the same folder and then using the fdopen\n    # functionality to wrap it in a Python file.  Then we wrap it in an\n    # atomic file that moves the file over on close.\n    import errno\n    import random\n\n    try:\n        perm: int | None = os.stat(filename).st_mode\n    except OSError:\n        perm = None\n\n    flags = os.O_RDWR | os.O_CREAT | os.O_EXCL\n\n    if binary:\n        flags |= getattr(os, \"O_BINARY\", 0)\n\n    while True:\n        tmp_filename = os.path.join(\n            os.path.dirname(filename),\n            f\".__atomic-write{random.randrange(1 << 32):08x}\",\n        )\n        try:\n            fd = os.open(tmp_filename, flags, 0o666 if perm is None else perm)\n            break\n        except OSError as e:\n            if e.errno == errno.EEXIST or (\n                os.name == \"nt\"\n                and e.errno == errno.EACCES\n                and os.path.isdir(e.filename)\n                and os.access(e.filename, os.W_OK)\n            ):\n                continue\n            raise\n\n    if perm is not None:\n        os.chmod(tmp_filename, perm)  # in case perm includes bits in umask\n\n    f = _wrap_io_open(fd, mode, encoding, errors)\n    af = _AtomicFile(f, tmp_filename, os.path.realpath(filename))\n    return t.cast(t.IO[t.Any], af), True\n\n\nclass _AtomicFile:\n    def __init__(self, f: t.IO[t.Any], tmp_filename: str, real_filename: str) -> None:\n        self._f = f\n        self._tmp_filename = tmp_filename\n        self._real_filename = real_filename\n        self.closed = False\n\n    @property\n    def name(self) -> str:\n        return self._real_filename\n\n    def close(self, delete: bool = False) -> None:\n        if self.closed:\n            return\n        self._f.close()\n        os.replace(self._tmp_filename, self._real_filename)\n        self.closed = True\n\n    def __getattr__(self, name: str) -> t.Any:\n        return getattr(self._f, name)\n\n    def __enter__(self) -> _AtomicFile:\n        return self\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_value: BaseException | None,\n        tb: TracebackType | None,\n    ) -> None:\n        self.close(delete=exc_type is not None)\n\n    def __repr__(self) -> str:\n        return repr(self._f)\n\n\ndef strip_ansi(value: str) -> str:\n    return _ansi_re.sub(\"\", value)\n\n\ndef _is_jupyter_kernel_output(stream: t.IO[t.Any]) -> bool:\n    while isinstance(stream, (_FixupStream, _NonClosingTextIOWrapper)):\n        stream = stream._stream\n\n    return stream.__class__.__module__.startswith(\"ipykernel.\")\n\n\ndef should_strip_ansi(\n    stream: t.IO[t.Any] | None = None, color: bool | None = None\n) -> bool:\n    if color is None:\n        if stream is None:\n            stream = sys.stdin\n        return not isatty(stream) and not _is_jupyter_kernel_output(stream)\n    return not color\n\n\n# On Windows, wrap the output streams with colorama to support ANSI\n# color codes.\n# NOTE: double check is needed so mypy does not analyze this on Linux\nif sys.platform.startswith(\"win\") and WIN:\n    from ._winconsole import _get_windows_console_stream\n\n    def _get_argv_encoding() -> str:\n        import locale\n\n        return locale.getpreferredencoding()\n\n    _ansi_stream_wrappers: cabc.MutableMapping[t.TextIO, t.TextIO] = WeakKeyDictionary()\n\n    def auto_wrap_for_ansi(stream: t.TextIO, color: bool | None = None) -> t.TextIO:\n        \"\"\"Support ANSI color and style codes on Windows by wrapping a\n        stream with colorama.\n        \"\"\"\n        try:\n            cached = _ansi_stream_wrappers.get(stream)\n        except Exception:\n            cached = None\n\n        if cached is not None:\n            return cached\n\n        import colorama\n\n        strip = should_strip_ansi(stream, color)\n        ansi_wrapper = colorama.AnsiToWin32(stream, strip=strip)\n        rv = t.cast(t.TextIO, ansi_wrapper.stream)\n        _write = rv.write\n\n        def _safe_write(s: str) -> int:\n            try:\n                return _write(s)\n            except BaseException:\n                ansi_wrapper.reset_all()\n                raise\n\n        rv.write = _safe_write  # type: ignore[method-assign]\n\n        try:\n            _ansi_stream_wrappers[stream] = rv\n        except Exception:\n            pass\n\n        return rv\n\nelse:\n\n    def _get_argv_encoding() -> str:\n        return getattr(sys.stdin, \"encoding\", None) or sys.getfilesystemencoding()\n\n    def _get_windows_console_stream(\n        f: t.TextIO, encoding: str | None, errors: str | None\n    ) -> t.TextIO | None:\n        return None\n\n\ndef term_len(x: str) -> int:\n    return len(strip_ansi(x))\n\n\ndef isatty(stream: t.IO[t.Any]) -> bool:\n    try:\n        return stream.isatty()\n    except Exception:\n        return False\n\n\ndef _make_cached_stream_func(\n    src_func: t.Callable[[], t.TextIO | None],\n    wrapper_func: t.Callable[[], t.TextIO],\n) -> t.Callable[[], t.TextIO | None]:\n    cache: cabc.MutableMapping[t.TextIO, t.TextIO] = WeakKeyDictionary()\n\n    def func() -> t.TextIO | None:\n        stream = src_func()\n\n        if stream is None:\n            return None\n\n        try:\n            rv = cache.get(stream)\n        except Exception:\n            rv = None\n        if rv is not None:\n            return rv\n        rv = wrapper_func()\n        try:\n            cache[stream] = rv\n        except Exception:\n            pass\n        return rv\n\n    return func\n\n\n_default_text_stdin = _make_cached_stream_func(lambda: sys.stdin, get_text_stdin)\n_default_text_stdout = _make_cached_stream_func(lambda: sys.stdout, get_text_stdout)\n_default_text_stderr = _make_cached_stream_func(lambda: sys.stderr, get_text_stderr)\n\n\nbinary_streams: cabc.Mapping[str, t.Callable[[], t.BinaryIO]] = {\n    \"stdin\": get_binary_stdin,\n    \"stdout\": get_binary_stdout,\n    \"stderr\": get_binary_stderr,\n}\n\ntext_streams: cabc.Mapping[str, t.Callable[[str | None, str | None], t.TextIO]] = {\n    \"stdin\": get_text_stdin,\n    \"stdout\": get_text_stdout,\n    \"stderr\": get_text_stderr,\n}\n"
  },
  {
    "path": "src/click/_termui_impl.py",
    "content": "\"\"\"\nThis module contains implementations for the termui module. To keep the\nimport time of Click down, some infrequently used functionality is\nplaced in this module and only imported as needed.\n\"\"\"\n\nfrom __future__ import annotations\n\nimport collections.abc as cabc\nimport contextlib\nimport math\nimport os\nimport shlex\nimport sys\nimport time\nimport typing as t\nfrom gettext import gettext as _\nfrom io import StringIO\nfrom pathlib import Path\nfrom types import TracebackType\n\nfrom ._compat import _default_text_stdout\nfrom ._compat import CYGWIN\nfrom ._compat import get_best_encoding\nfrom ._compat import isatty\nfrom ._compat import open_stream\nfrom ._compat import strip_ansi\nfrom ._compat import term_len\nfrom ._compat import WIN\nfrom .exceptions import ClickException\nfrom .utils import echo\n\nV = t.TypeVar(\"V\")\n\nif os.name == \"nt\":\n    BEFORE_BAR = \"\\r\"\n    AFTER_BAR = \"\\n\"\nelse:\n    BEFORE_BAR = \"\\r\\033[?25l\"\n    AFTER_BAR = \"\\033[?25h\\n\"\n\n\nclass ProgressBar(t.Generic[V]):\n    def __init__(\n        self,\n        iterable: cabc.Iterable[V] | None,\n        length: int | None = None,\n        fill_char: str = \"#\",\n        empty_char: str = \" \",\n        bar_template: str = \"%(bar)s\",\n        info_sep: str = \"  \",\n        hidden: bool = False,\n        show_eta: bool = True,\n        show_percent: bool | None = None,\n        show_pos: bool = False,\n        item_show_func: t.Callable[[V | None], str | None] | None = None,\n        label: str | None = None,\n        file: t.TextIO | None = None,\n        color: bool | None = None,\n        update_min_steps: int = 1,\n        width: int = 30,\n    ) -> None:\n        self.fill_char = fill_char\n        self.empty_char = empty_char\n        self.bar_template = bar_template\n        self.info_sep = info_sep\n        self.hidden = hidden\n        self.show_eta = show_eta\n        self.show_percent = show_percent\n        self.show_pos = show_pos\n        self.item_show_func = item_show_func\n        self.label: str = label or \"\"\n\n        if file is None:\n            file = _default_text_stdout()\n\n            # There are no standard streams attached to write to. For example,\n            # pythonw on Windows.\n            if file is None:\n                file = StringIO()\n\n        self.file = file\n        self.color = color\n        self.update_min_steps = update_min_steps\n        self._completed_intervals = 0\n        self.width: int = width\n        self.autowidth: bool = width == 0\n\n        if length is None:\n            from operator import length_hint\n\n            length = length_hint(iterable, -1)\n\n            if length == -1:\n                length = None\n        if iterable is None:\n            if length is None:\n                raise TypeError(\"iterable or length is required\")\n            iterable = t.cast(\"cabc.Iterable[V]\", range(length))\n        self.iter: cabc.Iterable[V] = iter(iterable)\n        self.length = length\n        self.pos: int = 0\n        self.avg: list[float] = []\n        self.last_eta: float\n        self.start: float\n        self.start = self.last_eta = time.time()\n        self.eta_known: bool = False\n        self.finished: bool = False\n        self.max_width: int | None = None\n        self.entered: bool = False\n        self.current_item: V | None = None\n        self._is_atty = isatty(self.file)\n        self._last_line: str | None = None\n\n    def __enter__(self) -> ProgressBar[V]:\n        self.entered = True\n        self.render_progress()\n        return self\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_value: BaseException | None,\n        tb: TracebackType | None,\n    ) -> None:\n        self.render_finish()\n\n    def __iter__(self) -> cabc.Iterator[V]:\n        if not self.entered:\n            raise RuntimeError(\"You need to use progress bars in a with block.\")\n        self.render_progress()\n        return self.generator()\n\n    def __next__(self) -> V:\n        # Iteration is defined in terms of a generator function,\n        # returned by iter(self); use that to define next(). This works\n        # because `self.iter` is an iterable consumed by that generator,\n        # so it is re-entry safe. Calling `next(self.generator())`\n        # twice works and does \"what you want\".\n        return next(iter(self))\n\n    def render_finish(self) -> None:\n        if self.hidden or not self._is_atty:\n            return\n        self.file.write(AFTER_BAR)\n        self.file.flush()\n\n    @property\n    def pct(self) -> float:\n        if self.finished:\n            return 1.0\n        return min(self.pos / (float(self.length or 1) or 1), 1.0)\n\n    @property\n    def time_per_iteration(self) -> float:\n        if not self.avg:\n            return 0.0\n        return sum(self.avg) / float(len(self.avg))\n\n    @property\n    def eta(self) -> float:\n        if self.length is not None and not self.finished:\n            return self.time_per_iteration * (self.length - self.pos)\n        return 0.0\n\n    def format_eta(self) -> str:\n        if self.eta_known:\n            t = int(self.eta)\n            seconds = t % 60\n            t //= 60\n            minutes = t % 60\n            t //= 60\n            hours = t % 24\n            t //= 24\n            if t > 0:\n                return f\"{t}d {hours:02}:{minutes:02}:{seconds:02}\"\n            else:\n                return f\"{hours:02}:{minutes:02}:{seconds:02}\"\n        return \"\"\n\n    def format_pos(self) -> str:\n        pos = str(self.pos)\n        if self.length is not None:\n            pos += f\"/{self.length}\"\n        return pos\n\n    def format_pct(self) -> str:\n        return f\"{int(self.pct * 100): 4}%\"[1:]\n\n    def format_bar(self) -> str:\n        if self.length is not None:\n            bar_length = int(self.pct * self.width)\n            bar = self.fill_char * bar_length\n            bar += self.empty_char * (self.width - bar_length)\n        elif self.finished:\n            bar = self.fill_char * self.width\n        else:\n            chars = list(self.empty_char * (self.width or 1))\n            if self.time_per_iteration != 0:\n                chars[\n                    int(\n                        (math.cos(self.pos * self.time_per_iteration) / 2.0 + 0.5)\n                        * self.width\n                    )\n                ] = self.fill_char\n            bar = \"\".join(chars)\n        return bar\n\n    def format_progress_line(self) -> str:\n        show_percent = self.show_percent\n\n        info_bits = []\n        if self.length is not None and show_percent is None:\n            show_percent = not self.show_pos\n\n        if self.show_pos:\n            info_bits.append(self.format_pos())\n        if show_percent:\n            info_bits.append(self.format_pct())\n        if self.show_eta and self.eta_known and not self.finished:\n            info_bits.append(self.format_eta())\n        if self.item_show_func is not None:\n            item_info = self.item_show_func(self.current_item)\n            if item_info is not None:\n                info_bits.append(item_info)\n\n        return (\n            self.bar_template\n            % {\n                \"label\": self.label,\n                \"bar\": self.format_bar(),\n                \"info\": self.info_sep.join(info_bits),\n            }\n        ).rstrip()\n\n    def render_progress(self) -> None:\n        if self.hidden:\n            return\n\n        if not self._is_atty:\n            # Only output the label once if the output is not a TTY.\n            if self._last_line != self.label:\n                self._last_line = self.label\n                echo(self.label, file=self.file, color=self.color)\n            return\n\n        buf = []\n        # Update width in case the terminal has been resized\n        if self.autowidth:\n            import shutil\n\n            old_width = self.width\n            self.width = 0\n            clutter_length = term_len(self.format_progress_line())\n            new_width = max(0, shutil.get_terminal_size().columns - clutter_length)\n            if new_width < old_width and self.max_width is not None:\n                buf.append(BEFORE_BAR)\n                buf.append(\" \" * self.max_width)\n                self.max_width = new_width\n            self.width = new_width\n\n        clear_width = self.width\n        if self.max_width is not None:\n            clear_width = self.max_width\n\n        buf.append(BEFORE_BAR)\n        line = self.format_progress_line()\n        line_len = term_len(line)\n        if self.max_width is None or self.max_width < line_len:\n            self.max_width = line_len\n\n        buf.append(line)\n        buf.append(\" \" * (clear_width - line_len))\n        line = \"\".join(buf)\n        # Render the line only if it changed.\n\n        if line != self._last_line:\n            self._last_line = line\n            echo(line, file=self.file, color=self.color, nl=False)\n            self.file.flush()\n\n    def make_step(self, n_steps: int) -> None:\n        self.pos += n_steps\n        if self.length is not None and self.pos >= self.length:\n            self.finished = True\n\n        if (time.time() - self.last_eta) < 1.0:\n            return\n\n        self.last_eta = time.time()\n\n        # self.avg is a rolling list of length <= 7 of steps where steps are\n        # defined as time elapsed divided by the total progress through\n        # self.length.\n        if self.pos:\n            step = (time.time() - self.start) / self.pos\n        else:\n            step = time.time() - self.start\n\n        self.avg = self.avg[-6:] + [step]\n\n        self.eta_known = self.length is not None\n\n    def update(self, n_steps: int, current_item: V | None = None) -> None:\n        \"\"\"Update the progress bar by advancing a specified number of\n        steps, and optionally set the ``current_item`` for this new\n        position.\n\n        :param n_steps: Number of steps to advance.\n        :param current_item: Optional item to set as ``current_item``\n            for the updated position.\n\n        .. versionchanged:: 8.0\n            Added the ``current_item`` optional parameter.\n\n        .. versionchanged:: 8.0\n            Only render when the number of steps meets the\n            ``update_min_steps`` threshold.\n        \"\"\"\n        if current_item is not None:\n            self.current_item = current_item\n\n        self._completed_intervals += n_steps\n\n        if self._completed_intervals >= self.update_min_steps:\n            self.make_step(self._completed_intervals)\n            self.render_progress()\n            self._completed_intervals = 0\n\n    def finish(self) -> None:\n        self.eta_known = False\n        self.current_item = None\n        self.finished = True\n\n    def generator(self) -> cabc.Iterator[V]:\n        \"\"\"Return a generator which yields the items added to the bar\n        during construction, and updates the progress bar *after* the\n        yielded block returns.\n        \"\"\"\n        # WARNING: the iterator interface for `ProgressBar` relies on\n        # this and only works because this is a simple generator which\n        # doesn't create or manage additional state. If this function\n        # changes, the impact should be evaluated both against\n        # `iter(bar)` and `next(bar)`. `next()` in particular may call\n        # `self.generator()` repeatedly, and this must remain safe in\n        # order for that interface to work.\n        if not self.entered:\n            raise RuntimeError(\"You need to use progress bars in a with block.\")\n\n        if not self._is_atty:\n            yield from self.iter\n        else:\n            for rv in self.iter:\n                self.current_item = rv\n\n                # This allows show_item_func to be updated before the\n                # item is processed. Only trigger at the beginning of\n                # the update interval.\n                if self._completed_intervals == 0:\n                    self.render_progress()\n\n                yield rv\n                self.update(1)\n\n            self.finish()\n            self.render_progress()\n\n\ndef pager(generator: cabc.Iterable[str], color: bool | None = None) -> None:\n    \"\"\"Decide what method to use for paging through text.\"\"\"\n    stdout = _default_text_stdout()\n\n    # There are no standard streams attached to write to. For example,\n    # pythonw on Windows.\n    if stdout is None:\n        stdout = StringIO()\n\n    if not isatty(sys.stdin) or not isatty(stdout):\n        return _nullpager(stdout, generator, color)\n\n    # Split and normalize the pager command into parts.\n    pager_cmd_parts = shlex.split(os.environ.get(\"PAGER\", \"\"), posix=False)\n    if pager_cmd_parts:\n        if WIN:\n            if _tempfilepager(generator, pager_cmd_parts, color):\n                return\n        elif _pipepager(generator, pager_cmd_parts, color):\n            return\n\n    if os.environ.get(\"TERM\") in (\"dumb\", \"emacs\"):\n        return _nullpager(stdout, generator, color)\n    if (WIN or sys.platform.startswith(\"os2\")) and _tempfilepager(\n        generator, [\"more\"], color\n    ):\n        return\n    if _pipepager(generator, [\"less\"], color):\n        return\n\n    import tempfile\n\n    fd, filename = tempfile.mkstemp()\n    os.close(fd)\n    try:\n        if _pipepager(generator, [\"more\"], color):\n            return\n        return _nullpager(stdout, generator, color)\n    finally:\n        os.unlink(filename)\n\n\ndef _pipepager(\n    generator: cabc.Iterable[str], cmd_parts: list[str], color: bool | None\n) -> bool:\n    \"\"\"Page through text by feeding it to another program. Invoking a\n    pager through this might support colors.\n\n    Returns `True` if the command was found, `False` otherwise and thus another\n    pager should be attempted.\n    \"\"\"\n    # Split the command into the invoked CLI and its parameters.\n    if not cmd_parts:\n        return False\n\n    import shutil\n\n    cmd = cmd_parts[0]\n    cmd_params = cmd_parts[1:]\n\n    cmd_filepath = shutil.which(cmd)\n    if not cmd_filepath:\n        return False\n\n    # Produces a normalized absolute path string.\n    # multi-call binaries such as busybox derive their identity from the symlink\n    # less -> busybox. resolve() causes them to misbehave. (eg. less becomes busybox)\n    cmd_path = Path(cmd_filepath).absolute()\n    cmd_name = cmd_path.name\n\n    import subprocess\n\n    # Make a local copy of the environment to not affect the global one.\n    env = dict(os.environ)\n\n    # If we're piping to less and the user hasn't decided on colors, we enable\n    # them by default we find the -R flag in the command line arguments.\n    if color is None and cmd_name == \"less\":\n        less_flags = f\"{os.environ.get('LESS', '')}{' '.join(cmd_params)}\"\n        if not less_flags:\n            env[\"LESS\"] = \"-R\"\n            color = True\n        elif \"r\" in less_flags or \"R\" in less_flags:\n            color = True\n\n    c = subprocess.Popen(\n        [str(cmd_path)] + cmd_params,\n        shell=False,\n        stdin=subprocess.PIPE,\n        env=env,\n        errors=\"replace\",\n        text=True,\n    )\n    assert c.stdin is not None\n    try:\n        for text in generator:\n            if not color:\n                text = strip_ansi(text)\n\n            c.stdin.write(text)\n    except BrokenPipeError:\n        # In case the pager exited unexpectedly, ignore the broken pipe error.\n        pass\n    except Exception as e:\n        # In case there is an exception we want to close the pager immediately\n        # and let the caller handle it.\n        # Otherwise the pager will keep running, and the user may not notice\n        # the error message, or worse yet it may leave the terminal in a broken state.\n        c.terminate()\n        raise e\n    finally:\n        # We must close stdin and wait for the pager to exit before we continue\n        try:\n            c.stdin.close()\n        # Close implies flush, so it might throw a BrokenPipeError if the pager\n        # process exited already.\n        except BrokenPipeError:\n            pass\n\n        # Less doesn't respect ^C, but catches it for its own UI purposes (aborting\n        # search or other commands inside less).\n        #\n        # That means when the user hits ^C, the parent process (click) terminates,\n        # but less is still alive, paging the output and messing up the terminal.\n        #\n        # If the user wants to make the pager exit on ^C, they should set\n        # `LESS='-K'`. It's not our decision to make.\n        while True:\n            try:\n                c.wait()\n            except KeyboardInterrupt:\n                pass\n            else:\n                break\n\n    return True\n\n\ndef _tempfilepager(\n    generator: cabc.Iterable[str], cmd_parts: list[str], color: bool | None\n) -> bool:\n    \"\"\"Page through text by invoking a program on a temporary file.\n\n    Returns `True` if the command was found, `False` otherwise and thus another\n    pager should be attempted.\n    \"\"\"\n    # Split the command into the invoked CLI and its parameters.\n    if not cmd_parts:\n        return False\n\n    import shutil\n\n    cmd = cmd_parts[0]\n\n    cmd_filepath = shutil.which(cmd)\n    if not cmd_filepath:\n        return False\n    # Produces a normalized absolute path string.\n    # multi-call binaries such as busybox derive their identity from the symlink\n    # less -> busybox. resolve() causes them to misbehave. (eg. less becomes busybox)\n    cmd_path = Path(cmd_filepath).absolute()\n\n    import subprocess\n    import tempfile\n\n    fd, filename = tempfile.mkstemp()\n    # TODO: This never terminates if the passed generator never terminates.\n    text = \"\".join(generator)\n    if not color:\n        text = strip_ansi(text)\n    encoding = get_best_encoding(sys.stdout)\n    with open_stream(filename, \"wb\")[0] as f:\n        f.write(text.encode(encoding))\n    try:\n        subprocess.call([str(cmd_path), filename])\n    except OSError:\n        # Command not found\n        pass\n    finally:\n        os.close(fd)\n        os.unlink(filename)\n\n    return True\n\n\ndef _nullpager(\n    stream: t.TextIO, generator: cabc.Iterable[str], color: bool | None\n) -> None:\n    \"\"\"Simply print unformatted text.  This is the ultimate fallback.\"\"\"\n    for text in generator:\n        if not color:\n            text = strip_ansi(text)\n        stream.write(text)\n\n\nclass Editor:\n    def __init__(\n        self,\n        editor: str | None = None,\n        env: cabc.Mapping[str, str] | None = None,\n        require_save: bool = True,\n        extension: str = \".txt\",\n    ) -> None:\n        self.editor = editor\n        self.env = env\n        self.require_save = require_save\n        self.extension = extension\n\n    def get_editor(self) -> str:\n        if self.editor is not None:\n            return self.editor\n        for key in \"VISUAL\", \"EDITOR\":\n            rv = os.environ.get(key)\n            if rv:\n                return rv\n        if WIN:\n            return \"notepad\"\n\n        from shutil import which\n\n        for editor in \"sensible-editor\", \"vim\", \"nano\":\n            if which(editor) is not None:\n                return editor\n        return \"vi\"\n\n    def edit_files(self, filenames: cabc.Iterable[str]) -> None:\n        import subprocess\n\n        editor = self.get_editor()\n        environ: dict[str, str] | None = None\n\n        if self.env:\n            environ = os.environ.copy()\n            environ.update(self.env)\n\n        exc_filename = \" \".join(f'\"{filename}\"' for filename in filenames)\n\n        try:\n            c = subprocess.Popen(\n                args=f\"{editor} {exc_filename}\", env=environ, shell=True\n            )\n            exit_code = c.wait()\n            if exit_code != 0:\n                raise ClickException(\n                    _(\"{editor}: Editing failed\").format(editor=editor)\n                )\n        except OSError as e:\n            raise ClickException(\n                _(\"{editor}: Editing failed: {e}\").format(editor=editor, e=e)\n            ) from e\n\n    @t.overload\n    def edit(self, text: bytes | bytearray) -> bytes | None: ...\n\n    # We cannot know whether or not the type expected is str or bytes when None\n    # is passed, so str is returned as that was what was done before.\n    @t.overload\n    def edit(self, text: str | None) -> str | None: ...\n\n    def edit(self, text: str | bytes | bytearray | None) -> str | bytes | None:\n        import tempfile\n\n        if text is None:\n            data: bytes | bytearray = b\"\"\n        elif isinstance(text, (bytes, bytearray)):\n            data = text\n        else:\n            if text and not text.endswith(\"\\n\"):\n                text += \"\\n\"\n\n            if WIN:\n                data = text.replace(\"\\n\", \"\\r\\n\").encode(\"utf-8-sig\")\n            else:\n                data = text.encode(\"utf-8\")\n\n        fd, name = tempfile.mkstemp(prefix=\"editor-\", suffix=self.extension)\n        f: t.BinaryIO\n\n        try:\n            with os.fdopen(fd, \"wb\") as f:\n                f.write(data)\n\n            # If the filesystem resolution is 1 second, like Mac OS\n            # 10.12 Extended, or 2 seconds, like FAT32, and the editor\n            # closes very fast, require_save can fail. Set the modified\n            # time to be 2 seconds in the past to work around this.\n            os.utime(name, (os.path.getatime(name), os.path.getmtime(name) - 2))\n            # Depending on the resolution, the exact value might not be\n            # recorded, so get the new recorded value.\n            timestamp = os.path.getmtime(name)\n\n            self.edit_files((name,))\n\n            if self.require_save and os.path.getmtime(name) == timestamp:\n                return None\n\n            with open(name, \"rb\") as f:\n                rv = f.read()\n\n            if isinstance(text, (bytes, bytearray)):\n                return rv\n\n            return rv.decode(\"utf-8-sig\").replace(\"\\r\\n\", \"\\n\")\n        finally:\n            os.unlink(name)\n\n\ndef open_url(url: str, wait: bool = False, locate: bool = False) -> int:\n    import subprocess\n\n    def _unquote_file(url: str) -> str:\n        from urllib.parse import unquote\n\n        if url.startswith(\"file://\"):\n            url = unquote(url[7:])\n\n        return url\n\n    if sys.platform == \"darwin\":\n        args = [\"open\"]\n        if wait:\n            args.append(\"-W\")\n        if locate:\n            args.append(\"-R\")\n        args.append(_unquote_file(url))\n        null = open(\"/dev/null\", \"w\")\n        try:\n            return subprocess.Popen(args, stderr=null).wait()\n        finally:\n            null.close()\n    elif WIN:\n        if locate:\n            url = _unquote_file(url)\n            args = [\"explorer\", f\"/select,{url}\"]\n        else:\n            args = [\"start\"]\n            if wait:\n                args.append(\"/WAIT\")\n            args.append(\"\")\n            args.append(url)\n        try:\n            return subprocess.call(args)\n        except OSError:\n            # Command not found\n            return 127\n    elif CYGWIN:\n        if locate:\n            url = _unquote_file(url)\n            args = [\"cygstart\", os.path.dirname(url)]\n        else:\n            args = [\"cygstart\"]\n            if wait:\n                args.append(\"-w\")\n            args.append(url)\n        try:\n            return subprocess.call(args)\n        except OSError:\n            # Command not found\n            return 127\n\n    try:\n        if locate:\n            url = os.path.dirname(_unquote_file(url)) or \".\"\n        else:\n            url = _unquote_file(url)\n        c = subprocess.Popen([\"xdg-open\", url])\n        if wait:\n            return c.wait()\n        return 0\n    except OSError:\n        if url.startswith((\"http://\", \"https://\")) and not locate and not wait:\n            import webbrowser\n\n            webbrowser.open(url)\n            return 0\n        return 1\n\n\ndef _translate_ch_to_exc(ch: str) -> None:\n    if ch == \"\\x03\":\n        raise KeyboardInterrupt()\n\n    if ch == \"\\x04\" and not WIN:  # Unix-like, Ctrl+D\n        raise EOFError()\n\n    if ch == \"\\x1a\" and WIN:  # Windows, Ctrl+Z\n        raise EOFError()\n\n    return None\n\n\nif sys.platform == \"win32\":\n    import msvcrt\n\n    @contextlib.contextmanager\n    def raw_terminal() -> cabc.Iterator[int]:\n        yield -1\n\n    def getchar(echo: bool) -> str:\n        # The function `getch` will return a bytes object corresponding to\n        # the pressed character. Since Windows 10 build 1803, it will also\n        # return \\x00 when called a second time after pressing a regular key.\n        #\n        # `getwch` does not share this probably-bugged behavior. Moreover, it\n        # returns a Unicode object by default, which is what we want.\n        #\n        # Either of these functions will return \\x00 or \\xe0 to indicate\n        # a special key, and you need to call the same function again to get\n        # the \"rest\" of the code. The fun part is that \\u00e0 is\n        # \"latin small letter a with grave\", so if you type that on a French\n        # keyboard, you _also_ get a \\xe0.\n        # E.g., consider the Up arrow. This returns \\xe0 and then \\x48. The\n        # resulting Unicode string reads as \"a with grave\" + \"capital H\".\n        # This is indistinguishable from when the user actually types\n        # \"a with grave\" and then \"capital H\".\n        #\n        # When \\xe0 is returned, we assume it's part of a special-key sequence\n        # and call `getwch` again, but that means that when the user types\n        # the \\u00e0 character, `getchar` doesn't return until a second\n        # character is typed.\n        # The alternative is returning immediately, but that would mess up\n        # cross-platform handling of arrow keys and others that start with\n        # \\xe0. Another option is using `getch`, but then we can't reliably\n        # read non-ASCII characters, because return values of `getch` are\n        # limited to the current 8-bit codepage.\n        #\n        # Anyway, Click doesn't claim to do this Right(tm), and using `getwch`\n        # is doing the right thing in more situations than with `getch`.\n\n        if echo:\n            func = t.cast(t.Callable[[], str], msvcrt.getwche)\n        else:\n            func = t.cast(t.Callable[[], str], msvcrt.getwch)\n\n        rv = func()\n\n        if rv in (\"\\x00\", \"\\xe0\"):\n            # \\x00 and \\xe0 are control characters that indicate special key,\n            # see above.\n            rv += func()\n\n        _translate_ch_to_exc(rv)\n        return rv\n\nelse:\n    import termios\n    import tty\n\n    @contextlib.contextmanager\n    def raw_terminal() -> cabc.Iterator[int]:\n        f: t.TextIO | None\n        fd: int\n\n        if not isatty(sys.stdin):\n            f = open(\"/dev/tty\")\n            fd = f.fileno()\n        else:\n            fd = sys.stdin.fileno()\n            f = None\n\n        try:\n            old_settings = termios.tcgetattr(fd)\n\n            try:\n                tty.setraw(fd)\n                yield fd\n            finally:\n                termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)\n                sys.stdout.flush()\n\n                if f is not None:\n                    f.close()\n        except termios.error:\n            pass\n\n    def getchar(echo: bool) -> str:\n        with raw_terminal() as fd:\n            ch = os.read(fd, 32).decode(get_best_encoding(sys.stdin), \"replace\")\n\n            if echo and isatty(sys.stdout):\n                sys.stdout.write(ch)\n\n            _translate_ch_to_exc(ch)\n            return ch\n"
  },
  {
    "path": "src/click/_textwrap.py",
    "content": "from __future__ import annotations\n\nimport collections.abc as cabc\nimport textwrap\nfrom contextlib import contextmanager\n\n\nclass TextWrapper(textwrap.TextWrapper):\n    def _handle_long_word(\n        self,\n        reversed_chunks: list[str],\n        cur_line: list[str],\n        cur_len: int,\n        width: int,\n    ) -> None:\n        space_left = max(width - cur_len, 1)\n\n        if self.break_long_words:\n            last = reversed_chunks[-1]\n            cut = last[:space_left]\n            res = last[space_left:]\n            cur_line.append(cut)\n            reversed_chunks[-1] = res\n        elif not cur_line:\n            cur_line.append(reversed_chunks.pop())\n\n    @contextmanager\n    def extra_indent(self, indent: str) -> cabc.Iterator[None]:\n        old_initial_indent = self.initial_indent\n        old_subsequent_indent = self.subsequent_indent\n        self.initial_indent += indent\n        self.subsequent_indent += indent\n\n        try:\n            yield\n        finally:\n            self.initial_indent = old_initial_indent\n            self.subsequent_indent = old_subsequent_indent\n\n    def indent_only(self, text: str) -> str:\n        rv = []\n\n        for idx, line in enumerate(text.splitlines()):\n            indent = self.initial_indent\n\n            if idx > 0:\n                indent = self.subsequent_indent\n\n            rv.append(f\"{indent}{line}\")\n\n        return \"\\n\".join(rv)\n"
  },
  {
    "path": "src/click/_utils.py",
    "content": "from __future__ import annotations\n\nimport enum\nimport typing as t\n\n\nclass Sentinel(enum.Enum):\n    \"\"\"Enum used to define sentinel values.\n\n    .. seealso::\n\n        `PEP 661 - Sentinel Values <https://peps.python.org/pep-0661/>`_.\n    \"\"\"\n\n    UNSET = object()\n    FLAG_NEEDS_VALUE = object()\n\n    def __repr__(self) -> str:\n        return f\"{self.__class__.__name__}.{self.name}\"\n\n\nUNSET = Sentinel.UNSET\n\"\"\"Sentinel used to indicate that a value is not set.\"\"\"\n\nFLAG_NEEDS_VALUE = Sentinel.FLAG_NEEDS_VALUE\n\"\"\"Sentinel used to indicate an option was passed as a flag without a\nvalue but is not a flag option.\n\n``Option.consume_value`` uses this to prompt or use the ``flag_value``.\n\"\"\"\n\nT_UNSET = t.Literal[UNSET]  # type: ignore[valid-type]\n\"\"\"Type hint for the :data:`UNSET` sentinel value.\"\"\"\n\nT_FLAG_NEEDS_VALUE = t.Literal[FLAG_NEEDS_VALUE]  # type: ignore[valid-type]\n\"\"\"Type hint for the :data:`FLAG_NEEDS_VALUE` sentinel value.\"\"\"\n"
  },
  {
    "path": "src/click/_winconsole.py",
    "content": "# This module is based on the excellent work by Adam Bartoš who\n# provided a lot of what went into the implementation here in\n# the discussion to issue1602 in the Python bug tracker.\n#\n# There are some general differences in regards to how this works\n# compared to the original patches as we do not need to patch\n# the entire interpreter but just work in our little world of\n# echo and prompt.\nfrom __future__ import annotations\n\nimport collections.abc as cabc\nimport io\nimport sys\nimport time\nimport typing as t\nfrom ctypes import Array\nfrom ctypes import byref\nfrom ctypes import c_char\nfrom ctypes import c_char_p\nfrom ctypes import c_int\nfrom ctypes import c_ssize_t\nfrom ctypes import c_ulong\nfrom ctypes import c_void_p\nfrom ctypes import POINTER\nfrom ctypes import py_object\nfrom ctypes import Structure\nfrom ctypes.wintypes import DWORD\nfrom ctypes.wintypes import HANDLE\nfrom ctypes.wintypes import LPCWSTR\nfrom ctypes.wintypes import LPWSTR\n\nfrom ._compat import _NonClosingTextIOWrapper\n\nassert sys.platform == \"win32\"\nimport msvcrt  # noqa: E402\nfrom ctypes import windll  # noqa: E402\nfrom ctypes import WINFUNCTYPE  # noqa: E402\n\nc_ssize_p = POINTER(c_ssize_t)\n\nkernel32 = windll.kernel32\nGetStdHandle = kernel32.GetStdHandle\nReadConsoleW = kernel32.ReadConsoleW\nWriteConsoleW = kernel32.WriteConsoleW\nGetConsoleMode = kernel32.GetConsoleMode\nGetLastError = kernel32.GetLastError\nGetCommandLineW = WINFUNCTYPE(LPWSTR)((\"GetCommandLineW\", windll.kernel32))\nCommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))(\n    (\"CommandLineToArgvW\", windll.shell32)\n)\nLocalFree = WINFUNCTYPE(c_void_p, c_void_p)((\"LocalFree\", windll.kernel32))\n\nSTDIN_HANDLE = GetStdHandle(-10)\nSTDOUT_HANDLE = GetStdHandle(-11)\nSTDERR_HANDLE = GetStdHandle(-12)\n\nPyBUF_SIMPLE = 0\nPyBUF_WRITABLE = 1\n\nERROR_SUCCESS = 0\nERROR_NOT_ENOUGH_MEMORY = 8\nERROR_OPERATION_ABORTED = 995\n\nSTDIN_FILENO = 0\nSTDOUT_FILENO = 1\nSTDERR_FILENO = 2\n\nEOF = b\"\\x1a\"\nMAX_BYTES_WRITTEN = 32767\n\nif t.TYPE_CHECKING:\n    try:\n        # Using `typing_extensions.Buffer` instead of `collections.abc`\n        # on Windows for some reason does not have `Sized` implemented.\n        from collections.abc import Buffer  # type: ignore\n    except ImportError:\n        from typing_extensions import Buffer\n\ntry:\n    from ctypes import pythonapi\nexcept ImportError:\n    # On PyPy we cannot get buffers so our ability to operate here is\n    # severely limited.\n    get_buffer = None\nelse:\n\n    class Py_buffer(Structure):\n        _fields_ = [  # noqa: RUF012\n            (\"buf\", c_void_p),\n            (\"obj\", py_object),\n            (\"len\", c_ssize_t),\n            (\"itemsize\", c_ssize_t),\n            (\"readonly\", c_int),\n            (\"ndim\", c_int),\n            (\"format\", c_char_p),\n            (\"shape\", c_ssize_p),\n            (\"strides\", c_ssize_p),\n            (\"suboffsets\", c_ssize_p),\n            (\"internal\", c_void_p),\n        ]\n\n    PyObject_GetBuffer = pythonapi.PyObject_GetBuffer\n    PyBuffer_Release = pythonapi.PyBuffer_Release\n\n    def get_buffer(obj: Buffer, writable: bool = False) -> Array[c_char]:\n        buf = Py_buffer()\n        flags: int = PyBUF_WRITABLE if writable else PyBUF_SIMPLE\n        PyObject_GetBuffer(py_object(obj), byref(buf), flags)\n\n        try:\n            buffer_type = c_char * buf.len\n            out: Array[c_char] = buffer_type.from_address(buf.buf)\n            return out\n        finally:\n            PyBuffer_Release(byref(buf))\n\n\nclass _WindowsConsoleRawIOBase(io.RawIOBase):\n    def __init__(self, handle: int | None) -> None:\n        self.handle = handle\n\n    def isatty(self) -> t.Literal[True]:\n        super().isatty()\n        return True\n\n\nclass _WindowsConsoleReader(_WindowsConsoleRawIOBase):\n    def readable(self) -> t.Literal[True]:\n        return True\n\n    def readinto(self, b: Buffer) -> int:\n        bytes_to_be_read = len(b)\n        if not bytes_to_be_read:\n            return 0\n        elif bytes_to_be_read % 2:\n            raise ValueError(\n                \"cannot read odd number of bytes from UTF-16-LE encoded console\"\n            )\n\n        buffer = get_buffer(b, writable=True)\n        code_units_to_be_read = bytes_to_be_read // 2\n        code_units_read = c_ulong()\n\n        rv = ReadConsoleW(\n            HANDLE(self.handle),\n            buffer,\n            code_units_to_be_read,\n            byref(code_units_read),\n            None,\n        )\n        if GetLastError() == ERROR_OPERATION_ABORTED:\n            # wait for KeyboardInterrupt\n            time.sleep(0.1)\n        if not rv:\n            raise OSError(f\"Windows error: {GetLastError()}\")\n\n        if buffer[0] == EOF:\n            return 0\n        return 2 * code_units_read.value\n\n\nclass _WindowsConsoleWriter(_WindowsConsoleRawIOBase):\n    def writable(self) -> t.Literal[True]:\n        return True\n\n    @staticmethod\n    def _get_error_message(errno: int) -> str:\n        if errno == ERROR_SUCCESS:\n            return \"ERROR_SUCCESS\"\n        elif errno == ERROR_NOT_ENOUGH_MEMORY:\n            return \"ERROR_NOT_ENOUGH_MEMORY\"\n        return f\"Windows error {errno}\"\n\n    def write(self, b: Buffer) -> int:\n        bytes_to_be_written = len(b)\n        buf = get_buffer(b)\n        code_units_to_be_written = min(bytes_to_be_written, MAX_BYTES_WRITTEN) // 2\n        code_units_written = c_ulong()\n\n        WriteConsoleW(\n            HANDLE(self.handle),\n            buf,\n            code_units_to_be_written,\n            byref(code_units_written),\n            None,\n        )\n        bytes_written = 2 * code_units_written.value\n\n        if bytes_written == 0 and bytes_to_be_written > 0:\n            raise OSError(self._get_error_message(GetLastError()))\n        return bytes_written\n\n\nclass ConsoleStream:\n    def __init__(self, text_stream: t.TextIO, byte_stream: t.BinaryIO) -> None:\n        self._text_stream = text_stream\n        self.buffer = byte_stream\n\n    @property\n    def name(self) -> str:\n        return self.buffer.name\n\n    def write(self, x: t.AnyStr) -> int:\n        if isinstance(x, str):\n            return self._text_stream.write(x)\n        try:\n            self.flush()\n        except Exception:\n            pass\n        return self.buffer.write(x)\n\n    def writelines(self, lines: cabc.Iterable[t.AnyStr]) -> None:\n        for line in lines:\n            self.write(line)\n\n    def __getattr__(self, name: str) -> t.Any:\n        return getattr(self._text_stream, name)\n\n    def isatty(self) -> bool:\n        return self.buffer.isatty()\n\n    def __repr__(self) -> str:\n        return f\"<ConsoleStream name={self.name!r} encoding={self.encoding!r}>\"\n\n\ndef _get_text_stdin(buffer_stream: t.BinaryIO) -> t.TextIO:\n    text_stream = _NonClosingTextIOWrapper(\n        io.BufferedReader(_WindowsConsoleReader(STDIN_HANDLE)),\n        \"utf-16-le\",\n        \"strict\",\n        line_buffering=True,\n    )\n    return t.cast(t.TextIO, ConsoleStream(text_stream, buffer_stream))\n\n\ndef _get_text_stdout(buffer_stream: t.BinaryIO) -> t.TextIO:\n    text_stream = _NonClosingTextIOWrapper(\n        io.BufferedWriter(_WindowsConsoleWriter(STDOUT_HANDLE)),\n        \"utf-16-le\",\n        \"strict\",\n        line_buffering=True,\n    )\n    return t.cast(t.TextIO, ConsoleStream(text_stream, buffer_stream))\n\n\ndef _get_text_stderr(buffer_stream: t.BinaryIO) -> t.TextIO:\n    text_stream = _NonClosingTextIOWrapper(\n        io.BufferedWriter(_WindowsConsoleWriter(STDERR_HANDLE)),\n        \"utf-16-le\",\n        \"strict\",\n        line_buffering=True,\n    )\n    return t.cast(t.TextIO, ConsoleStream(text_stream, buffer_stream))\n\n\n_stream_factories: cabc.Mapping[int, t.Callable[[t.BinaryIO], t.TextIO]] = {\n    0: _get_text_stdin,\n    1: _get_text_stdout,\n    2: _get_text_stderr,\n}\n\n\ndef _is_console(f: t.TextIO) -> bool:\n    if not hasattr(f, \"fileno\"):\n        return False\n\n    try:\n        fileno = f.fileno()\n    except (OSError, io.UnsupportedOperation):\n        return False\n\n    handle = msvcrt.get_osfhandle(fileno)\n    return bool(GetConsoleMode(handle, byref(DWORD())))\n\n\ndef _get_windows_console_stream(\n    f: t.TextIO, encoding: str | None, errors: str | None\n) -> t.TextIO | None:\n    if (\n        get_buffer is None\n        or encoding not in {\"utf-16-le\", None}\n        or errors not in {\"strict\", None}\n        or not _is_console(f)\n    ):\n        return None\n\n    func = _stream_factories.get(f.fileno())\n    if func is None:\n        return None\n\n    b = getattr(f, \"buffer\", None)\n\n    if b is None:\n        return None\n\n    return func(b)\n"
  },
  {
    "path": "src/click/core.py",
    "content": "from __future__ import annotations\n\nimport collections.abc as cabc\nimport enum\nimport errno\nimport inspect\nimport os\nimport sys\nimport typing as t\nfrom collections import abc\nfrom collections import Counter\nfrom contextlib import AbstractContextManager\nfrom contextlib import contextmanager\nfrom contextlib import ExitStack\nfrom functools import update_wrapper\nfrom gettext import gettext as _\nfrom gettext import ngettext\nfrom itertools import repeat\nfrom types import TracebackType\n\nfrom . import types\nfrom ._utils import FLAG_NEEDS_VALUE\nfrom ._utils import UNSET\nfrom .exceptions import Abort\nfrom .exceptions import BadParameter\nfrom .exceptions import ClickException\nfrom .exceptions import Exit\nfrom .exceptions import MissingParameter\nfrom .exceptions import NoArgsIsHelpError\nfrom .exceptions import UsageError\nfrom .formatting import HelpFormatter\nfrom .formatting import join_options\nfrom .globals import pop_context\nfrom .globals import push_context\nfrom .parser import _OptionParser\nfrom .parser import _split_opt\nfrom .termui import confirm\nfrom .termui import prompt\nfrom .termui import style\nfrom .utils import _detect_program_name\nfrom .utils import _expand_args\nfrom .utils import echo\nfrom .utils import make_default_short_help\nfrom .utils import make_str\nfrom .utils import PacifyFlushWrapper\n\nif t.TYPE_CHECKING:\n    from .shell_completion import CompletionItem\n\nF = t.TypeVar(\"F\", bound=\"t.Callable[..., t.Any]\")\nV = t.TypeVar(\"V\")\n\n\ndef _complete_visible_commands(\n    ctx: Context, incomplete: str\n) -> cabc.Iterator[tuple[str, Command]]:\n    \"\"\"List all the subcommands of a group that start with the\n    incomplete value and aren't hidden.\n\n    :param ctx: Invocation context for the group.\n    :param incomplete: Value being completed. May be empty.\n    \"\"\"\n    multi = t.cast(Group, ctx.command)\n\n    for name in multi.list_commands(ctx):\n        if name.startswith(incomplete):\n            command = multi.get_command(ctx, name)\n\n            if command is not None and not command.hidden:\n                yield name, command\n\n\ndef _check_nested_chain(\n    base_command: Group, cmd_name: str, cmd: Command, register: bool = False\n) -> None:\n    if not base_command.chain or not isinstance(cmd, Group):\n        return\n\n    if register:\n        message = (\n            f\"It is not possible to add the group {cmd_name!r} to another\"\n            f\" group {base_command.name!r} that is in chain mode.\"\n        )\n    else:\n        message = (\n            f\"Found the group {cmd_name!r} as subcommand to another group \"\n            f\" {base_command.name!r} that is in chain mode. This is not supported.\"\n        )\n\n    raise RuntimeError(message)\n\n\ndef batch(iterable: cabc.Iterable[V], batch_size: int) -> list[tuple[V, ...]]:\n    return list(zip(*repeat(iter(iterable), batch_size), strict=False))\n\n\n@contextmanager\ndef augment_usage_errors(\n    ctx: Context, param: Parameter | None = None\n) -> cabc.Iterator[None]:\n    \"\"\"Context manager that attaches extra information to exceptions.\"\"\"\n    try:\n        yield\n    except BadParameter as e:\n        if e.ctx is None:\n            e.ctx = ctx\n        if param is not None and e.param is None:\n            e.param = param\n        raise\n    except UsageError as e:\n        if e.ctx is None:\n            e.ctx = ctx\n        raise\n\n\ndef iter_params_for_processing(\n    invocation_order: cabc.Sequence[Parameter],\n    declaration_order: cabc.Sequence[Parameter],\n) -> list[Parameter]:\n    \"\"\"Returns all declared parameters in the order they should be processed.\n\n    The declared parameters are re-shuffled depending on the order in which\n    they were invoked, as well as the eagerness of each parameters.\n\n    The invocation order takes precedence over the declaration order. I.e. the\n    order in which the user provided them to the CLI is respected.\n\n    This behavior and its effect on callback evaluation is detailed at:\n    https://click.palletsprojects.com/en/stable/advanced/#callback-evaluation-order\n    \"\"\"\n\n    def sort_key(item: Parameter) -> tuple[bool, float]:\n        try:\n            idx: float = invocation_order.index(item)\n        except ValueError:\n            idx = float(\"inf\")\n\n        return not item.is_eager, idx\n\n    return sorted(declaration_order, key=sort_key)\n\n\nclass ParameterSource(enum.Enum):\n    \"\"\"This is an :class:`~enum.Enum` that indicates the source of a\n    parameter's value.\n\n    Use :meth:`click.Context.get_parameter_source` to get the\n    source for a parameter by name.\n\n    .. versionchanged:: 8.0\n        Use :class:`~enum.Enum` and drop the ``validate`` method.\n\n    .. versionchanged:: 8.0\n        Added the ``PROMPT`` value.\n    \"\"\"\n\n    COMMANDLINE = enum.auto()\n    \"\"\"The value was provided by the command line args.\"\"\"\n    ENVIRONMENT = enum.auto()\n    \"\"\"The value was provided with an environment variable.\"\"\"\n    DEFAULT = enum.auto()\n    \"\"\"Used the default specified by the parameter.\"\"\"\n    DEFAULT_MAP = enum.auto()\n    \"\"\"Used a default provided by :attr:`Context.default_map`.\"\"\"\n    PROMPT = enum.auto()\n    \"\"\"Used a prompt to confirm a default or provide a value.\"\"\"\n\n\nclass Context:\n    \"\"\"The context is a special internal object that holds state relevant\n    for the script execution at every single level.  It's normally invisible\n    to commands unless they opt-in to getting access to it.\n\n    The context is useful as it can pass internal objects around and can\n    control special execution features such as reading data from\n    environment variables.\n\n    A context can be used as context manager in which case it will call\n    :meth:`close` on teardown.\n\n    :param command: the command class for this context.\n    :param parent: the parent context.\n    :param info_name: the info name for this invocation.  Generally this\n                      is the most descriptive name for the script or\n                      command.  For the toplevel script it is usually\n                      the name of the script, for commands below it it's\n                      the name of the script.\n    :param obj: an arbitrary object of user data.\n    :param auto_envvar_prefix: the prefix to use for automatic environment\n                               variables.  If this is `None` then reading\n                               from environment variables is disabled.  This\n                               does not affect manually set environment\n                               variables which are always read.\n    :param default_map: a dictionary (like object) with default values\n                        for parameters.\n    :param terminal_width: the width of the terminal.  The default is\n                           inherit from parent context.  If no context\n                           defines the terminal width then auto\n                           detection will be applied.\n    :param max_content_width: the maximum width for content rendered by\n                              Click (this currently only affects help\n                              pages).  This defaults to 80 characters if\n                              not overridden.  In other words: even if the\n                              terminal is larger than that, Click will not\n                              format things wider than 80 characters by\n                              default.  In addition to that, formatters might\n                              add some safety mapping on the right.\n    :param resilient_parsing: if this flag is enabled then Click will\n                              parse without any interactivity or callback\n                              invocation.  Default values will also be\n                              ignored.  This is useful for implementing\n                              things such as completion support.\n    :param allow_extra_args: if this is set to `True` then extra arguments\n                             at the end will not raise an error and will be\n                             kept on the context.  The default is to inherit\n                             from the command.\n    :param allow_interspersed_args: if this is set to `False` then options\n                                    and arguments cannot be mixed.  The\n                                    default is to inherit from the command.\n    :param ignore_unknown_options: instructs click to ignore options it does\n                                   not know and keeps them for later\n                                   processing.\n    :param help_option_names: optionally a list of strings that define how\n                              the default help parameter is named.  The\n                              default is ``['--help']``.\n    :param token_normalize_func: an optional function that is used to\n                                 normalize tokens (options, choices,\n                                 etc.).  This for instance can be used to\n                                 implement case insensitive behavior.\n    :param color: controls if the terminal supports ANSI colors or not.  The\n                  default is autodetection.  This is only needed if ANSI\n                  codes are used in texts that Click prints which is by\n                  default not the case.  This for instance would affect\n                  help output.\n    :param show_default: Show the default value for commands. If this\n        value is not set, it defaults to the value from the parent\n        context. ``Command.show_default`` overrides this default for the\n        specific command.\n\n    .. versionchanged:: 8.2\n        The ``protected_args`` attribute is deprecated and will be removed in\n        Click 9.0. ``args`` will contain remaining unparsed tokens.\n\n    .. versionchanged:: 8.1\n        The ``show_default`` parameter is overridden by\n        ``Command.show_default``, instead of the other way around.\n\n    .. versionchanged:: 8.0\n        The ``show_default`` parameter defaults to the value from the\n        parent context.\n\n    .. versionchanged:: 7.1\n       Added the ``show_default`` parameter.\n\n    .. versionchanged:: 4.0\n        Added the ``color``, ``ignore_unknown_options``, and\n        ``max_content_width`` parameters.\n\n    .. versionchanged:: 3.0\n        Added the ``allow_extra_args`` and ``allow_interspersed_args``\n        parameters.\n\n    .. versionchanged:: 2.0\n        Added the ``resilient_parsing``, ``help_option_names``, and\n        ``token_normalize_func`` parameters.\n    \"\"\"\n\n    #: The formatter class to create with :meth:`make_formatter`.\n    #:\n    #: .. versionadded:: 8.0\n    formatter_class: type[HelpFormatter] = HelpFormatter\n\n    def __init__(\n        self,\n        command: Command,\n        parent: Context | None = None,\n        info_name: str | None = None,\n        obj: t.Any | None = None,\n        auto_envvar_prefix: str | None = None,\n        default_map: cabc.MutableMapping[str, t.Any] | None = None,\n        terminal_width: int | None = None,\n        max_content_width: int | None = None,\n        resilient_parsing: bool = False,\n        allow_extra_args: bool | None = None,\n        allow_interspersed_args: bool | None = None,\n        ignore_unknown_options: bool | None = None,\n        help_option_names: list[str] | None = None,\n        token_normalize_func: t.Callable[[str], str] | None = None,\n        color: bool | None = None,\n        show_default: bool | None = None,\n    ) -> None:\n        #: the parent context or `None` if none exists.\n        self.parent = parent\n        #: the :class:`Command` for this context.\n        self.command = command\n        #: the descriptive information name\n        self.info_name = info_name\n        #: Map of parameter names to their parsed values. Parameters\n        #: with ``expose_value=False`` are not stored.\n        self.params: dict[str, t.Any] = {}\n        #: the leftover arguments.\n        self.args: list[str] = []\n        #: protected arguments.  These are arguments that are prepended\n        #: to `args` when certain parsing scenarios are encountered but\n        #: must be never propagated to another arguments.  This is used\n        #: to implement nested parsing.\n        self._protected_args: list[str] = []\n        #: the collected prefixes of the command's options.\n        self._opt_prefixes: set[str] = set(parent._opt_prefixes) if parent else set()\n\n        if obj is None and parent is not None:\n            obj = parent.obj\n\n        #: the user object stored.\n        self.obj: t.Any = obj\n        self._meta: dict[str, t.Any] = getattr(parent, \"meta\", {})\n\n        #: A dictionary (-like object) with defaults for parameters.\n        if (\n            default_map is None\n            and info_name is not None\n            and parent is not None\n            and parent.default_map is not None\n        ):\n            default_map = parent.default_map.get(info_name)\n\n        self.default_map: cabc.MutableMapping[str, t.Any] | None = default_map\n\n        #: This flag indicates if a subcommand is going to be executed. A\n        #: group callback can use this information to figure out if it's\n        #: being executed directly or because the execution flow passes\n        #: onwards to a subcommand. By default it's None, but it can be\n        #: the name of the subcommand to execute.\n        #:\n        #: If chaining is enabled this will be set to ``'*'`` in case\n        #: any commands are executed.  It is however not possible to\n        #: figure out which ones.  If you require this knowledge you\n        #: should use a :func:`result_callback`.\n        self.invoked_subcommand: str | None = None\n\n        if terminal_width is None and parent is not None:\n            terminal_width = parent.terminal_width\n\n        #: The width of the terminal (None is autodetection).\n        self.terminal_width: int | None = terminal_width\n\n        if max_content_width is None and parent is not None:\n            max_content_width = parent.max_content_width\n\n        #: The maximum width of formatted content (None implies a sensible\n        #: default which is 80 for most things).\n        self.max_content_width: int | None = max_content_width\n\n        if allow_extra_args is None:\n            allow_extra_args = command.allow_extra_args\n\n        #: Indicates if the context allows extra args or if it should\n        #: fail on parsing.\n        #:\n        #: .. versionadded:: 3.0\n        self.allow_extra_args = allow_extra_args\n\n        if allow_interspersed_args is None:\n            allow_interspersed_args = command.allow_interspersed_args\n\n        #: Indicates if the context allows mixing of arguments and\n        #: options or not.\n        #:\n        #: .. versionadded:: 3.0\n        self.allow_interspersed_args: bool = allow_interspersed_args\n\n        if ignore_unknown_options is None:\n            ignore_unknown_options = command.ignore_unknown_options\n\n        #: Instructs click to ignore options that a command does not\n        #: understand and will store it on the context for later\n        #: processing.  This is primarily useful for situations where you\n        #: want to call into external programs.  Generally this pattern is\n        #: strongly discouraged because it's not possibly to losslessly\n        #: forward all arguments.\n        #:\n        #: .. versionadded:: 4.0\n        self.ignore_unknown_options: bool = ignore_unknown_options\n\n        if help_option_names is None:\n            if parent is not None:\n                help_option_names = parent.help_option_names\n            else:\n                help_option_names = [\"--help\"]\n\n        #: The names for the help options.\n        self.help_option_names: list[str] = help_option_names\n\n        if token_normalize_func is None and parent is not None:\n            token_normalize_func = parent.token_normalize_func\n\n        #: An optional normalization function for tokens.  This is\n        #: options, choices, commands etc.\n        self.token_normalize_func: t.Callable[[str], str] | None = token_normalize_func\n\n        #: Indicates if resilient parsing is enabled.  In that case Click\n        #: will do its best to not cause any failures and default values\n        #: will be ignored. Useful for completion.\n        self.resilient_parsing: bool = resilient_parsing\n\n        # If there is no envvar prefix yet, but the parent has one and\n        # the command on this level has a name, we can expand the envvar\n        # prefix automatically.\n        if auto_envvar_prefix is None:\n            if (\n                parent is not None\n                and parent.auto_envvar_prefix is not None\n                and self.info_name is not None\n            ):\n                auto_envvar_prefix = (\n                    f\"{parent.auto_envvar_prefix}_{self.info_name.upper()}\"\n                )\n        else:\n            auto_envvar_prefix = auto_envvar_prefix.upper()\n\n        if auto_envvar_prefix is not None:\n            auto_envvar_prefix = auto_envvar_prefix.replace(\"-\", \"_\")\n\n        self.auto_envvar_prefix: str | None = auto_envvar_prefix\n\n        if color is None and parent is not None:\n            color = parent.color\n\n        #: Controls if styling output is wanted or not.\n        self.color: bool | None = color\n\n        if show_default is None and parent is not None:\n            show_default = parent.show_default\n\n        #: Show option default values when formatting help text.\n        self.show_default: bool | None = show_default\n\n        self._close_callbacks: list[t.Callable[[], t.Any]] = []\n        self._depth = 0\n        self._parameter_source: dict[str, ParameterSource] = {}\n        self._exit_stack = ExitStack()\n\n    @property\n    def protected_args(self) -> list[str]:\n        import warnings\n\n        warnings.warn(\n            \"'protected_args' is deprecated and will be removed in Click 9.0.\"\n            \" 'args' will contain remaining unparsed tokens.\",\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return self._protected_args\n\n    def to_info_dict(self) -> dict[str, t.Any]:\n        \"\"\"Gather information that could be useful for a tool generating\n        user-facing documentation. This traverses the entire CLI\n        structure.\n\n        .. code-block:: python\n\n            with Context(cli) as ctx:\n                info = ctx.to_info_dict()\n\n        .. versionadded:: 8.0\n        \"\"\"\n        return {\n            \"command\": self.command.to_info_dict(self),\n            \"info_name\": self.info_name,\n            \"allow_extra_args\": self.allow_extra_args,\n            \"allow_interspersed_args\": self.allow_interspersed_args,\n            \"ignore_unknown_options\": self.ignore_unknown_options,\n            \"auto_envvar_prefix\": self.auto_envvar_prefix,\n        }\n\n    def __enter__(self) -> Context:\n        self._depth += 1\n        push_context(self)\n        return self\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_value: BaseException | None,\n        tb: TracebackType | None,\n    ) -> bool | None:\n        self._depth -= 1\n        exit_result: bool | None = None\n        if self._depth == 0:\n            exit_result = self._close_with_exception_info(exc_type, exc_value, tb)\n        pop_context()\n\n        return exit_result\n\n    @contextmanager\n    def scope(self, cleanup: bool = True) -> cabc.Iterator[Context]:\n        \"\"\"This helper method can be used with the context object to promote\n        it to the current thread local (see :func:`get_current_context`).\n        The default behavior of this is to invoke the cleanup functions which\n        can be disabled by setting `cleanup` to `False`.  The cleanup\n        functions are typically used for things such as closing file handles.\n\n        If the cleanup is intended the context object can also be directly\n        used as a context manager.\n\n        Example usage::\n\n            with ctx.scope():\n                assert get_current_context() is ctx\n\n        This is equivalent::\n\n            with ctx:\n                assert get_current_context() is ctx\n\n        .. versionadded:: 5.0\n\n        :param cleanup: controls if the cleanup functions should be run or\n                        not.  The default is to run these functions.  In\n                        some situations the context only wants to be\n                        temporarily pushed in which case this can be disabled.\n                        Nested pushes automatically defer the cleanup.\n        \"\"\"\n        if not cleanup:\n            self._depth += 1\n        try:\n            with self as rv:\n                yield rv\n        finally:\n            if not cleanup:\n                self._depth -= 1\n\n    @property\n    def meta(self) -> dict[str, t.Any]:\n        \"\"\"This is a dictionary which is shared with all the contexts\n        that are nested.  It exists so that click utilities can store some\n        state here if they need to.  It is however the responsibility of\n        that code to manage this dictionary well.\n\n        The keys are supposed to be unique dotted strings.  For instance\n        module paths are a good choice for it.  What is stored in there is\n        irrelevant for the operation of click.  However what is important is\n        that code that places data here adheres to the general semantics of\n        the system.\n\n        Example usage::\n\n            LANG_KEY = f'{__name__}.lang'\n\n            def set_language(value):\n                ctx = get_current_context()\n                ctx.meta[LANG_KEY] = value\n\n            def get_language():\n                return get_current_context().meta.get(LANG_KEY, 'en_US')\n\n        .. versionadded:: 5.0\n        \"\"\"\n        return self._meta\n\n    def make_formatter(self) -> HelpFormatter:\n        \"\"\"Creates the :class:`~click.HelpFormatter` for the help and\n        usage output.\n\n        To quickly customize the formatter class used without overriding\n        this method, set the :attr:`formatter_class` attribute.\n\n        .. versionchanged:: 8.0\n            Added the :attr:`formatter_class` attribute.\n        \"\"\"\n        return self.formatter_class(\n            width=self.terminal_width, max_width=self.max_content_width\n        )\n\n    def with_resource(self, context_manager: AbstractContextManager[V]) -> V:\n        \"\"\"Register a resource as if it were used in a ``with``\n        statement. The resource will be cleaned up when the context is\n        popped.\n\n        Uses :meth:`contextlib.ExitStack.enter_context`. It calls the\n        resource's ``__enter__()`` method and returns the result. When\n        the context is popped, it closes the stack, which calls the\n        resource's ``__exit__()`` method.\n\n        To register a cleanup function for something that isn't a\n        context manager, use :meth:`call_on_close`. Or use something\n        from :mod:`contextlib` to turn it into a context manager first.\n\n        .. code-block:: python\n\n            @click.group()\n            @click.option(\"--name\")\n            @click.pass_context\n            def cli(ctx):\n                ctx.obj = ctx.with_resource(connect_db(name))\n\n        :param context_manager: The context manager to enter.\n        :return: Whatever ``context_manager.__enter__()`` returns.\n\n        .. versionadded:: 8.0\n        \"\"\"\n        return self._exit_stack.enter_context(context_manager)\n\n    def call_on_close(self, f: t.Callable[..., t.Any]) -> t.Callable[..., t.Any]:\n        \"\"\"Register a function to be called when the context tears down.\n\n        This can be used to close resources opened during the script\n        execution. Resources that support Python's context manager\n        protocol which would be used in a ``with`` statement should be\n        registered with :meth:`with_resource` instead.\n\n        :param f: The function to execute on teardown.\n        \"\"\"\n        return self._exit_stack.callback(f)\n\n    def close(self) -> None:\n        \"\"\"Invoke all close callbacks registered with\n        :meth:`call_on_close`, and exit all context managers entered\n        with :meth:`with_resource`.\n        \"\"\"\n        self._close_with_exception_info(None, None, None)\n\n    def _close_with_exception_info(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_value: BaseException | None,\n        tb: TracebackType | None,\n    ) -> bool | None:\n        \"\"\"Unwind the exit stack by calling its :meth:`__exit__` providing the exception\n        information to allow for exception handling by the various resources registered\n        using :meth;`with_resource`\n\n        :return: Whatever ``exit_stack.__exit__()`` returns.\n        \"\"\"\n        exit_result = self._exit_stack.__exit__(exc_type, exc_value, tb)\n        # In case the context is reused, create a new exit stack.\n        self._exit_stack = ExitStack()\n\n        return exit_result\n\n    @property\n    def command_path(self) -> str:\n        \"\"\"The computed command path.  This is used for the ``usage``\n        information on the help page.  It's automatically created by\n        combining the info names of the chain of contexts to the root.\n        \"\"\"\n        rv = \"\"\n        if self.info_name is not None:\n            rv = self.info_name\n        if self.parent is not None:\n            parent_command_path = [self.parent.command_path]\n\n            if isinstance(self.parent.command, Command):\n                for param in self.parent.command.get_params(self):\n                    parent_command_path.extend(param.get_usage_pieces(self))\n\n            rv = f\"{' '.join(parent_command_path)} {rv}\"\n        return rv.lstrip()\n\n    def find_root(self) -> Context:\n        \"\"\"Finds the outermost context.\"\"\"\n        node = self\n        while node.parent is not None:\n            node = node.parent\n        return node\n\n    def find_object(self, object_type: type[V]) -> V | None:\n        \"\"\"Finds the closest object of a given type.\"\"\"\n        node: Context | None = self\n\n        while node is not None:\n            if isinstance(node.obj, object_type):\n                return node.obj\n\n            node = node.parent\n\n        return None\n\n    def ensure_object(self, object_type: type[V]) -> V:\n        \"\"\"Like :meth:`find_object` but sets the innermost object to a\n        new instance of `object_type` if it does not exist.\n        \"\"\"\n        rv = self.find_object(object_type)\n        if rv is None:\n            self.obj = rv = object_type()\n        return rv\n\n    @t.overload\n    def lookup_default(\n        self, name: str, call: t.Literal[True] = True\n    ) -> t.Any | None: ...\n\n    @t.overload\n    def lookup_default(\n        self, name: str, call: t.Literal[False] = ...\n    ) -> t.Any | t.Callable[[], t.Any] | None: ...\n\n    def lookup_default(self, name: str, call: bool = True) -> t.Any | None:\n        \"\"\"Get the default for a parameter from :attr:`default_map`.\n\n        :param name: Name of the parameter.\n        :param call: If the default is a callable, call it. Disable to\n            return the callable instead.\n\n        .. versionchanged:: 8.0\n            Added the ``call`` parameter.\n        \"\"\"\n        if self.default_map is not None:\n            value = self.default_map.get(name, UNSET)\n\n            if call and callable(value):\n                return value()\n\n            return value\n\n        return UNSET\n\n    def fail(self, message: str) -> t.NoReturn:\n        \"\"\"Aborts the execution of the program with a specific error\n        message.\n\n        :param message: the error message to fail with.\n        \"\"\"\n        raise UsageError(message, self)\n\n    def abort(self) -> t.NoReturn:\n        \"\"\"Aborts the script.\"\"\"\n        raise Abort()\n\n    def exit(self, code: int = 0) -> t.NoReturn:\n        \"\"\"Exits the application with a given exit code.\n\n        .. versionchanged:: 8.2\n            Callbacks and context managers registered with :meth:`call_on_close`\n            and :meth:`with_resource` are closed before exiting.\n        \"\"\"\n        self.close()\n        raise Exit(code)\n\n    def get_usage(self) -> str:\n        \"\"\"Helper method to get formatted usage string for the current\n        context and command.\n        \"\"\"\n        return self.command.get_usage(self)\n\n    def get_help(self) -> str:\n        \"\"\"Helper method to get formatted help page for the current\n        context and command.\n        \"\"\"\n        return self.command.get_help(self)\n\n    def _make_sub_context(self, command: Command) -> Context:\n        \"\"\"Create a new context of the same type as this context, but\n        for a new command.\n\n        :meta private:\n        \"\"\"\n        return type(self)(command, info_name=command.name, parent=self)\n\n    @t.overload\n    def invoke(\n        self, callback: t.Callable[..., V], /, *args: t.Any, **kwargs: t.Any\n    ) -> V: ...\n\n    @t.overload\n    def invoke(self, callback: Command, /, *args: t.Any, **kwargs: t.Any) -> t.Any: ...\n\n    def invoke(\n        self, callback: Command | t.Callable[..., V], /, *args: t.Any, **kwargs: t.Any\n    ) -> t.Any | V:\n        \"\"\"Invokes a command callback in exactly the way it expects.  There\n        are two ways to invoke this method:\n\n        1.  the first argument can be a callback and all other arguments and\n            keyword arguments are forwarded directly to the function.\n        2.  the first argument is a click command object.  In that case all\n            arguments are forwarded as well but proper click parameters\n            (options and click arguments) must be keyword arguments and Click\n            will fill in defaults.\n\n        .. versionchanged:: 8.0\n            All ``kwargs`` are tracked in :attr:`params` so they will be\n            passed if :meth:`forward` is called at multiple levels.\n\n        .. versionchanged:: 3.2\n            A new context is created, and missing arguments use default values.\n        \"\"\"\n        if isinstance(callback, Command):\n            other_cmd = callback\n\n            if other_cmd.callback is None:\n                raise TypeError(\n                    \"The given command does not have a callback that can be invoked.\"\n                )\n            else:\n                callback = t.cast(\"t.Callable[..., V]\", other_cmd.callback)\n\n            ctx = self._make_sub_context(other_cmd)\n\n            for param in other_cmd.params:\n                if param.name not in kwargs and param.expose_value:\n                    default_value = param.get_default(ctx)\n                    # We explicitly hide the :attr:`UNSET` value to the user, as we\n                    # choose to make it an implementation detail. And because ``invoke``\n                    # has been designed as part of Click public API, we return ``None``\n                    # instead. Refs:\n                    # https://github.com/pallets/click/issues/3066\n                    # https://github.com/pallets/click/issues/3065\n                    # https://github.com/pallets/click/pull/3068\n                    if default_value is UNSET:\n                        default_value = None\n                    kwargs[param.name] = param.type_cast_value(  # type: ignore\n                        ctx, default_value\n                    )\n\n            # Track all kwargs as params, so that forward() will pass\n            # them on in subsequent calls.\n            ctx.params.update(kwargs)\n        else:\n            ctx = self\n\n        with augment_usage_errors(self):\n            with ctx:\n                return callback(*args, **kwargs)\n\n    def forward(self, cmd: Command, /, *args: t.Any, **kwargs: t.Any) -> t.Any:\n        \"\"\"Similar to :meth:`invoke` but fills in default keyword\n        arguments from the current context if the other command expects\n        it.  This cannot invoke callbacks directly, only other commands.\n\n        .. versionchanged:: 8.0\n            All ``kwargs`` are tracked in :attr:`params` so they will be\n            passed if ``forward`` is called at multiple levels.\n        \"\"\"\n        # Can only forward to other commands, not direct callbacks.\n        if not isinstance(cmd, Command):\n            raise TypeError(\"Callback is not a command.\")\n\n        for param in self.params:\n            if param not in kwargs:\n                kwargs[param] = self.params[param]\n\n        return self.invoke(cmd, *args, **kwargs)\n\n    def set_parameter_source(self, name: str, source: ParameterSource) -> None:\n        \"\"\"Set the source of a parameter. This indicates the location\n        from which the value of the parameter was obtained.\n\n        :param name: The name of the parameter.\n        :param source: A member of :class:`~click.core.ParameterSource`.\n        \"\"\"\n        self._parameter_source[name] = source\n\n    def get_parameter_source(self, name: str) -> ParameterSource | None:\n        \"\"\"Get the source of a parameter. This indicates the location\n        from which the value of the parameter was obtained.\n\n        This can be useful for determining when a user specified a value\n        on the command line that is the same as the default value. It\n        will be :attr:`~click.core.ParameterSource.DEFAULT` only if the\n        value was actually taken from the default.\n\n        :param name: The name of the parameter.\n        :rtype: ParameterSource\n\n        .. versionchanged:: 8.0\n            Returns ``None`` if the parameter was not provided from any\n            source.\n        \"\"\"\n        return self._parameter_source.get(name)\n\n\nclass Command:\n    \"\"\"Commands are the basic building block of command line interfaces in\n    Click.  A basic command handles command line parsing and might dispatch\n    more parsing to commands nested below it.\n\n    :param name: the name of the command to use unless a group overrides it.\n    :param context_settings: an optional dictionary with defaults that are\n                             passed to the context object.\n    :param callback: the callback to invoke.  This is optional.\n    :param params: the parameters to register with this command.  This can\n                   be either :class:`Option` or :class:`Argument` objects.\n    :param help: the help string to use for this command.\n    :param epilog: like the help string but it's printed at the end of the\n                   help page after everything else.\n    :param short_help: the short help to use for this command.  This is\n                       shown on the command listing of the parent command.\n    :param add_help_option: by default each command registers a ``--help``\n                            option.  This can be disabled by this parameter.\n    :param no_args_is_help: this controls what happens if no arguments are\n                            provided.  This option is disabled by default.\n                            If enabled this will add ``--help`` as argument\n                            if no arguments are passed\n    :param hidden: hide this command from help outputs.\n    :param deprecated: If ``True`` or non-empty string, issues a message\n                        indicating that the command is deprecated and highlights\n                        its deprecation in --help. The message can be customized\n                        by using a string as the value.\n\n    .. versionchanged:: 8.2\n        This is the base class for all commands, not ``BaseCommand``.\n        ``deprecated`` can be set to a string as well to customize the\n        deprecation message.\n\n    .. versionchanged:: 8.1\n        ``help``, ``epilog``, and ``short_help`` are stored unprocessed,\n        all formatting is done when outputting help text, not at init,\n        and is done even if not using the ``@command`` decorator.\n\n    .. versionchanged:: 8.0\n        Added a ``repr`` showing the command name.\n\n    .. versionchanged:: 7.1\n        Added the ``no_args_is_help`` parameter.\n\n    .. versionchanged:: 2.0\n        Added the ``context_settings`` parameter.\n    \"\"\"\n\n    #: The context class to create with :meth:`make_context`.\n    #:\n    #: .. versionadded:: 8.0\n    context_class: type[Context] = Context\n\n    #: the default for the :attr:`Context.allow_extra_args` flag.\n    allow_extra_args = False\n\n    #: the default for the :attr:`Context.allow_interspersed_args` flag.\n    allow_interspersed_args = True\n\n    #: the default for the :attr:`Context.ignore_unknown_options` flag.\n    ignore_unknown_options = False\n\n    def __init__(\n        self,\n        name: str | None,\n        context_settings: cabc.MutableMapping[str, t.Any] | None = None,\n        callback: t.Callable[..., t.Any] | None = None,\n        params: list[Parameter] | None = None,\n        help: str | None = None,\n        epilog: str | None = None,\n        short_help: str | None = None,\n        options_metavar: str | None = \"[OPTIONS]\",\n        add_help_option: bool = True,\n        no_args_is_help: bool = False,\n        hidden: bool = False,\n        deprecated: bool | str = False,\n    ) -> None:\n        #: the name the command thinks it has.  Upon registering a command\n        #: on a :class:`Group` the group will default the command name\n        #: with this information.  You should instead use the\n        #: :class:`Context`\\'s :attr:`~Context.info_name` attribute.\n        self.name = name\n\n        if context_settings is None:\n            context_settings = {}\n\n        #: an optional dictionary with defaults passed to the context.\n        self.context_settings: cabc.MutableMapping[str, t.Any] = context_settings\n\n        #: the callback to execute when the command fires.  This might be\n        #: `None` in which case nothing happens.\n        self.callback = callback\n        #: the list of parameters for this command in the order they\n        #: should show up in the help page and execute.  Eager parameters\n        #: will automatically be handled before non eager ones.\n        self.params: list[Parameter] = params or []\n        self.help = help\n        self.epilog = epilog\n        self.options_metavar = options_metavar\n        self.short_help = short_help\n        self.add_help_option = add_help_option\n        self._help_option = None\n        self.no_args_is_help = no_args_is_help\n        self.hidden = hidden\n        self.deprecated = deprecated\n\n    def to_info_dict(self, ctx: Context) -> dict[str, t.Any]:\n        return {\n            \"name\": self.name,\n            \"params\": [param.to_info_dict() for param in self.get_params(ctx)],\n            \"help\": self.help,\n            \"epilog\": self.epilog,\n            \"short_help\": self.short_help,\n            \"hidden\": self.hidden,\n            \"deprecated\": self.deprecated,\n        }\n\n    def __repr__(self) -> str:\n        return f\"<{self.__class__.__name__} {self.name}>\"\n\n    def get_usage(self, ctx: Context) -> str:\n        \"\"\"Formats the usage line into a string and returns it.\n\n        Calls :meth:`format_usage` internally.\n        \"\"\"\n        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 odering 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        for param in self.get_params(ctx):\n            param.add_to_parser(parser, ctx)\n        return parser\n\n    def get_help(self, ctx: Context) -> str:\n        \"\"\"Formats the help into a string and returns it.\n\n        Calls :meth:`format_help` internally.\n        \"\"\"\n        formatter = ctx.make_formatter()\n        self.format_help(ctx, formatter)\n        return formatter.getvalue().rstrip(\"\\n\")\n\n    def get_short_help_str(self, limit: int = 45) -> str:\n        \"\"\"Gets short help for the command or makes it by shortening the\n        long help string.\n        \"\"\"\n        if self.short_help:\n            text = inspect.cleandoc(self.short_help)\n        elif self.help:\n            text = make_default_short_help(self.help, limit)\n        else:\n            text = \"\"\n\n        if self.deprecated:\n            deprecated_message = (\n                f\"(DEPRECATED: {self.deprecated})\"\n                if isinstance(self.deprecated, str)\n                else \"(DEPRECATED)\"\n            )\n            text = _(\"{text} {deprecated_message}\").format(\n                text=text, deprecated_message=deprecated_message\n            )\n\n        return text.strip()\n\n    def format_help(self, ctx: Context, formatter: HelpFormatter) -> None:\n        \"\"\"Writes the help into the formatter if it exists.\n\n        This is a low-level method called by :meth:`get_help`.\n\n        This calls the following methods:\n\n        -   :meth:`format_usage`\n        -   :meth:`format_help_text`\n        -   :meth:`format_options`\n        -   :meth:`format_epilog`\n        \"\"\"\n        self.format_usage(ctx, formatter)\n        self.format_help_text(ctx, formatter)\n        self.format_options(ctx, formatter)\n        self.format_epilog(ctx, formatter)\n\n    def format_help_text(self, ctx: Context, formatter: HelpFormatter) -> None:\n        \"\"\"Writes the help text to the formatter if it exists.\"\"\"\n        if self.help is not None:\n            # truncate the help text to the first form feed\n            text = inspect.cleandoc(self.help).partition(\"\\f\")[0]\n        else:\n            text = \"\"\n\n        if self.deprecated:\n            deprecated_message = (\n                f\"(DEPRECATED: {self.deprecated})\"\n                if isinstance(self.deprecated, str)\n                else \"(DEPRECATED)\"\n            )\n            text = _(\"{text} {deprecated_message}\").format(\n                text=text, deprecated_message=deprecated_message\n            )\n\n        if text:\n            formatter.write_paragraph()\n\n            with formatter.indentation():\n                formatter.write_text(text)\n\n    def format_options(self, ctx: Context, formatter: HelpFormatter) -> None:\n        \"\"\"Writes all the options into the formatter if they exist.\"\"\"\n        opts = []\n        for param in self.get_params(ctx):\n            rv = param.get_help_record(ctx)\n            if rv is not None:\n                opts.append(rv)\n\n        if opts:\n            with formatter.section(_(\"Options\")):\n                formatter.write_dl(opts)\n\n    def format_epilog(self, ctx: Context, formatter: HelpFormatter) -> None:\n        \"\"\"Writes the epilog into the formatter if it exists.\"\"\"\n        if self.epilog:\n            epilog = inspect.cleandoc(self.epilog)\n            formatter.write_paragraph()\n\n            with formatter.indentation():\n                formatter.write_text(epilog)\n\n    def make_context(\n        self,\n        info_name: str | None,\n        args: list[str],\n        parent: Context | None = None,\n        **extra: t.Any,\n    ) -> Context:\n        \"\"\"This function when given an info name and arguments will kick\n        off the parsing and create a new :class:`Context`.  It does not\n        invoke the actual command callback though.\n\n        To quickly customize the context class used without overriding\n        this method, set the :attr:`context_class` attribute.\n\n        :param info_name: the info name for this invocation.  Generally this\n                          is the most descriptive name for the script or\n                          command.  For the toplevel script it's usually\n                          the name of the script, for commands below it's\n                          the name of the command.\n        :param args: the arguments to parse as list of strings.\n        :param parent: the parent context if available.\n        :param extra: extra keyword arguments forwarded to the context\n                      constructor.\n\n        .. versionchanged:: 8.0\n            Added the :attr:`context_class` attribute.\n        \"\"\"\n        for key, value in self.context_settings.items():\n            if key not in extra:\n                extra[key] = value\n\n        ctx = self.context_class(self, info_name=info_name, parent=parent, **extra)\n\n        with ctx.scope(cleanup=False):\n            self.parse_args(ctx, args)\n        return ctx\n\n    def parse_args(self, ctx: Context, args: list[str]) -> list[str]:\n        if not args and self.no_args_is_help and not ctx.resilient_parsing:\n            raise NoArgsIsHelpError(ctx)\n\n        parser = self.make_parser(ctx)\n        opts, args, param_order = parser.parse_args(args=args)\n\n        for param in iter_params_for_processing(param_order, self.get_params(ctx)):\n            _, args = param.handle_parse_result(ctx, opts, args)\n\n        # We now have all parameters' values into `ctx.params`, but the data may contain\n        # the `UNSET` sentinel.\n        # Convert `UNSET` to `None` to ensure that the user doesn't see `UNSET`.\n        #\n        # Waiting until after the initial parse to convert allows us to treat `UNSET`\n        # more like a missing value when multiple params use the same name.\n        # Refs:\n        # https://github.com/pallets/click/issues/3071\n        # https://github.com/pallets/click/pull/3079\n        for name, value in ctx.params.items():\n            if value is UNSET:\n                ctx.params[name] = None\n\n        if args and not ctx.allow_extra_args and not ctx.resilient_parsing:\n            ctx.fail(\n                ngettext(\n                    \"Got unexpected extra argument ({args})\",\n                    \"Got unexpected extra arguments ({args})\",\n                    len(args),\n                ).format(args=\" \".join(map(str, args)))\n            )\n\n        ctx.args = args\n        ctx._opt_prefixes.update(parser._opt_prefixes)\n        return args\n\n    def invoke(self, ctx: Context) -> t.Any:\n        \"\"\"Given a context, this invokes the attached callback (if it exists)\n        in the right way.\n        \"\"\"\n        if self.deprecated:\n            extra_message = (\n                f\" {self.deprecated}\" if isinstance(self.deprecated, str) else \"\"\n            )\n            message = _(\n                \"DeprecationWarning: The command {name!r} is deprecated.{extra_message}\"\n            ).format(name=self.name, extra_message=extra_message)\n            echo(style(message, fg=\"red\"), err=True)\n\n        if self.callback is not None:\n            return ctx.invoke(self.callback, **ctx.params)\n\n    def shell_complete(self, ctx: Context, incomplete: str) -> list[CompletionItem]:\n        \"\"\"Return a list of completions for the incomplete value. Looks\n        at the names of options and chained multi-commands.\n\n        Any command could be part of a chained multi-command, so sibling\n        commands are valid at any point during command completion.\n\n        :param ctx: Invocation context for this command.\n        :param incomplete: Value being completed. May be empty.\n\n        .. versionadded:: 8.0\n        \"\"\"\n        from click.shell_completion import CompletionItem\n\n        results: list[CompletionItem] = []\n\n        if incomplete and not incomplete[0].isalnum():\n            for param in self.get_params(ctx):\n                if (\n                    not isinstance(param, Option)\n                    or param.hidden\n                    or (\n                        not param.multiple\n                        and ctx.get_parameter_source(param.name)  # type: ignore\n                        is ParameterSource.COMMANDLINE\n                    )\n                ):\n                    continue\n\n                results.extend(\n                    CompletionItem(name, help=param.help)\n                    for name in [*param.opts, *param.secondary_opts]\n                    if name.startswith(incomplete)\n                )\n\n        while ctx.parent is not None:\n            ctx = ctx.parent\n\n            if isinstance(ctx.command, Group) and ctx.command.chain:\n                results.extend(\n                    CompletionItem(name, help=command.get_short_help_str())\n                    for name, command in _complete_visible_commands(ctx, incomplete)\n                    if name not in ctx._protected_args\n                )\n\n        return results\n\n    @t.overload\n    def main(\n        self,\n        args: cabc.Sequence[str] | None = None,\n        prog_name: str | None = None,\n        complete_var: str | None = None,\n        standalone_mode: t.Literal[True] = True,\n        **extra: t.Any,\n    ) -> t.NoReturn: ...\n\n    @t.overload\n    def main(\n        self,\n        args: cabc.Sequence[str] | None = None,\n        prog_name: str | None = None,\n        complete_var: str | None = None,\n        standalone_mode: bool = ...,\n        **extra: t.Any,\n    ) -> t.Any: ...\n\n    def main(\n        self,\n        args: cabc.Sequence[str] | None = None,\n        prog_name: str | None = None,\n        complete_var: str | None = None,\n        standalone_mode: bool = True,\n        windows_expand_args: bool = True,\n        **extra: t.Any,\n    ) -> t.Any:\n        \"\"\"This is the way to invoke a script with all the bells and\n        whistles as a command line application.  This will always terminate\n        the application after a call.  If this is not wanted, ``SystemExit``\n        needs to be caught.\n\n        This method is also available by directly calling the instance of\n        a :class:`Command`.\n\n        :param args: the arguments that should be used for parsing.  If not\n                     provided, ``sys.argv[1:]`` is used.\n        :param prog_name: the program name that should be used.  By default\n                          the program name is constructed by taking the file\n                          name from ``sys.argv[0]``.\n        :param complete_var: the environment variable that controls the\n                             bash completion support.  The default is\n                             ``\"_<prog_name>_COMPLETE\"`` with prog_name in\n                             uppercase.\n        :param standalone_mode: the default behavior is to invoke the script\n                                in standalone mode.  Click will then\n                                handle exceptions and convert them into\n                                error messages and the function will never\n                                return but shut down the interpreter.  If\n                                this is set to `False` they will be\n                                propagated to the caller and the return\n                                value of this function is the return value\n                                of :meth:`invoke`.\n        :param windows_expand_args: Expand glob patterns, user dir, and\n            env vars in command line args on Windows.\n        :param extra: extra keyword arguments are forwarded to the context\n                      constructor.  See :class:`Context` for more information.\n\n        .. versionchanged:: 8.0.1\n            Added the ``windows_expand_args`` parameter to allow\n            disabling command line arg expansion on Windows.\n\n        .. versionchanged:: 8.0\n            When taking arguments from ``sys.argv`` on Windows, glob\n            patterns, user dir, and env vars are expanded.\n\n        .. versionchanged:: 3.0\n           Added the ``standalone_mode`` parameter.\n        \"\"\"\n        if args is None:\n            args = sys.argv[1:]\n\n            if os.name == \"nt\" and windows_expand_args:\n                args = _expand_args(args)\n        else:\n            args = list(args)\n\n        if prog_name is None:\n            prog_name = _detect_program_name()\n\n        # Process shell completion requests and exit early.\n        self._main_shell_completion(extra, prog_name, complete_var)\n\n        try:\n            try:\n                with self.make_context(prog_name, args, **extra) as ctx:\n                    rv = self.invoke(ctx)\n                    if not standalone_mode:\n                        return rv\n                    # it's not safe to `ctx.exit(rv)` here!\n                    # note that `rv` may actually contain data like \"1\" which\n                    # has obvious effects\n                    # more subtle case: `rv=[None, None]` can come out of\n                    # chained commands which all returned `None` -- so it's not\n                    # even always obvious that `rv` indicates success/failure\n                    # by its truthiness/falsiness\n                    ctx.exit()\n            except (EOFError, KeyboardInterrupt) as e:\n                echo(file=sys.stderr)\n                raise Abort() from e\n            except ClickException as e:\n                if not standalone_mode:\n                    raise\n                e.show()\n                sys.exit(e.exit_code)\n            except OSError as e:\n                if e.errno == errno.EPIPE:\n                    sys.stdout = t.cast(t.TextIO, PacifyFlushWrapper(sys.stdout))\n                    sys.stderr = t.cast(t.TextIO, PacifyFlushWrapper(sys.stderr))\n                    sys.exit(1)\n                else:\n                    raise\n        except Exit as e:\n            if standalone_mode:\n                sys.exit(e.exit_code)\n            else:\n                # in non-standalone mode, return the exit code\n                # note that this is only reached if `self.invoke` above raises\n                # an Exit explicitly -- thus bypassing the check there which\n                # would return its result\n                # the results of non-standalone execution may therefore be\n                # somewhat ambiguous: if there are codepaths which lead to\n                # `ctx.exit(1)` and to `return 1`, the caller won't be able to\n                # tell the difference between the two\n                return e.exit_code\n        except Abort:\n            if not standalone_mode:\n                raise\n            echo(_(\"Aborted!\"), file=sys.stderr)\n            sys.exit(1)\n\n    def _main_shell_completion(\n        self,\n        ctx_args: cabc.MutableMapping[str, t.Any],\n        prog_name: str,\n        complete_var: str | None = None,\n    ) -> None:\n        \"\"\"Check if the shell is asking for tab completion, process\n        that, then exit early. Called from :meth:`main` before the\n        program is invoked.\n\n        :param prog_name: Name of the executable in the shell.\n        :param complete_var: Name of the environment variable that holds\n            the completion instruction. Defaults to\n            ``_{PROG_NAME}_COMPLETE``.\n\n        .. versionchanged:: 8.2.0\n            Dots (``.``) in ``prog_name`` are replaced with underscores (``_``).\n        \"\"\"\n        if complete_var is None:\n            complete_name = prog_name.replace(\"-\", \"_\").replace(\".\", \"_\")\n            complete_var = f\"_{complete_name}_COMPLETE\".upper()\n\n        instruction = os.environ.get(complete_var)\n\n        if not instruction:\n            return\n\n        from .shell_completion import shell_complete\n\n        rv = shell_complete(self, ctx_args, prog_name, complete_var, instruction)\n        sys.exit(rv)\n\n    def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Any:\n        \"\"\"Alias for :meth:`main`.\"\"\"\n        return self.main(*args, **kwargs)\n\n\nclass _FakeSubclassCheck(type):\n    def __subclasscheck__(cls, subclass: type) -> bool:\n        return issubclass(subclass, cls.__bases__[0])\n\n    def __instancecheck__(cls, instance: t.Any) -> bool:\n        return isinstance(instance, cls.__bases__[0])\n\n\nclass _BaseCommand(Command, metaclass=_FakeSubclassCheck):\n    \"\"\"\n    .. deprecated:: 8.2\n        Will be removed in Click 9.0. Use ``Command`` instead.\n    \"\"\"\n\n\nclass Group(Command):\n    \"\"\"A group is a command that nests other commands (or more groups).\n\n    :param name: The name of the group command.\n    :param commands: Map names to :class:`Command` objects. Can be a list, which\n        will use :attr:`Command.name` as the keys.\n    :param invoke_without_command: Invoke the group's callback even if a\n        subcommand is not given.\n    :param no_args_is_help: If no arguments are given, show the group's help and\n        exit. Defaults to the opposite of ``invoke_without_command``.\n    :param subcommand_metavar: How to represent the subcommand argument in help.\n        The default will represent whether ``chain`` is set or not.\n    :param chain: Allow passing more than one subcommand argument. After parsing\n        a command's arguments, if any arguments remain another command will be\n        matched, and so on.\n    :param result_callback: A function to call after the group's and\n        subcommand's callbacks. The value returned by the subcommand is passed.\n        If ``chain`` is enabled, the value will be a list of values returned by\n        all the commands. If ``invoke_without_command`` is enabled, the value\n        will be the value returned by the group's callback, or an empty list if\n        ``chain`` is enabled.\n    :param kwargs: Other arguments passed to :class:`Command`.\n\n    .. versionchanged:: 8.0\n        The ``commands`` argument can be a list of command objects.\n\n    .. versionchanged:: 8.2\n        Merged with and replaces the ``MultiCommand`` base class.\n    \"\"\"\n\n    allow_extra_args = True\n    allow_interspersed_args = False\n\n    #: If set, this is used by the group's :meth:`command` decorator\n    #: as the default :class:`Command` class. This is useful to make all\n    #: subcommands use a custom command class.\n    #:\n    #: .. versionadded:: 8.0\n    command_class: type[Command] | None = None\n\n    #: If set, this is used by the group's :meth:`group` decorator\n    #: as the default :class:`Group` class. This is useful to make all\n    #: subgroups use a custom group class.\n    #:\n    #: If set to the special value :class:`type` (literally\n    #: ``group_class = type``), this group's class will be used as the\n    #: default class. This makes a custom group class continue to make\n    #: custom groups.\n    #:\n    #: .. versionadded:: 8.0\n    group_class: type[Group] | type[type] | None = None\n    # Literal[type] isn't valid, so use Type[type]\n\n    def __init__(\n        self,\n        name: str | None = None,\n        commands: cabc.MutableMapping[str, Command]\n        | cabc.Sequence[Command]\n        | None = None,\n        invoke_without_command: bool = False,\n        no_args_is_help: bool | None = None,\n        subcommand_metavar: str | None = None,\n        chain: bool = False,\n        result_callback: t.Callable[..., t.Any] | None = None,\n        **kwargs: t.Any,\n    ) -> None:\n        super().__init__(name, **kwargs)\n\n        if commands is None:\n            commands = {}\n        elif isinstance(commands, abc.Sequence):\n            commands = {c.name: c for c in commands if c.name is not None}\n\n        #: The registered subcommands by their exported names.\n        self.commands: cabc.MutableMapping[str, Command] = commands\n\n        if no_args_is_help is None:\n            no_args_is_help = not invoke_without_command\n\n        self.no_args_is_help = no_args_is_help\n        self.invoke_without_command = invoke_without_command\n\n        if subcommand_metavar is None:\n            if chain:\n                subcommand_metavar = \"COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...\"\n            else:\n                subcommand_metavar = \"COMMAND [ARGS]...\"\n\n        self.subcommand_metavar = subcommand_metavar\n        self.chain = chain\n        # The result callback that is stored. This can be set or\n        # overridden with the :func:`result_callback` decorator.\n        self._result_callback = result_callback\n\n        if self.chain:\n            for param in self.params:\n                if isinstance(param, Argument) and not param.required:\n                    raise RuntimeError(\n                        \"A group in chain mode cannot have optional arguments.\"\n                    )\n\n    def to_info_dict(self, ctx: Context) -> dict[str, t.Any]:\n        info_dict = super().to_info_dict(ctx)\n        commands = {}\n\n        for name in self.list_commands(ctx):\n            command = self.get_command(ctx, name)\n\n            if command is None:\n                continue\n\n            sub_ctx = ctx._make_sub_context(command)\n\n            with sub_ctx.scope(cleanup=False):\n                commands[name] = command.to_info_dict(sub_ctx)\n\n        info_dict.update(commands=commands, chain=self.chain)\n        return info_dict\n\n    def add_command(self, cmd: Command, name: str | None = None) -> None:\n        \"\"\"Registers another :class:`Command` with this group.  If the name\n        is not provided, the name of the command is used.\n        \"\"\"\n        name = name or cmd.name\n        if name is None:\n            raise TypeError(\"Command has no name.\")\n        _check_nested_chain(self, name, cmd, register=True)\n        self.commands[name] = cmd\n\n    @t.overload\n    def command(self, __func: t.Callable[..., t.Any]) -> Command: ...\n\n    @t.overload\n    def command(\n        self, *args: t.Any, **kwargs: t.Any\n    ) -> t.Callable[[t.Callable[..., t.Any]], Command]: ...\n\n    def command(\n        self, *args: t.Any, **kwargs: t.Any\n    ) -> t.Callable[[t.Callable[..., t.Any]], Command] | Command:\n        \"\"\"A shortcut decorator for declaring and attaching a command to\n        the group. This takes the same arguments as :func:`command` and\n        immediately registers the created command with this group by\n        calling :meth:`add_command`.\n\n        To customize the command class used, set the\n        :attr:`command_class` attribute.\n\n        .. versionchanged:: 8.1\n            This decorator can be applied without parentheses.\n\n        .. versionchanged:: 8.0\n            Added the :attr:`command_class` attribute.\n        \"\"\"\n        from .decorators import command\n\n        func: t.Callable[..., t.Any] | None = None\n\n        if args and callable(args[0]):\n            assert len(args) == 1 and not kwargs, (\n                \"Use 'command(**kwargs)(callable)' to provide arguments.\"\n            )\n            (func,) = args\n            args = ()\n\n        if self.command_class and kwargs.get(\"cls\") is None:\n            kwargs[\"cls\"] = self.command_class\n\n        def decorator(f: t.Callable[..., t.Any]) -> Command:\n            cmd: Command = command(*args, **kwargs)(f)\n            self.add_command(cmd)\n            return cmd\n\n        if func is not None:\n            return decorator(func)\n\n        return decorator\n\n    @t.overload\n    def group(self, __func: t.Callable[..., t.Any]) -> Group: ...\n\n    @t.overload\n    def group(\n        self, *args: t.Any, **kwargs: t.Any\n    ) -> t.Callable[[t.Callable[..., t.Any]], Group]: ...\n\n    def group(\n        self, *args: t.Any, **kwargs: t.Any\n    ) -> t.Callable[[t.Callable[..., t.Any]], Group] | Group:\n        \"\"\"A shortcut decorator for declaring and attaching a group to\n        the group. This takes the same arguments as :func:`group` and\n        immediately registers the created group with this group by\n        calling :meth:`add_command`.\n\n        To customize the group class used, set the :attr:`group_class`\n        attribute.\n\n        .. versionchanged:: 8.1\n            This decorator can be applied without parentheses.\n\n        .. versionchanged:: 8.0\n            Added the :attr:`group_class` attribute.\n        \"\"\"\n        from .decorators import group\n\n        func: t.Callable[..., t.Any] | None = None\n\n        if args and callable(args[0]):\n            assert len(args) == 1 and not kwargs, (\n                \"Use 'group(**kwargs)(callable)' to provide arguments.\"\n            )\n            (func,) = args\n            args = ()\n\n        if self.group_class is not None and kwargs.get(\"cls\") is None:\n            if self.group_class is type:\n                kwargs[\"cls\"] = type(self)\n            else:\n                kwargs[\"cls\"] = self.group_class\n\n        def decorator(f: t.Callable[..., t.Any]) -> Group:\n            cmd: Group = group(*args, **kwargs)(f)\n            self.add_command(cmd)\n            return cmd\n\n        if func is not None:\n            return decorator(func)\n\n        return decorator\n\n    def result_callback(self, replace: bool = False) -> t.Callable[[F], F]:\n        \"\"\"Adds a result callback to the command.  By default if a\n        result callback is already registered this will chain them but\n        this can be disabled with the `replace` parameter.  The result\n        callback is invoked with the return value of the subcommand\n        (or the list of return values from all subcommands if chaining\n        is enabled) as well as the parameters as they would be passed\n        to the main callback.\n\n        Example::\n\n            @click.group()\n            @click.option('-i', '--input', default=23)\n            def cli(input):\n                return 42\n\n            @cli.result_callback()\n            def process_result(result, input):\n                return result + input\n\n        :param replace: if set to `True` an already existing result\n                        callback will be removed.\n\n        .. versionchanged:: 8.0\n            Renamed from ``resultcallback``.\n\n        .. versionadded:: 3.0\n        \"\"\"\n\n        def decorator(f: F) -> F:\n            old_callback = self._result_callback\n\n            if old_callback is None or replace:\n                self._result_callback = f\n                return f\n\n            def function(value: t.Any, /, *args: t.Any, **kwargs: t.Any) -> t.Any:\n                inner = old_callback(value, *args, **kwargs)\n                return f(inner, *args, **kwargs)\n\n            self._result_callback = rv = update_wrapper(t.cast(F, function), f)\n            return rv  # type: ignore[return-value]\n\n        return decorator\n\n    def get_command(self, ctx: Context, cmd_name: str) -> Command | None:\n        \"\"\"Given a context and a command name, this returns a :class:`Command`\n        object if it exists or returns ``None``.\n        \"\"\"\n        return self.commands.get(cmd_name)\n\n    def list_commands(self, ctx: Context) -> list[str]:\n        \"\"\"Returns a list of subcommand names in the order they should appear.\"\"\"\n        return sorted(self.commands)\n\n    def collect_usage_pieces(self, ctx: Context) -> list[str]:\n        rv = super().collect_usage_pieces(ctx)\n        rv.append(self.subcommand_metavar)\n        return rv\n\n    def format_options(self, ctx: Context, formatter: HelpFormatter) -> None:\n        super().format_options(ctx, formatter)\n        self.format_commands(ctx, formatter)\n\n    def format_commands(self, ctx: Context, formatter: HelpFormatter) -> None:\n        \"\"\"Extra format methods for multi methods that adds all the commands\n        after the options.\n        \"\"\"\n        commands = []\n        for subcommand in self.list_commands(ctx):\n            cmd = self.get_command(ctx, subcommand)\n            # What is this, the tool lied about a command.  Ignore it\n            if cmd is None:\n                continue\n            if cmd.hidden:\n                continue\n\n            commands.append((subcommand, cmd))\n\n        # allow for 3 times the default spacing\n        if len(commands):\n            limit = formatter.width - 6 - max(len(cmd[0]) for cmd in commands)\n\n            rows = []\n            for subcommand, cmd in commands:\n                help = cmd.get_short_help_str(limit)\n                rows.append((subcommand, help))\n\n            if rows:\n                with formatter.section(_(\"Commands\")):\n                    formatter.write_dl(rows)\n\n    def parse_args(self, ctx: Context, args: list[str]) -> list[str]:\n        if not args and self.no_args_is_help and not ctx.resilient_parsing:\n            raise NoArgsIsHelpError(ctx)\n\n        rest = super().parse_args(ctx, args)\n\n        if self.chain:\n            ctx._protected_args = rest\n            ctx.args = []\n        elif rest:\n            ctx._protected_args, ctx.args = rest[:1], rest[1:]\n\n        return ctx.args\n\n    def invoke(self, ctx: Context) -> t.Any:\n        def _process_result(value: t.Any) -> t.Any:\n            if self._result_callback is not None:\n                value = ctx.invoke(self._result_callback, value, **ctx.params)\n            return value\n\n        if not ctx._protected_args:\n            if self.invoke_without_command:\n                # No subcommand was invoked, so the result callback is\n                # invoked with the group return value for regular\n                # groups, or an empty list for chained groups.\n                with ctx:\n                    rv = super().invoke(ctx)\n                    return _process_result([] if self.chain else rv)\n            ctx.fail(_(\"Missing command.\"))\n\n        # Fetch args back out\n        args = [*ctx._protected_args, *ctx.args]\n        ctx.args = []\n        ctx._protected_args = []\n\n        # If we're not in chain mode, we only allow the invocation of a\n        # single command but we also inform the current context about the\n        # name of the command to invoke.\n        if not self.chain:\n            # Make sure the context is entered so we do not clean up\n            # resources until the result processor has worked.\n            with ctx:\n                cmd_name, cmd, args = self.resolve_command(ctx, args)\n                assert cmd is not None\n                ctx.invoked_subcommand = cmd_name\n                super().invoke(ctx)\n                sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)\n                with sub_ctx:\n                    return _process_result(sub_ctx.command.invoke(sub_ctx))\n\n        # In chain mode we create the contexts step by step, but after the\n        # base command has been invoked.  Because at that point we do not\n        # know the subcommands yet, the invoked subcommand attribute is\n        # set to ``*`` to inform the command that subcommands are executed\n        # but nothing else.\n        with ctx:\n            ctx.invoked_subcommand = \"*\" if args else None\n            super().invoke(ctx)\n\n            # Otherwise we make every single context and invoke them in a\n            # chain.  In that case the return value to the result processor\n            # is the list of all invoked subcommand's results.\n            contexts = []\n            while args:\n                cmd_name, cmd, args = self.resolve_command(ctx, args)\n                assert cmd is not None\n                sub_ctx = cmd.make_context(\n                    cmd_name,\n                    args,\n                    parent=ctx,\n                    allow_extra_args=True,\n                    allow_interspersed_args=False,\n                )\n                contexts.append(sub_ctx)\n                args, sub_ctx.args = sub_ctx.args, []\n\n            rv = []\n            for sub_ctx in contexts:\n                with sub_ctx:\n                    rv.append(sub_ctx.command.invoke(sub_ctx))\n            return _process_result(rv)\n\n    def resolve_command(\n        self, ctx: Context, args: list[str]\n    ) -> tuple[str | None, Command | None, list[str]]:\n        cmd_name = make_str(args[0])\n        original_cmd_name = cmd_name\n\n        # Get the command\n        cmd = self.get_command(ctx, cmd_name)\n\n        # If we can't find the command but there is a normalization\n        # function available, we try with that one.\n        if cmd is None and ctx.token_normalize_func is not None:\n            cmd_name = ctx.token_normalize_func(cmd_name)\n            cmd = self.get_command(ctx, cmd_name)\n\n        # If we don't find the command we want to show an error message\n        # to the user that it was not provided.  However, there is\n        # something else we should do: if the first argument looks like\n        # an option we want to kick off parsing again for arguments to\n        # resolve things like --help which now should go to the main\n        # place.\n        if cmd is None and not ctx.resilient_parsing:\n            if _split_opt(cmd_name)[0]:\n                self.parse_args(ctx, args)\n            ctx.fail(_(\"No such command {name!r}.\").format(name=original_cmd_name))\n        return cmd_name if cmd else None, cmd, args[1:]\n\n    def shell_complete(self, ctx: Context, incomplete: str) -> list[CompletionItem]:\n        \"\"\"Return a list of completions for the incomplete value. Looks\n        at the names of options, subcommands, and chained\n        multi-commands.\n\n        :param ctx: Invocation context for this command.\n        :param incomplete: Value being completed. May be empty.\n\n        .. versionadded:: 8.0\n        \"\"\"\n        from click.shell_completion import CompletionItem\n\n        results = [\n            CompletionItem(name, help=command.get_short_help_str())\n            for name, command in _complete_visible_commands(ctx, incomplete)\n        ]\n        results.extend(super().shell_complete(ctx, incomplete))\n        return results\n\n\nclass _MultiCommand(Group, metaclass=_FakeSubclassCheck):\n    \"\"\"\n    .. deprecated:: 8.2\n        Will be removed in Click 9.0. Use ``Group`` instead.\n    \"\"\"\n\n\nclass CommandCollection(Group):\n    \"\"\"A :class:`Group` that looks up subcommands on other groups. If a command\n    is not found on this group, each registered source is checked in order.\n    Parameters on a source are not added to this group, and a source's callback\n    is not invoked when invoking its commands. In other words, this \"flattens\"\n    commands in many groups into this one group.\n\n    :param name: The name of the group command.\n    :param sources: A list of :class:`Group` objects to look up commands from.\n    :param kwargs: Other arguments passed to :class:`Group`.\n\n    .. versionchanged:: 8.2\n        This is a subclass of ``Group``. Commands are looked up first on this\n        group, then each of its sources.\n    \"\"\"\n\n    def __init__(\n        self,\n        name: str | None = None,\n        sources: list[Group] | None = None,\n        **kwargs: t.Any,\n    ) -> None:\n        super().__init__(name, **kwargs)\n        #: The list of registered groups.\n        self.sources: list[Group] = sources or []\n\n    def add_source(self, group: Group) -> None:\n        \"\"\"Add a group as a source of commands.\"\"\"\n        self.sources.append(group)\n\n    def get_command(self, ctx: Context, cmd_name: str) -> Command | None:\n        rv = super().get_command(ctx, cmd_name)\n\n        if rv is not None:\n            return rv\n\n        for source in self.sources:\n            rv = source.get_command(ctx, cmd_name)\n\n            if rv is not None:\n                if self.chain:\n                    _check_nested_chain(self, cmd_name, rv)\n\n                return rv\n\n        return None\n\n    def list_commands(self, ctx: Context) -> list[str]:\n        rv: set[str] = set(super().list_commands(ctx))\n\n        for source in self.sources:\n            rv.update(source.list_commands(ctx))\n\n        return sorted(rv)\n\n\ndef _check_iter(value: t.Any) -> cabc.Iterator[t.Any]:\n    \"\"\"Check if the value is iterable but not a string. Raises a type\n    error, or return an iterator over the value.\n    \"\"\"\n    if isinstance(value, str):\n        raise TypeError\n\n    return iter(value)\n\n\nclass Parameter:\n    r\"\"\"A parameter to a command comes in two versions: they are either\n    :class:`Option`\\s or :class:`Argument`\\s.  Other subclasses are currently\n    not supported by design as some of the internals for parsing are\n    intentionally not finalized.\n\n    Some settings are supported by both options and arguments.\n\n    :param param_decls: the parameter declarations for this option or\n                        argument.  This is a list of flags or argument\n                        names.\n    :param type: the type that should be used.  Either a :class:`ParamType`\n                 or a Python type.  The latter is converted into the former\n                 automatically if supported.\n    :param required: controls if this is optional or not.\n    :param default: the default value if omitted.  This can also be a callable,\n                    in which case it's invoked when the default is needed\n                    without any arguments.\n    :param callback: A function to further process or validate the value\n        after type conversion. It is called as ``f(ctx, param, value)``\n        and must return the value. It is called for all sources,\n        including prompts.\n    :param nargs: the number of arguments to match.  If not ``1`` the return\n                  value is a tuple instead of single value.  The default for\n                  nargs is ``1`` (except if the type is a tuple, then it's\n                  the arity of the tuple). If ``nargs=-1``, all remaining\n                  parameters are collected.\n    :param metavar: how the value is represented in the help page.\n    :param expose_value: if this is `True` then the value is passed onwards\n                         to the command callback and stored on the context,\n                         otherwise it's skipped.\n    :param is_eager: eager values are processed before non eager ones.  This\n                     should not be set for arguments or it will inverse the\n                     order of processing.\n    :param envvar: environment variable(s) that are used to provide a default value for\n        this parameter. This can be a string or a sequence of strings. If a sequence is\n        given, only the first non-empty environment variable is used for the parameter.\n    :param shell_complete: A function that returns custom shell\n        completions. Used instead of the param's type completion if\n        given. Takes ``ctx, param, incomplete`` and must return a list\n        of :class:`~click.shell_completion.CompletionItem` or a list of\n        strings.\n    :param deprecated: If ``True`` or non-empty string, issues a message\n                        indicating that the argument is deprecated and highlights\n                        its deprecation in --help. The message can be customized\n                        by using a string as the value. A deprecated parameter\n                        cannot be required, a ValueError will be raised otherwise.\n\n    .. versionchanged:: 8.2.0\n        Introduction of ``deprecated``.\n\n    .. versionchanged:: 8.2\n        Adding duplicate parameter names to a :class:`~click.core.Command` will\n        result in a ``UserWarning`` being shown.\n\n    .. versionchanged:: 8.2\n        Adding duplicate parameter names to a :class:`~click.core.Command` will\n        result in a ``UserWarning`` being shown.\n\n    .. versionchanged:: 8.0\n        ``process_value`` validates required parameters and bounded\n        ``nargs``, and invokes the parameter callback before returning\n        the value. This allows the callback to validate prompts.\n        ``full_process_value`` is removed.\n\n    .. versionchanged:: 8.0\n        ``autocompletion`` is renamed to ``shell_complete`` and has new\n        semantics described above. The old name is deprecated and will\n        be removed in 8.1, until then it will be wrapped to match the\n        new requirements.\n\n    .. versionchanged:: 8.0\n        For ``multiple=True, nargs>1``, the default must be a list of\n        tuples.\n\n    .. versionchanged:: 8.0\n        Setting a default is no longer required for ``nargs>1``, it will\n        default to ``None``. ``multiple=True`` or ``nargs=-1`` will\n        default to ``()``.\n\n    .. versionchanged:: 7.1\n        Empty environment variables are ignored rather than taking the\n        empty string value. This makes it possible for scripts to clear\n        variables if they can't unset them.\n\n    .. versionchanged:: 2.0\n        Changed signature for parameter callback to also be passed the\n        parameter. The old callback format will still work, but it will\n        raise a warning to give you a chance to migrate the code easier.\n    \"\"\"\n\n    param_type_name = \"parameter\"\n\n    def __init__(\n        self,\n        param_decls: cabc.Sequence[str] | None = None,\n        type: types.ParamType | t.Any | None = None,\n        required: bool = False,\n        # XXX The default historically embed two concepts:\n        # - the declaration of a Parameter object carrying the default (handy to\n        #   arbitrage the default value of coupled Parameters sharing the same\n        #   self.name, like flag options),\n        # - and the actual value of the default.\n        # It is confusing and is the source of many issues discussed in:\n        # https://github.com/pallets/click/pull/3030\n        # In the future, we might think of splitting it in two, not unlike\n        # Option.is_flag and Option.flag_value: we could have something like\n        # Parameter.is_default and Parameter.default_value.\n        default: t.Any | t.Callable[[], t.Any] | None = UNSET,\n        callback: t.Callable[[Context, Parameter, t.Any], t.Any] | None = None,\n        nargs: int | None = None,\n        multiple: bool = False,\n        metavar: str | None = None,\n        expose_value: bool = True,\n        is_eager: bool = False,\n        envvar: str | cabc.Sequence[str] | None = None,\n        shell_complete: t.Callable[\n            [Context, Parameter, str], list[CompletionItem] | list[str]\n        ]\n        | None = None,\n        deprecated: bool | str = False,\n    ) -> None:\n        self.name: str | None\n        self.opts: list[str]\n        self.secondary_opts: list[str]\n        self.name, self.opts, self.secondary_opts = self._parse_decls(\n            param_decls or (), expose_value\n        )\n        self.type: types.ParamType = types.convert_type(type, default)\n\n        # Default nargs to what the type tells us if we have that\n        # information available.\n        if nargs is None:\n            if self.type.is_composite:\n                nargs = self.type.arity\n            else:\n                nargs = 1\n\n        self.required = required\n        self.callback = callback\n        self.nargs = nargs\n        self.multiple = multiple\n        self.expose_value = expose_value\n        self.default: t.Any | t.Callable[[], t.Any] | None = default\n        self.is_eager = is_eager\n        self.metavar = metavar\n        self.envvar = envvar\n        self._custom_shell_complete = shell_complete\n        self.deprecated = deprecated\n\n        if __debug__:\n            if self.type.is_composite and nargs != self.type.arity:\n                raise ValueError(\n                    f\"'nargs' must be {self.type.arity} (or None) for\"\n                    f\" type {self.type!r}, but it was {nargs}.\"\n                )\n\n            if required and deprecated:\n                raise ValueError(\n                    f\"The {self.param_type_name} '{self.human_readable_name}' \"\n                    \"is deprecated and still required. A deprecated \"\n                    f\"{self.param_type_name} cannot be required.\"\n                )\n\n    def to_info_dict(self) -> dict[str, t.Any]:\n        \"\"\"Gather information that could be useful for a tool generating\n        user-facing documentation.\n\n        Use :meth:`click.Context.to_info_dict` to traverse the entire\n        CLI structure.\n\n        .. versionchanged:: 8.3.0\n            Returns ``None`` for the :attr:`default` if it was not set.\n\n        .. versionadded:: 8.0\n        \"\"\"\n        return {\n            \"name\": self.name,\n            \"param_type_name\": self.param_type_name,\n            \"opts\": self.opts,\n            \"secondary_opts\": self.secondary_opts,\n            \"type\": self.type.to_info_dict(),\n            \"required\": self.required,\n            \"nargs\": self.nargs,\n            \"multiple\": self.multiple,\n            # We explicitly hide the :attr:`UNSET` value to the user, as we choose to\n            # make it an implementation detail. And because ``to_info_dict`` has been\n            # designed for documentation purposes, we return ``None`` instead.\n            \"default\": self.default if self.default is not UNSET else None,\n            \"envvar\": self.envvar,\n        }\n\n    def __repr__(self) -> str:\n        return f\"<{self.__class__.__name__} {self.name}>\"\n\n    def _parse_decls(\n        self, decls: cabc.Sequence[str], expose_value: bool\n    ) -> tuple[str | None, list[str], list[str]]:\n        raise NotImplementedError()\n\n    @property\n    def human_readable_name(self) -> str:\n        \"\"\"Returns the human readable name of this parameter.  This is the\n        same as the name for options, but the metavar for arguments.\n        \"\"\"\n        return self.name  # type: ignore\n\n    def make_metavar(self, ctx: Context) -> str:\n        if self.metavar is not None:\n            return self.metavar\n\n        metavar = self.type.get_metavar(param=self, ctx=ctx)\n\n        if metavar is None:\n            metavar = self.type.name.upper()\n\n        if self.nargs != 1:\n            metavar += \"...\"\n\n        return metavar\n\n    @t.overload\n    def get_default(\n        self, ctx: Context, call: t.Literal[True] = True\n    ) -> t.Any | None: ...\n\n    @t.overload\n    def get_default(\n        self, ctx: Context, call: bool = ...\n    ) -> t.Any | t.Callable[[], t.Any] | None: ...\n\n    def get_default(\n        self, ctx: Context, call: bool = True\n    ) -> t.Any | t.Callable[[], t.Any] | None:\n        \"\"\"Get the default for the parameter. Tries\n        :meth:`Context.lookup_default` first, then the local default.\n\n        :param ctx: Current context.\n        :param call: If the default is a callable, call it. Disable to\n            return the callable instead.\n\n        .. versionchanged:: 8.0.2\n            Type casting is no longer performed when getting a default.\n\n        .. versionchanged:: 8.0.1\n            Type casting can fail in resilient parsing mode. Invalid\n            defaults will not prevent showing help text.\n\n        .. versionchanged:: 8.0\n            Looks at ``ctx.default_map`` first.\n\n        .. versionchanged:: 8.0\n            Added the ``call`` parameter.\n        \"\"\"\n        value = ctx.lookup_default(self.name, call=False)  # type: ignore\n\n        if value is UNSET:\n            value = self.default\n\n        if call and callable(value):\n            value = value()\n\n        return value\n\n    def add_to_parser(self, parser: _OptionParser, ctx: Context) -> None:\n        raise NotImplementedError()\n\n    def consume_value(\n        self, ctx: Context, opts: cabc.Mapping[str, t.Any]\n    ) -> tuple[t.Any, ParameterSource]:\n        \"\"\"Returns the parameter value produced by the parser.\n\n        If the parser did not produce a value from user input, the value is either\n        sourced from the environment variable, the default map, or the parameter's\n        default value. In that order of precedence.\n\n        If no value is found, an internal sentinel value is returned.\n\n        :meta private:\n        \"\"\"\n        # Collect from the parse the value passed by the user to the CLI.\n        value = opts.get(self.name, UNSET)  # type: ignore\n        # If the value is set, it means it was sourced from the command line by the\n        # parser, otherwise it left unset by default.\n        source = (\n            ParameterSource.COMMANDLINE\n            if value is not UNSET\n            else ParameterSource.DEFAULT\n        )\n\n        if value is UNSET:\n            envvar_value = self.value_from_envvar(ctx)\n            if envvar_value is not None:\n                value = envvar_value\n                source = ParameterSource.ENVIRONMENT\n\n        if value is UNSET:\n            default_map_value = ctx.lookup_default(self.name)  # type: ignore\n            if default_map_value is not UNSET:\n                value = default_map_value\n                source = ParameterSource.DEFAULT_MAP\n\n        if value is UNSET:\n            default_value = self.get_default(ctx)\n            if default_value is not UNSET:\n                value = default_value\n                source = ParameterSource.DEFAULT\n\n        return value, source\n\n    def type_cast_value(self, ctx: Context, value: t.Any) -> t.Any:\n        \"\"\"Convert and validate a value against the parameter's\n        :attr:`type`, :attr:`multiple`, and :attr:`nargs`.\n        \"\"\"\n        if value is None:\n            if self.multiple or self.nargs == -1:\n                return ()\n            else:\n                return value\n\n        def check_iter(value: t.Any) -> cabc.Iterator[t.Any]:\n            try:\n                return _check_iter(value)\n            except TypeError:\n                # This should only happen when passing in args manually,\n                # the parser should construct an iterable when parsing\n                # the command line.\n                raise BadParameter(\n                    _(\"Value must be an iterable.\"), ctx=ctx, param=self\n                ) from None\n\n        # Define the conversion function based on nargs and type.\n\n        if self.nargs == 1 or self.type.is_composite:\n\n            def convert(value: t.Any) -> t.Any:\n                return self.type(value, param=self, ctx=ctx)\n\n        elif self.nargs == -1:\n\n            def convert(value: t.Any) -> t.Any:  # tuple[t.Any, ...]\n                return tuple(self.type(x, self, ctx) for x in check_iter(value))\n\n        else:  # nargs > 1\n\n            def convert(value: t.Any) -> t.Any:  # tuple[t.Any, ...]\n                value = tuple(check_iter(value))\n\n                if len(value) != self.nargs:\n                    raise BadParameter(\n                        ngettext(\n                            \"Takes {nargs} values but 1 was given.\",\n                            \"Takes {nargs} values but {len} were given.\",\n                            len(value),\n                        ).format(nargs=self.nargs, len=len(value)),\n                        ctx=ctx,\n                        param=self,\n                    )\n\n                return tuple(self.type(x, self, ctx) for x in value)\n\n        if self.multiple:\n            return tuple(convert(x) for x in check_iter(value))\n\n        return convert(value)\n\n    def value_is_missing(self, value: t.Any) -> bool:\n        \"\"\"A value is considered missing if:\n\n        - it is :attr:`UNSET`,\n        - or if it is an empty sequence while the parameter is suppose to have\n          non-single value (i.e. :attr:`nargs` is not ``1`` or :attr:`multiple` is\n          set).\n\n        :meta private:\n        \"\"\"\n        if value is UNSET:\n            return True\n\n        if (self.nargs != 1 or self.multiple) and value == ():\n            return True\n\n        return False\n\n    def process_value(self, ctx: Context, value: t.Any) -> t.Any:\n        \"\"\"Process the value of this parameter:\n\n        1. Type cast the value using :meth:`type_cast_value`.\n        2. Check if the value is missing (see: :meth:`value_is_missing`), and raise\n           :exc:`MissingParameter` if it is required.\n        3. If a :attr:`callback` is set, call it to have the value replaced by the\n           result of the callback. If the value was not set, the callback receive\n           ``None``. This keep the legacy behavior as it was before the introduction of\n           the :attr:`UNSET` sentinel.\n\n        :meta private:\n        \"\"\"\n        # shelter `type_cast_value` from ever seeing an `UNSET` value by handling the\n        # cases in which `UNSET` gets special treatment explicitly at this layer\n        #\n        # Refs:\n        # https://github.com/pallets/click/issues/3069\n        if value is UNSET:\n            if self.multiple or self.nargs == -1:\n                value = ()\n        else:\n            value = self.type_cast_value(ctx, value)\n\n        if self.required and self.value_is_missing(value):\n            raise MissingParameter(ctx=ctx, param=self)\n\n        if self.callback is not None:\n            # Legacy case: UNSET is not exposed directly to the callback, but converted\n            # to None.\n            if value is UNSET:\n                value = None\n\n            # Search for parameters with UNSET values in the context.\n            unset_keys = {k: None for k, v in ctx.params.items() if v is UNSET}\n            # No UNSET values, call the callback as usual.\n            if not unset_keys:\n                value = self.callback(ctx, self, value)\n\n            # Legacy case: provide a temporarily manipulated context to the callback\n            # to hide UNSET values as None.\n            #\n            # Refs:\n            # https://github.com/pallets/click/issues/3136\n            # https://github.com/pallets/click/pull/3137\n            else:\n                # Add another layer to the context stack to clearly hint that the\n                # context is temporarily modified.\n                with ctx:\n                    # Update the context parameters to replace UNSET with None.\n                    ctx.params.update(unset_keys)\n                    # Feed these fake context parameters to the callback.\n                    value = self.callback(ctx, self, value)\n                    # Restore the UNSET values in the context parameters.\n                    ctx.params.update(\n                        {\n                            k: UNSET\n                            for k in unset_keys\n                            # Only restore keys that are present and still None, in case\n                            # the callback modified other parameters.\n                            if k in ctx.params and ctx.params[k] is None\n                        }\n                    )\n\n        return value\n\n    def resolve_envvar_value(self, ctx: Context) -> str | None:\n        \"\"\"Returns the value found in the environment variable(s) attached to this\n        parameter.\n\n        Environment variables values are `always returned as strings\n        <https://docs.python.org/3/library/os.html#os.environ>`_.\n\n        This method returns ``None`` if:\n\n        - the :attr:`envvar` property is not set on the :class:`Parameter`,\n        - the environment variable is not found in the environment,\n        - the variable is found in the environment but its value is empty (i.e. the\n          environment variable is present but has an empty string).\n\n        If :attr:`envvar` is setup with multiple environment variables,\n        then only the first non-empty value is returned.\n\n        .. caution::\n\n            The raw value extracted from the environment is not normalized and is\n            returned as-is. Any normalization or reconciliation is performed later by\n            the :class:`Parameter`'s :attr:`type`.\n\n        :meta private:\n        \"\"\"\n        if not self.envvar:\n            return None\n\n        if isinstance(self.envvar, str):\n            rv = os.environ.get(self.envvar)\n\n            if rv:\n                return rv\n        else:\n            for envvar in self.envvar:\n                rv = os.environ.get(envvar)\n\n                # Return the first non-empty value of the list of environment variables.\n                if rv:\n                    return rv\n                # Else, absence of value is interpreted as an environment variable that\n                # is not set, so proceed to the next one.\n\n        return None\n\n    def value_from_envvar(self, ctx: Context) -> str | cabc.Sequence[str] | None:\n        \"\"\"Process the raw environment variable string for this parameter.\n\n        Returns the string as-is or splits it into a sequence of strings if the\n        parameter is expecting multiple values (i.e. its :attr:`nargs` property is set\n        to a value other than ``1``).\n\n        :meta private:\n        \"\"\"\n        rv = self.resolve_envvar_value(ctx)\n\n        if rv is not None and self.nargs != 1:\n            return self.type.split_envvar_value(rv)\n\n        return rv\n\n    def handle_parse_result(\n        self, ctx: Context, opts: cabc.Mapping[str, t.Any], args: list[str]\n    ) -> tuple[t.Any, list[str]]:\n        \"\"\"Process the value produced by the parser from user input.\n\n        Always process the value through the Parameter's :attr:`type`, wherever it\n        comes from.\n\n        If the parameter is deprecated, this method warn the user about it. But only if\n        the value has been explicitly set by the user (and as such, is not coming from\n        a default).\n\n        :meta private:\n        \"\"\"\n        with augment_usage_errors(ctx, param=self):\n            value, source = self.consume_value(ctx, opts)\n\n            ctx.set_parameter_source(self.name, source)  # type: ignore\n\n            # Display a deprecation warning if necessary.\n            if (\n                self.deprecated\n                and value is not UNSET\n                and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)\n            ):\n                extra_message = (\n                    f\" {self.deprecated}\" if isinstance(self.deprecated, str) else \"\"\n                )\n                message = _(\n                    \"DeprecationWarning: The {param_type} {name!r} is deprecated.\"\n                    \"{extra_message}\"\n                ).format(\n                    param_type=self.param_type_name,\n                    name=self.human_readable_name,\n                    extra_message=extra_message,\n                )\n                echo(style(message, fg=\"red\"), err=True)\n\n            # Process the value through the parameter's type.\n            try:\n                value = self.process_value(ctx, value)\n            except Exception:\n                if not ctx.resilient_parsing:\n                    raise\n                # In resilient parsing mode, we do not want to fail the command if the\n                # value is incompatible with the parameter type, so we reset the value\n                # to UNSET, which will be interpreted as a missing value.\n                value = UNSET\n\n        # Add parameter's value to the context.\n        if (\n            self.expose_value\n            # We skip adding the value if it was previously set by another parameter\n            # targeting the same variable name. This prevents parameters competing for\n            # the same name to override each other.\n            and (self.name not in ctx.params or ctx.params[self.name] is UNSET)\n        ):\n            # Click is logically enforcing that the name is None if the parameter is\n            # not to be exposed. We still assert it here to please the type checker.\n            assert self.name is not None, (\n                f\"{self!r} parameter's name should not be None when exposing value.\"\n            )\n            ctx.params[self.name] = value\n\n        return value, args\n\n    def get_help_record(self, ctx: Context) -> tuple[str, str] | None:\n        pass\n\n    def get_usage_pieces(self, ctx: Context) -> list[str]:\n        return []\n\n    def get_error_hint(self, ctx: Context) -> str:\n        \"\"\"Get a stringified version of the param for use in error messages to\n        indicate which param caused the error.\n        \"\"\"\n        hint_list = self.opts or [self.human_readable_name]\n        return \" / \".join(f\"'{x}'\" for x in hint_list)\n\n    def shell_complete(self, ctx: Context, incomplete: str) -> list[CompletionItem]:\n        \"\"\"Return a list of completions for the incomplete value. If a\n        ``shell_complete`` function was given during init, it is used.\n        Otherwise, the :attr:`type`\n        :meth:`~click.types.ParamType.shell_complete` function is used.\n\n        :param ctx: Invocation context for this command.\n        :param incomplete: Value being completed. May be empty.\n\n        .. versionadded:: 8.0\n        \"\"\"\n        if self._custom_shell_complete is not None:\n            results = self._custom_shell_complete(ctx, self, incomplete)\n\n            if results and isinstance(results[0], str):\n                from click.shell_completion import CompletionItem\n\n                results = [CompletionItem(c) for c in results]\n\n            return t.cast(\"list[CompletionItem]\", results)\n\n        return self.type.shell_complete(ctx, self, incomplete)\n\n\nclass Option(Parameter):\n    \"\"\"Options are usually optional values on the command line and\n    have some extra features that arguments don't have.\n\n    All other parameters are passed onwards to the parameter constructor.\n\n    :param show_default: Show the default value for this option in its\n        help text. Values are not shown by default, unless\n        :attr:`Context.show_default` is ``True``. If this value is a\n        string, it shows that string in parentheses instead of the\n        actual value. This is particularly useful for dynamic options.\n        For single option boolean flags, the default remains hidden if\n        its value is ``False``.\n    :param show_envvar: Controls if an environment variable should be\n        shown on the help page and error messages.\n        Normally, environment variables are not shown.\n    :param prompt: If set to ``True`` or a non empty string then the\n        user will be prompted for input. If set to ``True`` the prompt\n        will be the option name capitalized. A deprecated option cannot be\n        prompted.\n    :param confirmation_prompt: Prompt a second time to confirm the\n        value if it was prompted for. Can be set to a string instead of\n        ``True`` to customize the message.\n    :param prompt_required: If set to ``False``, the user will be\n        prompted for input only when the option was specified as a flag\n        without a value.\n    :param hide_input: If this is ``True`` then the input on the prompt\n        will be hidden from the user. This is useful for password input.\n    :param is_flag: forces this option to act as a flag.  The default is\n                    auto detection.\n    :param flag_value: which value should be used for this flag if it's\n                       enabled.  This is set to a boolean automatically if\n                       the option string contains a slash to mark two options.\n    :param multiple: if this is set to `True` then the argument is accepted\n                     multiple times and recorded.  This is similar to ``nargs``\n                     in how it works but supports arbitrary number of\n                     arguments.\n    :param count: this flag makes an option increment an integer.\n    :param allow_from_autoenv: if this is enabled then the value of this\n                               parameter will be pulled from an environment\n                               variable in case a prefix is defined on the\n                               context.\n    :param help: the help string.\n    :param hidden: hide this option from help outputs.\n    :param attrs: Other command arguments described in :class:`Parameter`.\n\n    .. versionchanged:: 8.2\n        ``envvar`` used with ``flag_value`` will always use the ``flag_value``,\n        previously it would use the value of the environment variable.\n\n    .. versionchanged:: 8.1\n        Help text indentation is cleaned here instead of only in the\n        ``@option`` decorator.\n\n    .. versionchanged:: 8.1\n        The ``show_default`` parameter overrides\n        ``Context.show_default``.\n\n    .. versionchanged:: 8.1\n        The default of a single option boolean flag is not shown if the\n        default value is ``False``.\n\n    .. versionchanged:: 8.0.1\n        ``type`` is detected from ``flag_value`` if given.\n    \"\"\"\n\n    param_type_name = \"option\"\n\n    def __init__(\n        self,\n        param_decls: cabc.Sequence[str] | None = None,\n        show_default: bool | str | None = None,\n        prompt: bool | str = False,\n        confirmation_prompt: bool | str = False,\n        prompt_required: bool = True,\n        hide_input: bool = False,\n        is_flag: bool | None = None,\n        flag_value: t.Any = UNSET,\n        multiple: bool = False,\n        count: bool = False,\n        allow_from_autoenv: bool = True,\n        type: types.ParamType | t.Any | None = None,\n        help: str | None = None,\n        hidden: bool = False,\n        show_choices: bool = True,\n        show_envvar: bool = False,\n        deprecated: bool | str = False,\n        **attrs: t.Any,\n    ) -> None:\n        if help:\n            help = inspect.cleandoc(help)\n\n        super().__init__(\n            param_decls, type=type, multiple=multiple, deprecated=deprecated, **attrs\n        )\n\n        if prompt is True:\n            if self.name is None:\n                raise TypeError(\"'name' is required with 'prompt=True'.\")\n\n            prompt_text: str | None = self.name.replace(\"_\", \" \").capitalize()\n        elif prompt is False:\n            prompt_text = None\n        else:\n            prompt_text = prompt\n\n        if deprecated:\n            deprecated_message = (\n                f\"(DEPRECATED: {deprecated})\"\n                if isinstance(deprecated, str)\n                else \"(DEPRECATED)\"\n            )\n            help = help + deprecated_message if help is not None else deprecated_message\n\n        self.prompt = prompt_text\n        self.confirmation_prompt = confirmation_prompt\n        self.prompt_required = prompt_required\n        self.hide_input = hide_input\n        self.hidden = hidden\n\n        # The _flag_needs_value property tells the parser that this option is a flag\n        # that cannot be used standalone and needs a value. With this information, the\n        # parser can determine whether to consider the next user-provided argument in\n        # the CLI as a value for this flag or as a new option.\n        # If prompt is enabled but not required, then it opens the possibility for the\n        # option to gets its value from the user.\n        self._flag_needs_value = self.prompt is not None and not self.prompt_required\n\n        # Auto-detect if this is a flag or not.\n        if is_flag is None:\n            # Implicitly a flag because flag_value was set.\n            if flag_value is not UNSET:\n                is_flag = True\n            # Not a flag, but when used as a flag it shows a prompt.\n            elif self._flag_needs_value:\n                is_flag = False\n            # Implicitly a flag because secondary options names were given.\n            elif self.secondary_opts:\n                is_flag = True\n\n        # The option is explicitly not a flag, but to determine whether or not it needs\n        # value, we need to check if `flag_value` or `default` was set. Either one is\n        # sufficient.\n        # Ref: https://github.com/pallets/click/issues/3084\n        elif is_flag is False and not self._flag_needs_value:\n            self._flag_needs_value = flag_value is not UNSET or self.default is UNSET\n\n        if is_flag:\n            # Set missing default for flags if not explicitly required or prompted.\n            if self.default is UNSET and not self.required and not self.prompt:\n                if multiple:\n                    self.default = ()\n\n            # Auto-detect the type of the flag based on the flag_value.\n            if type is None:\n                # A flag without a flag_value is a boolean flag.\n                if flag_value is UNSET:\n                    self.type: types.ParamType = types.BoolParamType()\n                # If the flag value is a boolean, use BoolParamType.\n                elif isinstance(flag_value, bool):\n                    self.type = types.BoolParamType()\n                # Otherwise, guess the type from the flag value.\n                else:\n                    self.type = types.convert_type(None, flag_value)\n\n        self.is_flag: bool = bool(is_flag)\n        self.is_bool_flag: bool = bool(\n            is_flag and isinstance(self.type, types.BoolParamType)\n        )\n        self.flag_value: t.Any = flag_value\n\n        # Set boolean flag default to False if unset and not required.\n        if self.is_bool_flag:\n            if self.default is UNSET and not self.required:\n                self.default = False\n\n        # Support the special case of aligning the default value with the flag_value\n        # for flags whose default is explicitly set to True. Note that as long as we\n        # have this condition, there is no way a flag can have a default set to True,\n        # and a flag_value set to something else. Refs:\n        # https://github.com/pallets/click/issues/3024#issuecomment-3146199461\n        # https://github.com/pallets/click/pull/3030/commits/06847da\n        if self.default is True and self.flag_value is not UNSET:\n            self.default = self.flag_value\n\n        # Set the default flag_value if it is not set.\n        if self.flag_value is UNSET:\n            if self.is_flag:\n                self.flag_value = True\n            else:\n                self.flag_value = None\n\n        # Counting.\n        self.count = count\n        if count:\n            if type is None:\n                self.type = types.IntRange(min=0)\n            if self.default is UNSET:\n                self.default = 0\n\n        self.allow_from_autoenv = allow_from_autoenv\n        self.help = help\n        self.show_default = show_default\n        self.show_choices = show_choices\n        self.show_envvar = show_envvar\n\n        if __debug__:\n            if deprecated and prompt:\n                raise ValueError(\"`deprecated` options cannot use `prompt`.\")\n\n            if self.nargs == -1:\n                raise TypeError(\"nargs=-1 is not supported for options.\")\n\n            if not self.is_bool_flag and self.secondary_opts:\n                raise TypeError(\"Secondary flag is not valid for non-boolean flag.\")\n\n            if self.is_bool_flag and self.hide_input and self.prompt is not None:\n                raise TypeError(\n                    \"'prompt' with 'hide_input' is not valid for boolean flag.\"\n                )\n\n            if self.count:\n                if self.multiple:\n                    raise TypeError(\"'count' is not valid with 'multiple'.\")\n\n                if self.is_flag:\n                    raise TypeError(\"'count' is not valid with 'is_flag'.\")\n\n    def to_info_dict(self) -> dict[str, t.Any]:\n        \"\"\"\n        .. versionchanged:: 8.3.0\n            Returns ``None`` for the :attr:`flag_value` if it was not set.\n        \"\"\"\n        info_dict = super().to_info_dict()\n        info_dict.update(\n            help=self.help,\n            prompt=self.prompt,\n            is_flag=self.is_flag,\n            # We explicitly hide the :attr:`UNSET` value to the user, as we choose to\n            # make it an implementation detail. And because ``to_info_dict`` has been\n            # designed for documentation purposes, we return ``None`` instead.\n            flag_value=self.flag_value if self.flag_value is not UNSET else None,\n            count=self.count,\n            hidden=self.hidden,\n        )\n        return info_dict\n\n    def get_error_hint(self, ctx: Context) -> str:\n        result = super().get_error_hint(ctx)\n        if self.show_envvar and self.envvar is not None:\n            result += f\" (env var: '{self.envvar}')\"\n        return result\n\n    def _parse_decls(\n        self, decls: cabc.Sequence[str], expose_value: bool\n    ) -> tuple[str | None, list[str], list[str]]:\n        opts = []\n        secondary_opts = []\n        name = None\n        possible_names = []\n\n        for decl in decls:\n            if decl.isidentifier():\n                if name is not None:\n                    raise TypeError(f\"Name '{name}' defined twice\")\n                name = decl\n            else:\n                split_char = \";\" if decl[:1] == \"/\" else \"/\"\n                if split_char in decl:\n                    first, second = decl.split(split_char, 1)\n                    first = first.rstrip()\n                    if first:\n                        possible_names.append(_split_opt(first))\n                        opts.append(first)\n                    second = second.lstrip()\n                    if second:\n                        secondary_opts.append(second.lstrip())\n                    if first == second:\n                        raise ValueError(\n                            f\"Boolean option {decl!r} cannot use the\"\n                            \" same flag for true/false.\"\n                        )\n                else:\n                    possible_names.append(_split_opt(decl))\n                    opts.append(decl)\n\n        if name is None and possible_names:\n            possible_names.sort(key=lambda x: -len(x[0]))  # group long options first\n            name = possible_names[0][1].replace(\"-\", \"_\").lower()\n            if not name.isidentifier():\n                name = None\n\n        if name is None:\n            if not expose_value:\n                return None, opts, secondary_opts\n            raise TypeError(\n                f\"Could not determine name for option with declarations {decls!r}\"\n            )\n\n        if not opts and not secondary_opts:\n            raise TypeError(\n                f\"No options defined but a name was passed ({name}).\"\n                \" Did you mean to declare an argument instead? Did\"\n                f\" you mean to pass '--{name}'?\"\n            )\n\n        return name, opts, secondary_opts\n\n    def add_to_parser(self, parser: _OptionParser, ctx: Context) -> None:\n        if self.multiple:\n            action = \"append\"\n        elif self.count:\n            action = \"count\"\n        else:\n            action = \"store\"\n\n        if self.is_flag:\n            action = f\"{action}_const\"\n\n            if self.is_bool_flag and self.secondary_opts:\n                parser.add_option(\n                    obj=self, opts=self.opts, dest=self.name, action=action, const=True\n                )\n                parser.add_option(\n                    obj=self,\n                    opts=self.secondary_opts,\n                    dest=self.name,\n                    action=action,\n                    const=False,\n                )\n            else:\n                parser.add_option(\n                    obj=self,\n                    opts=self.opts,\n                    dest=self.name,\n                    action=action,\n                    const=self.flag_value,\n                )\n        else:\n            parser.add_option(\n                obj=self,\n                opts=self.opts,\n                dest=self.name,\n                action=action,\n                nargs=self.nargs,\n            )\n\n    def get_help_record(self, ctx: Context) -> tuple[str, str] | None:\n        if self.hidden:\n            return None\n\n        any_prefix_is_slash = False\n\n        def _write_opts(opts: cabc.Sequence[str]) -> str:\n            nonlocal any_prefix_is_slash\n\n            rv, any_slashes = join_options(opts)\n\n            if any_slashes:\n                any_prefix_is_slash = True\n\n            if not self.is_flag and not self.count:\n                rv += f\" {self.make_metavar(ctx=ctx)}\"\n\n            return rv\n\n        rv = [_write_opts(self.opts)]\n\n        if self.secondary_opts:\n            rv.append(_write_opts(self.secondary_opts))\n\n        help = self.help or \"\"\n\n        extra = self.get_help_extra(ctx)\n        extra_items = []\n        if \"envvars\" in extra:\n            extra_items.append(\n                _(\"env var: {var}\").format(var=\", \".join(extra[\"envvars\"]))\n            )\n        if \"default\" in extra:\n            extra_items.append(_(\"default: {default}\").format(default=extra[\"default\"]))\n        if \"range\" in extra:\n            extra_items.append(extra[\"range\"])\n        if \"required\" in extra:\n            extra_items.append(_(extra[\"required\"]))\n\n        if extra_items:\n            extra_str = \"; \".join(extra_items)\n            help = f\"{help}  [{extra_str}]\" if help else f\"[{extra_str}]\"\n\n        return (\"; \" if any_prefix_is_slash else \" / \").join(rv), help\n\n    def get_help_extra(self, ctx: Context) -> types.OptionHelpExtra:\n        extra: types.OptionHelpExtra = {}\n\n        if self.show_envvar:\n            envvar = self.envvar\n\n            if envvar is None:\n                if (\n                    self.allow_from_autoenv\n                    and ctx.auto_envvar_prefix is not None\n                    and self.name is not None\n                ):\n                    envvar = f\"{ctx.auto_envvar_prefix}_{self.name.upper()}\"\n\n            if envvar is not None:\n                if isinstance(envvar, str):\n                    extra[\"envvars\"] = (envvar,)\n                else:\n                    extra[\"envvars\"] = tuple(str(d) for d in envvar)\n\n        # Temporarily enable resilient parsing to avoid type casting\n        # failing for the default. Might be possible to extend this to\n        # help formatting in general.\n        resilient = ctx.resilient_parsing\n        ctx.resilient_parsing = True\n\n        try:\n            default_value = self.get_default(ctx, call=False)\n        finally:\n            ctx.resilient_parsing = resilient\n\n        show_default = False\n        show_default_is_str = False\n\n        if self.show_default is not None:\n            if isinstance(self.show_default, str):\n                show_default_is_str = show_default = True\n            else:\n                show_default = self.show_default\n        elif ctx.show_default is not None:\n            show_default = ctx.show_default\n\n        if show_default_is_str or (\n            show_default and (default_value not in (None, UNSET))\n        ):\n            if show_default_is_str:\n                default_string = f\"({self.show_default})\"\n            elif isinstance(default_value, (list, tuple)):\n                default_string = \", \".join(str(d) for d in default_value)\n            elif isinstance(default_value, enum.Enum):\n                default_string = default_value.name\n            elif inspect.isfunction(default_value):\n                default_string = _(\"(dynamic)\")\n            elif self.is_bool_flag and self.secondary_opts:\n                # For boolean flags that have distinct True/False opts,\n                # use the opt without prefix instead of the value.\n                default_string = _split_opt(\n                    (self.opts if default_value else self.secondary_opts)[0]\n                )[1]\n            elif self.is_bool_flag and not self.secondary_opts and not default_value:\n                default_string = \"\"\n            elif default_value == \"\":\n                default_string = '\"\"'\n            else:\n                default_string = str(default_value)\n\n            if default_string:\n                extra[\"default\"] = default_string\n\n        if (\n            isinstance(self.type, types._NumberRangeBase)\n            # skip count with default range type\n            and not (self.count and self.type.min == 0 and self.type.max is None)\n        ):\n            range_str = self.type._describe_range()\n\n            if range_str:\n                extra[\"range\"] = range_str\n\n        if self.required:\n            extra[\"required\"] = \"required\"\n\n        return extra\n\n    def prompt_for_value(self, ctx: Context) -> t.Any:\n        \"\"\"This is an alternative flow that can be activated in the full\n        value processing if a value does not exist.  It will prompt the\n        user until a valid value exists and then returns the processed\n        value as result.\n        \"\"\"\n        assert self.prompt is not None\n\n        # Calculate the default before prompting anything to lock in the value before\n        # attempting any user interaction.\n        default = self.get_default(ctx)\n\n        # A boolean flag can use a simplified [y/n] confirmation prompt.\n        if self.is_bool_flag:\n            # If we have no boolean default, we force the user to explicitly provide\n            # one.\n            if default in (UNSET, None):\n                default = None\n            # Nothing prevent you to declare an option that is simultaneously:\n            # 1) auto-detected as a boolean flag,\n            # 2) allowed to prompt, and\n            # 3) still declare a non-boolean default.\n            # This forced casting into a boolean is necessary to align any non-boolean\n            # default to the prompt, which is going to be a [y/n]-style confirmation\n            # because the option is still a boolean flag. That way, instead of [y/n],\n            # we get [Y/n] or [y/N] depending on the truthy value of the default.\n            # Refs: https://github.com/pallets/click/pull/3030#discussion_r2289180249\n            else:\n                default = bool(default)\n            return confirm(self.prompt, default)\n\n        # If show_default is set to True/False, provide this to `prompt` as well. For\n        # non-bool values of `show_default`, we use `prompt`'s default behavior\n        prompt_kwargs: t.Any = {}\n        if isinstance(self.show_default, bool):\n            prompt_kwargs[\"show_default\"] = self.show_default\n\n        return prompt(\n            self.prompt,\n            # Use ``None`` to inform the prompt() function to reiterate until a valid\n            # value is provided by the user if we have no default.\n            default=None if default is UNSET else default,\n            type=self.type,\n            hide_input=self.hide_input,\n            show_choices=self.show_choices,\n            confirmation_prompt=self.confirmation_prompt,\n            value_proc=lambda x: self.process_value(ctx, x),\n            **prompt_kwargs,\n        )\n\n    def resolve_envvar_value(self, ctx: Context) -> str | None:\n        \"\"\":class:`Option` resolves its environment variable the same way as\n        :func:`Parameter.resolve_envvar_value`, but it also supports\n        :attr:`Context.auto_envvar_prefix`. If we could not find an environment from\n        the :attr:`envvar` property, we fallback on :attr:`Context.auto_envvar_prefix`\n        to build dynamiccaly the environment variable name using the\n        :python:`{ctx.auto_envvar_prefix}_{self.name.upper()}` template.\n\n        :meta private:\n        \"\"\"\n        rv = super().resolve_envvar_value(ctx)\n\n        if rv is not None:\n            return rv\n\n        if (\n            self.allow_from_autoenv\n            and ctx.auto_envvar_prefix is not None\n            and self.name is not None\n        ):\n            envvar = f\"{ctx.auto_envvar_prefix}_{self.name.upper()}\"\n            rv = os.environ.get(envvar)\n\n            if rv:\n                return rv\n\n        return None\n\n    def value_from_envvar(self, ctx: Context) -> t.Any:\n        \"\"\"For :class:`Option`, this method processes the raw environment variable\n        string the same way as :func:`Parameter.value_from_envvar` does.\n\n        But in the case of non-boolean flags, the value is analyzed to determine if the\n        flag is activated or not, and returns a boolean of its activation, or the\n        :attr:`flag_value` if the latter is set.\n\n        This method also takes care of repeated options (i.e. options with\n        :attr:`multiple` set to ``True``).\n\n        :meta private:\n        \"\"\"\n        rv = self.resolve_envvar_value(ctx)\n\n        # Absent environment variable or an empty string is interpreted as unset.\n        if rv is None:\n            return None\n\n        # Non-boolean flags are more liberal in what they accept. But a flag being a\n        # flag, its envvar value still needs to be analyzed to determine if the flag is\n        # activated or not.\n        if self.is_flag and not self.is_bool_flag:\n            # If the flag_value is set and match the envvar value, return it\n            # directly.\n            if self.flag_value is not UNSET and rv == self.flag_value:\n                return self.flag_value\n            # Analyze the envvar value as a boolean to know if the flag is\n            # activated or not.\n            return types.BoolParamType.str_to_bool(rv)\n\n        # Split the envvar value if it is allowed to be repeated.\n        value_depth = (self.nargs != 1) + bool(self.multiple)\n        if value_depth > 0:\n            multi_rv = self.type.split_envvar_value(rv)\n            if self.multiple and self.nargs != 1:\n                multi_rv = batch(multi_rv, self.nargs)  # type: ignore[assignment]\n\n            return multi_rv\n\n        return rv\n\n    def consume_value(\n        self, ctx: Context, opts: cabc.Mapping[str, Parameter]\n    ) -> tuple[t.Any, ParameterSource]:\n        \"\"\"For :class:`Option`, the value can be collected from an interactive prompt\n        if the option is a flag that needs a value (and the :attr:`prompt` property is\n        set).\n\n        Additionally, this method handles flag option that are activated without a\n        value, in which case the :attr:`flag_value` is returned.\n\n        :meta private:\n        \"\"\"\n        value, source = super().consume_value(ctx, opts)\n\n        # The parser will emit a sentinel value if the option is allowed to as a flag\n        # without a value.\n        if value is FLAG_NEEDS_VALUE:\n            # If the option allows for a prompt, we start an interaction with the user.\n            if self.prompt is not None and not ctx.resilient_parsing:\n                value = self.prompt_for_value(ctx)\n                source = ParameterSource.PROMPT\n            # Else the flag takes its flag_value as value.\n            else:\n                value = self.flag_value\n                source = ParameterSource.COMMANDLINE\n\n        # A flag which is activated always returns the flag value, unless the value\n        # comes from the explicitly sets default.\n        elif (\n            self.is_flag\n            and value is True\n            and not self.is_bool_flag\n            and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)\n        ):\n            value = self.flag_value\n\n        # Re-interpret a multiple option which has been sent as-is by the parser.\n        # Here we replace each occurrence of value-less flags (marked by the\n        # FLAG_NEEDS_VALUE sentinel) with the flag_value.\n        elif (\n            self.multiple\n            and value is not UNSET\n            and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)\n            and any(v is FLAG_NEEDS_VALUE for v in value)\n        ):\n            value = [self.flag_value if v is FLAG_NEEDS_VALUE else v for v in value]\n            source = ParameterSource.COMMANDLINE\n\n        # The value wasn't set, or used the param's default, prompt for one to the user\n        # if prompting is enabled.\n        elif (\n            (\n                value is UNSET\n                or source in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)\n            )\n            and self.prompt is not None\n            and (self.required or self.prompt_required)\n            and not ctx.resilient_parsing\n        ):\n            value = self.prompt_for_value(ctx)\n            source = ParameterSource.PROMPT\n\n        return value, source\n\n    def process_value(self, ctx: Context, value: t.Any) -> t.Any:\n        # process_value has to be overridden on Options in order to capture\n        # `value == UNSET` cases before `type_cast_value()` gets called.\n        #\n        # Refs:\n        # https://github.com/pallets/click/issues/3069\n        if self.is_flag and not self.required and self.is_bool_flag and value is UNSET:\n            value = False\n\n            if self.callback is not None:\n                value = self.callback(ctx, self, value)\n\n            return value\n\n        # in the normal case, rely on Parameter.process_value\n        return super().process_value(ctx, value)\n\n\nclass Argument(Parameter):\n    \"\"\"Arguments are positional parameters to a command.  They generally\n    provide fewer features than options but can have infinite ``nargs``\n    and are required by default.\n\n    All parameters are passed onwards to the constructor of :class:`Parameter`.\n    \"\"\"\n\n    param_type_name = \"argument\"\n\n    def __init__(\n        self,\n        param_decls: cabc.Sequence[str],\n        required: bool | None = None,\n        **attrs: t.Any,\n    ) -> None:\n        # Auto-detect the requirement status of the argument if not explicitly set.\n        if required is None:\n            # The argument gets automatically required if it has no explicit default\n            # value set and is setup to match at least one value.\n            if attrs.get(\"default\", UNSET) is UNSET:\n                required = attrs.get(\"nargs\", 1) > 0\n            # If the argument has a default value, it is not required.\n            else:\n                required = False\n\n        if \"multiple\" in attrs:\n            raise TypeError(\"__init__() got an unexpected keyword argument 'multiple'.\")\n\n        super().__init__(param_decls, required=required, **attrs)\n\n    @property\n    def human_readable_name(self) -> str:\n        if self.metavar is not None:\n            return self.metavar\n        return self.name.upper()  # type: ignore\n\n    def make_metavar(self, ctx: Context) -> str:\n        if self.metavar is not None:\n            return self.metavar\n        var = self.type.get_metavar(param=self, ctx=ctx)\n        if not var:\n            var = self.name.upper()  # type: ignore\n        if self.deprecated:\n            var += \"!\"\n        if not self.required:\n            var = f\"[{var}]\"\n        if self.nargs != 1:\n            var += \"...\"\n        return var\n\n    def _parse_decls(\n        self, decls: cabc.Sequence[str], expose_value: bool\n    ) -> tuple[str | None, list[str], list[str]]:\n        if not decls:\n            if not expose_value:\n                return None, [], []\n            raise TypeError(\"Argument is marked as exposed, but does not have a name.\")\n        if len(decls) == 1:\n            name = arg = decls[0]\n            name = name.replace(\"-\", \"_\").lower()\n        else:\n            raise TypeError(\n                \"Arguments take exactly one parameter declaration, got\"\n                f\" {len(decls)}: {decls}.\"\n            )\n        return name, [arg], []\n\n    def get_usage_pieces(self, ctx: Context) -> list[str]:\n        return [self.make_metavar(ctx)]\n\n    def get_error_hint(self, ctx: Context) -> str:\n        return f\"'{self.make_metavar(ctx)}'\"\n\n    def add_to_parser(self, parser: _OptionParser, ctx: Context) -> None:\n        parser.add_argument(dest=self.name, nargs=self.nargs, obj=self)\n\n\ndef __getattr__(name: str) -> object:\n    import warnings\n\n    if name == \"BaseCommand\":\n        warnings.warn(\n            \"'BaseCommand' is deprecated and will be removed in Click 9.0. Use\"\n            \" 'Command' instead.\",\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return _BaseCommand\n\n    if name == \"MultiCommand\":\n        warnings.warn(\n            \"'MultiCommand' is deprecated and will be removed in Click 9.0. Use\"\n            \" 'Group' instead.\",\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return _MultiCommand\n\n    raise AttributeError(name)\n"
  },
  {
    "path": "src/click/decorators.py",
    "content": "from __future__ import annotations\n\nimport inspect\nimport typing as t\nfrom functools import update_wrapper\nfrom gettext import gettext as _\n\nfrom .core import Argument\nfrom .core import Command\nfrom .core import Context\nfrom .core import Group\nfrom .core import Option\nfrom .core import Parameter\nfrom .globals import get_current_context\nfrom .utils import echo\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    P = te.ParamSpec(\"P\")\n\nR = t.TypeVar(\"R\")\nT = t.TypeVar(\"T\")\n_AnyCallable = t.Callable[..., t.Any]\nFC = t.TypeVar(\"FC\", bound=\"_AnyCallable | Command\")\n\n\ndef pass_context(f: t.Callable[te.Concatenate[Context, P], R]) -> t.Callable[P, R]:\n    \"\"\"Marks a callback as wanting to receive the current context\n    object as first argument.\n    \"\"\"\n\n    def new_func(*args: P.args, **kwargs: P.kwargs) -> R:\n        return f(get_current_context(), *args, **kwargs)\n\n    return update_wrapper(new_func, f)\n\n\ndef pass_obj(f: t.Callable[te.Concatenate[T, P], R]) -> t.Callable[P, R]:\n    \"\"\"Similar to :func:`pass_context`, but only pass the object on the\n    context onwards (:attr:`Context.obj`).  This is useful if that object\n    represents the state of a nested system.\n    \"\"\"\n\n    def new_func(*args: P.args, **kwargs: P.kwargs) -> R:\n        return f(get_current_context().obj, *args, **kwargs)\n\n    return update_wrapper(new_func, f)\n\n\ndef make_pass_decorator(\n    object_type: type[T], ensure: bool = False\n) -> t.Callable[[t.Callable[te.Concatenate[T, P], R]], t.Callable[P, R]]:\n    \"\"\"Given an object type this creates a decorator that will work\n    similar to :func:`pass_obj` but instead of passing the object of the\n    current context, it will find the innermost context of type\n    :func:`object_type`.\n\n    This generates a decorator that works roughly like this::\n\n        from functools import update_wrapper\n\n        def decorator(f):\n            @pass_context\n            def new_func(ctx, *args, **kwargs):\n                obj = ctx.find_object(object_type)\n                return ctx.invoke(f, obj, *args, **kwargs)\n            return update_wrapper(new_func, f)\n        return decorator\n\n    :param object_type: the type of the object to pass.\n    :param ensure: if set to `True`, a new object will be created and\n                   remembered on the context if it's not there yet.\n    \"\"\"\n\n    def decorator(f: t.Callable[te.Concatenate[T, P], R]) -> t.Callable[P, R]:\n        def new_func(*args: P.args, **kwargs: P.kwargs) -> R:\n            ctx = get_current_context()\n\n            obj: T | None\n            if ensure:\n                obj = ctx.ensure_object(object_type)\n            else:\n                obj = ctx.find_object(object_type)\n\n            if obj is None:\n                raise RuntimeError(\n                    \"Managed to invoke callback without a context\"\n                    f\" object of type {object_type.__name__!r}\"\n                    \" existing.\"\n                )\n\n            return ctx.invoke(f, obj, *args, **kwargs)\n\n        return update_wrapper(new_func, f)\n\n    return decorator\n\n\ndef pass_meta_key(\n    key: str, *, doc_description: str | None = None\n) -> t.Callable[[t.Callable[te.Concatenate[T, P], R]], t.Callable[P, R]]:\n    \"\"\"Create a decorator that passes a key from\n    :attr:`click.Context.meta` as the first argument to the decorated\n    function.\n\n    :param key: Key in ``Context.meta`` to pass.\n    :param doc_description: Description of the object being passed,\n        inserted into the decorator's docstring. Defaults to \"the 'key'\n        key from Context.meta\".\n\n    .. versionadded:: 8.0\n    \"\"\"\n\n    def decorator(f: t.Callable[te.Concatenate[T, P], R]) -> t.Callable[P, R]:\n        def new_func(*args: P.args, **kwargs: P.kwargs) -> R:\n            ctx = get_current_context()\n            obj = ctx.meta[key]\n            return ctx.invoke(f, obj, *args, **kwargs)\n\n        return update_wrapper(new_func, f)\n\n    if doc_description is None:\n        doc_description = f\"the {key!r} key from :attr:`click.Context.meta`\"\n\n    decorator.__doc__ = (\n        f\"Decorator that passes {doc_description} as the first argument\"\n        \" to the decorated function.\"\n    )\n    return decorator\n\n\nCmdType = t.TypeVar(\"CmdType\", bound=Command)\n\n\n# variant: no call, directly as decorator for a function.\n@t.overload\ndef command(name: _AnyCallable) -> Command: ...\n\n\n# variant: with positional name and with positional or keyword cls argument:\n# @command(namearg, CommandCls, ...) or @command(namearg, cls=CommandCls, ...)\n@t.overload\ndef command(\n    name: str | None,\n    cls: type[CmdType],\n    **attrs: t.Any,\n) -> t.Callable[[_AnyCallable], CmdType]: ...\n\n\n# variant: name omitted, cls _must_ be a keyword argument, @command(cls=CommandCls, ...)\n@t.overload\ndef command(\n    name: None = None,\n    *,\n    cls: type[CmdType],\n    **attrs: t.Any,\n) -> t.Callable[[_AnyCallable], CmdType]: ...\n\n\n# variant: with optional string name, no cls argument provided.\n@t.overload\ndef command(\n    name: str | None = ..., cls: None = None, **attrs: t.Any\n) -> t.Callable[[_AnyCallable], Command]: ...\n\n\ndef command(\n    name: str | _AnyCallable | None = None,\n    cls: type[CmdType] | None = None,\n    **attrs: t.Any,\n) -> Command | t.Callable[[_AnyCallable], Command | CmdType]:\n    r\"\"\"Creates a new :class:`Command` and uses the decorated function as\n    callback.  This will also automatically attach all decorated\n    :func:`option`\\s and :func:`argument`\\s as parameters to the command.\n\n    The name of the command defaults to the name of the function, converted to\n    lowercase, with underscores ``_`` replaced by dashes ``-``, and the suffixes\n    ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed. For example,\n    ``init_data_command`` becomes ``init-data``.\n\n    All keyword arguments are forwarded to the underlying command class.\n    For the ``params`` argument, any decorated params are appended to\n    the end of the list.\n\n    Once decorated the function turns into a :class:`Command` instance\n    that can be invoked as a command line utility or be attached to a\n    command :class:`Group`.\n\n    :param name: The name of the command. Defaults to modifying the function's\n        name as described above.\n    :param cls: The command class to create. Defaults to :class:`Command`.\n\n    .. versionchanged:: 8.2\n        The suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are\n        removed when generating the name.\n\n    .. versionchanged:: 8.1\n        This decorator can be applied without parentheses.\n\n    .. versionchanged:: 8.1\n        The ``params`` argument can be used. Decorated params are\n        appended to the end of the list.\n    \"\"\"\n\n    func: t.Callable[[_AnyCallable], t.Any] | None = None\n\n    if callable(name):\n        func = name\n        name = None\n        assert cls is None, \"Use 'command(cls=cls)(callable)' to specify a class.\"\n        assert not attrs, \"Use 'command(**kwargs)(callable)' to provide arguments.\"\n\n    if cls is None:\n        cls = t.cast(\"type[CmdType]\", Command)\n\n    def decorator(f: _AnyCallable) -> CmdType:\n        if isinstance(f, Command):\n            raise TypeError(\"Attempted to convert a callback into a command twice.\")\n\n        attr_params = attrs.pop(\"params\", None)\n        params = attr_params if attr_params is not None else []\n\n        try:\n            decorator_params = f.__click_params__  # type: ignore\n        except AttributeError:\n            pass\n        else:\n            del f.__click_params__  # type: ignore\n            params.extend(reversed(decorator_params))\n\n        if attrs.get(\"help\") is None:\n            attrs[\"help\"] = f.__doc__\n\n        if t.TYPE_CHECKING:\n            assert cls is not None\n            assert not callable(name)\n\n        if name is not None:\n            cmd_name = name\n        else:\n            cmd_name = f.__name__.lower().replace(\"_\", \"-\")\n            cmd_left, sep, suffix = cmd_name.rpartition(\"-\")\n\n            if sep and suffix in {\"command\", \"cmd\", \"group\", \"grp\"}:\n                cmd_name = cmd_left\n\n        cmd = cls(name=cmd_name, callback=f, params=params, **attrs)\n        cmd.__doc__ = f.__doc__\n        return cmd\n\n    if func is not None:\n        return decorator(func)\n\n    return decorator\n\n\nGrpType = t.TypeVar(\"GrpType\", bound=Group)\n\n\n# variant: no call, directly as decorator for a function.\n@t.overload\ndef group(name: _AnyCallable) -> Group: ...\n\n\n# variant: with positional name and with positional or keyword cls argument:\n# @group(namearg, GroupCls, ...) or @group(namearg, cls=GroupCls, ...)\n@t.overload\ndef group(\n    name: str | None,\n    cls: type[GrpType],\n    **attrs: t.Any,\n) -> t.Callable[[_AnyCallable], GrpType]: ...\n\n\n# variant: name omitted, cls _must_ be a keyword argument, @group(cmd=GroupCls, ...)\n@t.overload\ndef group(\n    name: None = None,\n    *,\n    cls: type[GrpType],\n    **attrs: t.Any,\n) -> t.Callable[[_AnyCallable], GrpType]: ...\n\n\n# variant: with optional string name, no cls argument provided.\n@t.overload\ndef group(\n    name: str | None = ..., cls: None = None, **attrs: t.Any\n) -> t.Callable[[_AnyCallable], Group]: ...\n\n\ndef group(\n    name: str | _AnyCallable | None = None,\n    cls: type[GrpType] | None = None,\n    **attrs: t.Any,\n) -> Group | t.Callable[[_AnyCallable], Group | GrpType]:\n    \"\"\"Creates a new :class:`Group` with a function as callback.  This\n    works otherwise the same as :func:`command` just that the `cls`\n    parameter is set to :class:`Group`.\n\n    .. versionchanged:: 8.1\n        This decorator can be applied without parentheses.\n    \"\"\"\n    if cls is None:\n        cls = t.cast(\"type[GrpType]\", Group)\n\n    if callable(name):\n        return command(cls=cls, **attrs)(name)\n\n    return command(name, cls, **attrs)\n\n\ndef _param_memo(f: t.Callable[..., t.Any], param: Parameter) -> None:\n    if isinstance(f, Command):\n        f.params.append(param)\n    else:\n        if not hasattr(f, \"__click_params__\"):\n            f.__click_params__ = []  # type: ignore\n\n        f.__click_params__.append(param)  # type: ignore\n\n\ndef argument(\n    *param_decls: str, cls: type[Argument] | None = None, **attrs: t.Any\n) -> t.Callable[[FC], FC]:\n    \"\"\"Attaches an argument to the command.  All positional arguments are\n    passed as parameter declarations to :class:`Argument`; all keyword\n    arguments are forwarded unchanged (except ``cls``).\n    This is equivalent to creating an :class:`Argument` instance manually\n    and attaching it to the :attr:`Command.params` list.\n\n    For the default argument class, refer to :class:`Argument` and\n    :class:`Parameter` for descriptions of parameters.\n\n    :param cls: the argument class to instantiate.  This defaults to\n                :class:`Argument`.\n    :param param_decls: Passed as positional arguments to the constructor of\n        ``cls``.\n    :param attrs: Passed as keyword arguments to the constructor of ``cls``.\n    \"\"\"\n    if cls is None:\n        cls = Argument\n\n    def decorator(f: FC) -> FC:\n        _param_memo(f, cls(param_decls, **attrs))\n        return f\n\n    return decorator\n\n\ndef option(\n    *param_decls: str, cls: type[Option] | None = None, **attrs: t.Any\n) -> t.Callable[[FC], FC]:\n    \"\"\"Attaches an option to the command.  All positional arguments are\n    passed as parameter declarations to :class:`Option`; all keyword\n    arguments are forwarded unchanged (except ``cls``).\n    This is equivalent to creating an :class:`Option` instance manually\n    and attaching it to the :attr:`Command.params` list.\n\n    For the default option class, refer to :class:`Option` and\n    :class:`Parameter` for descriptions of parameters.\n\n    :param cls: the option class to instantiate.  This defaults to\n                :class:`Option`.\n    :param param_decls: Passed as positional arguments to the constructor of\n        ``cls``.\n    :param attrs: Passed as keyword arguments to the constructor of ``cls``.\n    \"\"\"\n    if cls is None:\n        cls = Option\n\n    def decorator(f: FC) -> FC:\n        _param_memo(f, cls(param_decls, **attrs))\n        return f\n\n    return decorator\n\n\ndef confirmation_option(*param_decls: str, **kwargs: t.Any) -> t.Callable[[FC], FC]:\n    \"\"\"Add a ``--yes`` option which shows a prompt before continuing if\n    not passed. If the prompt is declined, the program will exit.\n\n    :param param_decls: One or more option names. Defaults to the single\n        value ``\"--yes\"``.\n    :param kwargs: Extra arguments are passed to :func:`option`.\n    \"\"\"\n\n    def callback(ctx: Context, param: Parameter, value: bool) -> None:\n        if not value:\n            ctx.abort()\n\n    if not param_decls:\n        param_decls = (\"--yes\",)\n\n    kwargs.setdefault(\"is_flag\", True)\n    kwargs.setdefault(\"callback\", callback)\n    kwargs.setdefault(\"expose_value\", False)\n    kwargs.setdefault(\"prompt\", \"Do you want to continue?\")\n    kwargs.setdefault(\"help\", \"Confirm the action without prompting.\")\n    return option(*param_decls, **kwargs)\n\n\ndef password_option(*param_decls: str, **kwargs: t.Any) -> t.Callable[[FC], FC]:\n    \"\"\"Add a ``--password`` option which prompts for a password, hiding\n    input and asking to enter the value again for confirmation.\n\n    :param param_decls: One or more option names. Defaults to the single\n        value ``\"--password\"``.\n    :param kwargs: Extra arguments are passed to :func:`option`.\n    \"\"\"\n    if not param_decls:\n        param_decls = (\"--password\",)\n\n    kwargs.setdefault(\"prompt\", True)\n    kwargs.setdefault(\"confirmation_prompt\", True)\n    kwargs.setdefault(\"hide_input\", True)\n    return option(*param_decls, **kwargs)\n\n\ndef version_option(\n    version: str | None = None,\n    *param_decls: str,\n    package_name: str | None = None,\n    prog_name: str | None = None,\n    message: str | None = None,\n    **kwargs: t.Any,\n) -> t.Callable[[FC], FC]:\n    \"\"\"Add a ``--version`` option which immediately prints the version\n    number and exits the program.\n\n    If ``version`` is not provided, Click will try to detect it using\n    :func:`importlib.metadata.version` to get the version for the\n    ``package_name``.\n\n    If ``package_name`` is not provided, Click will try to detect it by\n    inspecting the stack frames. This will be used to detect the\n    version, so it must match the name of the installed package.\n\n    :param version: The version number to show. If not provided, Click\n        will try to detect it.\n    :param param_decls: One or more option names. Defaults to the single\n        value ``\"--version\"``.\n    :param package_name: The package name to detect the version from. If\n        not provided, Click will try to detect it.\n    :param prog_name: The name of the CLI to show in the message. If not\n        provided, it will be detected from the command.\n    :param message: The message to show. The values ``%(prog)s``,\n        ``%(package)s``, and ``%(version)s`` are available. Defaults to\n        ``\"%(prog)s, version %(version)s\"``.\n    :param kwargs: Extra arguments are passed to :func:`option`.\n    :raise RuntimeError: ``version`` could not be detected.\n\n    .. versionchanged:: 8.0\n        Add the ``package_name`` parameter, and the ``%(package)s``\n        value for messages.\n\n    .. versionchanged:: 8.0\n        Use :mod:`importlib.metadata` instead of ``pkg_resources``. The\n        version is detected based on the package name, not the entry\n        point name. The Python package name must match the installed\n        package name, or be passed with ``package_name=``.\n    \"\"\"\n    if message is None:\n        message = _(\"%(prog)s, version %(version)s\")\n\n    if version is None and package_name is None:\n        frame = inspect.currentframe()\n        f_back = frame.f_back if frame is not None else None\n        f_globals = f_back.f_globals if f_back is not None else None\n        # break reference cycle\n        # https://docs.python.org/3/library/inspect.html#the-interpreter-stack\n        del frame\n\n        if f_globals is not None:\n            package_name = f_globals.get(\"__name__\")\n\n            if package_name == \"__main__\":\n                package_name = f_globals.get(\"__package__\")\n\n            if package_name:\n                package_name = package_name.partition(\".\")[0]\n\n    def callback(ctx: Context, param: Parameter, value: bool) -> None:\n        if not value or ctx.resilient_parsing:\n            return\n\n        nonlocal prog_name\n        nonlocal version\n\n        if prog_name is None:\n            prog_name = ctx.find_root().info_name\n\n        if version is None and package_name is not None:\n            import importlib.metadata\n\n            try:\n                version = importlib.metadata.version(package_name)\n            except importlib.metadata.PackageNotFoundError:\n                raise RuntimeError(\n                    f\"{package_name!r} is not installed. Try passing\"\n                    \" 'package_name' instead.\"\n                ) from None\n\n        if version is None:\n            raise RuntimeError(\n                f\"Could not determine the version for {package_name!r} automatically.\"\n            )\n\n        echo(\n            message % {\"prog\": prog_name, \"package\": package_name, \"version\": version},\n            color=ctx.color,\n        )\n        ctx.exit()\n\n    if not param_decls:\n        param_decls = (\"--version\",)\n\n    kwargs.setdefault(\"is_flag\", True)\n    kwargs.setdefault(\"expose_value\", False)\n    kwargs.setdefault(\"is_eager\", True)\n    kwargs.setdefault(\"help\", _(\"Show the version and exit.\"))\n    kwargs[\"callback\"] = callback\n    return option(*param_decls, **kwargs)\n\n\ndef 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"
  },
  {
    "path": "src/click/exceptions.py",
    "content": "from __future__ import annotations\n\nimport collections.abc as cabc\nimport typing as t\nfrom gettext import gettext as _\nfrom gettext import ngettext\n\nfrom ._compat import get_text_stderr\nfrom .globals import resolve_color_default\nfrom .utils import echo\nfrom .utils import format_filename\n\nif t.TYPE_CHECKING:\n    from .core import Command\n    from .core import Context\n    from .core import Parameter\n\n\ndef _join_param_hints(param_hint: cabc.Sequence[str] | str | None) -> str | None:\n    if param_hint is not None and not isinstance(param_hint, str):\n        return \" / \".join(repr(x) for x in param_hint)\n\n    return param_hint\n\n\nclass ClickException(Exception):\n    \"\"\"An exception that Click can handle and show to the user.\"\"\"\n\n    #: The exit code for this exception.\n    exit_code = 1\n\n    def __init__(self, message: str) -> None:\n        super().__init__(message)\n        # The context will be removed by the time we print the message, so cache\n        # the color settings here to be used later on (in `show`)\n        self.show_color: bool | None = resolve_color_default()\n        self.message = message\n\n    def format_message(self) -> str:\n        return self.message\n\n    def __str__(self) -> str:\n        return self.message\n\n    def show(self, file: t.IO[t.Any] | None = None) -> None:\n        if file is None:\n            file = get_text_stderr()\n\n        echo(\n            _(\"Error: {message}\").format(message=self.format_message()),\n            file=file,\n            color=self.show_color,\n        )\n\n\nclass UsageError(ClickException):\n    \"\"\"An internal exception that signals a usage error.  This typically\n    aborts any further handling.\n\n    :param message: the error message to display.\n    :param ctx: optionally the context that caused this error.  Click will\n                fill in the context automatically in some situations.\n    \"\"\"\n\n    exit_code = 2\n\n    def __init__(self, message: str, ctx: Context | None = None) -> None:\n        super().__init__(message)\n        self.ctx = ctx\n        self.cmd: Command | None = self.ctx.command if self.ctx else None\n\n    def show(self, file: t.IO[t.Any] | None = None) -> None:\n        if file is None:\n            file = get_text_stderr()\n        color = None\n        hint = \"\"\n        if (\n            self.ctx is not None\n            and self.ctx.command.get_help_option(self.ctx) is not None\n        ):\n            hint = _(\"Try '{command} {option}' for help.\").format(\n                command=self.ctx.command_path, option=self.ctx.help_option_names[0]\n            )\n            hint = f\"{hint}\\n\"\n        if self.ctx is not None:\n            color = self.ctx.color\n            echo(f\"{self.ctx.get_usage()}\\n{hint}\", file=file, color=color)\n        echo(\n            _(\"Error: {message}\").format(message=self.format_message()),\n            file=file,\n            color=color,\n        )\n\n\nclass BadParameter(UsageError):\n    \"\"\"An exception that formats out a standardized error message for a\n    bad parameter.  This is useful when thrown from a callback or type as\n    Click will attach contextual information to it (for instance, which\n    parameter it is).\n\n    .. versionadded:: 2.0\n\n    :param param: the parameter object that caused this error.  This can\n                  be left out, and Click will attach this info itself\n                  if possible.\n    :param param_hint: a string that shows up as parameter name.  This\n                       can be used as alternative to `param` in cases\n                       where custom validation should happen.  If it is\n                       a string it's used as such, if it's a list then\n                       each item is quoted and separated.\n    \"\"\"\n\n    def __init__(\n        self,\n        message: str,\n        ctx: Context | None = None,\n        param: Parameter | None = None,\n        param_hint: cabc.Sequence[str] | str | None = None,\n    ) -> None:\n        super().__init__(message, ctx)\n        self.param = param\n        self.param_hint = param_hint\n\n    def format_message(self) -> str:\n        if self.param_hint is not None:\n            param_hint = self.param_hint\n        elif self.param is not None:\n            param_hint = self.param.get_error_hint(self.ctx)  # type: ignore\n        else:\n            return _(\"Invalid value: {message}\").format(message=self.message)\n\n        return _(\"Invalid value for {param_hint}: {message}\").format(\n            param_hint=_join_param_hints(param_hint), message=self.message\n        )\n\n\nclass MissingParameter(BadParameter):\n    \"\"\"Raised if click required an option or argument but it was not\n    provided when invoking the script.\n\n    .. versionadded:: 4.0\n\n    :param param_type: a string that indicates the type of the parameter.\n                       The default is to inherit the parameter type from\n                       the given `param`.  Valid values are ``'parameter'``,\n                       ``'option'`` or ``'argument'``.\n    \"\"\"\n\n    def __init__(\n        self,\n        message: str | None = None,\n        ctx: Context | None = None,\n        param: Parameter | None = None,\n        param_hint: cabc.Sequence[str] | str | None = None,\n        param_type: str | None = None,\n    ) -> None:\n        super().__init__(message or \"\", ctx, param, param_hint)\n        self.param_type = param_type\n\n    def format_message(self) -> str:\n        if self.param_hint is not None:\n            param_hint: cabc.Sequence[str] | str | None = self.param_hint\n        elif self.param is not None:\n            param_hint = self.param.get_error_hint(self.ctx)  # type: ignore\n        else:\n            param_hint = None\n\n        param_hint = _join_param_hints(param_hint)\n        param_hint = f\" {param_hint}\" if param_hint else \"\"\n\n        param_type = self.param_type\n        if param_type is None and self.param is not None:\n            param_type = self.param.param_type_name\n\n        msg = self.message\n        if self.param is not None:\n            msg_extra = self.param.type.get_missing_message(\n                param=self.param, ctx=self.ctx\n            )\n            if msg_extra:\n                if msg:\n                    msg += f\". {msg_extra}\"\n                else:\n                    msg = msg_extra\n\n        msg = f\" {msg}\" if msg else \"\"\n\n        # Translate param_type for known types.\n        if param_type == \"argument\":\n            missing = _(\"Missing argument\")\n        elif param_type == \"option\":\n            missing = _(\"Missing option\")\n        elif param_type == \"parameter\":\n            missing = _(\"Missing parameter\")\n        else:\n            missing = _(\"Missing {param_type}\").format(param_type=param_type)\n\n        return f\"{missing}{param_hint}.{msg}\"\n\n    def __str__(self) -> str:\n        if not self.message:\n            param_name = self.param.name if self.param else None\n            return _(\"Missing parameter: {param_name}\").format(param_name=param_name)\n        else:\n            return self.message\n\n\nclass NoSuchOption(UsageError):\n    \"\"\"Raised if click attempted to handle an option that does not\n    exist.\n\n    .. versionadded:: 4.0\n    \"\"\"\n\n    def __init__(\n        self,\n        option_name: str,\n        message: str | None = None,\n        possibilities: cabc.Sequence[str] | None = None,\n        ctx: Context | None = None,\n    ) -> None:\n        if message is None:\n            message = _(\"No such option: {name}\").format(name=option_name)\n\n        super().__init__(message, ctx)\n        self.option_name = option_name\n        self.possibilities = possibilities\n\n    def format_message(self) -> str:\n        if not self.possibilities:\n            return self.message\n\n        possibility_str = \", \".join(sorted(self.possibilities))\n        suggest = ngettext(\n            \"Did you mean {possibility}?\",\n            \"(Possible options: {possibilities})\",\n            len(self.possibilities),\n        ).format(possibility=possibility_str, possibilities=possibility_str)\n        return f\"{self.message} {suggest}\"\n\n\nclass BadOptionUsage(UsageError):\n    \"\"\"Raised if an option is generally supplied but the use of the option\n    was incorrect.  This is for instance raised if the number of arguments\n    for an option is not correct.\n\n    .. versionadded:: 4.0\n\n    :param option_name: the name of the option being used incorrectly.\n    \"\"\"\n\n    def __init__(\n        self, option_name: str, message: str, ctx: Context | None = None\n    ) -> None:\n        super().__init__(message, ctx)\n        self.option_name = option_name\n\n\nclass BadArgumentUsage(UsageError):\n    \"\"\"Raised if an argument is generally supplied but the use of the argument\n    was incorrect.  This is for instance raised if the number of values\n    for an argument is not correct.\n\n    .. versionadded:: 6.0\n    \"\"\"\n\n\nclass NoArgsIsHelpError(UsageError):\n    def __init__(self, ctx: Context) -> None:\n        self.ctx: Context\n        super().__init__(ctx.get_help(), ctx=ctx)\n\n    def show(self, file: t.IO[t.Any] | None = None) -> None:\n        echo(self.format_message(), file=file, err=True, color=self.ctx.color)\n\n\nclass FileError(ClickException):\n    \"\"\"Raised if a file cannot be opened.\"\"\"\n\n    def __init__(self, filename: str, hint: str | None = None) -> None:\n        if hint is None:\n            hint = _(\"unknown error\")\n\n        super().__init__(hint)\n        self.ui_filename: str = format_filename(filename)\n        self.filename = filename\n\n    def format_message(self) -> str:\n        return _(\"Could not open file {filename!r}: {message}\").format(\n            filename=self.ui_filename, message=self.message\n        )\n\n\nclass Abort(RuntimeError):\n    \"\"\"An internal signalling exception that signals Click to abort.\"\"\"\n\n\nclass Exit(RuntimeError):\n    \"\"\"An exception that indicates that the application should exit with some\n    status code.\n\n    :param code: the status code to exit with.\n    \"\"\"\n\n    __slots__ = (\"exit_code\",)\n\n    def __init__(self, code: int = 0) -> None:\n        self.exit_code: int = code\n"
  },
  {
    "path": "src/click/formatting.py",
    "content": "from __future__ import annotations\n\nimport collections.abc as cabc\nfrom contextlib import contextmanager\nfrom gettext import gettext as _\n\nfrom ._compat import term_len\nfrom .parser import _split_opt\n\n# Can force a width.  This is used by the test system\nFORCED_WIDTH: int | None = None\n\n\ndef measure_table(rows: cabc.Iterable[tuple[str, str]]) -> tuple[int, ...]:\n    widths: dict[int, int] = {}\n\n    for row in rows:\n        for idx, col in enumerate(row):\n            widths[idx] = max(widths.get(idx, 0), term_len(col))\n\n    return tuple(y for x, y in sorted(widths.items()))\n\n\ndef iter_rows(\n    rows: cabc.Iterable[tuple[str, str]], col_count: int\n) -> cabc.Iterator[tuple[str, ...]]:\n    for row in rows:\n        yield row + (\"\",) * (col_count - len(row))\n\n\ndef wrap_text(\n    text: str,\n    width: int = 78,\n    initial_indent: str = \"\",\n    subsequent_indent: str = \"\",\n    preserve_paragraphs: bool = False,\n) -> str:\n    \"\"\"A helper function that intelligently wraps text.  By default, it\n    assumes that it operates on a single paragraph of text but if the\n    `preserve_paragraphs` parameter is provided it will intelligently\n    handle paragraphs (defined by two empty lines).\n\n    If paragraphs are handled, a paragraph can be prefixed with an empty\n    line containing the ``\\\\b`` character (``\\\\x08``) to indicate that\n    no rewrapping should happen in that block.\n\n    :param text: the text that should be rewrapped.\n    :param width: the maximum width for the text.\n    :param initial_indent: the initial indent that should be placed on the\n                           first line as a string.\n    :param subsequent_indent: the indent string that should be placed on\n                              each consecutive line.\n    :param preserve_paragraphs: if this flag is set then the wrapping will\n                                intelligently handle paragraphs.\n    \"\"\"\n    from ._textwrap import TextWrapper\n\n    text = text.expandtabs()\n    wrapper = TextWrapper(\n        width,\n        initial_indent=initial_indent,\n        subsequent_indent=subsequent_indent,\n        replace_whitespace=False,\n    )\n    if not preserve_paragraphs:\n        return wrapper.fill(text)\n\n    p: list[tuple[int, bool, str]] = []\n    buf: list[str] = []\n    indent = None\n\n    def _flush_par() -> None:\n        if not buf:\n            return\n        if buf[0].strip() == \"\\b\":\n            p.append((indent or 0, True, \"\\n\".join(buf[1:])))\n        else:\n            p.append((indent or 0, False, \" \".join(buf)))\n        del buf[:]\n\n    for line in text.splitlines():\n        if not line:\n            _flush_par()\n            indent = None\n        else:\n            if indent is None:\n                orig_len = term_len(line)\n                line = line.lstrip()\n                indent = orig_len - term_len(line)\n            buf.append(line)\n    _flush_par()\n\n    rv = []\n    for indent, raw, text in p:\n        with wrapper.extra_indent(\" \" * indent):\n            if raw:\n                rv.append(wrapper.indent_only(text))\n            else:\n                rv.append(wrapper.fill(text))\n\n    return \"\\n\\n\".join(rv)\n\n\nclass HelpFormatter:\n    \"\"\"This class helps with formatting text-based help pages.  It's\n    usually just needed for very special internal cases, but it's also\n    exposed so that developers can write their own fancy outputs.\n\n    At present, it always writes into memory.\n\n    :param indent_increment: the additional increment for each level.\n    :param width: the width for the text.  This defaults to the terminal\n                  width clamped to a maximum of 78.\n    \"\"\"\n\n    def __init__(\n        self,\n        indent_increment: int = 2,\n        width: int | None = None,\n        max_width: int | None = None,\n    ) -> None:\n        self.indent_increment = indent_increment\n        if max_width is None:\n            max_width = 80\n        if width is None:\n            import shutil\n\n            width = FORCED_WIDTH\n            if width is None:\n                width = max(min(shutil.get_terminal_size().columns, max_width) - 2, 50)\n        self.width = width\n        self.current_indent: int = 0\n        self.buffer: list[str] = []\n\n    def write(self, string: str) -> None:\n        \"\"\"Writes a unicode string into the internal buffer.\"\"\"\n        self.buffer.append(string)\n\n    def indent(self) -> None:\n        \"\"\"Increases the indentation.\"\"\"\n        self.current_indent += self.indent_increment\n\n    def dedent(self) -> None:\n        \"\"\"Decreases the indentation.\"\"\"\n        self.current_indent -= self.indent_increment\n\n    def write_usage(self, prog: str, args: str = \"\", prefix: str | None = None) -> None:\n        \"\"\"Writes a usage line into the buffer.\n\n        :param prog: the program name.\n        :param args: whitespace separated list of arguments.\n        :param prefix: The prefix for the first line. Defaults to\n            ``\"Usage: \"``.\n        \"\"\"\n        if prefix is None:\n            prefix = f\"{_('Usage:')} \"\n\n        usage_prefix = f\"{prefix:>{self.current_indent}}{prog} \"\n        text_width = self.width - self.current_indent\n\n        if text_width >= (term_len(usage_prefix) + 20):\n            # The arguments will fit to the right of the prefix.\n            indent = \" \" * term_len(usage_prefix)\n            self.write(\n                wrap_text(\n                    args,\n                    text_width,\n                    initial_indent=usage_prefix,\n                    subsequent_indent=indent,\n                )\n            )\n        else:\n            # The prefix is too long, put the arguments on the next line.\n            self.write(usage_prefix)\n            self.write(\"\\n\")\n            indent = \" \" * (max(self.current_indent, term_len(prefix)) + 4)\n            self.write(\n                wrap_text(\n                    args, text_width, initial_indent=indent, subsequent_indent=indent\n                )\n            )\n\n        self.write(\"\\n\")\n\n    def write_heading(self, heading: str) -> None:\n        \"\"\"Writes a heading into the buffer.\"\"\"\n        self.write(f\"{'':>{self.current_indent}}{heading}:\\n\")\n\n    def write_paragraph(self) -> None:\n        \"\"\"Writes a paragraph into the buffer.\"\"\"\n        if self.buffer:\n            self.write(\"\\n\")\n\n    def write_text(self, text: str) -> None:\n        \"\"\"Writes re-indented text into the buffer.  This rewraps and\n        preserves paragraphs.\n        \"\"\"\n        indent = \" \" * self.current_indent\n        self.write(\n            wrap_text(\n                text,\n                self.width,\n                initial_indent=indent,\n                subsequent_indent=indent,\n                preserve_paragraphs=True,\n            )\n        )\n        self.write(\"\\n\")\n\n    def write_dl(\n        self,\n        rows: cabc.Sequence[tuple[str, str]],\n        col_max: int = 30,\n        col_spacing: int = 2,\n    ) -> None:\n        \"\"\"Writes a definition list into the buffer.  This is how options\n        and commands are usually formatted.\n\n        :param rows: a list of two item tuples for the terms and values.\n        :param col_max: the maximum width of the first column.\n        :param col_spacing: the number of spaces between the first and\n                            second column.\n        \"\"\"\n        rows = list(rows)\n        widths = measure_table(rows)\n        if len(widths) != 2:\n            raise TypeError(\"Expected two columns for definition list\")\n\n        first_col = min(widths[0], col_max) + col_spacing\n\n        for first, second in iter_rows(rows, len(widths)):\n            self.write(f\"{'':>{self.current_indent}}{first}\")\n            if not second:\n                self.write(\"\\n\")\n                continue\n            if term_len(first) <= first_col - col_spacing:\n                self.write(\" \" * (first_col - term_len(first)))\n            else:\n                self.write(\"\\n\")\n                self.write(\" \" * (first_col + self.current_indent))\n\n            text_width = max(self.width - first_col - 2, 10)\n            wrapped_text = wrap_text(second, text_width, preserve_paragraphs=True)\n            lines = wrapped_text.splitlines()\n\n            if lines:\n                self.write(f\"{lines[0]}\\n\")\n\n                for line in lines[1:]:\n                    self.write(f\"{'':>{first_col + self.current_indent}}{line}\\n\")\n            else:\n                self.write(\"\\n\")\n\n    @contextmanager\n    def section(self, name: str) -> cabc.Iterator[None]:\n        \"\"\"Helpful context manager that writes a paragraph, a heading,\n        and the indents.\n\n        :param name: the section name that is written as heading.\n        \"\"\"\n        self.write_paragraph()\n        self.write_heading(name)\n        self.indent()\n        try:\n            yield\n        finally:\n            self.dedent()\n\n    @contextmanager\n    def indentation(self) -> cabc.Iterator[None]:\n        \"\"\"A context manager that increases the indentation.\"\"\"\n        self.indent()\n        try:\n            yield\n        finally:\n            self.dedent()\n\n    def getvalue(self) -> str:\n        \"\"\"Returns the buffer contents.\"\"\"\n        return \"\".join(self.buffer)\n\n\ndef join_options(options: cabc.Sequence[str]) -> tuple[str, bool]:\n    \"\"\"Given a list of option strings this joins them in the most appropriate\n    way and returns them in the form ``(formatted_string,\n    any_prefix_is_slash)`` where the second item in the tuple is a flag that\n    indicates if any of the option prefixes was a slash.\n    \"\"\"\n    rv = []\n    any_prefix_is_slash = False\n\n    for opt in options:\n        prefix = _split_opt(opt)[0]\n\n        if prefix == \"/\":\n            any_prefix_is_slash = True\n\n        rv.append((len(prefix), opt))\n\n    rv.sort(key=lambda x: x[0])\n    return \", \".join(x[1] for x in rv), any_prefix_is_slash\n"
  },
  {
    "path": "src/click/globals.py",
    "content": "from __future__ import annotations\n\nimport typing as t\nfrom threading import local\n\nif t.TYPE_CHECKING:\n    from .core import Context\n\n_local = local()\n\n\n@t.overload\ndef get_current_context(silent: t.Literal[False] = False) -> Context: ...\n\n\n@t.overload\ndef get_current_context(silent: bool = ...) -> Context | None: ...\n\n\ndef get_current_context(silent: bool = False) -> Context | None:\n    \"\"\"Returns the current click context.  This can be used as a way to\n    access the current context object from anywhere.  This is a more implicit\n    alternative to the :func:`pass_context` decorator.  This function is\n    primarily useful for helpers such as :func:`echo` which might be\n    interested in changing its behavior based on the current context.\n\n    To push the current context, :meth:`Context.scope` can be used.\n\n    .. versionadded:: 5.0\n\n    :param silent: if set to `True` the return value is `None` if no context\n                   is available.  The default behavior is to raise a\n                   :exc:`RuntimeError`.\n    \"\"\"\n    try:\n        return t.cast(\"Context\", _local.stack[-1])\n    except (AttributeError, IndexError) as e:\n        if not silent:\n            raise RuntimeError(\"There is no active click context.\") from e\n\n    return None\n\n\ndef push_context(ctx: Context) -> None:\n    \"\"\"Pushes a new context to the current stack.\"\"\"\n    _local.__dict__.setdefault(\"stack\", []).append(ctx)\n\n\ndef pop_context() -> None:\n    \"\"\"Removes the top level from the stack.\"\"\"\n    _local.stack.pop()\n\n\ndef resolve_color_default(color: bool | None = None) -> bool | None:\n    \"\"\"Internal helper to get the default value of the color flag.  If a\n    value is passed it's returned unchanged, otherwise it's looked up from\n    the current context.\n    \"\"\"\n    if color is not None:\n        return color\n\n    ctx = get_current_context(silent=True)\n\n    if ctx is not None:\n        return ctx.color\n\n    return None\n"
  },
  {
    "path": "src/click/parser.py",
    "content": "\"\"\"\nThis module started out as largely a copy paste from the stdlib's\noptparse module with the features removed that we do not need from\noptparse because we implement them in Click on a higher level (for\ninstance type handling, help formatting and a lot more).\n\nThe plan is to remove more and more from here over time.\n\nThe reason this is a different module and not optparse from the stdlib\nis that there are differences in 2.x and 3.x about the error messages\ngenerated and optparse in the stdlib uses gettext for no good reason\nand might cause us issues.\n\nClick uses parts of optparse written by Gregory P. Ward and maintained\nby the Python Software Foundation. This is limited to code in parser.py.\n\nCopyright 2001-2006 Gregory P. Ward. All rights reserved.\nCopyright 2002-2006 Python Software Foundation. All rights reserved.\n\"\"\"\n\n# This code uses parts of optparse written by Gregory P. Ward and\n# maintained by the Python Software Foundation.\n# Copyright 2001-2006 Gregory P. Ward\n# Copyright 2002-2006 Python Software Foundation\nfrom __future__ import annotations\n\nimport collections.abc as cabc\nimport typing as t\nfrom collections import deque\nfrom gettext import gettext as _\nfrom gettext import ngettext\n\nfrom ._utils import FLAG_NEEDS_VALUE\nfrom ._utils import UNSET\nfrom .exceptions import BadArgumentUsage\nfrom .exceptions import BadOptionUsage\nfrom .exceptions import NoSuchOption\nfrom .exceptions import UsageError\n\nif t.TYPE_CHECKING:\n    from ._utils import T_FLAG_NEEDS_VALUE\n    from ._utils import T_UNSET\n    from .core import Argument as CoreArgument\n    from .core import Context\n    from .core import Option as CoreOption\n    from .core import Parameter as CoreParameter\n\nV = t.TypeVar(\"V\")\n\n\ndef _unpack_args(\n    args: cabc.Sequence[str], nargs_spec: cabc.Sequence[int]\n) -> tuple[cabc.Sequence[str | cabc.Sequence[str | None] | None], list[str]]:\n    \"\"\"Given an iterable of arguments and an iterable of nargs specifications,\n    it returns a tuple with all the unpacked arguments at the first index\n    and all remaining arguments as the second.\n\n    The nargs specification is the number of arguments that should be consumed\n    or `-1` to indicate that this position should eat up all the remainders.\n\n    Missing items are filled with ``UNSET``.\n    \"\"\"\n    args = deque(args)\n    nargs_spec = deque(nargs_spec)\n    rv: list[str | tuple[str | T_UNSET, ...] | T_UNSET] = []\n    spos: int | None = None\n\n    def _fetch(c: deque[V]) -> V | T_UNSET:\n        try:\n            if spos is None:\n                return c.popleft()\n            else:\n                return c.pop()\n        except IndexError:\n            return UNSET\n\n    while nargs_spec:\n        nargs = _fetch(nargs_spec)\n\n        if nargs is None:\n            continue\n\n        if nargs == 1:\n            rv.append(_fetch(args))  # type: ignore[arg-type]\n        elif nargs > 1:\n            x = [_fetch(args) for _ in range(nargs)]\n\n            # If we're reversed, we're pulling in the arguments in reverse,\n            # so we need to turn them around.\n            if spos is not None:\n                x.reverse()\n\n            rv.append(tuple(x))\n        elif nargs < 0:\n            if spos is not None:\n                raise TypeError(\"Cannot have two nargs < 0\")\n\n            spos = len(rv)\n            rv.append(UNSET)\n\n    # spos is the position of the wildcard (star).  If it's not `None`,\n    # we fill it with the remainder.\n    if spos is not None:\n        rv[spos] = tuple(args)\n        args = []\n        rv[spos + 1 :] = reversed(rv[spos + 1 :])\n\n    return tuple(rv), list(args)\n\n\ndef _split_opt(opt: str) -> tuple[str, str]:\n    first = opt[:1]\n    if first.isalnum():\n        return \"\", opt\n    if opt[1:2] == first:\n        return opt[:2], opt[2:]\n    return first, opt[1:]\n\n\ndef _normalize_opt(opt: str, ctx: Context | None) -> str:\n    if ctx is None or ctx.token_normalize_func is None:\n        return opt\n    prefix, opt = _split_opt(opt)\n    return f\"{prefix}{ctx.token_normalize_func(opt)}\"\n\n\nclass _Option:\n    def __init__(\n        self,\n        obj: CoreOption,\n        opts: cabc.Sequence[str],\n        dest: str | None,\n        action: str | None = None,\n        nargs: int = 1,\n        const: t.Any | None = None,\n    ):\n        self._short_opts = []\n        self._long_opts = []\n        self.prefixes: set[str] = set()\n\n        for opt in opts:\n            prefix, value = _split_opt(opt)\n            if not prefix:\n                raise ValueError(f\"Invalid start character for option ({opt})\")\n            self.prefixes.add(prefix[0])\n            if len(prefix) == 1 and len(value) == 1:\n                self._short_opts.append(opt)\n            else:\n                self._long_opts.append(opt)\n                self.prefixes.add(prefix)\n\n        if action is None:\n            action = \"store\"\n\n        self.dest = dest\n        self.action = action\n        self.nargs = nargs\n        self.const = const\n        self.obj = obj\n\n    @property\n    def takes_value(self) -> bool:\n        return self.action in (\"store\", \"append\")\n\n    def process(self, value: t.Any, state: _ParsingState) -> None:\n        if self.action == \"store\":\n            state.opts[self.dest] = value  # type: ignore\n        elif self.action == \"store_const\":\n            state.opts[self.dest] = self.const  # type: ignore\n        elif self.action == \"append\":\n            state.opts.setdefault(self.dest, []).append(value)  # type: ignore\n        elif self.action == \"append_const\":\n            state.opts.setdefault(self.dest, []).append(self.const)  # type: ignore\n        elif self.action == \"count\":\n            state.opts[self.dest] = state.opts.get(self.dest, 0) + 1  # type: ignore\n        else:\n            raise ValueError(f\"unknown action '{self.action}'\")\n        state.order.append(self.obj)\n\n\nclass _Argument:\n    def __init__(self, obj: CoreArgument, dest: str | None, nargs: int = 1):\n        self.dest = dest\n        self.nargs = nargs\n        self.obj = obj\n\n    def process(\n        self,\n        value: str | cabc.Sequence[str | None] | None | T_UNSET,\n        state: _ParsingState,\n    ) -> None:\n        if self.nargs > 1:\n            assert isinstance(value, cabc.Sequence)\n            holes = sum(1 for x in value if x is UNSET)\n            if holes == len(value):\n                value = UNSET\n            elif holes != 0:\n                raise BadArgumentUsage(\n                    _(\"Argument {name!r} takes {nargs} values.\").format(\n                        name=self.dest, nargs=self.nargs\n                    )\n                )\n\n        # We failed to collect any argument value so we consider the argument as unset.\n        if value == ():\n            value = UNSET\n\n        state.opts[self.dest] = value  # type: ignore\n        state.order.append(self.obj)\n\n\nclass _ParsingState:\n    def __init__(self, rargs: list[str]) -> None:\n        self.opts: dict[str, t.Any] = {}\n        self.largs: list[str] = []\n        self.rargs = rargs\n        self.order: list[CoreParameter] = []\n\n\nclass _OptionParser:\n    \"\"\"The option parser is an internal class that is ultimately used to\n    parse options and arguments.  It's modelled after optparse and brings\n    a similar but vastly simplified API.  It should generally not be used\n    directly as the high level Click classes wrap it for you.\n\n    It's not nearly as extensible as optparse or argparse as it does not\n    implement features that are implemented on a higher level (such as\n    types or defaults).\n\n    :param ctx: optionally the :class:`~click.Context` where this parser\n                should go with.\n\n    .. deprecated:: 8.2\n        Will be removed in Click 9.0.\n    \"\"\"\n\n    def __init__(self, ctx: Context | None = None) -> None:\n        #: The :class:`~click.Context` for this parser.  This might be\n        #: `None` for some advanced use cases.\n        self.ctx = ctx\n        #: This controls how the parser deals with interspersed arguments.\n        #: If this is set to `False`, the parser will stop on the first\n        #: non-option.  Click uses this to implement nested subcommands\n        #: safely.\n        self.allow_interspersed_args: bool = True\n        #: This tells the parser how to deal with unknown options.  By\n        #: default it will error out (which is sensible), but there is a\n        #: second mode where it will ignore it and continue processing\n        #: after shifting all the unknown options into the resulting args.\n        self.ignore_unknown_options: bool = False\n\n        if ctx is not None:\n            self.allow_interspersed_args = ctx.allow_interspersed_args\n            self.ignore_unknown_options = ctx.ignore_unknown_options\n\n        self._short_opt: dict[str, _Option] = {}\n        self._long_opt: dict[str, _Option] = {}\n        self._opt_prefixes = {\"-\", \"--\"}\n        self._args: list[_Argument] = []\n\n    def add_option(\n        self,\n        obj: CoreOption,\n        opts: cabc.Sequence[str],\n        dest: str | None,\n        action: str | None = None,\n        nargs: int = 1,\n        const: t.Any | None = None,\n    ) -> None:\n        \"\"\"Adds a new option named `dest` to the parser.  The destination\n        is not inferred (unlike with optparse) and needs to be explicitly\n        provided.  Action can be any of ``store``, ``store_const``,\n        ``append``, ``append_const`` or ``count``.\n\n        The `obj` can be used to identify the option in the order list\n        that is returned from the parser.\n        \"\"\"\n        opts = [_normalize_opt(opt, self.ctx) for opt in opts]\n        option = _Option(obj, opts, dest, action=action, nargs=nargs, const=const)\n        self._opt_prefixes.update(option.prefixes)\n        for opt in option._short_opts:\n            self._short_opt[opt] = option\n        for opt in option._long_opts:\n            self._long_opt[opt] = option\n\n    def add_argument(self, obj: CoreArgument, dest: str | None, nargs: int = 1) -> None:\n        \"\"\"Adds a positional argument named `dest` to the parser.\n\n        The `obj` can be used to identify the option in the order list\n        that is returned from the parser.\n        \"\"\"\n        self._args.append(_Argument(obj, dest=dest, nargs=nargs))\n\n    def parse_args(\n        self, args: list[str]\n    ) -> tuple[dict[str, t.Any], list[str], list[CoreParameter]]:\n        \"\"\"Parses positional arguments and returns ``(values, args, order)``\n        for the parsed options and arguments as well as the leftover\n        arguments if there are any.  The order is a list of objects as they\n        appear on the command line.  If arguments appear multiple times they\n        will be memorized multiple times as well.\n        \"\"\"\n        state = _ParsingState(args)\n        try:\n            self._process_args_for_options(state)\n            self._process_args_for_args(state)\n        except UsageError:\n            if self.ctx is None or not self.ctx.resilient_parsing:\n                raise\n        return state.opts, state.largs, state.order\n\n    def _process_args_for_args(self, state: _ParsingState) -> None:\n        pargs, args = _unpack_args(\n            state.largs + state.rargs, [x.nargs for x in self._args]\n        )\n\n        for idx, arg in enumerate(self._args):\n            arg.process(pargs[idx], state)\n\n        state.largs = args\n        state.rargs = []\n\n    def _process_args_for_options(self, state: _ParsingState) -> None:\n        while state.rargs:\n            arg = state.rargs.pop(0)\n            arglen = len(arg)\n            # Double dashes always handled explicitly regardless of what\n            # prefixes are valid.\n            if arg == \"--\":\n                return\n            elif arg[:1] in self._opt_prefixes and arglen > 1:\n                self._process_opts(arg, state)\n            elif self.allow_interspersed_args:\n                state.largs.append(arg)\n            else:\n                state.rargs.insert(0, arg)\n                return\n\n        # Say this is the original argument list:\n        # [arg0, arg1, ..., arg(i-1), arg(i), arg(i+1), ..., arg(N-1)]\n        #                            ^\n        # (we are about to process arg(i)).\n        #\n        # Then rargs is [arg(i), ..., arg(N-1)] and largs is a *subset* of\n        # [arg0, ..., arg(i-1)] (any options and their arguments will have\n        # been removed from largs).\n        #\n        # The while loop will usually consume 1 or more arguments per pass.\n        # If it consumes 1 (eg. arg is an option that takes no arguments),\n        # then after _process_arg() is done the situation is:\n        #\n        #   largs = subset of [arg0, ..., arg(i)]\n        #   rargs = [arg(i+1), ..., arg(N-1)]\n        #\n        # If allow_interspersed_args is false, largs will always be\n        # *empty* -- still a subset of [arg0, ..., arg(i-1)], but\n        # not a very interesting subset!\n\n    def _match_long_opt(\n        self, opt: str, explicit_value: str | None, state: _ParsingState\n    ) -> None:\n        if opt not in self._long_opt:\n            from difflib import get_close_matches\n\n            possibilities = get_close_matches(opt, self._long_opt)\n            raise NoSuchOption(opt, possibilities=possibilities, ctx=self.ctx)\n\n        option = self._long_opt[opt]\n        if option.takes_value:\n            # At this point it's safe to modify rargs by injecting the\n            # explicit value, because no exception is raised in this\n            # branch.  This means that the inserted value will be fully\n            # consumed.\n            if explicit_value is not None:\n                state.rargs.insert(0, explicit_value)\n\n            value = self._get_value_from_state(opt, option, state)\n\n        elif explicit_value is not None:\n            raise BadOptionUsage(\n                opt, _(\"Option {name!r} does not take a value.\").format(name=opt)\n            )\n\n        else:\n            value = UNSET\n\n        option.process(value, state)\n\n    def _match_short_opt(self, arg: str, state: _ParsingState) -> None:\n        stop = False\n        i = 1\n        prefix = arg[0]\n        unknown_options = []\n\n        for ch in arg[1:]:\n            opt = _normalize_opt(f\"{prefix}{ch}\", self.ctx)\n            option = self._short_opt.get(opt)\n            i += 1\n\n            if not option:\n                if self.ignore_unknown_options:\n                    unknown_options.append(ch)\n                    continue\n                raise NoSuchOption(opt, ctx=self.ctx)\n            if option.takes_value:\n                # Any characters left in arg?  Pretend they're the\n                # next arg, and stop consuming characters of arg.\n                if i < len(arg):\n                    state.rargs.insert(0, arg[i:])\n                    stop = True\n\n                value = self._get_value_from_state(opt, option, state)\n\n            else:\n                value = UNSET\n\n            option.process(value, state)\n\n            if stop:\n                break\n\n        # If we got any unknown options we recombine the string of the\n        # remaining options and re-attach the prefix, then report that\n        # to the state as new larg.  This way there is basic combinatorics\n        # that can be achieved while still ignoring unknown arguments.\n        if self.ignore_unknown_options and unknown_options:\n            state.largs.append(f\"{prefix}{''.join(unknown_options)}\")\n\n    def _get_value_from_state(\n        self, option_name: str, option: _Option, state: _ParsingState\n    ) -> str | cabc.Sequence[str] | T_FLAG_NEEDS_VALUE:\n        nargs = option.nargs\n\n        value: str | cabc.Sequence[str] | T_FLAG_NEEDS_VALUE\n\n        if len(state.rargs) < nargs:\n            if option.obj._flag_needs_value:\n                # Option allows omitting the value.\n                value = FLAG_NEEDS_VALUE\n            else:\n                raise BadOptionUsage(\n                    option_name,\n                    ngettext(\n                        \"Option {name!r} requires an argument.\",\n                        \"Option {name!r} requires {nargs} arguments.\",\n                        nargs,\n                    ).format(name=option_name, nargs=nargs),\n                )\n        elif nargs == 1:\n            next_rarg = state.rargs[0]\n\n            if (\n                option.obj._flag_needs_value\n                and isinstance(next_rarg, str)\n                and next_rarg[:1] in self._opt_prefixes\n                and len(next_rarg) > 1\n            ):\n                # The next arg looks like the start of an option, don't\n                # use it as the value if omitting the value is allowed.\n                value = FLAG_NEEDS_VALUE\n            else:\n                value = state.rargs.pop(0)\n        else:\n            value = tuple(state.rargs[:nargs])\n            del state.rargs[:nargs]\n\n        return value\n\n    def _process_opts(self, arg: str, state: _ParsingState) -> None:\n        explicit_value = None\n        # Long option handling happens in two parts.  The first part is\n        # supporting explicitly attached values.  In any case, we will try\n        # to long match the option first.\n        if \"=\" in arg:\n            long_opt, explicit_value = arg.split(\"=\", 1)\n        else:\n            long_opt = arg\n        norm_long_opt = _normalize_opt(long_opt, self.ctx)\n\n        # At this point we will match the (assumed) long option through\n        # the long option matching code.  Note that this allows options\n        # like \"-foo\" to be matched as long options.\n        try:\n            self._match_long_opt(norm_long_opt, explicit_value, state)\n        except NoSuchOption:\n            # At this point the long option matching failed, and we need\n            # to try with short options.  However there is a special rule\n            # which says, that if we have a two character options prefix\n            # (applies to \"--foo\" for instance), we do not dispatch to the\n            # short option code and will instead raise the no option\n            # error.\n            if arg[:2] not in self._opt_prefixes:\n                self._match_short_opt(arg, state)\n                return\n\n            if not self.ignore_unknown_options:\n                raise\n\n            state.largs.append(arg)\n\n\ndef __getattr__(name: str) -> object:\n    import warnings\n\n    if name in {\n        \"OptionParser\",\n        \"Argument\",\n        \"Option\",\n        \"split_opt\",\n        \"normalize_opt\",\n        \"ParsingState\",\n    }:\n        warnings.warn(\n            f\"'parser.{name}' is deprecated and will be removed in Click 9.0.\"\n            \" The old parser is available in 'optparse'.\",\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return globals()[f\"_{name}\"]\n\n    if name == \"split_arg_string\":\n        from .shell_completion import split_arg_string\n\n        warnings.warn(\n            \"Importing 'parser.split_arg_string' is deprecated, it will only be\"\n            \" available in 'shell_completion' in Click 9.0.\",\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return split_arg_string\n\n    raise AttributeError(name)\n"
  },
  {
    "path": "src/click/py.typed",
    "content": ""
  },
  {
    "path": "src/click/shell_completion.py",
    "content": "from __future__ import annotations\n\nimport collections.abc as cabc\nimport os\nimport re\nimport typing as t\nfrom gettext import gettext as _\n\nfrom .core import Argument\nfrom .core import Command\nfrom .core import Context\nfrom .core import Group\nfrom .core import Option\nfrom .core import Parameter\nfrom .core import ParameterSource\nfrom .utils import echo\n\n\ndef shell_complete(\n    cli: Command,\n    ctx_args: cabc.MutableMapping[str, t.Any],\n    prog_name: str,\n    complete_var: str,\n    instruction: str,\n) -> int:\n    \"\"\"Perform shell completion for the given CLI program.\n\n    :param cli: Command being called.\n    :param ctx_args: Extra arguments to pass to\n        ``cli.make_context``.\n    :param prog_name: Name of the executable in the shell.\n    :param complete_var: Name of the environment variable that holds\n        the completion instruction.\n    :param instruction: Value of ``complete_var`` with the completion\n        instruction and shell, in the form ``instruction_shell``.\n    :return: Status code to exit with.\n    \"\"\"\n    shell, _, instruction = instruction.partition(\"_\")\n    comp_cls = get_completion_class(shell)\n\n    if comp_cls is None:\n        return 1\n\n    comp = comp_cls(cli, ctx_args, prog_name, complete_var)\n\n    if instruction == \"source\":\n        echo(comp.source())\n        return 0\n\n    if instruction == \"complete\":\n        echo(comp.complete())\n        return 0\n\n    return 1\n\n\nclass CompletionItem:\n    \"\"\"Represents a completion value and metadata about the value. The\n    default metadata is ``type`` to indicate special shell handling,\n    and ``help`` if a shell supports showing a help string next to the\n    value.\n\n    Arbitrary parameters can be passed when creating the object, and\n    accessed using ``item.attr``. If an attribute wasn't passed,\n    accessing it returns ``None``.\n\n    :param value: The completion suggestion.\n    :param type: Tells the shell script to provide special completion\n        support for the type. Click uses ``\"dir\"`` and ``\"file\"``.\n    :param help: String shown next to the value if supported.\n    :param kwargs: Arbitrary metadata. The built-in implementations\n        don't use this, but custom type completions paired with custom\n        shell support could use it.\n    \"\"\"\n\n    __slots__ = (\"value\", \"type\", \"help\", \"_info\")\n\n    def __init__(\n        self,\n        value: t.Any,\n        type: str = \"plain\",\n        help: str | None = None,\n        **kwargs: t.Any,\n    ) -> None:\n        self.value: t.Any = value\n        self.type: str = type\n        self.help: str | None = help\n        self._info = kwargs\n\n    def __getattr__(self, name: str) -> t.Any:\n        return self._info.get(name)\n\n\n# Only Bash >= 4.4 has the nosort option.\n_SOURCE_BASH = \"\"\"\\\n%(complete_func)s() {\n    local IFS=$'\\\\n'\n    local response\n\n    response=$(env COMP_WORDS=\"${COMP_WORDS[*]}\" COMP_CWORD=$COMP_CWORD \\\n%(complete_var)s=bash_complete $1)\n\n    for completion in $response; do\n        IFS=',' read type value <<< \"$completion\"\n\n        if [[ $type == 'dir' ]]; then\n            COMPREPLY=()\n            compopt -o dirnames\n        elif [[ $type == 'file' ]]; then\n            COMPREPLY=()\n            compopt -o default\n        elif [[ $type == 'plain' ]]; then\n            COMPREPLY+=($value)\n        fi\n    done\n\n    return 0\n}\n\n%(complete_func)s_setup() {\n    complete -o nosort -F %(complete_func)s %(prog_name)s\n}\n\n%(complete_func)s_setup;\n\"\"\"\n\n# See ZshComplete.format_completion below, and issue #2703, before\n# changing this script.\n#\n# (TL;DR: _describe is picky about the format, but this Zsh script snippet\n# is already widely deployed.  So freeze this script, and use clever-ish\n# handling of colons in ZshComplet.format_completion.)\n_SOURCE_ZSH = \"\"\"\\\n#compdef %(prog_name)s\n\n%(complete_func)s() {\n    local -a completions\n    local -a completions_with_descriptions\n    local -a response\n    (( ! $+commands[%(prog_name)s] )) && return 1\n\n    response=(\"${(@f)$(env COMP_WORDS=\"${words[*]}\" COMP_CWORD=$((CURRENT-1)) \\\n%(complete_var)s=zsh_complete %(prog_name)s)}\")\n\n    for type key descr in ${response}; do\n        if [[ \"$type\" == \"plain\" ]]; then\n            if [[ \"$descr\" == \"_\" ]]; then\n                completions+=(\"$key\")\n            else\n                completions_with_descriptions+=(\"$key\":\"$descr\")\n            fi\n        elif [[ \"$type\" == \"dir\" ]]; then\n            _path_files -/\n        elif [[ \"$type\" == \"file\" ]]; then\n            _path_files -f\n        fi\n    done\n\n    if [ -n \"$completions_with_descriptions\" ]; then\n        _describe -V unsorted completions_with_descriptions -U\n    fi\n\n    if [ -n \"$completions\" ]; then\n        compadd -U -V unsorted -a completions\n    fi\n}\n\nif [[ $zsh_eval_context[-1] == loadautofunc ]]; then\n    # autoload from fpath, call function directly\n    %(complete_func)s \"$@\"\nelse\n    # eval/source/. command, register function for later\n    compdef %(complete_func)s %(prog_name)s\nfi\n\"\"\"\n\n_SOURCE_FISH = \"\"\"\\\nfunction %(complete_func)s;\n    set -l response (env %(complete_var)s=fish_complete COMP_WORDS=(commandline -cp) \\\nCOMP_CWORD=(commandline -t) %(prog_name)s);\n\n    for completion in $response;\n        set -l metadata (string split \",\" $completion);\n\n        if test $metadata[1] = \"dir\";\n            __fish_complete_directories $metadata[2];\n        else if test $metadata[1] = \"file\";\n            __fish_complete_path $metadata[2];\n        else if test $metadata[1] = \"plain\";\n            echo $metadata[2];\n        end;\n    end;\nend;\n\ncomplete --no-files --command %(prog_name)s --arguments \\\n\"(%(complete_func)s)\";\n\"\"\"\n\n\nclass ShellComplete:\n    \"\"\"Base class for providing shell completion support. A subclass for\n    a given shell will override attributes and methods to implement the\n    completion instructions (``source`` and ``complete``).\n\n    :param cli: Command being called.\n    :param prog_name: Name of the executable in the shell.\n    :param complete_var: Name of the environment variable that holds\n        the completion instruction.\n\n    .. versionadded:: 8.0\n    \"\"\"\n\n    name: t.ClassVar[str]\n    \"\"\"Name to register the shell as with :func:`add_completion_class`.\n    This is used in completion instructions (``{name}_source`` and\n    ``{name}_complete``).\n    \"\"\"\n\n    source_template: t.ClassVar[str]\n    \"\"\"Completion script template formatted by :meth:`source`. This must\n    be provided by subclasses.\n    \"\"\"\n\n    def __init__(\n        self,\n        cli: Command,\n        ctx_args: cabc.MutableMapping[str, t.Any],\n        prog_name: str,\n        complete_var: str,\n    ) -> None:\n        self.cli = cli\n        self.ctx_args = ctx_args\n        self.prog_name = prog_name\n        self.complete_var = complete_var\n\n    @property\n    def func_name(self) -> str:\n        \"\"\"The name of the shell function defined by the completion\n        script.\n        \"\"\"\n        safe_name = re.sub(r\"\\W*\", \"\", self.prog_name.replace(\"-\", \"_\"), flags=re.ASCII)\n        return f\"_{safe_name}_completion\"\n\n    def source_vars(self) -> dict[str, t.Any]:\n        \"\"\"Vars for formatting :attr:`source_template`.\n\n        By default this provides ``complete_func``, ``complete_var``,\n        and ``prog_name``.\n        \"\"\"\n        return {\n            \"complete_func\": self.func_name,\n            \"complete_var\": self.complete_var,\n            \"prog_name\": self.prog_name,\n        }\n\n    def source(self) -> str:\n        \"\"\"Produce the shell script that defines the completion\n        function. By default this ``%``-style formats\n        :attr:`source_template` with the dict returned by\n        :meth:`source_vars`.\n        \"\"\"\n        return self.source_template % self.source_vars()\n\n    def get_completion_args(self) -> tuple[list[str], str]:\n        \"\"\"Use the env vars defined by the shell script to return a\n        tuple of ``args, incomplete``. This must be implemented by\n        subclasses.\n        \"\"\"\n        raise NotImplementedError\n\n    def get_completions(self, args: list[str], incomplete: str) -> list[CompletionItem]:\n        \"\"\"Determine the context and last complete command or parameter\n        from the complete args. Call that object's ``shell_complete``\n        method to get the completions for the incomplete value.\n\n        :param args: List of complete args before the incomplete value.\n        :param incomplete: Value being completed. May be empty.\n        \"\"\"\n        ctx = _resolve_context(self.cli, self.ctx_args, self.prog_name, args)\n        obj, incomplete = _resolve_incomplete(ctx, args, incomplete)\n        return obj.shell_complete(ctx, incomplete)\n\n    def format_completion(self, item: CompletionItem) -> str:\n        \"\"\"Format a completion item into the form recognized by the\n        shell script. This must be implemented by subclasses.\n\n        :param item: Completion item to format.\n        \"\"\"\n        raise NotImplementedError\n\n    def complete(self) -> str:\n        \"\"\"Produce the completion data to send back to the shell.\n\n        By default this calls :meth:`get_completion_args`, gets the\n        completions, then calls :meth:`format_completion` for each\n        completion.\n        \"\"\"\n        args, incomplete = self.get_completion_args()\n        completions = self.get_completions(args, incomplete)\n        out = [self.format_completion(item) for item in completions]\n        return \"\\n\".join(out)\n\n\nclass BashComplete(ShellComplete):\n    \"\"\"Shell completion for Bash.\"\"\"\n\n    name = \"bash\"\n    source_template = _SOURCE_BASH\n\n    @staticmethod\n    def _check_version() -> None:\n        import shutil\n        import subprocess\n\n        bash_exe = shutil.which(\"bash\")\n\n        if bash_exe is None:\n            match = None\n        else:\n            output = subprocess.run(\n                [bash_exe, \"--norc\", \"-c\", 'echo \"${BASH_VERSION}\"'],\n                stdout=subprocess.PIPE,\n            )\n            match = re.search(r\"^(\\d+)\\.(\\d+)\\.\\d+\", output.stdout.decode())\n\n        if match is not None:\n            major, minor = match.groups()\n\n            if major < \"4\" or major == \"4\" and minor < \"4\":\n                echo(\n                    _(\n                        \"Shell completion is not supported for Bash\"\n                        \" versions older than 4.4.\"\n                    ),\n                    err=True,\n                )\n        else:\n            echo(\n                _(\"Couldn't detect Bash version, shell completion is not supported.\"),\n                err=True,\n            )\n\n    def source(self) -> str:\n        self._check_version()\n        return super().source()\n\n    def get_completion_args(self) -> tuple[list[str], str]:\n        cwords = split_arg_string(os.environ[\"COMP_WORDS\"])\n        cword = int(os.environ[\"COMP_CWORD\"])\n        args = cwords[1:cword]\n\n        try:\n            incomplete = cwords[cword]\n        except IndexError:\n            incomplete = \"\"\n\n        return args, incomplete\n\n    def format_completion(self, item: CompletionItem) -> str:\n        return f\"{item.type},{item.value}\"\n\n\nclass ZshComplete(ShellComplete):\n    \"\"\"Shell completion for Zsh.\"\"\"\n\n    name = \"zsh\"\n    source_template = _SOURCE_ZSH\n\n    def get_completion_args(self) -> tuple[list[str], str]:\n        cwords = split_arg_string(os.environ[\"COMP_WORDS\"])\n        cword = int(os.environ[\"COMP_CWORD\"])\n        args = cwords[1:cword]\n\n        try:\n            incomplete = cwords[cword]\n        except IndexError:\n            incomplete = \"\"\n\n        return args, incomplete\n\n    def format_completion(self, item: CompletionItem) -> str:\n        help_ = item.help or \"_\"\n        # The zsh completion script uses `_describe` on items with help\n        # texts (which splits the item help from the item value at the\n        # first unescaped colon) and `compadd` on items without help\n        # text (which uses the item value as-is and does not support\n        # colon escaping).  So escape colons in the item value if and\n        # only if the item help is not the sentinel \"_\" value, as used\n        # by the completion script.\n        #\n        # (The zsh completion script is potentially widely deployed, and\n        # thus harder to fix than this method.)\n        #\n        # See issue #1812 and issue #2703 for further context.\n        value = item.value.replace(\":\", r\"\\:\") if help_ != \"_\" else item.value\n        return f\"{item.type}\\n{value}\\n{help_}\"\n\n\nclass FishComplete(ShellComplete):\n    \"\"\"Shell completion for Fish.\"\"\"\n\n    name = \"fish\"\n    source_template = _SOURCE_FISH\n\n    def get_completion_args(self) -> tuple[list[str], str]:\n        cwords = split_arg_string(os.environ[\"COMP_WORDS\"])\n        incomplete = os.environ[\"COMP_CWORD\"]\n        if incomplete:\n            incomplete = split_arg_string(incomplete)[0]\n        args = cwords[1:]\n\n        # Fish stores the partial word in both COMP_WORDS and\n        # COMP_CWORD, remove it from complete args.\n        if incomplete and args and args[-1] == incomplete:\n            args.pop()\n\n        return args, incomplete\n\n    def format_completion(self, item: CompletionItem) -> str:\n        if item.help:\n            return f\"{item.type},{item.value}\\t{item.help}\"\n\n        return f\"{item.type},{item.value}\"\n\n\nShellCompleteType = t.TypeVar(\"ShellCompleteType\", bound=\"type[ShellComplete]\")\n\n\n_available_shells: dict[str, type[ShellComplete]] = {\n    \"bash\": BashComplete,\n    \"fish\": FishComplete,\n    \"zsh\": ZshComplete,\n}\n\n\ndef add_completion_class(\n    cls: ShellCompleteType, name: str | None = None\n) -> ShellCompleteType:\n    \"\"\"Register a :class:`ShellComplete` subclass under the given name.\n    The name will be provided by the completion instruction environment\n    variable during completion.\n\n    :param cls: The completion class that will handle completion for the\n        shell.\n    :param name: Name to register the class under. Defaults to the\n        class's ``name`` attribute.\n    \"\"\"\n    if name is None:\n        name = cls.name\n\n    _available_shells[name] = cls\n\n    return cls\n\n\ndef get_completion_class(shell: str) -> type[ShellComplete] | None:\n    \"\"\"Look up a registered :class:`ShellComplete` subclass by the name\n    provided by the completion instruction environment variable. If the\n    name isn't registered, returns ``None``.\n\n    :param shell: Name the class is registered under.\n    \"\"\"\n    return _available_shells.get(shell)\n\n\ndef split_arg_string(string: str) -> list[str]:\n    \"\"\"Split an argument string as with :func:`shlex.split`, but don't\n    fail if the string is incomplete. Ignores a missing closing quote or\n    incomplete escape sequence and uses the partial token as-is.\n\n    .. code-block:: python\n\n        split_arg_string(\"example 'my file\")\n        [\"example\", \"my file\"]\n\n        split_arg_string(\"example my\\\\\")\n        [\"example\", \"my\"]\n\n    :param string: String to split.\n\n    .. versionchanged:: 8.2\n        Moved to ``shell_completion`` from ``parser``.\n    \"\"\"\n    import shlex\n\n    lex = shlex.shlex(string, posix=True)\n    lex.whitespace_split = True\n    lex.commenters = \"\"\n    out = []\n\n    try:\n        for token in lex:\n            out.append(token)\n    except ValueError:\n        # Raised when end-of-string is reached in an invalid state. Use\n        # the partial token as-is. The quote or escape character is in\n        # lex.state, not lex.token.\n        out.append(lex.token)\n\n    return out\n\n\ndef _is_incomplete_argument(ctx: Context, param: Parameter) -> bool:\n    \"\"\"Determine if the given parameter is an argument that can still\n    accept values.\n\n    :param ctx: Invocation context for the command represented by the\n        parsed complete args.\n    :param param: Argument object being checked.\n    \"\"\"\n    if not isinstance(param, Argument):\n        return False\n\n    assert param.name is not None\n    # Will be None if expose_value is False.\n    value = ctx.params.get(param.name)\n    return (\n        param.nargs == -1\n        or ctx.get_parameter_source(param.name) is not ParameterSource.COMMANDLINE\n        or (\n            param.nargs > 1\n            and isinstance(value, (tuple, list))\n            and len(value) < param.nargs\n        )\n    )\n\n\ndef _start_of_option(ctx: Context, value: str) -> bool:\n    \"\"\"Check if the value looks like the start of an option.\"\"\"\n    if not value:\n        return False\n\n    c = value[0]\n    return c in ctx._opt_prefixes\n\n\ndef _is_incomplete_option(ctx: Context, args: list[str], param: Parameter) -> bool:\n    \"\"\"Determine if the given parameter is an option that needs a value.\n\n    :param args: List of complete args before the incomplete value.\n    :param param: Option object being checked.\n    \"\"\"\n    if not isinstance(param, Option):\n        return False\n\n    if param.is_flag or param.count:\n        return False\n\n    last_option = None\n\n    for index, arg in enumerate(reversed(args)):\n        if index + 1 > param.nargs:\n            break\n\n        if _start_of_option(ctx, arg):\n            last_option = arg\n            break\n\n    return last_option is not None and last_option in param.opts\n\n\ndef _resolve_context(\n    cli: Command,\n    ctx_args: cabc.MutableMapping[str, t.Any],\n    prog_name: str,\n    args: list[str],\n) -> Context:\n    \"\"\"Produce the context hierarchy starting with the command and\n    traversing the complete arguments. This only follows the commands,\n    it doesn't trigger input prompts or callbacks.\n\n    :param cli: Command being called.\n    :param prog_name: Name of the executable in the shell.\n    :param args: List of complete args before the incomplete value.\n    \"\"\"\n    ctx_args[\"resilient_parsing\"] = True\n    with cli.make_context(prog_name, args.copy(), **ctx_args) as ctx:\n        args = ctx._protected_args + ctx.args\n\n        while args:\n            command = ctx.command\n\n            if isinstance(command, Group):\n                if not command.chain:\n                    name, cmd, args = command.resolve_command(ctx, args)\n\n                    if cmd is None:\n                        return ctx\n\n                    with cmd.make_context(\n                        name, args, parent=ctx, resilient_parsing=True\n                    ) as sub_ctx:\n                        ctx = sub_ctx\n                        args = ctx._protected_args + ctx.args\n                else:\n                    sub_ctx = ctx\n\n                    while args:\n                        name, cmd, args = command.resolve_command(ctx, args)\n\n                        if cmd is None:\n                            return ctx\n\n                        with cmd.make_context(\n                            name,\n                            args,\n                            parent=ctx,\n                            allow_extra_args=True,\n                            allow_interspersed_args=False,\n                            resilient_parsing=True,\n                        ) as sub_sub_ctx:\n                            sub_ctx = sub_sub_ctx\n                            args = sub_ctx.args\n\n                    ctx = sub_ctx\n                    args = [*sub_ctx._protected_args, *sub_ctx.args]\n            else:\n                break\n\n    return ctx\n\n\ndef _resolve_incomplete(\n    ctx: Context, args: list[str], incomplete: str\n) -> tuple[Command | Parameter, str]:\n    \"\"\"Find the Click object that will handle the completion of the\n    incomplete value. Return the object and the incomplete value.\n\n    :param ctx: Invocation context for the command represented by\n        the parsed complete args.\n    :param args: List of complete args before the incomplete value.\n    :param incomplete: Value being completed. May be empty.\n    \"\"\"\n    # Different shells treat an \"=\" between a long option name and\n    # value differently. Might keep the value joined, return the \"=\"\n    # as a separate item, or return the split name and value. Always\n    # split and discard the \"=\" to make completion easier.\n    if incomplete == \"=\":\n        incomplete = \"\"\n    elif \"=\" in incomplete and _start_of_option(ctx, incomplete):\n        name, _, incomplete = incomplete.partition(\"=\")\n        args.append(name)\n\n    # The \"--\" marker tells Click to stop treating values as options\n    # even if they start with the option character. If it hasn't been\n    # given and the incomplete arg looks like an option, the current\n    # command will provide option name completions.\n    if \"--\" not in args and _start_of_option(ctx, incomplete):\n        return ctx.command, incomplete\n\n    params = ctx.command.get_params(ctx)\n\n    # If the last complete arg is an option name with an incomplete\n    # value, the option will provide value completions.\n    for param in params:\n        if _is_incomplete_option(ctx, args, param):\n            return param, incomplete\n\n    # It's not an option name or value. The first argument without a\n    # parsed value will provide value completions.\n    for param in params:\n        if _is_incomplete_argument(ctx, param):\n            return param, incomplete\n\n    # There were no unparsed arguments, the command may be a group that\n    # will provide command name completions.\n    return ctx.command, incomplete\n"
  },
  {
    "path": "src/click/termui.py",
    "content": "from __future__ import annotations\n\nimport collections.abc as cabc\nimport inspect\nimport io\nimport itertools\nimport sys\nimport typing as t\nfrom contextlib import AbstractContextManager\nfrom gettext import gettext as _\n\nfrom ._compat import isatty\nfrom ._compat import strip_ansi\nfrom .exceptions import Abort\nfrom .exceptions import UsageError\nfrom .globals import resolve_color_default\nfrom .types import Choice\nfrom .types import convert_type\nfrom .types import ParamType\nfrom .utils import echo\nfrom .utils import LazyFile\n\nif t.TYPE_CHECKING:\n    from ._termui_impl import ProgressBar\n\nV = t.TypeVar(\"V\")\n\n# The prompt functions to use.  The doc tools currently override these\n# functions to customize how they work.\nvisible_prompt_func: t.Callable[[str], str] = input\n\n_ansi_colors = {\n    \"black\": 30,\n    \"red\": 31,\n    \"green\": 32,\n    \"yellow\": 33,\n    \"blue\": 34,\n    \"magenta\": 35,\n    \"cyan\": 36,\n    \"white\": 37,\n    \"reset\": 39,\n    \"bright_black\": 90,\n    \"bright_red\": 91,\n    \"bright_green\": 92,\n    \"bright_yellow\": 93,\n    \"bright_blue\": 94,\n    \"bright_magenta\": 95,\n    \"bright_cyan\": 96,\n    \"bright_white\": 97,\n}\n_ansi_reset_all = \"\\033[0m\"\n\n\ndef hidden_prompt_func(prompt: str) -> str:\n    import getpass\n\n    return getpass.getpass(prompt)\n\n\ndef _build_prompt(\n    text: str,\n    suffix: str,\n    show_default: bool = False,\n    default: t.Any | None = None,\n    show_choices: bool = True,\n    type: ParamType | None = None,\n) -> str:\n    prompt = text\n    if type is not None and show_choices and isinstance(type, Choice):\n        prompt += f\" ({', '.join(map(str, type.choices))})\"\n    if default is not None and show_default:\n        prompt = f\"{prompt} [{_format_default(default)}]\"\n    return f\"{prompt}{suffix}\"\n\n\ndef _format_default(default: t.Any) -> t.Any:\n    if isinstance(default, (io.IOBase, LazyFile)) and hasattr(default, \"name\"):\n        return default.name\n\n    return default\n\n\ndef prompt(\n    text: str,\n    default: t.Any | None = None,\n    hide_input: bool = False,\n    confirmation_prompt: bool | str = False,\n    type: ParamType | t.Any | None = None,\n    value_proc: t.Callable[[str], t.Any] | None = None,\n    prompt_suffix: str = \": \",\n    show_default: bool = True,\n    err: bool = False,\n    show_choices: bool = True,\n) -> t.Any:\n    \"\"\"Prompts a user for input.  This is a convenience function that can\n    be used to prompt a user for input later.\n\n    If the user aborts the input by sending an interrupt signal, this\n    function will catch it and raise a :exc:`Abort` exception.\n\n    :param text: the text to show for the prompt.\n    :param default: the default value to use if no input happens.  If this\n                    is not given it will prompt until it's aborted.\n    :param hide_input: if this is set to true then the input value will\n                       be hidden.\n    :param confirmation_prompt: Prompt a second time to confirm the\n        value. Can be set to a string instead of ``True`` to customize\n        the message.\n    :param type: the type to use to check the value against.\n    :param value_proc: if this parameter is provided it's a function that\n                       is invoked instead of the type conversion to\n                       convert a value.\n    :param prompt_suffix: a suffix that should be added to the prompt.\n    :param show_default: shows or hides the default value in the prompt.\n    :param err: if set to true the file defaults to ``stderr`` instead of\n                ``stdout``, the same as with echo.\n    :param show_choices: Show or hide choices if the passed type is a Choice.\n                         For example if type is a Choice of either day or week,\n                         show_choices is true and text is \"Group by\" then the\n                         prompt will be \"Group by (day, week): \".\n\n    .. versionchanged:: 8.3.1\n        A space is no longer appended to the prompt.\n\n    .. versionadded:: 8.0\n        ``confirmation_prompt`` can be a custom string.\n\n    .. versionadded:: 7.0\n        Added the ``show_choices`` parameter.\n\n    .. versionadded:: 6.0\n        Added unicode support for cmd.exe on Windows.\n\n    .. versionadded:: 4.0\n        Added the `err` parameter.\n\n    \"\"\"\n\n    def prompt_func(text: str) -> str:\n        f = hidden_prompt_func if hide_input else visible_prompt_func\n        try:\n            # Write the prompt separately so that we get nice\n            # coloring through colorama on Windows\n            echo(text[:-1], nl=False, err=err)\n            # Echo the last character to stdout to work around an issue where\n            # readline causes backspace to clear the whole line.\n            return f(text[-1:])\n        except (KeyboardInterrupt, EOFError):\n            # getpass doesn't print a newline if the user aborts input with ^C.\n            # Allegedly this behavior is inherited from getpass(3).\n            # A doc bug has been filed at https://bugs.python.org/issue24711\n            if hide_input:\n                echo(None, err=err)\n            raise Abort() from None\n\n    if value_proc is None:\n        value_proc = convert_type(type, default)\n\n    prompt = _build_prompt(\n        text, prompt_suffix, show_default, default, show_choices, type\n    )\n\n    if confirmation_prompt:\n        if confirmation_prompt is True:\n            confirmation_prompt = _(\"Repeat for confirmation\")\n\n        confirmation_prompt = _build_prompt(confirmation_prompt, prompt_suffix)\n\n    while True:\n        while True:\n            value = prompt_func(prompt)\n            if value:\n                break\n            elif default is not None:\n                value = default\n                break\n        try:\n            result = value_proc(value)\n        except UsageError as e:\n            if hide_input:\n                echo(_(\"Error: The value you entered was invalid.\"), err=err)\n            else:\n                echo(_(\"Error: {e.message}\").format(e=e), err=err)\n            continue\n        if not confirmation_prompt:\n            return result\n        while True:\n            value2 = prompt_func(confirmation_prompt)\n            is_empty = not value and not value2\n            if value2 or is_empty:\n                break\n        if value == value2:\n            return result\n        echo(_(\"Error: The two entered values do not match.\"), err=err)\n\n\ndef confirm(\n    text: str,\n    default: bool | None = False,\n    abort: bool = False,\n    prompt_suffix: str = \": \",\n    show_default: bool = True,\n    err: bool = False,\n) -> bool:\n    \"\"\"Prompts for confirmation (yes/no question).\n\n    If the user aborts the input by sending a interrupt signal this\n    function will catch it and raise a :exc:`Abort` exception.\n\n    :param text: the question to ask.\n    :param default: The default value to use when no input is given. If\n        ``None``, repeat until input is given.\n    :param abort: if this is set to `True` a negative answer aborts the\n                  exception by raising :exc:`Abort`.\n    :param prompt_suffix: a suffix that should be added to the prompt.\n    :param show_default: shows or hides the default value in the prompt.\n    :param err: if set to true the file defaults to ``stderr`` instead of\n                ``stdout``, the same as with echo.\n\n    .. versionchanged:: 8.3.1\n        A space is no longer appended to the prompt.\n\n    .. versionchanged:: 8.0\n        Repeat until input is given if ``default`` is ``None``.\n\n    .. versionadded:: 4.0\n        Added the ``err`` parameter.\n    \"\"\"\n    prompt = _build_prompt(\n        text,\n        prompt_suffix,\n        show_default,\n        \"y/n\" if default is None else (\"Y/n\" if default else \"y/N\"),\n    )\n\n    while True:\n        try:\n            # Write the prompt separately so that we get nice\n            # coloring through colorama on Windows\n            echo(prompt[:-1], nl=False, err=err)\n            # Echo the last character to stdout to work around an issue where\n            # readline causes backspace to clear the whole line.\n            value = visible_prompt_func(prompt[-1:]).lower().strip()\n        except (KeyboardInterrupt, EOFError):\n            raise Abort() from None\n        if value in (\"y\", \"yes\"):\n            rv = True\n        elif value in (\"n\", \"no\"):\n            rv = False\n        elif default is not None and value == \"\":\n            rv = default\n        else:\n            echo(_(\"Error: invalid input\"), err=err)\n            continue\n        break\n    if abort and not rv:\n        raise Abort()\n    return rv\n\n\ndef echo_via_pager(\n    text_or_generator: cabc.Iterable[str] | t.Callable[[], cabc.Iterable[str]] | str,\n    color: bool | None = None,\n) -> None:\n    \"\"\"This function takes a text and shows it via an environment specific\n    pager on stdout.\n\n    .. versionchanged:: 3.0\n       Added the `color` flag.\n\n    :param text_or_generator: the text to page, or alternatively, a\n                              generator emitting the text to page.\n    :param color: controls if the pager supports ANSI colors or not.  The\n                  default is autodetection.\n    \"\"\"\n    color = resolve_color_default(color)\n\n    if inspect.isgeneratorfunction(text_or_generator):\n        i = t.cast(\"t.Callable[[], cabc.Iterable[str]]\", text_or_generator)()\n    elif isinstance(text_or_generator, str):\n        i = [text_or_generator]\n    else:\n        i = iter(t.cast(\"cabc.Iterable[str]\", text_or_generator))\n\n    # convert every element of i to a text type if necessary\n    text_generator = (el if isinstance(el, str) else str(el) for el in i)\n\n    from ._termui_impl import pager\n\n    return pager(itertools.chain(text_generator, \"\\n\"), color)\n\n\n@t.overload\ndef progressbar(\n    *,\n    length: int,\n    label: str | None = None,\n    hidden: bool = False,\n    show_eta: bool = True,\n    show_percent: bool | None = None,\n    show_pos: bool = False,\n    fill_char: str = \"#\",\n    empty_char: str = \"-\",\n    bar_template: str = \"%(label)s  [%(bar)s]  %(info)s\",\n    info_sep: str = \"  \",\n    width: int = 36,\n    file: t.TextIO | None = None,\n    color: bool | None = None,\n    update_min_steps: int = 1,\n) -> ProgressBar[int]: ...\n\n\n@t.overload\ndef progressbar(\n    iterable: cabc.Iterable[V] | None = None,\n    length: int | None = None,\n    label: str | None = None,\n    hidden: bool = False,\n    show_eta: bool = True,\n    show_percent: bool | None = None,\n    show_pos: bool = False,\n    item_show_func: t.Callable[[V | None], str | None] | None = None,\n    fill_char: str = \"#\",\n    empty_char: str = \"-\",\n    bar_template: str = \"%(label)s  [%(bar)s]  %(info)s\",\n    info_sep: str = \"  \",\n    width: int = 36,\n    file: t.TextIO | None = None,\n    color: bool | None = None,\n    update_min_steps: int = 1,\n) -> ProgressBar[V]: ...\n\n\ndef progressbar(\n    iterable: cabc.Iterable[V] | None = None,\n    length: int | None = None,\n    label: str | None = None,\n    hidden: bool = False,\n    show_eta: bool = True,\n    show_percent: bool | None = None,\n    show_pos: bool = False,\n    item_show_func: t.Callable[[V | None], str | None] | None = None,\n    fill_char: str = \"#\",\n    empty_char: str = \"-\",\n    bar_template: str = \"%(label)s  [%(bar)s]  %(info)s\",\n    info_sep: str = \"  \",\n    width: int = 36,\n    file: t.TextIO | None = None,\n    color: bool | None = None,\n    update_min_steps: int = 1,\n) -> ProgressBar[V]:\n    \"\"\"This function creates an iterable context manager that can be used\n    to iterate over something while showing a progress bar.  It will\n    either iterate over the `iterable` or `length` items (that are counted\n    up).  While iteration happens, this function will print a rendered\n    progress bar to the given `file` (defaults to stdout) and will attempt\n    to calculate remaining time and more.  By default, this progress bar\n    will not be rendered if the file is not a terminal.\n\n    The context manager creates the progress bar.  When the context\n    manager is entered the progress bar is already created.  With every\n    iteration over the progress bar, the iterable passed to the bar is\n    advanced and the bar is updated.  When the context manager exits,\n    a newline is printed and the progress bar is finalized on screen.\n\n    Note: The progress bar is currently designed for use cases where the\n    total progress can be expected to take at least several seconds.\n    Because of this, the ProgressBar class object won't display\n    progress that is considered too fast, and progress where the time\n    between steps is less than a second.\n\n    No printing must happen or the progress bar will be unintentionally\n    destroyed.\n\n    Example usage::\n\n        with progressbar(items) as bar:\n            for item in bar:\n                do_something_with(item)\n\n    Alternatively, if no iterable is specified, one can manually update the\n    progress bar through the `update()` method instead of directly\n    iterating over the progress bar.  The update method accepts the number\n    of steps to increment the bar with::\n\n        with progressbar(length=chunks.total_bytes) as bar:\n            for chunk in chunks:\n                process_chunk(chunk)\n                bar.update(chunks.bytes)\n\n    The ``update()`` method also takes an optional value specifying the\n    ``current_item`` at the new position. This is useful when used\n    together with ``item_show_func`` to customize the output for each\n    manual step::\n\n        with click.progressbar(\n            length=total_size,\n            label='Unzipping archive',\n            item_show_func=lambda a: a.filename\n        ) as bar:\n            for archive in zip_file:\n                archive.extract()\n                bar.update(archive.size, archive)\n\n    :param iterable: an iterable to iterate over.  If not provided the length\n                     is required.\n    :param length: the number of items to iterate over.  By default the\n                   progressbar will attempt to ask the iterator about its\n                   length, which might or might not work.  If an iterable is\n                   also provided this parameter can be used to override the\n                   length.  If an iterable is not provided the progress bar\n                   will iterate over a range of that length.\n    :param label: the label to show next to the progress bar.\n    :param hidden: hide the progressbar. Defaults to ``False``. When no tty is\n        detected, it will only print the progressbar label. Setting this to\n        ``False`` also disables that.\n    :param show_eta: enables or disables the estimated time display.  This is\n                     automatically disabled if the length cannot be\n                     determined.\n    :param show_percent: enables or disables the percentage display.  The\n                         default is `True` if the iterable has a length or\n                         `False` if not.\n    :param show_pos: enables or disables the absolute position display.  The\n                     default is `False`.\n    :param item_show_func: A function called with the current item which\n        can return a string to show next to the progress bar. If the\n        function returns ``None`` nothing is shown. The current item can\n        be ``None``, such as when entering and exiting the bar.\n    :param fill_char: the character to use to show the filled part of the\n                      progress bar.\n    :param empty_char: the character to use to show the non-filled part of\n                       the progress bar.\n    :param bar_template: the format string to use as template for the bar.\n                         The parameters in it are ``label`` for the label,\n                         ``bar`` for the progress bar and ``info`` for the\n                         info section.\n    :param info_sep: the separator between multiple info items (eta etc.)\n    :param width: the width of the progress bar in characters, 0 means full\n                  terminal width\n    :param file: The file to write to. If this is not a terminal then\n        only the label is printed.\n    :param color: controls if the terminal supports ANSI colors or not.  The\n                  default is autodetection.  This is only needed if ANSI\n                  codes are included anywhere in the progress bar output\n                  which is not the case by default.\n    :param update_min_steps: Render only when this many updates have\n        completed. This allows tuning for very fast iterators.\n\n    .. versionadded:: 8.2\n        The ``hidden`` argument.\n\n    .. versionchanged:: 8.0\n        Output is shown even if execution time is less than 0.5 seconds.\n\n    .. versionchanged:: 8.0\n        ``item_show_func`` shows the current item, not the previous one.\n\n    .. versionchanged:: 8.0\n        Labels are echoed if the output is not a TTY. Reverts a change\n        in 7.0 that removed all output.\n\n    .. versionadded:: 8.0\n       The ``update_min_steps`` parameter.\n\n    .. versionadded:: 4.0\n        The ``color`` parameter and ``update`` method.\n\n    .. versionadded:: 2.0\n    \"\"\"\n    from ._termui_impl import ProgressBar\n\n    color = resolve_color_default(color)\n    return ProgressBar(\n        iterable=iterable,\n        length=length,\n        hidden=hidden,\n        show_eta=show_eta,\n        show_percent=show_percent,\n        show_pos=show_pos,\n        item_show_func=item_show_func,\n        fill_char=fill_char,\n        empty_char=empty_char,\n        bar_template=bar_template,\n        info_sep=info_sep,\n        file=file,\n        label=label,\n        width=width,\n        color=color,\n        update_min_steps=update_min_steps,\n    )\n\n\ndef clear() -> None:\n    \"\"\"Clears the terminal screen.  This will have the effect of clearing\n    the whole visible space of the terminal and moving the cursor to the\n    top left.  This does not do anything if not connected to a terminal.\n\n    .. versionadded:: 2.0\n    \"\"\"\n    if not isatty(sys.stdout):\n        return\n\n    # ANSI escape \\033[2J clears the screen, \\033[1;1H moves the cursor\n    echo(\"\\033[2J\\033[1;1H\", nl=False)\n\n\ndef _interpret_color(color: int | tuple[int, int, int] | str, offset: int = 0) -> str:\n    if isinstance(color, int):\n        return f\"{38 + offset};5;{color:d}\"\n\n    if isinstance(color, (tuple, list)):\n        r, g, b = color\n        return f\"{38 + offset};2;{r:d};{g:d};{b:d}\"\n\n    return str(_ansi_colors[color] + offset)\n\n\ndef style(\n    text: t.Any,\n    fg: int | tuple[int, int, int] | str | None = None,\n    bg: int | tuple[int, int, int] | str | None = None,\n    bold: bool | None = None,\n    dim: bool | None = None,\n    underline: bool | None = None,\n    overline: bool | None = None,\n    italic: bool | None = None,\n    blink: bool | None = None,\n    reverse: bool | None = None,\n    strikethrough: bool | None = None,\n    reset: bool = True,\n) -> str:\n    \"\"\"Styles a text with ANSI styles and returns the new string.  By\n    default the styling is self contained which means that at the end\n    of the string a reset code is issued.  This can be prevented by\n    passing ``reset=False``.\n\n    Examples::\n\n        click.echo(click.style('Hello World!', fg='green'))\n        click.echo(click.style('ATTENTION!', blink=True))\n        click.echo(click.style('Some things', reverse=True, fg='cyan'))\n        click.echo(click.style('More colors', fg=(255, 12, 128), bg=117))\n\n    Supported color names:\n\n    * ``black`` (might be a gray)\n    * ``red``\n    * ``green``\n    * ``yellow`` (might be an orange)\n    * ``blue``\n    * ``magenta``\n    * ``cyan``\n    * ``white`` (might be light gray)\n    * ``bright_black``\n    * ``bright_red``\n    * ``bright_green``\n    * ``bright_yellow``\n    * ``bright_blue``\n    * ``bright_magenta``\n    * ``bright_cyan``\n    * ``bright_white``\n    * ``reset`` (reset the color code only)\n\n    If the terminal supports it, color may also be specified as:\n\n    -   An integer in the interval [0, 255]. The terminal must support\n        8-bit/256-color mode.\n    -   An RGB tuple of three integers in [0, 255]. The terminal must\n        support 24-bit/true-color mode.\n\n    See https://en.wikipedia.org/wiki/ANSI_color and\n    https://gist.github.com/XVilka/8346728 for more information.\n\n    :param text: the string to style with ansi codes.\n    :param fg: if provided this will become the foreground color.\n    :param bg: if provided this will become the background color.\n    :param bold: if provided this will enable or disable bold mode.\n    :param dim: if provided this will enable or disable dim mode.  This is\n                badly supported.\n    :param underline: if provided this will enable or disable underline.\n    :param overline: if provided this will enable or disable overline.\n    :param italic: if provided this will enable or disable italic.\n    :param blink: if provided this will enable or disable blinking.\n    :param reverse: if provided this will enable or disable inverse\n                    rendering (foreground becomes background and the\n                    other way round).\n    :param strikethrough: if provided this will enable or disable\n        striking through text.\n    :param reset: by default a reset-all code is added at the end of the\n                  string which means that styles do not carry over.  This\n                  can be disabled to compose styles.\n\n    .. versionchanged:: 8.0\n        A non-string ``message`` is converted to a string.\n\n    .. versionchanged:: 8.0\n       Added support for 256 and RGB color codes.\n\n    .. versionchanged:: 8.0\n        Added the ``strikethrough``, ``italic``, and ``overline``\n        parameters.\n\n    .. versionchanged:: 7.0\n        Added support for bright colors.\n\n    .. versionadded:: 2.0\n    \"\"\"\n    if not isinstance(text, str):\n        text = str(text)\n\n    bits = []\n\n    if fg:\n        try:\n            bits.append(f\"\\033[{_interpret_color(fg)}m\")\n        except KeyError:\n            raise TypeError(f\"Unknown color {fg!r}\") from None\n\n    if bg:\n        try:\n            bits.append(f\"\\033[{_interpret_color(bg, 10)}m\")\n        except KeyError:\n            raise TypeError(f\"Unknown color {bg!r}\") from None\n\n    if bold is not None:\n        bits.append(f\"\\033[{1 if bold else 22}m\")\n    if dim is not None:\n        bits.append(f\"\\033[{2 if dim else 22}m\")\n    if underline is not None:\n        bits.append(f\"\\033[{4 if underline else 24}m\")\n    if overline is not None:\n        bits.append(f\"\\033[{53 if overline else 55}m\")\n    if italic is not None:\n        bits.append(f\"\\033[{3 if italic else 23}m\")\n    if blink is not None:\n        bits.append(f\"\\033[{5 if blink else 25}m\")\n    if reverse is not None:\n        bits.append(f\"\\033[{7 if reverse else 27}m\")\n    if strikethrough is not None:\n        bits.append(f\"\\033[{9 if strikethrough else 29}m\")\n    bits.append(text)\n    if reset:\n        bits.append(_ansi_reset_all)\n    return \"\".join(bits)\n\n\ndef unstyle(text: str) -> str:\n    \"\"\"Removes ANSI styling information from a string.  Usually it's not\n    necessary to use this function as Click's echo function will\n    automatically remove styling if necessary.\n\n    .. versionadded:: 2.0\n\n    :param text: the text to remove style information from.\n    \"\"\"\n    return strip_ansi(text)\n\n\ndef secho(\n    message: t.Any | None = None,\n    file: t.IO[t.AnyStr] | None = None,\n    nl: bool = True,\n    err: bool = False,\n    color: bool | None = None,\n    **styles: t.Any,\n) -> None:\n    \"\"\"This function combines :func:`echo` and :func:`style` into one\n    call.  As such the following two calls are the same::\n\n        click.secho('Hello World!', fg='green')\n        click.echo(click.style('Hello World!', fg='green'))\n\n    All keyword arguments are forwarded to the underlying functions\n    depending on which one they go with.\n\n    Non-string types will be converted to :class:`str`. However,\n    :class:`bytes` are passed directly to :meth:`echo` without applying\n    style. If you want to style bytes that represent text, call\n    :meth:`bytes.decode` first.\n\n    .. versionchanged:: 8.0\n        A non-string ``message`` is converted to a string. Bytes are\n        passed through without style applied.\n\n    .. versionadded:: 2.0\n    \"\"\"\n    if message is not None and not isinstance(message, (bytes, bytearray)):\n        message = style(message, **styles)\n\n    return echo(message, file=file, nl=nl, err=err, color=color)\n\n\n@t.overload\ndef edit(\n    text: bytes | bytearray,\n    editor: str | None = None,\n    env: cabc.Mapping[str, str] | None = None,\n    require_save: bool = False,\n    extension: str = \".txt\",\n) -> bytes | None: ...\n\n\n@t.overload\ndef edit(\n    text: str,\n    editor: str | None = None,\n    env: cabc.Mapping[str, str] | None = None,\n    require_save: bool = True,\n    extension: str = \".txt\",\n) -> str | None: ...\n\n\n@t.overload\ndef edit(\n    text: None = None,\n    editor: str | None = None,\n    env: cabc.Mapping[str, str] | None = None,\n    require_save: bool = True,\n    extension: str = \".txt\",\n    filename: str | cabc.Iterable[str] | None = None,\n) -> None: ...\n\n\ndef edit(\n    text: str | bytes | bytearray | None = None,\n    editor: str | None = None,\n    env: cabc.Mapping[str, str] | None = None,\n    require_save: bool = True,\n    extension: str = \".txt\",\n    filename: str | cabc.Iterable[str] | None = None,\n) -> str | bytes | bytearray | None:\n    r\"\"\"Edits the given text in the defined editor.  If an editor is given\n    (should be the full path to the executable but the regular operating\n    system search path is used for finding the executable) it overrides\n    the detected editor.  Optionally, some environment variables can be\n    used.  If the editor is closed without changes, `None` is returned.  In\n    case a file is edited directly the return value is always `None` and\n    `require_save` and `extension` are ignored.\n\n    If the editor cannot be opened a :exc:`UsageError` is raised.\n\n    Note for Windows: to simplify cross-platform usage, the newlines are\n    automatically converted from POSIX to Windows and vice versa.  As such,\n    the message here will have ``\\n`` as newline markers.\n\n    :param text: the text to edit.\n    :param editor: optionally the editor to use.  Defaults to automatic\n                   detection.\n    :param env: environment variables to forward to the editor.\n    :param require_save: if this is true, then not saving in the editor\n                         will make the return value become `None`.\n    :param extension: the extension to tell the editor about.  This defaults\n                      to `.txt` but changing this might change syntax\n                      highlighting.\n    :param filename: if provided it will edit this file instead of the\n                     provided text contents.  It will not use a temporary\n                     file as an indirection in that case. If the editor supports\n                     editing multiple files at once, a sequence of files may be\n                     passed as well. Invoke `click.file` once per file instead\n                     if multiple files cannot be managed at once or editing the\n                     files serially is desired.\n\n    .. versionchanged:: 8.2.0\n        ``filename`` now accepts any ``Iterable[str]`` in addition to a ``str``\n        if the ``editor`` supports editing multiple files at once.\n\n    \"\"\"\n    from ._termui_impl import Editor\n\n    ed = Editor(editor=editor, env=env, require_save=require_save, extension=extension)\n\n    if filename is None:\n        return ed.edit(text)\n\n    if isinstance(filename, str):\n        filename = (filename,)\n\n    ed.edit_files(filenames=filename)\n    return None\n\n\ndef launch(url: str, wait: bool = False, locate: bool = False) -> int:\n    \"\"\"This function launches the given URL (or filename) in the default\n    viewer application for this file type.  If this is an executable, it\n    might launch the executable in a new session.  The return value is\n    the exit code of the launched application.  Usually, ``0`` indicates\n    success.\n\n    Examples::\n\n        click.launch('https://click.palletsprojects.com/')\n        click.launch('/my/downloaded/file', locate=True)\n\n    .. versionadded:: 2.0\n\n    :param url: URL or filename of the thing to launch.\n    :param wait: Wait for the program to exit before returning. This\n        only works if the launched program blocks. In particular,\n        ``xdg-open`` on Linux does not block.\n    :param locate: if this is set to `True` then instead of launching the\n                   application associated with the URL it will attempt to\n                   launch a file manager with the file located.  This\n                   might have weird effects if the URL does not point to\n                   the filesystem.\n    \"\"\"\n    from ._termui_impl import open_url\n\n    return open_url(url, wait=wait, locate=locate)\n\n\n# If this is provided, getchar() calls into this instead.  This is used\n# for unittesting purposes.\n_getchar: t.Callable[[bool], str] | None = None\n\n\ndef getchar(echo: bool = False) -> str:\n    \"\"\"Fetches a single character from the terminal and returns it.  This\n    will always return a unicode character and under certain rare\n    circumstances this might return more than one character.  The\n    situations which more than one character is returned is when for\n    whatever reason multiple characters end up in the terminal buffer or\n    standard input was not actually a terminal.\n\n    Note that this will always read from the terminal, even if something\n    is piped into the standard input.\n\n    Note for Windows: in rare cases when typing non-ASCII characters, this\n    function might wait for a second character and then return both at once.\n    This is because certain Unicode characters look like special-key markers.\n\n    .. versionadded:: 2.0\n\n    :param echo: if set to `True`, the character read will also show up on\n                 the terminal.  The default is to not show it.\n    \"\"\"\n    global _getchar\n\n    if _getchar is None:\n        from ._termui_impl import getchar as f\n\n        _getchar = f\n\n    return _getchar(echo)\n\n\ndef raw_terminal() -> AbstractContextManager[int]:\n    from ._termui_impl import raw_terminal as f\n\n    return f()\n\n\ndef pause(info: str | None = None, err: bool = False) -> None:\n    \"\"\"This command stops execution and waits for the user to press any\n    key to continue.  This is similar to the Windows batch \"pause\"\n    command.  If the program is not run through a terminal, this command\n    will instead do nothing.\n\n    .. versionadded:: 2.0\n\n    .. versionadded:: 4.0\n       Added the `err` parameter.\n\n    :param info: The message to print before pausing. Defaults to\n        ``\"Press any key to continue...\"``.\n    :param err: if set to message goes to ``stderr`` instead of\n                ``stdout``, the same as with echo.\n    \"\"\"\n    if not isatty(sys.stdin) or not isatty(sys.stdout):\n        return\n\n    if info is None:\n        info = _(\"Press any key to continue...\")\n\n    try:\n        if info:\n            echo(info, nl=False, err=err)\n        try:\n            getchar()\n        except (KeyboardInterrupt, EOFError):\n            pass\n    finally:\n        if info:\n            echo(err=err)\n"
  },
  {
    "path": "src/click/testing.py",
    "content": "from __future__ import annotations\n\nimport collections.abc as cabc\nimport contextlib\nimport io\nimport os\nimport shlex\nimport sys\nimport tempfile\nimport typing as t\nfrom types import TracebackType\n\nfrom . import _compat\nfrom . import formatting\nfrom . import termui\nfrom . import utils\nfrom ._compat import _find_binary_reader\n\nif t.TYPE_CHECKING:\n    from _typeshed import ReadableBuffer\n\n    from .core import Command\n\n\nclass EchoingStdin:\n    def __init__(self, input: t.BinaryIO, output: t.BinaryIO) -> None:\n        self._input = input\n        self._output = output\n        self._paused = False\n\n    def __getattr__(self, x: str) -> t.Any:\n        return getattr(self._input, x)\n\n    def _echo(self, rv: bytes) -> bytes:\n        if not self._paused:\n            self._output.write(rv)\n\n        return rv\n\n    def read(self, n: int = -1) -> bytes:\n        return self._echo(self._input.read(n))\n\n    def read1(self, n: int = -1) -> bytes:\n        return self._echo(self._input.read1(n))  # type: ignore\n\n    def readline(self, n: int = -1) -> bytes:\n        return self._echo(self._input.readline(n))\n\n    def readlines(self) -> list[bytes]:\n        return [self._echo(x) for x in self._input.readlines()]\n\n    def __iter__(self) -> cabc.Iterator[bytes]:\n        return iter(self._echo(x) for x in self._input)\n\n    def __repr__(self) -> str:\n        return repr(self._input)\n\n\n@contextlib.contextmanager\ndef _pause_echo(stream: EchoingStdin | None) -> cabc.Iterator[None]:\n    if stream is None:\n        yield\n    else:\n        stream._paused = True\n        yield\n        stream._paused = False\n\n\nclass BytesIOCopy(io.BytesIO):\n    \"\"\"Patch ``io.BytesIO`` to let the written stream be copied to another.\n\n    .. versionadded:: 8.2\n    \"\"\"\n\n    def __init__(self, copy_to: io.BytesIO) -> None:\n        super().__init__()\n        self.copy_to = copy_to\n\n    def flush(self) -> None:\n        super().flush()\n        self.copy_to.flush()\n\n    def write(self, b: ReadableBuffer) -> int:\n        self.copy_to.write(b)\n        return super().write(b)\n\n\nclass StreamMixer:\n    \"\"\"Mixes `<stdout>` and `<stderr>` streams.\n\n    The result is available in the ``output`` attribute.\n\n    .. versionadded:: 8.2\n    \"\"\"\n\n    def __init__(self) -> None:\n        self.output: io.BytesIO = io.BytesIO()\n        self.stdout: io.BytesIO = BytesIOCopy(copy_to=self.output)\n        self.stderr: io.BytesIO = BytesIOCopy(copy_to=self.output)\n\n    def __del__(self) -> None:\n        \"\"\"\n        Guarantee that embedded file-like objects are closed in a\n        predictable order, protecting against races between\n        self.output being closed and other streams being flushed on close\n\n        .. versionadded:: 8.2.2\n        \"\"\"\n        self.stderr.close()\n        self.stdout.close()\n        self.output.close()\n\n\nclass _NamedTextIOWrapper(io.TextIOWrapper):\n    def __init__(\n        self, buffer: t.BinaryIO, name: str, mode: str, **kwargs: t.Any\n    ) -> None:\n        super().__init__(buffer, **kwargs)\n        self._name = name\n        self._mode = mode\n\n    @property\n    def name(self) -> str:\n        return self._name\n\n    @property\n    def mode(self) -> str:\n        return self._mode\n\n\ndef make_input_stream(\n    input: str | bytes | t.IO[t.Any] | None, charset: str\n) -> t.BinaryIO:\n    # Is already an input stream.\n    if hasattr(input, \"read\"):\n        rv = _find_binary_reader(t.cast(\"t.IO[t.Any]\", input))\n\n        if rv is not None:\n            return rv\n\n        raise TypeError(\"Could not find binary reader for input stream.\")\n\n    if input is None:\n        input = b\"\"\n    elif isinstance(input, str):\n        input = input.encode(charset)\n\n    return io.BytesIO(input)\n\n\nclass Result:\n    \"\"\"Holds the captured result of an invoked CLI script.\n\n    :param runner: The runner that created the result\n    :param stdout_bytes: The standard output as bytes.\n    :param stderr_bytes: The standard error as bytes.\n    :param output_bytes: A mix of ``stdout_bytes`` and ``stderr_bytes``, as the\n        user would see  it in its terminal.\n    :param return_value: The value returned from the invoked command.\n    :param exit_code: The exit code as integer.\n    :param exception: The exception that happened if one did.\n    :param exc_info: Exception information (exception type, exception instance,\n        traceback type).\n\n    .. versionchanged:: 8.2\n        ``stderr_bytes`` no longer optional, ``output_bytes`` introduced and\n        ``mix_stderr`` has been removed.\n\n    .. versionadded:: 8.0\n        Added ``return_value``.\n    \"\"\"\n\n    def __init__(\n        self,\n        runner: CliRunner,\n        stdout_bytes: bytes,\n        stderr_bytes: bytes,\n        output_bytes: bytes,\n        return_value: t.Any,\n        exit_code: int,\n        exception: BaseException | None,\n        exc_info: tuple[type[BaseException], BaseException, TracebackType]\n        | None = None,\n    ):\n        self.runner = runner\n        self.stdout_bytes = stdout_bytes\n        self.stderr_bytes = stderr_bytes\n        self.output_bytes = output_bytes\n        self.return_value = return_value\n        self.exit_code = exit_code\n        self.exception = exception\n        self.exc_info = exc_info\n\n    @property\n    def output(self) -> str:\n        \"\"\"The terminal output as unicode string, as the user would see it.\n\n        .. versionchanged:: 8.2\n            No longer a proxy for ``self.stdout``. Now has its own independent stream\n            that is mixing `<stdout>` and `<stderr>`, in the order they were written.\n        \"\"\"\n        return self.output_bytes.decode(self.runner.charset, \"replace\").replace(\n            \"\\r\\n\", \"\\n\"\n        )\n\n    @property\n    def stdout(self) -> str:\n        \"\"\"The standard output as unicode string.\"\"\"\n        return self.stdout_bytes.decode(self.runner.charset, \"replace\").replace(\n            \"\\r\\n\", \"\\n\"\n        )\n\n    @property\n    def stderr(self) -> str:\n        \"\"\"The standard error as unicode string.\n\n        .. versionchanged:: 8.2\n            No longer raise an exception, always returns the `<stderr>` string.\n        \"\"\"\n        return self.stderr_bytes.decode(self.runner.charset, \"replace\").replace(\n            \"\\r\\n\", \"\\n\"\n        )\n\n    def __repr__(self) -> str:\n        exc_str = repr(self.exception) if self.exception else \"okay\"\n        return f\"<{type(self).__name__} {exc_str}>\"\n\n\nclass CliRunner:\n    \"\"\"The CLI runner provides functionality to invoke a Click command line\n    script for unittesting purposes in a isolated environment.  This only\n    works in single-threaded systems without any concurrency as it changes the\n    global interpreter state.\n\n    :param charset: the character set for the input and output data.\n    :param env: a dictionary with environment variables for overriding.\n    :param echo_stdin: if this is set to `True`, then reading from `<stdin>` writes\n                       to `<stdout>`.  This is useful for showing examples in\n                       some circumstances.  Note that regular prompts\n                       will automatically echo the input.\n    :param catch_exceptions: Whether to catch any exceptions other than\n                             ``SystemExit`` when running :meth:`~CliRunner.invoke`.\n\n    .. versionchanged:: 8.2\n        Added the ``catch_exceptions`` parameter.\n\n    .. versionchanged:: 8.2\n        ``mix_stderr`` parameter has been removed.\n    \"\"\"\n\n    def __init__(\n        self,\n        charset: str = \"utf-8\",\n        env: cabc.Mapping[str, str | None] | None = None,\n        echo_stdin: bool = False,\n        catch_exceptions: bool = True,\n    ) -> None:\n        self.charset = charset\n        self.env: cabc.Mapping[str, str | None] = env or {}\n        self.echo_stdin = echo_stdin\n        self.catch_exceptions = catch_exceptions\n\n    def get_default_prog_name(self, cli: Command) -> str:\n        \"\"\"Given a command object it will return the default program name\n        for it.  The default is the `name` attribute or ``\"root\"`` if not\n        set.\n        \"\"\"\n        return cli.name or \"root\"\n\n    def make_env(\n        self, overrides: cabc.Mapping[str, str | None] | None = None\n    ) -> cabc.Mapping[str, str | None]:\n        \"\"\"Returns the environment overrides for invoking a script.\"\"\"\n        rv = dict(self.env)\n        if overrides:\n            rv.update(overrides)\n        return rv\n\n    @contextlib.contextmanager\n    def isolation(\n        self,\n        input: str | bytes | t.IO[t.Any] | None = None,\n        env: cabc.Mapping[str, str | None] | None = None,\n        color: bool = False,\n    ) -> cabc.Iterator[tuple[io.BytesIO, io.BytesIO, io.BytesIO]]:\n        \"\"\"A context manager that sets up the isolation for invoking of a\n        command line tool.  This sets up `<stdin>` with the given input data\n        and `os.environ` with the overrides from the given dictionary.\n        This also rebinds some internals in Click to be mocked (like the\n        prompt functionality).\n\n        This is automatically done in the :meth:`invoke` method.\n\n        :param input: the input stream to put into `sys.stdin`.\n        :param env: the environment overrides as dictionary.\n        :param color: whether the output should contain color codes. The\n                      application can still override this explicitly.\n\n        .. versionadded:: 8.2\n            An additional output stream is returned, which is a mix of\n            `<stdout>` and `<stderr>` streams.\n\n        .. versionchanged:: 8.2\n            Always returns the `<stderr>` stream.\n\n        .. versionchanged:: 8.0\n            `<stderr>` is opened with ``errors=\"backslashreplace\"``\n            instead of the default ``\"strict\"``.\n\n        .. versionchanged:: 4.0\n            Added the ``color`` parameter.\n        \"\"\"\n        bytes_input = make_input_stream(input, self.charset)\n        echo_input = None\n\n        old_stdin = sys.stdin\n        old_stdout = sys.stdout\n        old_stderr = sys.stderr\n        old_forced_width = formatting.FORCED_WIDTH\n        formatting.FORCED_WIDTH = 80\n\n        env = self.make_env(env)\n\n        stream_mixer = StreamMixer()\n\n        if self.echo_stdin:\n            bytes_input = echo_input = t.cast(\n                t.BinaryIO, EchoingStdin(bytes_input, stream_mixer.stdout)\n            )\n\n        sys.stdin = text_input = _NamedTextIOWrapper(\n            bytes_input, encoding=self.charset, name=\"<stdin>\", mode=\"r\"\n        )\n\n        if self.echo_stdin:\n            # Force unbuffered reads, otherwise TextIOWrapper reads a\n            # large chunk which is echoed early.\n            text_input._CHUNK_SIZE = 1  # type: ignore\n\n        sys.stdout = _NamedTextIOWrapper(\n            stream_mixer.stdout, encoding=self.charset, name=\"<stdout>\", mode=\"w\"\n        )\n\n        sys.stderr = _NamedTextIOWrapper(\n            stream_mixer.stderr,\n            encoding=self.charset,\n            name=\"<stderr>\",\n            mode=\"w\",\n            errors=\"backslashreplace\",\n        )\n\n        @_pause_echo(echo_input)  # type: ignore\n        def visible_input(prompt: str | None = None) -> str:\n            sys.stdout.write(prompt or \"\")\n            try:\n                val = next(text_input).rstrip(\"\\r\\n\")\n            except StopIteration as e:\n                raise EOFError() from e\n            sys.stdout.write(f\"{val}\\n\")\n            sys.stdout.flush()\n            return val\n\n        @_pause_echo(echo_input)  # type: ignore\n        def hidden_input(prompt: str | None = None) -> str:\n            sys.stdout.write(f\"{prompt or ''}\\n\")\n            sys.stdout.flush()\n            try:\n                return next(text_input).rstrip(\"\\r\\n\")\n            except StopIteration as e:\n                raise EOFError() from e\n\n        @_pause_echo(echo_input)  # type: ignore\n        def _getchar(echo: bool) -> str:\n            char = sys.stdin.read(1)\n\n            if echo:\n                sys.stdout.write(char)\n\n            sys.stdout.flush()\n            return char\n\n        default_color = color\n\n        def should_strip_ansi(\n            stream: t.IO[t.Any] | None = None, color: bool | None = None\n        ) -> bool:\n            if color is None:\n                return not default_color\n            return not color\n\n        old_visible_prompt_func = termui.visible_prompt_func\n        old_hidden_prompt_func = termui.hidden_prompt_func\n        old__getchar_func = termui._getchar\n        old_should_strip_ansi = utils.should_strip_ansi  # type: ignore\n        old__compat_should_strip_ansi = _compat.should_strip_ansi\n        termui.visible_prompt_func = visible_input\n        termui.hidden_prompt_func = hidden_input\n        termui._getchar = _getchar\n        utils.should_strip_ansi = should_strip_ansi  # type: ignore\n        _compat.should_strip_ansi = should_strip_ansi\n\n        old_env = {}\n        try:\n            for key, value in env.items():\n                old_env[key] = os.environ.get(key)\n                if value is None:\n                    try:\n                        del os.environ[key]\n                    except Exception:\n                        pass\n                else:\n                    os.environ[key] = value\n            yield (stream_mixer.stdout, stream_mixer.stderr, stream_mixer.output)\n        finally:\n            for key, value in old_env.items():\n                if value is None:\n                    try:\n                        del os.environ[key]\n                    except Exception:\n                        pass\n                else:\n                    os.environ[key] = value\n            sys.stdout = old_stdout\n            sys.stderr = old_stderr\n            sys.stdin = old_stdin\n            termui.visible_prompt_func = old_visible_prompt_func\n            termui.hidden_prompt_func = old_hidden_prompt_func\n            termui._getchar = old__getchar_func\n            utils.should_strip_ansi = old_should_strip_ansi  # type: ignore\n            _compat.should_strip_ansi = old__compat_should_strip_ansi\n            formatting.FORCED_WIDTH = old_forced_width\n\n    def invoke(\n        self,\n        cli: Command,\n        args: str | cabc.Sequence[str] | None = None,\n        input: str | bytes | t.IO[t.Any] | None = None,\n        env: cabc.Mapping[str, str | None] | None = None,\n        catch_exceptions: bool | None = None,\n        color: bool = False,\n        **extra: t.Any,\n    ) -> Result:\n        \"\"\"Invokes a command in an isolated environment.  The arguments are\n        forwarded directly to the command line script, the `extra` keyword\n        arguments are passed to the :meth:`~clickpkg.Command.main` function of\n        the command.\n\n        This returns a :class:`Result` object.\n\n        :param cli: the command to invoke\n        :param args: the arguments to invoke. It may be given as an iterable\n                     or a string. When given as string it will be interpreted\n                     as a Unix shell command. More details at\n                     :func:`shlex.split`.\n        :param input: the input data for `sys.stdin`.\n        :param env: the environment overrides.\n        :param catch_exceptions: Whether to catch any other exceptions than\n                                 ``SystemExit``. If :data:`None`, the value\n                                 from :class:`CliRunner` is used.\n        :param extra: the keyword arguments to pass to :meth:`main`.\n        :param color: whether the output should contain color codes. The\n                      application can still override this explicitly.\n\n        .. versionadded:: 8.2\n            The result object has the ``output_bytes`` attribute with\n            the mix of ``stdout_bytes`` and ``stderr_bytes``, as the user would\n            see it in its terminal.\n\n        .. versionchanged:: 8.2\n            The result object always returns the ``stderr_bytes`` stream.\n\n        .. versionchanged:: 8.0\n            The result object has the ``return_value`` attribute with\n            the value returned from the invoked command.\n\n        .. versionchanged:: 4.0\n            Added the ``color`` parameter.\n\n        .. versionchanged:: 3.0\n            Added the ``catch_exceptions`` parameter.\n\n        .. versionchanged:: 3.0\n            The result object has the ``exc_info`` attribute with the\n            traceback if available.\n        \"\"\"\n        exc_info = None\n        if catch_exceptions is None:\n            catch_exceptions = self.catch_exceptions\n\n        with self.isolation(input=input, env=env, color=color) as outstreams:\n            return_value = None\n            exception: BaseException | None = None\n            exit_code = 0\n\n            if isinstance(args, str):\n                args = shlex.split(args)\n\n            try:\n                prog_name = extra.pop(\"prog_name\")\n            except KeyError:\n                prog_name = self.get_default_prog_name(cli)\n\n            try:\n                return_value = cli.main(args=args or (), prog_name=prog_name, **extra)\n            except SystemExit as e:\n                exc_info = sys.exc_info()\n                e_code = t.cast(\"int | t.Any | None\", e.code)\n\n                if e_code is None:\n                    e_code = 0\n\n                if e_code != 0:\n                    exception = e\n\n                if not isinstance(e_code, int):\n                    sys.stdout.write(str(e_code))\n                    sys.stdout.write(\"\\n\")\n                    e_code = 1\n\n                exit_code = e_code\n\n            except Exception as e:\n                if not catch_exceptions:\n                    raise\n                exception = e\n                exit_code = 1\n                exc_info = sys.exc_info()\n            finally:\n                sys.stdout.flush()\n                sys.stderr.flush()\n                stdout = outstreams[0].getvalue()\n                stderr = outstreams[1].getvalue()\n                output = outstreams[2].getvalue()\n\n        return Result(\n            runner=self,\n            stdout_bytes=stdout,\n            stderr_bytes=stderr,\n            output_bytes=output,\n            return_value=return_value,\n            exit_code=exit_code,\n            exception=exception,\n            exc_info=exc_info,  # type: ignore\n        )\n\n    @contextlib.contextmanager\n    def isolated_filesystem(\n        self, temp_dir: str | os.PathLike[str] | None = None\n    ) -> cabc.Iterator[str]:\n        \"\"\"A context manager that creates a temporary directory and\n        changes the current working directory to it. This isolates tests\n        that affect the contents of the CWD to prevent them from\n        interfering with each other.\n\n        :param temp_dir: Create the temporary directory under this\n            directory. If given, the created directory is not removed\n            when exiting.\n\n        .. versionchanged:: 8.0\n            Added the ``temp_dir`` parameter.\n        \"\"\"\n        cwd = os.getcwd()\n        dt = tempfile.mkdtemp(dir=temp_dir)\n        os.chdir(dt)\n\n        try:\n            yield dt\n        finally:\n            os.chdir(cwd)\n\n            if temp_dir is None:\n                import shutil\n\n                try:\n                    shutil.rmtree(dt)\n                except OSError:\n                    pass\n"
  },
  {
    "path": "src/click/types.py",
    "content": "from __future__ import annotations\n\nimport collections.abc as cabc\nimport enum\nimport os\nimport stat\nimport sys\nimport typing as t\nfrom datetime import datetime\nfrom gettext import gettext as _\nfrom gettext import ngettext\n\nfrom ._compat import _get_argv_encoding\nfrom ._compat import open_stream\nfrom .exceptions import BadParameter\nfrom .utils import format_filename\nfrom .utils import LazyFile\nfrom .utils import safecall\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    from .core import Context\n    from .core import Parameter\n    from .shell_completion import CompletionItem\n\nParamTypeValue = t.TypeVar(\"ParamTypeValue\")\n\n\nclass ParamType:\n    \"\"\"Represents the type of a parameter. Validates and converts values\n    from the command line or Python into the correct type.\n\n    To implement a custom type, subclass and implement at least the\n    following:\n\n    -   The :attr:`name` class attribute must be set.\n    -   Calling an instance of the type with ``None`` must return\n        ``None``. This is already implemented by default.\n    -   :meth:`convert` must convert string values to the correct type.\n    -   :meth:`convert` must accept values that are already the correct\n        type.\n    -   It must be able to convert a value if the ``ctx`` and ``param``\n        arguments are ``None``. This can occur when converting prompt\n        input.\n    \"\"\"\n\n    is_composite: t.ClassVar[bool] = False\n    arity: t.ClassVar[int] = 1\n\n    #: the descriptive name of this type\n    name: str\n\n    #: if a list of this type is expected and the value is pulled from a\n    #: string environment variable, this is what splits it up.  `None`\n    #: means any whitespace.  For all parameters the general rule is that\n    #: whitespace splits them up.  The exception are paths and files which\n    #: are split by ``os.path.pathsep`` by default (\":\" on Unix and \";\" on\n    #: Windows).\n    envvar_list_splitter: t.ClassVar[str | None] = None\n\n    def to_info_dict(self) -> dict[str, t.Any]:\n        \"\"\"Gather information that could be useful for a tool generating\n        user-facing documentation.\n\n        Use :meth:`click.Context.to_info_dict` to traverse the entire\n        CLI structure.\n\n        .. versionadded:: 8.0\n        \"\"\"\n        # The class name without the \"ParamType\" suffix.\n        param_type = type(self).__name__.partition(\"ParamType\")[0]\n        param_type = param_type.partition(\"ParameterType\")[0]\n\n        # Custom subclasses might not remember to set a name.\n        if hasattr(self, \"name\"):\n            name = self.name\n        else:\n            name = param_type\n\n        return {\"param_type\": param_type, \"name\": name}\n\n    def __call__(\n        self,\n        value: t.Any,\n        param: Parameter | None = None,\n        ctx: Context | None = None,\n    ) -> t.Any:\n        if value is not None:\n            return self.convert(value, param, ctx)\n\n    def get_metavar(self, param: Parameter, ctx: Context) -> str | None:\n        \"\"\"Returns the metavar default for this param if it provides one.\"\"\"\n\n    def get_missing_message(self, param: Parameter, ctx: Context | None) -> str | None:\n        \"\"\"Optionally might return extra information about a missing\n        parameter.\n\n        .. versionadded:: 2.0\n        \"\"\"\n\n    def convert(\n        self, value: t.Any, param: Parameter | None, ctx: Context | None\n    ) -> t.Any:\n        \"\"\"Convert the value to the correct type. This is not called if\n        the value is ``None`` (the missing value).\n\n        This must accept string values from the command line, as well as\n        values that are already the correct type. It may also convert\n        other compatible types.\n\n        The ``param`` and ``ctx`` arguments may be ``None`` in certain\n        situations, such as when converting prompt input.\n\n        If the value cannot be converted, call :meth:`fail` with a\n        descriptive message.\n\n        :param value: The value to convert.\n        :param param: The parameter that is using this type to convert\n            its value. May be ``None``.\n        :param ctx: The current context that arrived at this value. May\n            be ``None``.\n        \"\"\"\n        return value\n\n    def split_envvar_value(self, rv: str) -> cabc.Sequence[str]:\n        \"\"\"Given a value from an environment variable this splits it up\n        into small chunks depending on the defined envvar list splitter.\n\n        If the splitter is set to `None`, which means that whitespace splits,\n        then leading and trailing whitespace is ignored.  Otherwise, leading\n        and trailing splitters usually lead to empty items being included.\n        \"\"\"\n        return (rv or \"\").split(self.envvar_list_splitter)\n\n    def fail(\n        self,\n        message: str,\n        param: Parameter | None = None,\n        ctx: Context | None = None,\n    ) -> t.NoReturn:\n        \"\"\"Helper method to fail with an invalid value message.\"\"\"\n        raise BadParameter(message, ctx=ctx, param=param)\n\n    def shell_complete(\n        self, ctx: Context, param: Parameter, incomplete: str\n    ) -> list[CompletionItem]:\n        \"\"\"Return a list of\n        :class:`~click.shell_completion.CompletionItem` objects for the\n        incomplete value. Most types do not provide completions, but\n        some do, and this allows custom types to provide custom\n        completions as well.\n\n        :param ctx: Invocation context for this command.\n        :param param: The parameter that is requesting completion.\n        :param incomplete: Value being completed. May be empty.\n\n        .. versionadded:: 8.0\n        \"\"\"\n        return []\n\n\nclass CompositeParamType(ParamType):\n    is_composite = True\n\n    @property\n    def arity(self) -> int:  # type: ignore\n        raise NotImplementedError()\n\n\nclass FuncParamType(ParamType):\n    def __init__(self, func: t.Callable[[t.Any], t.Any]) -> None:\n        self.name: str = func.__name__\n        self.func = func\n\n    def to_info_dict(self) -> dict[str, t.Any]:\n        info_dict = super().to_info_dict()\n        info_dict[\"func\"] = self.func\n        return info_dict\n\n    def convert(\n        self, value: t.Any, param: Parameter | None, ctx: Context | None\n    ) -> t.Any:\n        try:\n            return self.func(value)\n        except ValueError:\n            try:\n                value = str(value)\n            except UnicodeError:\n                value = value.decode(\"utf-8\", \"replace\")\n\n            self.fail(value, param, ctx)\n\n\nclass UnprocessedParamType(ParamType):\n    name = \"text\"\n\n    def convert(\n        self, value: t.Any, param: Parameter | None, ctx: Context | None\n    ) -> t.Any:\n        return value\n\n    def __repr__(self) -> str:\n        return \"UNPROCESSED\"\n\n\nclass StringParamType(ParamType):\n    name = \"text\"\n\n    def convert(\n        self, value: t.Any, param: Parameter | None, ctx: Context | None\n    ) -> t.Any:\n        if isinstance(value, bytes):\n            enc = _get_argv_encoding()\n            try:\n                value = value.decode(enc)\n            except UnicodeError:\n                fs_enc = sys.getfilesystemencoding()\n                if fs_enc != enc:\n                    try:\n                        value = value.decode(fs_enc)\n                    except UnicodeError:\n                        value = value.decode(\"utf-8\", \"replace\")\n                else:\n                    value = value.decode(\"utf-8\", \"replace\")\n            return value\n        return str(value)\n\n    def __repr__(self) -> str:\n        return \"STRING\"\n\n\nclass Choice(ParamType, t.Generic[ParamTypeValue]):\n    \"\"\"The choice type allows a value to be checked against a fixed set\n    of supported values.\n\n    You may pass any iterable value which will be converted to a tuple\n    and thus will only be iterated once.\n\n    The resulting value will always be one of the originally passed choices.\n    See :meth:`normalize_choice` for more info on the mapping of strings\n    to choices. See :ref:`choice-opts` for an example.\n\n    :param case_sensitive: Set to false to make choices case\n        insensitive. Defaults to true.\n\n    .. versionchanged:: 8.2.0\n        Non-``str`` ``choices`` are now supported. It can additionally be any\n        iterable. Before you were not recommended to pass anything but a list or\n        tuple.\n\n    .. versionadded:: 8.2.0\n        Choice normalization can be overridden via :meth:`normalize_choice`.\n    \"\"\"\n\n    name = \"choice\"\n\n    def __init__(\n        self, choices: cabc.Iterable[ParamTypeValue], case_sensitive: bool = True\n    ) -> None:\n        self.choices: cabc.Sequence[ParamTypeValue] = tuple(choices)\n        self.case_sensitive = case_sensitive\n\n    def to_info_dict(self) -> dict[str, t.Any]:\n        info_dict = super().to_info_dict()\n        info_dict[\"choices\"] = self.choices\n        info_dict[\"case_sensitive\"] = self.case_sensitive\n        return info_dict\n\n    def _normalized_mapping(\n        self, ctx: Context | None = None\n    ) -> cabc.Mapping[ParamTypeValue, str]:\n        \"\"\"\n        Returns mapping where keys are the original choices and the values are\n        the normalized values that are accepted via the command line.\n\n        This is a simple wrapper around :meth:`normalize_choice`, use that\n        instead which is supported.\n        \"\"\"\n        return {\n            choice: self.normalize_choice(\n                choice=choice,\n                ctx=ctx,\n            )\n            for choice in self.choices\n        }\n\n    def normalize_choice(self, choice: ParamTypeValue, ctx: Context | None) -> str:\n        \"\"\"\n        Normalize a choice value, used to map a passed string to a choice.\n        Each choice must have a unique normalized value.\n\n        By default uses :meth:`Context.token_normalize_func` and if not case\n        sensitive, convert it to a casefolded value.\n\n        .. versionadded:: 8.2.0\n        \"\"\"\n        normed_value = choice.name if isinstance(choice, enum.Enum) else str(choice)\n\n        if ctx is not None and ctx.token_normalize_func is not None:\n            normed_value = ctx.token_normalize_func(normed_value)\n\n        if not self.case_sensitive:\n            normed_value = normed_value.casefold()\n\n        return normed_value\n\n    def get_metavar(self, param: Parameter, ctx: Context) -> str | None:\n        if param.param_type_name == \"option\" and not param.show_choices:  # type: ignore\n            choice_metavars = [\n                convert_type(type(choice)).name.upper() for choice in self.choices\n            ]\n            choices_str = \"|\".join([*dict.fromkeys(choice_metavars)])\n        else:\n            choices_str = \"|\".join(\n                [str(i) for i in self._normalized_mapping(ctx=ctx).values()]\n            )\n\n        # Use curly braces to indicate a required argument.\n        if param.required and param.param_type_name == \"argument\":\n            return f\"{{{choices_str}}}\"\n\n        # Use square braces to indicate an option or optional argument.\n        return f\"[{choices_str}]\"\n\n    def get_missing_message(self, param: Parameter, ctx: Context | None) -> str:\n        \"\"\"\n        Message shown when no choice is passed.\n\n        .. versionchanged:: 8.2.0 Added ``ctx`` argument.\n        \"\"\"\n        return _(\"Choose from:\\n\\t{choices}\").format(\n            choices=\",\\n\\t\".join(self._normalized_mapping(ctx=ctx).values())\n        )\n\n    def convert(\n        self, value: t.Any, param: Parameter | None, ctx: Context | None\n    ) -> ParamTypeValue:\n        \"\"\"\n        For a given value from the parser, normalize it and find its\n        matching normalized value in the list of choices. Then return the\n        matched \"original\" choice.\n        \"\"\"\n        normed_value = self.normalize_choice(choice=value, ctx=ctx)\n        normalized_mapping = self._normalized_mapping(ctx=ctx)\n\n        try:\n            return next(\n                original\n                for original, normalized in normalized_mapping.items()\n                if normalized == normed_value\n            )\n        except StopIteration:\n            self.fail(\n                self.get_invalid_choice_message(value=value, ctx=ctx),\n                param=param,\n                ctx=ctx,\n            )\n\n    def get_invalid_choice_message(self, value: t.Any, ctx: Context | None) -> str:\n        \"\"\"Get the error message when the given choice is invalid.\n\n        :param value: The invalid value.\n\n        .. versionadded:: 8.2\n        \"\"\"\n        choices_str = \", \".join(map(repr, self._normalized_mapping(ctx=ctx).values()))\n        return ngettext(\n            \"{value!r} is not {choice}.\",\n            \"{value!r} is not one of {choices}.\",\n            len(self.choices),\n        ).format(value=value, choice=choices_str, choices=choices_str)\n\n    def __repr__(self) -> str:\n        return f\"Choice({list(self.choices)})\"\n\n    def shell_complete(\n        self, ctx: Context, param: Parameter, incomplete: str\n    ) -> list[CompletionItem]:\n        \"\"\"Complete choices that start with the incomplete value.\n\n        :param ctx: Invocation context for this command.\n        :param param: The parameter that is requesting completion.\n        :param incomplete: Value being completed. May be empty.\n\n        .. versionadded:: 8.0\n        \"\"\"\n        from click.shell_completion import CompletionItem\n\n        str_choices = map(str, self.choices)\n\n        if self.case_sensitive:\n            matched = (c for c in str_choices if c.startswith(incomplete))\n        else:\n            incomplete = incomplete.lower()\n            matched = (c for c in str_choices if c.lower().startswith(incomplete))\n\n        return [CompletionItem(c) for c in matched]\n\n\nclass DateTime(ParamType):\n    \"\"\"The DateTime type converts date strings into `datetime` objects.\n\n    The format strings which are checked are configurable, but default to some\n    common (non-timezone aware) ISO 8601 formats.\n\n    When specifying *DateTime* formats, you should only pass a list or a tuple.\n    Other iterables, like generators, may lead to surprising results.\n\n    The format strings are processed using ``datetime.strptime``, and this\n    consequently defines the format strings which are allowed.\n\n    Parsing is tried using each format, in order, and the first format which\n    parses successfully is used.\n\n    :param formats: A list or tuple of date format strings, in the order in\n                    which they should be tried. Defaults to\n                    ``'%Y-%m-%d'``, ``'%Y-%m-%dT%H:%M:%S'``,\n                    ``'%Y-%m-%d %H:%M:%S'``.\n    \"\"\"\n\n    name = \"datetime\"\n\n    def __init__(self, formats: cabc.Sequence[str] | None = None):\n        self.formats: cabc.Sequence[str] = formats or [\n            \"%Y-%m-%d\",\n            \"%Y-%m-%dT%H:%M:%S\",\n            \"%Y-%m-%d %H:%M:%S\",\n        ]\n\n    def to_info_dict(self) -> dict[str, t.Any]:\n        info_dict = super().to_info_dict()\n        info_dict[\"formats\"] = self.formats\n        return info_dict\n\n    def get_metavar(self, param: Parameter, ctx: Context) -> str | None:\n        return f\"[{'|'.join(self.formats)}]\"\n\n    def _try_to_convert_date(self, value: t.Any, format: str) -> datetime | None:\n        try:\n            return datetime.strptime(value, format)\n        except ValueError:\n            return None\n\n    def convert(\n        self, value: t.Any, param: Parameter | None, ctx: Context | None\n    ) -> t.Any:\n        if isinstance(value, datetime):\n            return value\n\n        for format in self.formats:\n            converted = self._try_to_convert_date(value, format)\n\n            if converted is not None:\n                return converted\n\n        formats_str = \", \".join(map(repr, self.formats))\n        self.fail(\n            ngettext(\n                \"{value!r} does not match the format {format}.\",\n                \"{value!r} does not match the formats {formats}.\",\n                len(self.formats),\n            ).format(value=value, format=formats_str, formats=formats_str),\n            param,\n            ctx,\n        )\n\n    def __repr__(self) -> str:\n        return \"DateTime\"\n\n\nclass _NumberParamTypeBase(ParamType):\n    _number_class: t.ClassVar[type[t.Any]]\n\n    def convert(\n        self, value: t.Any, param: Parameter | None, ctx: Context | None\n    ) -> t.Any:\n        try:\n            return self._number_class(value)\n        except ValueError:\n            self.fail(\n                _(\"{value!r} is not a valid {number_type}.\").format(\n                    value=value, number_type=self.name\n                ),\n                param,\n                ctx,\n            )\n\n\nclass _NumberRangeBase(_NumberParamTypeBase):\n    def __init__(\n        self,\n        min: float | None = None,\n        max: float | None = None,\n        min_open: bool = False,\n        max_open: bool = False,\n        clamp: bool = False,\n    ) -> None:\n        self.min = min\n        self.max = max\n        self.min_open = min_open\n        self.max_open = max_open\n        self.clamp = clamp\n\n    def to_info_dict(self) -> dict[str, t.Any]:\n        info_dict = super().to_info_dict()\n        info_dict.update(\n            min=self.min,\n            max=self.max,\n            min_open=self.min_open,\n            max_open=self.max_open,\n            clamp=self.clamp,\n        )\n        return info_dict\n\n    def convert(\n        self, value: t.Any, param: Parameter | None, ctx: Context | None\n    ) -> t.Any:\n        import operator\n\n        rv = super().convert(value, param, ctx)\n        lt_min: bool = self.min is not None and (\n            operator.le if self.min_open else operator.lt\n        )(rv, self.min)\n        gt_max: bool = self.max is not None and (\n            operator.ge if self.max_open else operator.gt\n        )(rv, self.max)\n\n        if self.clamp:\n            if lt_min:\n                return self._clamp(self.min, 1, self.min_open)  # type: ignore\n\n            if gt_max:\n                return self._clamp(self.max, -1, self.max_open)  # type: ignore\n\n        if lt_min or gt_max:\n            self.fail(\n                _(\"{value} is not in the range {range}.\").format(\n                    value=rv, range=self._describe_range()\n                ),\n                param,\n                ctx,\n            )\n\n        return rv\n\n    def _clamp(self, bound: float, dir: t.Literal[1, -1], open: bool) -> float:\n        \"\"\"Find the valid value to clamp to bound in the given\n        direction.\n\n        :param bound: The boundary value.\n        :param dir: 1 or -1 indicating the direction to move.\n        :param open: If true, the range does not include the bound.\n        \"\"\"\n        raise NotImplementedError\n\n    def _describe_range(self) -> str:\n        \"\"\"Describe the range for use in help text.\"\"\"\n        if self.min is None:\n            op = \"<\" if self.max_open else \"<=\"\n            return f\"x{op}{self.max}\"\n\n        if self.max is None:\n            op = \">\" if self.min_open else \">=\"\n            return f\"x{op}{self.min}\"\n\n        lop = \"<\" if self.min_open else \"<=\"\n        rop = \"<\" if self.max_open else \"<=\"\n        return f\"{self.min}{lop}x{rop}{self.max}\"\n\n    def __repr__(self) -> str:\n        clamp = \" clamped\" if self.clamp else \"\"\n        return f\"<{type(self).__name__} {self._describe_range()}{clamp}>\"\n\n\nclass IntParamType(_NumberParamTypeBase):\n    name = \"integer\"\n    _number_class = int\n\n    def __repr__(self) -> str:\n        return \"INT\"\n\n\nclass IntRange(_NumberRangeBase, IntParamType):\n    \"\"\"Restrict an :data:`click.INT` value to a range of accepted\n    values. See :ref:`ranges`.\n\n    If ``min`` or ``max`` are not passed, any value is accepted in that\n    direction. If ``min_open`` or ``max_open`` are enabled, the\n    corresponding boundary is not included in the range.\n\n    If ``clamp`` is enabled, a value outside the range is clamped to the\n    boundary instead of failing.\n\n    .. versionchanged:: 8.0\n        Added the ``min_open`` and ``max_open`` parameters.\n    \"\"\"\n\n    name = \"integer range\"\n\n    def _clamp(  # type: ignore\n        self, bound: int, dir: t.Literal[1, -1], open: bool\n    ) -> int:\n        if not open:\n            return bound\n\n        return bound + dir\n\n\nclass FloatParamType(_NumberParamTypeBase):\n    name = \"float\"\n    _number_class = float\n\n    def __repr__(self) -> str:\n        return \"FLOAT\"\n\n\nclass FloatRange(_NumberRangeBase, FloatParamType):\n    \"\"\"Restrict a :data:`click.FLOAT` value to a range of accepted\n    values. See :ref:`ranges`.\n\n    If ``min`` or ``max`` are not passed, any value is accepted in that\n    direction. If ``min_open`` or ``max_open`` are enabled, the\n    corresponding boundary is not included in the range.\n\n    If ``clamp`` is enabled, a value outside the range is clamped to the\n    boundary instead of failing. This is not supported if either\n    boundary is marked ``open``.\n\n    .. versionchanged:: 8.0\n        Added the ``min_open`` and ``max_open`` parameters.\n    \"\"\"\n\n    name = \"float range\"\n\n    def __init__(\n        self,\n        min: float | None = None,\n        max: float | None = None,\n        min_open: bool = False,\n        max_open: bool = False,\n        clamp: bool = False,\n    ) -> None:\n        super().__init__(\n            min=min, max=max, min_open=min_open, max_open=max_open, clamp=clamp\n        )\n\n        if (min_open or max_open) and clamp:\n            raise TypeError(\"Clamping is not supported for open bounds.\")\n\n    def _clamp(self, bound: float, dir: t.Literal[1, -1], open: bool) -> float:\n        if not open:\n            return bound\n\n        # Could use math.nextafter here, but clamping an\n        # open float range doesn't seem to be particularly useful. It's\n        # left up to the user to write a callback to do it if needed.\n        raise RuntimeError(\"Clamping is not supported for open bounds.\")\n\n\nclass BoolParamType(ParamType):\n    name = \"boolean\"\n\n    bool_states: dict[str, bool] = {\n        \"1\": True,\n        \"0\": False,\n        \"yes\": True,\n        \"no\": False,\n        \"true\": True,\n        \"false\": False,\n        \"on\": True,\n        \"off\": False,\n        \"t\": True,\n        \"f\": False,\n        \"y\": True,\n        \"n\": False,\n        # Absence of value is considered False.\n        \"\": False,\n    }\n    \"\"\"A mapping of string values to boolean states.\n\n    Mapping is inspired by :py:attr:`configparser.ConfigParser.BOOLEAN_STATES`\n    and extends it.\n\n    .. caution::\n        String values are lower-cased, as the ``str_to_bool`` comparison function\n        below is case-insensitive.\n\n    .. warning::\n        The mapping is not exhaustive, and does not cover all possible boolean strings\n        representations. It will remains as it is to avoid endless bikeshedding.\n\n        Future work my be considered to make this mapping user-configurable from public\n        API.\n    \"\"\"\n\n    @staticmethod\n    def str_to_bool(value: str | bool) -> bool | None:\n        \"\"\"Convert a string to a boolean value.\n\n        If the value is already a boolean, it is returned as-is. If the value is a\n        string, it is stripped of whitespaces and lower-cased, then checked against\n        the known boolean states pre-defined in the `BoolParamType.bool_states` mapping\n        above.\n\n        Returns `None` if the value does not match any known boolean state.\n        \"\"\"\n        if isinstance(value, bool):\n            return value\n        return BoolParamType.bool_states.get(value.strip().lower())\n\n    def convert(\n        self, value: t.Any, param: Parameter | None, ctx: Context | None\n    ) -> bool:\n        normalized = self.str_to_bool(value)\n        if normalized is None:\n            self.fail(\n                _(\n                    \"{value!r} is not a valid boolean. Recognized values: {states}\"\n                ).format(value=value, states=\", \".join(sorted(self.bool_states))),\n                param,\n                ctx,\n            )\n        return normalized\n\n    def __repr__(self) -> str:\n        return \"BOOL\"\n\n\nclass UUIDParameterType(ParamType):\n    name = \"uuid\"\n\n    def convert(\n        self, value: t.Any, param: Parameter | None, ctx: Context | None\n    ) -> t.Any:\n        import uuid\n\n        if isinstance(value, uuid.UUID):\n            return value\n\n        value = value.strip()\n\n        try:\n            return uuid.UUID(value)\n        except ValueError:\n            self.fail(\n                _(\"{value!r} is not a valid UUID.\").format(value=value), param, ctx\n            )\n\n    def __repr__(self) -> str:\n        return \"UUID\"\n\n\nclass File(ParamType):\n    \"\"\"Declares a parameter to be a file for reading or writing.  The file\n    is automatically closed once the context tears down (after the command\n    finished working).\n\n    Files can be opened for reading or writing.  The special value ``-``\n    indicates stdin or stdout depending on the mode.\n\n    By default, the file is opened for reading text data, but it can also be\n    opened in binary mode or for writing.  The encoding parameter can be used\n    to force a specific encoding.\n\n    The `lazy` flag controls if the file should be opened immediately or upon\n    first IO. The default is to be non-lazy for standard input and output\n    streams as well as files opened for reading, `lazy` otherwise. When opening a\n    file lazily for reading, it is still opened temporarily for validation, but\n    will not be held open until first IO. lazy is mainly useful when opening\n    for writing to avoid creating the file until it is needed.\n\n    Files can also be opened atomically in which case all writes go into a\n    separate file in the same folder and upon completion the file will\n    be moved over to the original location.  This is useful if a file\n    regularly read by other users is modified.\n\n    See :ref:`file-args` for more information.\n\n    .. versionchanged:: 2.0\n        Added the ``atomic`` parameter.\n    \"\"\"\n\n    name = \"filename\"\n    envvar_list_splitter: t.ClassVar[str] = os.path.pathsep\n\n    def __init__(\n        self,\n        mode: str = \"r\",\n        encoding: str | None = None,\n        errors: str | None = \"strict\",\n        lazy: bool | None = None,\n        atomic: bool = False,\n    ) -> None:\n        self.mode = mode\n        self.encoding = encoding\n        self.errors = errors\n        self.lazy = lazy\n        self.atomic = atomic\n\n    def to_info_dict(self) -> dict[str, t.Any]:\n        info_dict = super().to_info_dict()\n        info_dict.update(mode=self.mode, encoding=self.encoding)\n        return info_dict\n\n    def resolve_lazy_flag(self, value: str | os.PathLike[str]) -> bool:\n        if self.lazy is not None:\n            return self.lazy\n        if os.fspath(value) == \"-\":\n            return False\n        elif \"w\" in self.mode:\n            return True\n        return False\n\n    def convert(\n        self,\n        value: str | os.PathLike[str] | t.IO[t.Any],\n        param: Parameter | None,\n        ctx: Context | None,\n    ) -> t.IO[t.Any]:\n        if _is_file_like(value):\n            return value\n\n        value = t.cast(\"str | os.PathLike[str]\", value)\n\n        try:\n            lazy = self.resolve_lazy_flag(value)\n\n            if lazy:\n                lf = LazyFile(\n                    value, self.mode, self.encoding, self.errors, atomic=self.atomic\n                )\n\n                if ctx is not None:\n                    ctx.call_on_close(lf.close_intelligently)\n\n                return t.cast(\"t.IO[t.Any]\", lf)\n\n            f, should_close = open_stream(\n                value, self.mode, self.encoding, self.errors, atomic=self.atomic\n            )\n\n            # If a context is provided, we automatically close the file\n            # at the end of the context execution (or flush out).  If a\n            # context does not exist, it's the caller's responsibility to\n            # properly close the file.  This for instance happens when the\n            # type is used with prompts.\n            if ctx is not None:\n                if should_close:\n                    ctx.call_on_close(safecall(f.close))\n                else:\n                    ctx.call_on_close(safecall(f.flush))\n\n            return f\n        except OSError as e:\n            self.fail(f\"'{format_filename(value)}': {e.strerror}\", param, ctx)\n\n    def shell_complete(\n        self, ctx: Context, param: Parameter, incomplete: str\n    ) -> list[CompletionItem]:\n        \"\"\"Return a special completion marker that tells the completion\n        system to use the shell to provide file path completions.\n\n        :param ctx: Invocation context for this command.\n        :param param: The parameter that is requesting completion.\n        :param incomplete: Value being completed. May be empty.\n\n        .. versionadded:: 8.0\n        \"\"\"\n        from click.shell_completion import CompletionItem\n\n        return [CompletionItem(incomplete, type=\"file\")]\n\n\ndef _is_file_like(value: t.Any) -> te.TypeGuard[t.IO[t.Any]]:\n    return hasattr(value, \"read\") or hasattr(value, \"write\")\n\n\nclass Path(ParamType):\n    \"\"\"The ``Path`` type is similar to the :class:`File` type, but\n    returns the filename instead of an open file. Various checks can be\n    enabled to validate the type of file and permissions.\n\n    :param exists: The file or directory needs to exist for the value to\n        be valid. If this is not set to ``True``, and the file does not\n        exist, then all further checks are silently skipped.\n    :param file_okay: Allow a file as a value.\n    :param dir_okay: Allow a directory as a value.\n    :param readable: if true, a readable check is performed.\n    :param writable: if true, a writable check is performed.\n    :param executable: if true, an executable check is performed.\n    :param resolve_path: Make the value absolute and resolve any\n        symlinks. A ``~`` is not expanded, as this is supposed to be\n        done by the shell only.\n    :param allow_dash: Allow a single dash as a value, which indicates\n        a standard stream (but does not open it). Use\n        :func:`~click.open_file` to handle opening this value.\n    :param path_type: Convert the incoming path value to this type. If\n        ``None``, keep Python's default, which is ``str``. Useful to\n        convert to :class:`pathlib.Path`.\n\n    .. versionchanged:: 8.1\n        Added the ``executable`` parameter.\n\n    .. versionchanged:: 8.0\n        Allow passing ``path_type=pathlib.Path``.\n\n    .. versionchanged:: 6.0\n        Added the ``allow_dash`` parameter.\n    \"\"\"\n\n    envvar_list_splitter: t.ClassVar[str] = os.path.pathsep\n\n    def __init__(\n        self,\n        exists: bool = False,\n        file_okay: bool = True,\n        dir_okay: bool = True,\n        writable: bool = False,\n        readable: bool = True,\n        resolve_path: bool = False,\n        allow_dash: bool = False,\n        path_type: type[t.Any] | None = None,\n        executable: bool = False,\n    ):\n        self.exists = exists\n        self.file_okay = file_okay\n        self.dir_okay = dir_okay\n        self.readable = readable\n        self.writable = writable\n        self.executable = executable\n        self.resolve_path = resolve_path\n        self.allow_dash = allow_dash\n        self.type = path_type\n\n        if self.file_okay and not self.dir_okay:\n            self.name: str = _(\"file\")\n        elif self.dir_okay and not self.file_okay:\n            self.name = _(\"directory\")\n        else:\n            self.name = _(\"path\")\n\n    def to_info_dict(self) -> dict[str, t.Any]:\n        info_dict = super().to_info_dict()\n        info_dict.update(\n            exists=self.exists,\n            file_okay=self.file_okay,\n            dir_okay=self.dir_okay,\n            writable=self.writable,\n            readable=self.readable,\n            allow_dash=self.allow_dash,\n        )\n        return info_dict\n\n    def coerce_path_result(\n        self, value: str | os.PathLike[str]\n    ) -> str | bytes | os.PathLike[str]:\n        if self.type is not None and not isinstance(value, self.type):\n            if self.type is str:\n                return os.fsdecode(value)\n            elif self.type is bytes:\n                return os.fsencode(value)\n            else:\n                return t.cast(\"os.PathLike[str]\", self.type(value))\n\n        return value\n\n    def convert(\n        self,\n        value: str | os.PathLike[str],\n        param: Parameter | None,\n        ctx: Context | None,\n    ) -> str | bytes | os.PathLike[str]:\n        rv = value\n\n        is_dash = self.file_okay and self.allow_dash and rv in (b\"-\", \"-\")\n\n        if not is_dash:\n            if self.resolve_path:\n                rv = os.path.realpath(rv)\n\n            try:\n                st = os.stat(rv)\n            except OSError:\n                if not self.exists:\n                    return self.coerce_path_result(rv)\n                self.fail(\n                    _(\"{name} {filename!r} does not exist.\").format(\n                        name=self.name.title(), filename=format_filename(value)\n                    ),\n                    param,\n                    ctx,\n                )\n\n            if not self.file_okay and stat.S_ISREG(st.st_mode):\n                self.fail(\n                    _(\"{name} {filename!r} is a file.\").format(\n                        name=self.name.title(), filename=format_filename(value)\n                    ),\n                    param,\n                    ctx,\n                )\n            if not self.dir_okay and stat.S_ISDIR(st.st_mode):\n                self.fail(\n                    _(\"{name} {filename!r} is a directory.\").format(\n                        name=self.name.title(), filename=format_filename(value)\n                    ),\n                    param,\n                    ctx,\n                )\n\n            if self.readable and not os.access(rv, os.R_OK):\n                self.fail(\n                    _(\"{name} {filename!r} is not readable.\").format(\n                        name=self.name.title(), filename=format_filename(value)\n                    ),\n                    param,\n                    ctx,\n                )\n\n            if self.writable and not os.access(rv, os.W_OK):\n                self.fail(\n                    _(\"{name} {filename!r} is not writable.\").format(\n                        name=self.name.title(), filename=format_filename(value)\n                    ),\n                    param,\n                    ctx,\n                )\n\n            if self.executable and not os.access(value, os.X_OK):\n                self.fail(\n                    _(\"{name} {filename!r} is not executable.\").format(\n                        name=self.name.title(), filename=format_filename(value)\n                    ),\n                    param,\n                    ctx,\n                )\n\n        return self.coerce_path_result(rv)\n\n    def shell_complete(\n        self, ctx: Context, param: Parameter, incomplete: str\n    ) -> list[CompletionItem]:\n        \"\"\"Return a special completion marker that tells the completion\n        system to use the shell to provide path completions for only\n        directories or any paths.\n\n        :param ctx: Invocation context for this command.\n        :param param: The parameter that is requesting completion.\n        :param incomplete: Value being completed. May be empty.\n\n        .. versionadded:: 8.0\n        \"\"\"\n        from click.shell_completion import CompletionItem\n\n        type = \"dir\" if self.dir_okay and not self.file_okay else \"file\"\n        return [CompletionItem(incomplete, type=type)]\n\n\nclass Tuple(CompositeParamType):\n    \"\"\"The default behavior of Click is to apply a type on a value directly.\n    This works well in most cases, except for when `nargs` is set to a fixed\n    count and different types should be used for different items.  In this\n    case the :class:`Tuple` type can be used.  This type can only be used\n    if `nargs` is set to a fixed number.\n\n    For more information see :ref:`tuple-type`.\n\n    This can be selected by using a Python tuple literal as a type.\n\n    :param types: a list of types that should be used for the tuple items.\n    \"\"\"\n\n    def __init__(self, types: cabc.Sequence[type[t.Any] | ParamType]) -> None:\n        self.types: cabc.Sequence[ParamType] = [convert_type(ty) for ty in types]\n\n    def to_info_dict(self) -> dict[str, t.Any]:\n        info_dict = super().to_info_dict()\n        info_dict[\"types\"] = [t.to_info_dict() for t in self.types]\n        return info_dict\n\n    @property\n    def name(self) -> str:  # type: ignore\n        return f\"<{' '.join(ty.name for ty in self.types)}>\"\n\n    @property\n    def arity(self) -> int:  # type: ignore\n        return len(self.types)\n\n    def convert(\n        self, value: t.Any, param: Parameter | None, ctx: Context | None\n    ) -> t.Any:\n        len_type = len(self.types)\n        len_value = len(value)\n\n        if len_value != len_type:\n            self.fail(\n                ngettext(\n                    \"{len_type} values are required, but {len_value} was given.\",\n                    \"{len_type} values are required, but {len_value} were given.\",\n                    len_value,\n                ).format(len_type=len_type, len_value=len_value),\n                param=param,\n                ctx=ctx,\n            )\n\n        return tuple(\n            ty(x, param, ctx) for ty, x in zip(self.types, value, strict=False)\n        )\n\n\ndef convert_type(ty: t.Any | None, default: t.Any | None = None) -> ParamType:\n    \"\"\"Find the most appropriate :class:`ParamType` for the given Python\n    type. If the type isn't provided, it can be inferred from a default\n    value.\n    \"\"\"\n    guessed_type = False\n\n    if ty is None and default is not None:\n        if isinstance(default, (tuple, list)):\n            # If the default is empty, ty will remain None and will\n            # return STRING.\n            if default:\n                item = default[0]\n\n                # A tuple of tuples needs to detect the inner types.\n                # Can't call convert recursively because that would\n                # incorrectly unwind the tuple to a single type.\n                if isinstance(item, (tuple, list)):\n                    ty = tuple(map(type, item))\n                else:\n                    ty = type(item)\n        else:\n            ty = type(default)\n\n        guessed_type = True\n\n    if isinstance(ty, tuple):\n        return Tuple(ty)\n\n    if isinstance(ty, ParamType):\n        return ty\n\n    if ty is str or ty is None:\n        return STRING\n\n    if ty is int:\n        return INT\n\n    if ty is float:\n        return FLOAT\n\n    if ty is bool:\n        return BOOL\n\n    if guessed_type:\n        return STRING\n\n    if __debug__:\n        try:\n            if issubclass(ty, ParamType):\n                raise AssertionError(\n                    f\"Attempted to use an uninstantiated parameter type ({ty}).\"\n                )\n        except TypeError:\n            # ty is an instance (correct), so issubclass fails.\n            pass\n\n    return FuncParamType(ty)\n\n\n#: A dummy parameter type that just does nothing.  From a user's\n#: perspective this appears to just be the same as `STRING` but\n#: internally no string conversion takes place if the input was bytes.\n#: This is usually useful when working with file paths as they can\n#: appear in bytes and unicode.\n#:\n#: For path related uses the :class:`Path` type is a better choice but\n#: there are situations where an unprocessed type is useful which is why\n#: it is is provided.\n#:\n#: .. versionadded:: 4.0\nUNPROCESSED = UnprocessedParamType()\n\n#: A unicode string parameter type which is the implicit default.  This\n#: can also be selected by using ``str`` as type.\nSTRING = StringParamType()\n\n#: An integer parameter.  This can also be selected by using ``int`` as\n#: type.\nINT = IntParamType()\n\n#: A floating point value parameter.  This can also be selected by using\n#: ``float`` as type.\nFLOAT = FloatParamType()\n\n#: A boolean parameter.  This is the default for boolean flags.  This can\n#: also be selected by using ``bool`` as a type.\nBOOL = BoolParamType()\n\n#: A UUID parameter.\nUUID = UUIDParameterType()\n\n\nclass OptionHelpExtra(t.TypedDict, total=False):\n    envvars: tuple[str, ...]\n    default: str\n    range: str\n    required: str\n"
  },
  {
    "path": "src/click/utils.py",
    "content": "from __future__ import annotations\n\nimport collections.abc as cabc\nimport os\nimport re\nimport sys\nimport typing as t\nfrom functools import update_wrapper\nfrom types import ModuleType\nfrom types import TracebackType\n\nfrom ._compat import _default_text_stderr\nfrom ._compat import _default_text_stdout\nfrom ._compat import _find_binary_writer\nfrom ._compat import auto_wrap_for_ansi\nfrom ._compat import binary_streams\nfrom ._compat import open_stream\nfrom ._compat import should_strip_ansi\nfrom ._compat import strip_ansi\nfrom ._compat import text_streams\nfrom ._compat import WIN\nfrom .globals import resolve_color_default\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    P = te.ParamSpec(\"P\")\n\nR = t.TypeVar(\"R\")\n\n\ndef _posixify(name: str) -> str:\n    return \"-\".join(name.split()).lower()\n\n\ndef safecall(func: t.Callable[P, R]) -> t.Callable[P, R | None]:\n    \"\"\"Wraps a function so that it swallows exceptions.\"\"\"\n\n    def wrapper(*args: P.args, **kwargs: P.kwargs) -> R | None:\n        try:\n            return func(*args, **kwargs)\n        except Exception:\n            pass\n        return None\n\n    return update_wrapper(wrapper, func)\n\n\ndef make_str(value: t.Any) -> str:\n    \"\"\"Converts a value into a valid string.\"\"\"\n    if isinstance(value, bytes):\n        try:\n            return value.decode(sys.getfilesystemencoding())\n        except UnicodeError:\n            return value.decode(\"utf-8\", \"replace\")\n    return str(value)\n\n\ndef make_default_short_help(help: str, max_length: int = 45) -> str:\n    \"\"\"Returns a condensed version of help string.\"\"\"\n    # Consider only the first paragraph.\n    paragraph_end = help.find(\"\\n\\n\")\n\n    if paragraph_end != -1:\n        help = help[:paragraph_end]\n\n    # Collapse newlines, tabs, and spaces.\n    words = help.split()\n\n    if not words:\n        return \"\"\n\n    # The first paragraph started with a \"no rewrap\" marker, ignore it.\n    if words[0] == \"\\b\":\n        words = words[1:]\n\n    total_length = 0\n    last_index = len(words) - 1\n\n    for i, word in enumerate(words):\n        total_length += len(word) + (i > 0)\n\n        if total_length > max_length:  # too long, truncate\n            break\n\n        if word[-1] == \".\":  # sentence end, truncate without \"...\"\n            return \" \".join(words[: i + 1])\n\n        if total_length == max_length and i != last_index:\n            break  # not at sentence end, truncate with \"...\"\n    else:\n        return \" \".join(words)  # no truncation needed\n\n    # Account for the length of the suffix.\n    total_length += len(\"...\")\n\n    # remove words until the length is short enough\n    while i > 0:\n        total_length -= len(words[i]) + (i > 0)\n\n        if total_length <= max_length:\n            break\n\n        i -= 1\n\n    return \" \".join(words[:i]) + \"...\"\n\n\nclass LazyFile:\n    \"\"\"A lazy file works like a regular file but it does not fully open\n    the file but it does perform some basic checks early to see if the\n    filename parameter does make sense.  This is useful for safely opening\n    files for writing.\n    \"\"\"\n\n    def __init__(\n        self,\n        filename: str | os.PathLike[str],\n        mode: str = \"r\",\n        encoding: str | None = None,\n        errors: str | None = \"strict\",\n        atomic: bool = False,\n    ):\n        self.name: str = os.fspath(filename)\n        self.mode = mode\n        self.encoding = encoding\n        self.errors = errors\n        self.atomic = atomic\n        self._f: t.IO[t.Any] | None\n        self.should_close: bool\n\n        if self.name == \"-\":\n            self._f, self.should_close = open_stream(filename, mode, encoding, errors)\n        else:\n            if \"r\" in mode:\n                # Open and close the file in case we're opening it for\n                # reading so that we can catch at least some errors in\n                # some cases early.\n                open(filename, mode).close()\n            self._f = None\n            self.should_close = True\n\n    def __getattr__(self, name: str) -> t.Any:\n        return getattr(self.open(), name)\n\n    def __repr__(self) -> str:\n        if self._f is not None:\n            return repr(self._f)\n        return f\"<unopened file '{format_filename(self.name)}' {self.mode}>\"\n\n    def open(self) -> t.IO[t.Any]:\n        \"\"\"Opens the file if it's not yet open.  This call might fail with\n        a :exc:`FileError`.  Not handling this error will produce an error\n        that Click shows.\n        \"\"\"\n        if self._f is not None:\n            return self._f\n        try:\n            rv, self.should_close = open_stream(\n                self.name, self.mode, self.encoding, self.errors, atomic=self.atomic\n            )\n        except OSError as e:\n            from .exceptions import FileError\n\n            raise FileError(self.name, hint=e.strerror) from e\n        self._f = rv\n        return rv\n\n    def close(self) -> None:\n        \"\"\"Closes the underlying file, no matter what.\"\"\"\n        if self._f is not None:\n            self._f.close()\n\n    def close_intelligently(self) -> None:\n        \"\"\"This function only closes the file if it was opened by the lazy\n        file wrapper.  For instance this will never close stdin.\n        \"\"\"\n        if self.should_close:\n            self.close()\n\n    def __enter__(self) -> LazyFile:\n        return self\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_value: BaseException | None,\n        tb: TracebackType | None,\n    ) -> None:\n        self.close_intelligently()\n\n    def __iter__(self) -> cabc.Iterator[t.AnyStr]:\n        self.open()\n        return iter(self._f)  # type: ignore\n\n\nclass KeepOpenFile:\n    def __init__(self, file: t.IO[t.Any]) -> None:\n        self._file: t.IO[t.Any] = file\n\n    def __getattr__(self, name: str) -> t.Any:\n        return getattr(self._file, name)\n\n    def __enter__(self) -> KeepOpenFile:\n        return self\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_value: BaseException | None,\n        tb: TracebackType | None,\n    ) -> None:\n        pass\n\n    def __repr__(self) -> str:\n        return repr(self._file)\n\n    def __iter__(self) -> cabc.Iterator[t.AnyStr]:\n        return iter(self._file)\n\n\ndef echo(\n    message: t.Any | None = None,\n    file: t.IO[t.Any] | None = None,\n    nl: bool = True,\n    err: bool = False,\n    color: bool | None = None,\n) -> None:\n    \"\"\"Print a message and newline to stdout or a file. This should be\n    used instead of :func:`print` because it provides better support\n    for different data, files, and environments.\n\n    Compared to :func:`print`, this does the following:\n\n    -   Ensures that the output encoding is not misconfigured on Linux.\n    -   Supports Unicode in the Windows console.\n    -   Supports writing to binary outputs, and supports writing bytes\n        to text outputs.\n    -   Supports colors and styles on Windows.\n    -   Removes ANSI color and style codes if the output does not look\n        like an interactive terminal.\n    -   Always flushes the output.\n\n    :param message: The string or bytes to output. Other objects are\n        converted to strings.\n    :param file: The file to write to. Defaults to ``stdout``.\n    :param err: Write to ``stderr`` instead of ``stdout``.\n    :param nl: Print a newline after the message. Enabled by default.\n    :param color: Force showing or hiding colors and other styles. By\n        default Click will remove color if the output does not look like\n        an interactive terminal.\n\n    .. versionchanged:: 6.0\n        Support Unicode output on the Windows console. Click does not\n        modify ``sys.stdout``, so ``sys.stdout.write()`` and ``print()``\n        will still not support Unicode.\n\n    .. versionchanged:: 4.0\n        Added the ``color`` parameter.\n\n    .. versionadded:: 3.0\n        Added the ``err`` parameter.\n\n    .. versionchanged:: 2.0\n        Support colors on Windows if colorama is installed.\n    \"\"\"\n    if file is None:\n        if err:\n            file = _default_text_stderr()\n        else:\n            file = _default_text_stdout()\n\n        # There are no standard streams attached to write to. For example,\n        # pythonw on Windows.\n        if file is None:\n            return\n\n    # Convert non bytes/text into the native string type.\n    if message is not None and not isinstance(message, (str, bytes, bytearray)):\n        out: str | bytes | bytearray | None = str(message)\n    else:\n        out = message\n\n    if nl:\n        out = out or \"\"\n        if isinstance(out, str):\n            out += \"\\n\"\n        else:\n            out += b\"\\n\"\n\n    if not out:\n        file.flush()\n        return\n\n    # If there is a message and the value looks like bytes, we manually\n    # need to find the binary stream and write the message in there.\n    # This is done separately so that most stream types will work as you\n    # would expect. Eg: you can write to StringIO for other cases.\n    if isinstance(out, (bytes, bytearray)):\n        binary_file = _find_binary_writer(file)\n\n        if binary_file is not None:\n            file.flush()\n            binary_file.write(out)\n            binary_file.flush()\n            return\n\n    # ANSI style code support. For no message or bytes, nothing happens.\n    # When outputting to a file instead of a terminal, strip codes.\n    else:\n        color = resolve_color_default(color)\n\n        if should_strip_ansi(file, color):\n            out = strip_ansi(out)\n        elif WIN:\n            if auto_wrap_for_ansi is not None:\n                file = auto_wrap_for_ansi(file, color)  # type: ignore\n            elif not color:\n                out = strip_ansi(out)\n\n    file.write(out)  # type: ignore\n    file.flush()\n\n\ndef get_binary_stream(name: t.Literal[\"stdin\", \"stdout\", \"stderr\"]) -> t.BinaryIO:\n    \"\"\"Returns a system stream for byte processing.\n\n    :param name: the name of the stream to open.  Valid names are ``'stdin'``,\n                 ``'stdout'`` and ``'stderr'``\n    \"\"\"\n    opener = binary_streams.get(name)\n    if opener is None:\n        raise TypeError(f\"Unknown standard stream '{name}'\")\n    return opener()\n\n\ndef get_text_stream(\n    name: t.Literal[\"stdin\", \"stdout\", \"stderr\"],\n    encoding: str | None = None,\n    errors: str | None = \"strict\",\n) -> t.TextIO:\n    \"\"\"Returns a system stream for text processing.  This usually returns\n    a wrapped stream around a binary stream returned from\n    :func:`get_binary_stream` but it also can take shortcuts for already\n    correctly configured streams.\n\n    :param name: the name of the stream to open.  Valid names are ``'stdin'``,\n                 ``'stdout'`` and ``'stderr'``\n    :param encoding: overrides the detected default encoding.\n    :param errors: overrides the default error mode.\n    \"\"\"\n    opener = text_streams.get(name)\n    if opener is None:\n        raise TypeError(f\"Unknown standard stream '{name}'\")\n    return opener(encoding, errors)\n\n\ndef open_file(\n    filename: str | os.PathLike[str],\n    mode: str = \"r\",\n    encoding: str | None = None,\n    errors: str | None = \"strict\",\n    lazy: bool = False,\n    atomic: bool = False,\n) -> t.IO[t.Any]:\n    \"\"\"Open a file, with extra behavior to handle ``'-'`` to indicate\n    a standard stream, lazy open on write, and atomic write. Similar to\n    the behavior of the :class:`~click.File` param type.\n\n    If ``'-'`` is given to open ``stdout`` or ``stdin``, the stream is\n    wrapped so that using it in a context manager will not close it.\n    This makes it possible to use the function without accidentally\n    closing a standard stream:\n\n    .. code-block:: python\n\n        with open_file(filename) as f:\n            ...\n\n    :param filename: The name or Path of the file to open, or ``'-'`` for\n        ``stdin``/``stdout``.\n    :param mode: The mode in which to open the file.\n    :param encoding: The encoding to decode or encode a file opened in\n        text mode.\n    :param errors: The error handling mode.\n    :param lazy: Wait to open the file until it is accessed. For read\n        mode, the file is temporarily opened to raise access errors\n        early, then closed until it is read again.\n    :param atomic: Write to a temporary file and replace the given file\n        on close.\n\n    .. versionadded:: 3.0\n    \"\"\"\n    if lazy:\n        return t.cast(\n            \"t.IO[t.Any]\", LazyFile(filename, mode, encoding, errors, atomic=atomic)\n        )\n\n    f, should_close = open_stream(filename, mode, encoding, errors, atomic=atomic)\n\n    if not should_close:\n        f = t.cast(\"t.IO[t.Any]\", KeepOpenFile(f))\n\n    return f\n\n\ndef format_filename(\n    filename: str | bytes | os.PathLike[str] | os.PathLike[bytes],\n    shorten: bool = False,\n) -> str:\n    \"\"\"Format a filename as a string for display. Ensures the filename can be\n    displayed by replacing any invalid bytes or surrogate escapes in the name\n    with the replacement character ``�``.\n\n    Invalid bytes or surrogate escapes will raise an error when written to a\n    stream with ``errors=\"strict\"``. This will typically happen with ``stdout``\n    when the locale is something like ``en_GB.UTF-8``.\n\n    Many scenarios *are* safe to write surrogates though, due to PEP 538 and\n    PEP 540, including:\n\n    -   Writing to ``stderr``, which uses ``errors=\"backslashreplace\"``.\n    -   The system has ``LANG=C.UTF-8``, ``C``, or ``POSIX``. Python opens\n        stdout and stderr with ``errors=\"surrogateescape\"``.\n    -   None of ``LANG/LC_*`` are set. Python assumes ``LANG=C.UTF-8``.\n    -   Python is started in UTF-8 mode  with  ``PYTHONUTF8=1`` or ``-X utf8``.\n        Python opens stdout and stderr with ``errors=\"surrogateescape\"``.\n\n    :param filename: formats a filename for UI display.  This will also convert\n                     the filename into unicode without failing.\n    :param shorten: this optionally shortens the filename to strip of the\n                    path that leads up to it.\n    \"\"\"\n    if shorten:\n        filename = os.path.basename(filename)\n    else:\n        filename = os.fspath(filename)\n\n    if isinstance(filename, bytes):\n        filename = filename.decode(sys.getfilesystemencoding(), \"replace\")\n    else:\n        filename = filename.encode(\"utf-8\", \"surrogateescape\").decode(\n            \"utf-8\", \"replace\"\n        )\n\n    return filename\n\n\ndef get_app_dir(app_name: str, roaming: bool = True, force_posix: bool = False) -> str:\n    r\"\"\"Returns the config folder for the application.  The default behavior\n    is to return whatever is most appropriate for the operating system.\n\n    To give you an idea, for an app called ``\"Foo Bar\"``, something like\n    the following folders could be returned:\n\n    Mac OS X:\n      ``~/Library/Application Support/Foo Bar``\n    Mac OS X (POSIX):\n      ``~/.foo-bar``\n    Unix:\n      ``~/.config/foo-bar``\n    Unix (POSIX):\n      ``~/.foo-bar``\n    Windows (roaming):\n      ``C:\\Users\\<user>\\AppData\\Roaming\\Foo Bar``\n    Windows (not roaming):\n      ``C:\\Users\\<user>\\AppData\\Local\\Foo Bar``\n\n    .. versionadded:: 2.0\n\n    :param app_name: the application name.  This should be properly capitalized\n                     and can contain whitespace.\n    :param roaming: controls if the folder should be roaming or not on Windows.\n                    Has no effect otherwise.\n    :param force_posix: if this is set to `True` then on any POSIX system the\n                        folder will be stored in the home folder with a leading\n                        dot instead of the XDG config home or darwin's\n                        application support folder.\n    \"\"\"\n    if WIN:\n        key = \"APPDATA\" if roaming else \"LOCALAPPDATA\"\n        folder = os.environ.get(key)\n        if folder is None:\n            folder = os.path.expanduser(\"~\")\n        return os.path.join(folder, app_name)\n    if force_posix:\n        return os.path.join(os.path.expanduser(f\"~/.{_posixify(app_name)}\"))\n    if sys.platform == \"darwin\":\n        return os.path.join(\n            os.path.expanduser(\"~/Library/Application Support\"), app_name\n        )\n    return os.path.join(\n        os.environ.get(\"XDG_CONFIG_HOME\", os.path.expanduser(\"~/.config\")),\n        _posixify(app_name),\n    )\n\n\nclass PacifyFlushWrapper:\n    \"\"\"This wrapper is used to catch and suppress BrokenPipeErrors resulting\n    from ``.flush()`` being called on broken pipe during the shutdown/final-GC\n    of the Python interpreter. Notably ``.flush()`` is always called on\n    ``sys.stdout`` and ``sys.stderr``. So as to have minimal impact on any\n    other cleanup code, and the case where the underlying file is not a broken\n    pipe, all calls and attributes are proxied.\n    \"\"\"\n\n    def __init__(self, wrapped: t.IO[t.Any]) -> None:\n        self.wrapped = wrapped\n\n    def flush(self) -> None:\n        try:\n            self.wrapped.flush()\n        except OSError as e:\n            import errno\n\n            if e.errno != errno.EPIPE:\n                raise\n\n    def __getattr__(self, attr: str) -> t.Any:\n        return getattr(self.wrapped, attr)\n\n\ndef _detect_program_name(\n    path: str | None = None, _main: ModuleType | None = None\n) -> str:\n    \"\"\"Determine the command used to run the program, for use in help\n    text. If a file or entry point was executed, the file name is\n    returned. If ``python -m`` was used to execute a module or package,\n    ``python -m name`` is returned.\n\n    This doesn't try to be too precise, the goal is to give a concise\n    name for help text. Files are only shown as their name without the\n    path. ``python`` is only shown for modules, and the full path to\n    ``sys.executable`` is not shown.\n\n    :param path: The Python file being executed. Python puts this in\n        ``sys.argv[0]``, which is used by default.\n    :param _main: The ``__main__`` module. This should only be passed\n        during internal testing.\n\n    .. versionadded:: 8.0\n        Based on command args detection in the Werkzeug reloader.\n\n    :meta private:\n    \"\"\"\n    if _main is None:\n        _main = sys.modules[\"__main__\"]\n\n    if not path:\n        path = sys.argv[0]\n\n    # The value of __package__ indicates how Python was called. It may\n    # not exist if a setuptools script is installed as an egg. It may be\n    # set incorrectly for entry points created with pip on Windows.\n    # It is set to \"\" inside a Shiv or PEX zipapp.\n    if getattr(_main, \"__package__\", None) in {None, \"\"} or (\n        os.name == \"nt\"\n        and _main.__package__ == \"\"\n        and not os.path.exists(path)\n        and os.path.exists(f\"{path}.exe\")\n    ):\n        # Executed a file, like \"python app.py\".\n        return os.path.basename(path)\n\n    # Executed a module, like \"python -m example\".\n    # Rewritten by Python from \"-m script\" to \"/path/to/script.py\".\n    # Need to look at main module to determine how it was executed.\n    py_module = t.cast(str, _main.__package__)\n    name = os.path.splitext(os.path.basename(path))[0]\n\n    # A submodule like \"example.cli\".\n    if name != \"__main__\":\n        py_module = f\"{py_module}.{name}\"\n\n    return f\"python -m {py_module.lstrip('.')}\"\n\n\ndef _expand_args(\n    args: cabc.Iterable[str],\n    *,\n    user: bool = True,\n    env: bool = True,\n    glob_recursive: bool = True,\n) -> list[str]:\n    \"\"\"Simulate Unix shell expansion with Python functions.\n\n    See :func:`glob.glob`, :func:`os.path.expanduser`, and\n    :func:`os.path.expandvars`.\n\n    This is intended for use on Windows, where the shell does not do any\n    expansion. It may not exactly match what a Unix shell would do.\n\n    :param args: List of command line arguments to expand.\n    :param user: Expand user home directory.\n    :param env: Expand environment variables.\n    :param glob_recursive: ``**`` matches directories recursively.\n\n    .. versionchanged:: 8.1\n        Invalid glob patterns are treated as empty expansions rather\n        than raising an error.\n\n    .. versionadded:: 8.0\n\n    :meta private:\n    \"\"\"\n    from glob import glob\n\n    out = []\n\n    for arg in args:\n        if user:\n            arg = os.path.expanduser(arg)\n\n        if env:\n            arg = os.path.expandvars(arg)\n\n        try:\n            matches = glob(arg, recursive=glob_recursive)\n        except re.error:\n            matches = []\n\n        if not matches:\n            out.append(arg)\n        else:\n            out.extend(matches)\n\n    return out\n"
  },
  {
    "path": "tests/conftest.py",
    "content": "import pytest\n\nfrom click.testing import CliRunner\n\n\n@pytest.fixture(scope=\"function\")\ndef runner(request):\n    return CliRunner()\n"
  },
  {
    "path": "tests/test_arguments.py",
    "content": "import sys\nfrom unittest import mock\n\nimport pytest\n\nimport click\nfrom click._utils import UNSET\n\n\ndef test_nargs_star(runner):\n    @click.command()\n    @click.argument(\"src\", nargs=-1)\n    @click.argument(\"dst\")\n    def copy(src, dst):\n        click.echo(f\"src={'|'.join(src)}\")\n        click.echo(f\"dst={dst}\")\n\n    result = runner.invoke(copy, [\"foo.txt\", \"bar.txt\", \"dir\"])\n    assert not result.exception\n    assert result.output.splitlines() == [\"src=foo.txt|bar.txt\", \"dst=dir\"]\n\n\ndef test_nargs_tup(runner):\n    @click.command()\n    @click.argument(\"name\", nargs=1)\n    @click.argument(\"point\", nargs=2, type=click.INT)\n    def copy(name, point):\n        click.echo(f\"name={name}\")\n        x, y = point\n        click.echo(f\"point={x}/{y}\")\n\n    result = runner.invoke(copy, [\"peter\", \"1\", \"2\"])\n    assert not result.exception\n    assert result.output.splitlines() == [\"name=peter\", \"point=1/2\"]\n\n\n@pytest.mark.parametrize(\n    \"opts\",\n    [\n        dict(type=(str, int)),\n        dict(type=click.Tuple([str, int])),\n        dict(nargs=2, type=click.Tuple([str, int])),\n        dict(nargs=2, type=(str, int)),\n    ],\n)\ndef test_nargs_tup_composite(runner, opts):\n    @click.command()\n    @click.argument(\"item\", **opts)\n    def copy(item):\n        name, id = item\n        click.echo(f\"name={name} id={id:d}\")\n\n    result = runner.invoke(copy, [\"peter\", \"1\"])\n    assert result.exception is None\n    assert result.output.splitlines() == [\"name=peter id=1\"]\n\n\ndef test_nargs_mismatch_with_tuple_type():\n    with pytest.raises(ValueError, match=\"nargs.*must be 2.*but it was 3\"):\n\n        @click.command()\n        @click.argument(\"test\", type=(str, int), nargs=3)\n        def cli(_):\n            pass\n\n\ndef test_nargs_err(runner):\n    @click.command()\n    @click.argument(\"x\")\n    def copy(x):\n        click.echo(x)\n\n    result = runner.invoke(copy, [\"foo\"])\n    assert not result.exception\n    assert result.output == \"foo\\n\"\n\n    result = runner.invoke(copy, [\"foo\", \"bar\"])\n    assert result.exit_code == 2\n    assert \"Got unexpected extra argument (bar)\" in result.output\n\n\ndef test_bytes_args(runner, monkeypatch):\n    @click.command()\n    @click.argument(\"arg\")\n    def from_bytes(arg):\n        assert isinstance(arg, str), (\n            \"UTF-8 encoded argument should be implicitly converted to Unicode\"\n        )\n\n    # Simulate empty locale environment variables\n    monkeypatch.setattr(sys, \"getfilesystemencoding\", lambda: \"utf-8\")\n    monkeypatch.setattr(sys, \"getdefaultencoding\", lambda: \"utf-8\")\n    # sys.stdin.encoding is readonly, needs some extra effort to patch.\n    stdin = mock.Mock(wraps=sys.stdin)\n    stdin.encoding = \"utf-8\"\n    monkeypatch.setattr(sys, \"stdin\", stdin)\n\n    runner.invoke(\n        from_bytes,\n        [\"Something outside of ASCII range: 林\".encode()],\n        catch_exceptions=False,\n    )\n\n\ndef test_file_args(runner):\n    @click.command()\n    @click.argument(\"input\", type=click.File(\"rb\"))\n    @click.argument(\"output\", type=click.File(\"wb\"))\n    def inout(input, output):\n        while True:\n            chunk = input.read(1024)\n            if not chunk:\n                break\n            output.write(chunk)\n\n    with runner.isolated_filesystem():\n        result = runner.invoke(inout, [\"-\", \"hello.txt\"], input=\"Hey!\")\n        assert result.output == \"\"\n        assert result.exit_code == 0\n        with open(\"hello.txt\", \"rb\") as f:\n            assert f.read() == b\"Hey!\"\n\n        result = runner.invoke(inout, [\"hello.txt\", \"-\"])\n        assert result.output == \"Hey!\"\n        assert result.exit_code == 0\n\n\ndef test_path_allow_dash(runner):\n    @click.command()\n    @click.argument(\"input\", type=click.Path(allow_dash=True))\n    def foo(input):\n        click.echo(input)\n\n    result = runner.invoke(foo, [\"-\"])\n    assert result.output == \"-\\n\"\n    assert result.exit_code == 0\n\n\ndef test_file_atomics(runner):\n    @click.command()\n    @click.argument(\"output\", type=click.File(\"wb\", atomic=True))\n    def inout(output):\n        output.write(b\"Foo bar baz\\n\")\n        output.flush()\n        with open(output.name, \"rb\") as f:\n            old_content = f.read()\n            assert old_content == b\"OLD\\n\"\n\n    with runner.isolated_filesystem():\n        with open(\"foo.txt\", \"wb\") as f:\n            f.write(b\"OLD\\n\")\n        result = runner.invoke(inout, [\"foo.txt\"], input=\"Hey!\", catch_exceptions=False)\n        assert result.output == \"\"\n        assert result.exit_code == 0\n        with open(\"foo.txt\", \"rb\") as f:\n            assert f.read() == b\"Foo bar baz\\n\"\n\n\ndef test_stdout_default(runner):\n    @click.command()\n    @click.argument(\"output\", type=click.File(\"w\"), default=\"-\")\n    def inout(output):\n        output.write(\"Foo bar baz\\n\")\n        output.flush()\n\n    result = runner.invoke(inout, [])\n    assert not result.exception\n    assert result.output == \"Foo bar baz\\n\"\n    assert result.stdout == \"Foo bar baz\\n\"\n    assert not result.stderr\n\n\n@pytest.mark.parametrize(\n    (\"nargs\", \"value\", \"expect\"),\n    [\n        (2, \"\", None),\n        (2, \"a\", \"Takes 2 values but 1 was given.\"),\n        (2, \"a b\", (\"a\", \"b\")),\n        (2, \"a b c\", \"Takes 2 values but 3 were given.\"),\n        (-1, \"a b c\", (\"a\", \"b\", \"c\")),\n        (-1, \"\", ()),\n    ],\n)\ndef test_nargs_envvar(runner, nargs, value, expect):\n    if nargs == -1:\n        param = click.argument(\"arg\", envvar=\"X\", nargs=nargs)\n    else:\n        param = click.option(\"--arg\", envvar=\"X\", nargs=nargs)\n\n    @click.command()\n    @param\n    def cmd(arg):\n        return arg\n\n    result = runner.invoke(cmd, env={\"X\": value}, standalone_mode=False)\n\n    if isinstance(expect, str):\n        assert isinstance(result.exception, click.BadParameter)\n        assert expect in result.exception.format_message()\n    else:\n        assert result.return_value == expect\n\n\ndef test_nargs_envvar_only_if_values_empty(runner):\n    @click.command()\n    @click.argument(\"arg\", envvar=\"X\", nargs=-1)\n    def cli(arg):\n        return arg\n\n    result = runner.invoke(cli, [\"a\", \"b\"], standalone_mode=False)\n    assert result.return_value == (\"a\", \"b\")\n\n    result = runner.invoke(cli, env={\"X\": \"a\"}, standalone_mode=False)\n    assert result.return_value == (\"a\",)\n\n\ndef test_empty_nargs(runner):\n    @click.command()\n    @click.argument(\"arg\", nargs=-1)\n    def cmd(arg):\n        click.echo(f\"arg:{'|'.join(arg)}\")\n\n    result = runner.invoke(cmd, [])\n    assert result.exit_code == 0\n    assert result.output == \"arg:\\n\"\n\n    @click.command()\n    @click.argument(\"arg\", nargs=-1, required=True)\n    def cmd2(arg):\n        click.echo(f\"arg:{'|'.join(arg)}\")\n\n    result = runner.invoke(cmd2, [])\n    assert result.exit_code == 2\n    assert \"Missing argument 'ARG...'\" in result.output\n\n\ndef test_missing_arg(runner):\n    @click.command()\n    @click.argument(\"arg\")\n    def cmd(arg):\n        click.echo(f\"arg:{arg}\")\n\n    result = runner.invoke(cmd, [])\n    assert result.exit_code == 2\n    assert \"Missing argument 'ARG'.\" in result.output\n\n\n@pytest.mark.parametrize(\n    (\"value\", \"expect_missing\", \"processed_value\"),\n    [\n        # Unspecified type of the argument fallback to string, so everything is\n        # processed the click.STRING type.\n        (\"\", False, \"\"),\n        (\"  \", False, \"  \"),\n        (\"foo\", False, \"foo\"),\n        (\"12\", False, \"12\"),\n        (12, False, \"12\"),\n        (12.1, False, \"12.1\"),\n        (list(), False, \"[]\"),\n        (tuple(), False, \"()\"),\n        (set(), False, \"set()\"),\n        (frozenset(), False, \"frozenset()\"),\n        (dict(), False, \"{}\"),\n        # None is a value that is allowed to be processed by a required argument\n        # because at this stage, the process_value method happens after the default is\n        # applied.\n        (None, False, None),\n        # An UNSET required argument will raise MissingParameter.\n        (UNSET, True, None),\n    ],\n)\ndef test_required_argument(value, expect_missing, processed_value):\n    \"\"\"Test how a required argument is processing the provided values.\"\"\"\n    ctx = click.Context(click.Command(\"\"))\n    argument = click.Argument([\"a\"], required=True)\n\n    if expect_missing:\n        with pytest.raises(click.MissingParameter) as excinfo:\n            argument.process_value(ctx, value)\n        assert str(excinfo.value) == \"Missing parameter: a\"\n\n    else:\n        value = argument.process_value(ctx, value)\n        assert value == processed_value\n\n\ndef test_implicit_non_required(runner):\n    @click.command()\n    @click.argument(\"f\", default=\"test\")\n    def cli(f):\n        click.echo(f)\n\n    result = runner.invoke(cli, [])\n    assert result.exit_code == 0\n    assert result.output == \"test\\n\"\n\n\ndef test_deprecated_usage(runner):\n    @click.command()\n    @click.argument(\"f\", required=False, deprecated=True)\n    def cli(f):\n        click.echo(f)\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert result.exit_code == 0, result.output\n    assert \"[F!]\" in result.output\n\n\n@pytest.mark.parametrize(\"deprecated\", [True, \"USE B INSTEAD\"])\ndef test_deprecated_warning(runner, deprecated):\n    @click.command()\n    @click.argument(\n        \"my-argument\", required=False, deprecated=deprecated, default=\"default argument\"\n    )\n    def cli(my_argument: str):\n        click.echo(f\"{my_argument}\")\n\n    # defaults should not give a deprecated warning\n    result = runner.invoke(cli, [])\n    assert result.exit_code == 0, result.output\n    assert \"is deprecated\" not in result.output\n\n    result = runner.invoke(cli, [\"hello\"])\n    assert result.exit_code == 0, result.output\n    assert \"argument 'MY_ARGUMENT' is deprecated\" in result.output\n\n    if isinstance(deprecated, str):\n        assert deprecated in result.output\n\n\ndef test_deprecated_required(runner):\n    with pytest.raises(ValueError, match=\"is deprecated and still required\"):\n        click.Argument([\"a\"], required=True, deprecated=True)\n\n\ndef test_eat_options(runner):\n    @click.command()\n    @click.option(\"-f\")\n    @click.argument(\"files\", nargs=-1)\n    def cmd(f, files):\n        for filename in files:\n            click.echo(filename)\n        click.echo(f)\n\n    result = runner.invoke(cmd, [\"--\", \"-foo\", \"bar\"])\n    assert result.output.splitlines() == [\"-foo\", \"bar\", \"\"]\n\n    result = runner.invoke(cmd, [\"-f\", \"-x\", \"--\", \"-foo\", \"bar\"])\n    assert result.output.splitlines() == [\"-foo\", \"bar\", \"-x\"]\n\n\ndef test_nargs_star_ordering(runner):\n    @click.command()\n    @click.argument(\"a\", nargs=-1)\n    @click.argument(\"b\")\n    @click.argument(\"c\")\n    def cmd(a, b, c):\n        for arg in (a, b, c):\n            click.echo(arg)\n\n    result = runner.invoke(cmd, [\"a\", \"b\", \"c\"])\n    assert result.output.splitlines() == [\"('a',)\", \"b\", \"c\"]\n\n\ndef test_nargs_specified_plus_star_ordering(runner):\n    @click.command()\n    @click.argument(\"a\", nargs=-1)\n    @click.argument(\"b\")\n    @click.argument(\"c\", nargs=2)\n    def cmd(a, b, c):\n        for arg in (a, b, c):\n            click.echo(arg)\n\n    result = runner.invoke(cmd, [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"])\n    assert result.output.splitlines() == [\"('a', 'b', 'c')\", \"d\", \"('e', 'f')\"]\n\n\n@pytest.mark.parametrize(\n    (\"argument_params\", \"args\", \"expected\"),\n    [\n        # Any iterable with the same number of arguments as nargs is valid.\n        [{\"nargs\": 2, \"default\": (1, 2)}, [], (1, 2)],\n        [{\"nargs\": 2, \"default\": (1.1, 2.2)}, [], (1, 2)],\n        [{\"nargs\": 2, \"default\": (\"1\", \"2\")}, [], (1, 2)],\n        [{\"nargs\": 2, \"default\": (None, None)}, [], (None, None)],\n        [{\"nargs\": 2, \"default\": [1, 2]}, [], (1, 2)],\n        [{\"nargs\": 2, \"default\": {1, 2}}, [], (1, 2)],\n        [{\"nargs\": 2, \"default\": frozenset([1, 2])}, [], (1, 2)],\n        [{\"nargs\": 2, \"default\": {1: \"a\", 2: \"b\"}}, [], (1, 2)],\n        # Empty iterable is valid if default is None.\n        [{\"nargs\": 2, \"default\": None}, [], None],\n        # Arguments overrides the default.\n        [{\"nargs\": 2, \"default\": (1, 2)}, [\"3\", \"4\"], (3, 4)],\n        # Unbounded arguments are allowed to have a default.\n        # See: https://github.com/pallets/click/issues/2164\n        [{\"nargs\": -1, \"default\": [42]}, [], (42,)],\n        [{\"nargs\": -1, \"default\": None}, [], ()],\n        [{\"nargs\": -1, \"default\": {1, 2, 3, 4, 5}}, [], (1, 2, 3, 4, 5)],\n    ],\n)\ndef test_good_defaults_for_nargs(runner, argument_params, args, expected):\n    @click.command()\n    @click.argument(\"a\", type=int, **argument_params)\n    def cmd(a):\n        click.echo(repr(a), nl=False)\n\n    result = runner.invoke(cmd, args)\n    assert result.output == repr(expected)\n\n\n@pytest.mark.parametrize(\n    (\"default\", \"message\"),\n    [\n        # Non-iterables defaults.\n        [\"Yo\", \"Error: Invalid value for '[A]...': Value must be an iterable.\"],\n        [\"\", \"Error: Invalid value for '[A]...': Value must be an iterable.\"],\n        [True, \"Error: Invalid value for '[A]...': Value must be an iterable.\"],\n        [False, \"Error: Invalid value for '[A]...': Value must be an iterable.\"],\n        [12, \"Error: Invalid value for '[A]...': Value must be an iterable.\"],\n        [7.9, \"Error: Invalid value for '[A]...': Value must be an iterable.\"],\n        # Generator default.\n        [(), \"Error: Invalid value for '[A]...': Takes 2 values but 0 were given.\"],\n        # Unset default.\n        [UNSET, \"Error: Missing argument 'A...'.\"],\n        # Tuples defaults with wrong length.\n        [\n            tuple(),\n            \"Error: Invalid value for '[A]...': Takes 2 values but 0 were given.\",\n        ],\n        [(1,), \"Error: Invalid value for '[A]...': Takes 2 values but 1 was given.\"],\n        [\n            (1, 2, 3),\n            \"Error: Invalid value for '[A]...': Takes 2 values but 3 were given.\",\n        ],\n        # Lists defaults with wrong length.\n        [list(), \"Error: Invalid value for '[A]...': Takes 2 values but 0 were given.\"],\n        [[1], \"Error: Invalid value for '[A]...': Takes 2 values but 1 was given.\"],\n        [\n            [1, 2, 3],\n            \"Error: Invalid value for '[A]...': Takes 2 values but 3 were given.\",\n        ],\n        # Sets defaults with wrong length.\n        [set(), \"Error: Invalid value for '[A]...': Takes 2 values but 0 were given.\"],\n        [\n            set([1]),\n            \"Error: Invalid value for '[A]...': Takes 2 values but 1 was given.\",\n        ],\n        [\n            set([1, 2, 3]),\n            \"Error: Invalid value for '[A]...': Takes 2 values but 3 were given.\",\n        ],\n        # Frozensets defaults with wrong length.\n        [\n            frozenset(),\n            \"Error: Invalid value for '[A]...': Takes 2 values but 0 were given.\",\n        ],\n        [\n            frozenset([1]),\n            \"Error: Invalid value for '[A]...': Takes 2 values but 1 was given.\",\n        ],\n        [\n            frozenset([1, 2, 3]),\n            \"Error: Invalid value for '[A]...': Takes 2 values but 3 were given.\",\n        ],\n        # Dictionaries defaults with wrong length.\n        [dict(), \"Error: Invalid value for '[A]...': Takes 2 values but 0 were given.\"],\n        [\n            {1: \"a\"},\n            \"Error: Invalid value for '[A]...': Takes 2 values but 1 was given.\",\n        ],\n        [\n            {1: \"a\", 2: \"b\", 3: \"c\"},\n            \"Error: Invalid value for '[A]...': Takes 2 values but 3 were given.\",\n        ],\n    ],\n)\ndef test_bad_defaults_for_nargs(runner, default, message):\n    \"\"\"Some defaults are not valid when nargs is set.\"\"\"\n\n    @click.command()\n    @click.argument(\"a\", nargs=2, type=int, default=default)\n    def cmd(a):\n        click.echo(repr(a))\n\n    result = runner.invoke(cmd, [])\n    assert message in result.stderr\n\n\ndef test_multiple_param_decls_not_allowed(runner):\n    with pytest.raises(TypeError):\n\n        @click.command()\n        @click.argument(\"x\", click.Choice([\"a\", \"b\"]))\n        def copy(x):\n            click.echo(x)\n\n\ndef test_multiple_not_allowed():\n    with pytest.raises(TypeError, match=\"multiple\"):\n        click.Argument([\"a\"], multiple=True)\n\n\ndef test_subcommand_help(runner):\n    @click.group()\n    @click.argument(\"name\")\n    @click.argument(\"val\")\n    @click.option(\"--opt\")\n    @click.pass_context\n    def cli(ctx, name, val, opt):\n        ctx.obj = dict(name=name, val=val)\n\n    @cli.command()\n    @click.pass_obj\n    def cmd(obj):\n        click.echo(f\"CMD for {obj['name']} with value {obj['val']}\")\n\n    result = runner.invoke(cli, [\"foo\", \"bar\", \"cmd\", \"--help\"])\n    assert not result.exception\n    assert \"Usage: cli NAME VAL cmd [OPTIONS]\" in result.output\n\n\ndef test_nested_subcommand_help(runner):\n    @click.group()\n    @click.argument(\"arg1\")\n    @click.option(\"--opt1\")\n    def cli(arg1, opt1):\n        pass\n\n    @cli.group()\n    @click.argument(\"arg2\")\n    @click.option(\"--opt2\")\n    def cmd(arg2, opt2):\n        pass\n\n    @cmd.command()\n    def subcmd():\n        click.echo(\"subcommand\")\n\n    result = runner.invoke(cli, [\"arg1\", \"cmd\", \"arg2\", \"subcmd\", \"--help\"])\n    assert not result.exception\n    assert \"Usage: cli ARG1 cmd ARG2 subcmd [OPTIONS]\" in result.output\n\n\ndef test_when_argument_decorator_is_used_multiple_times_cls_is_preserved():\n    class CustomArgument(click.Argument):\n        pass\n\n    reusable_argument = click.argument(\"art\", cls=CustomArgument)\n\n    @click.command()\n    @reusable_argument\n    def foo(arg):\n        pass\n\n    @click.command()\n    @reusable_argument\n    def bar(arg):\n        pass\n\n    assert isinstance(foo.params[0], CustomArgument)\n    assert isinstance(bar.params[0], CustomArgument)\n\n\n@pytest.mark.parametrize(\n    \"args_one,args_two\",\n    [\n        (\n            (\"aardvark\",),\n            (\"aardvark\",),\n        ),\n    ],\n)\ndef test_duplicate_names_warning(runner, args_one, args_two):\n    @click.command()\n    @click.argument(*args_one)\n    @click.argument(*args_two)\n    def cli(one, two):\n        pass\n\n    with pytest.warns(UserWarning):\n        runner.invoke(cli, [])\n\n\n@pytest.mark.parametrize(\n    (\"argument_kwargs\", \"pass_argv\"),\n    (\n        # there is a large potential parameter space to explore here\n        # this is just a very small sample of it\n        ({}, [\"myvalue\"]),\n        ({\"nargs\": -1}, []),\n        ({\"nargs\": -1}, [\"myvalue\"]),\n        ({\"default\": None}, [\"myvalue\"]),\n        ({\"required\": False}, []),\n        ({\"required\": False}, [\"myvalue\"]),\n    ),\n)\ndef test_argument_custom_class_can_override_type_cast_value_and_never_sees_unset(\n    runner, argument_kwargs, pass_argv\n):\n    \"\"\"\n    Test that overriding type_cast_value is supported\n\n    In particular, the argument is never passed an UNSET sentinel value.\n    \"\"\"\n\n    class CustomArgument(click.Argument):\n        def type_cast_value(self, ctx, value):\n            assert value is not UNSET\n            return value\n\n    @click.command()\n    @click.argument(\"myarg\", **argument_kwargs, cls=CustomArgument)\n    def cmd(myarg):\n        click.echo(\"ok\")\n\n    result = runner.invoke(cmd, pass_argv)\n    assert not result.exception\n    assert result.exit_code == 0\n"
  },
  {
    "path": "tests/test_basic.py",
    "content": "from __future__ import annotations\n\nimport enum\nimport os\nfrom itertools import chain\n\nimport pytest\n\nimport click\nfrom click._utils import UNSET\n\n\ndef test_basic_functionality(runner):\n    @click.command()\n    def cli():\n        \"\"\"Hello World!\"\"\"\n        click.echo(\"I EXECUTED\")\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert not result.exception\n    assert \"Hello World!\" in result.output\n    assert \"Show this message and exit.\" in result.output\n    assert result.exit_code == 0\n    assert \"I EXECUTED\" not in result.output\n\n    result = runner.invoke(cli, [])\n    assert not result.exception\n    assert \"I EXECUTED\" in result.output\n    assert result.exit_code == 0\n\n\ndef test_repr():\n    @click.command()\n    def command():\n        pass\n\n    @click.group()\n    def group():\n        pass\n\n    @group.command()\n    def subcommand():\n        pass\n\n    assert repr(command) == \"<Command command>\"\n    assert repr(group) == \"<Group group>\"\n    assert repr(subcommand) == \"<Command subcommand>\"\n\n\ndef test_return_values():\n    @click.command()\n    def cli():\n        return 42\n\n    with cli.make_context(\"foo\", []) as ctx:\n        rv = cli.invoke(ctx)\n        assert rv == 42\n\n\ndef test_basic_group(runner):\n    @click.group()\n    def cli():\n        \"\"\"This is the root.\"\"\"\n        click.echo(\"ROOT EXECUTED\")\n\n    @cli.command()\n    def subcommand():\n        \"\"\"This is a subcommand.\"\"\"\n        click.echo(\"SUBCOMMAND EXECUTED\")\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert not result.exception\n    assert \"COMMAND [ARGS]...\" in result.output\n    assert \"This is the root\" in result.output\n    assert \"This is a subcommand.\" in result.output\n    assert result.exit_code == 0\n    assert \"ROOT EXECUTED\" not in result.output\n\n    result = runner.invoke(cli, [\"subcommand\"])\n    assert not result.exception\n    assert result.exit_code == 0\n    assert \"ROOT EXECUTED\" in result.output\n    assert \"SUBCOMMAND EXECUTED\" in result.output\n\n\ndef test_group_commands_dict(runner):\n    \"\"\"A Group can be built with a dict of commands.\"\"\"\n\n    @click.command()\n    def sub():\n        click.echo(\"sub\", nl=False)\n\n    cli = click.Group(commands={\"other\": sub})\n    result = runner.invoke(cli, [\"other\"])\n    assert result.output == \"sub\"\n\n\ndef test_group_from_list(runner):\n    \"\"\"A Group can be built with a list of commands.\"\"\"\n\n    @click.command()\n    def sub():\n        click.echo(\"sub\", nl=False)\n\n    cli = click.Group(commands=[sub])\n    result = runner.invoke(cli, [\"sub\"])\n    assert result.output == \"sub\"\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"expect\"),\n    [\n        ([], \"S:[no value]\"),\n        ([\"--s=42\"], \"S:[42]\"),\n        ([\"--s\"], \"Error: Option '--s' requires an argument.\"),\n        ([\"--s=\"], \"S:[]\"),\n        ([\"--s=\\N{SNOWMAN}\"], \"S:[\\N{SNOWMAN}]\"),\n    ],\n)\ndef test_string_option(runner, args, expect):\n    @click.command()\n    @click.option(\"--s\", default=\"no value\")\n    def cli(s):\n        click.echo(f\"S:[{s}]\")\n\n    result = runner.invoke(cli, args)\n    assert expect in result.output\n\n    if expect.startswith(\"Error:\"):\n        assert result.exception is not None\n    else:\n        assert result.exception is None\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"expect\"),\n    [\n        ([], \"I:[84]\"),\n        ([\"--i=23\"], \"I:[46]\"),\n        ([\"--i=x\"], \"Error: Invalid value for '--i': 'x' is not a valid integer.\"),\n    ],\n)\ndef test_int_option(runner, args, expect):\n    @click.command()\n    @click.option(\"--i\", default=42)\n    def cli(i):\n        click.echo(f\"I:[{i * 2}]\")\n\n    result = runner.invoke(cli, args)\n    assert expect in result.output\n\n    if expect.startswith(\"Error:\"):\n        assert result.exception is not None\n    else:\n        assert result.exception is None\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"expect\"),\n    [\n        ([], \"U:[ba122011-349f-423b-873b-9d6a79c688ab]\"),\n        (\n            [\"--u=821592c1-c50e-4971-9cd6-e89dc6832f86\"],\n            \"U:[821592c1-c50e-4971-9cd6-e89dc6832f86]\",\n        ),\n        ([\"--u=x\"], \"Error: Invalid value for '--u': 'x' is not a valid UUID.\"),\n    ],\n)\ndef test_uuid_option(runner, args, expect):\n    @click.command()\n    @click.option(\n        \"--u\", default=\"ba122011-349f-423b-873b-9d6a79c688ab\", type=click.UUID\n    )\n    def cli(u):\n        click.echo(f\"U:[{u}]\")\n\n    result = runner.invoke(cli, args)\n    assert expect in result.output\n\n    if expect.startswith(\"Error:\"):\n        assert result.exception is not None\n    else:\n        assert result.exception is None\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"expect\"),\n    [\n        ([], \"F:[42.0]\"),\n        (\"--f=23.5\", \"F:[23.5]\"),\n        (\"--f=x\", \"Error: Invalid value for '--f': 'x' is not a valid float.\"),\n    ],\n)\ndef test_float_option(runner, args, expect):\n    @click.command()\n    @click.option(\"--f\", default=42.0)\n    def cli(f):\n        click.echo(f\"F:[{f}]\")\n\n    result = runner.invoke(cli, args)\n    assert expect in result.output\n\n    if expect.startswith(\"Error:\"):\n        assert result.exception is not None\n    else:\n        assert result.exception is None\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"default\", \"expect\"),\n    [\n        ([\"--on\"], True, True),\n        ([\"--on\"], False, True),\n        ([\"--on\"], None, True),\n        ([\"--on\"], UNSET, True),\n        ([\"--off\"], True, False),\n        ([\"--off\"], False, False),\n        ([\"--off\"], None, False),\n        ([\"--off\"], UNSET, False),\n        ([], True, True),\n        ([], False, False),\n        ([], None, None),\n        ([], UNSET, False),\n    ],\n)\ndef test_boolean_switch(runner, args, default, expect):\n    @click.command()\n    @click.option(\"--on/--off\", default=default)\n    def cli(on):\n        return on\n\n    result = runner.invoke(cli, args, standalone_mode=False)\n    assert result.return_value is expect\n\n\n@pytest.mark.parametrize(\n    (\"default\", \"args\", \"expect\"),\n    (\n        (True, [\"--f\"], True),\n        (True, [], True),\n        (False, [\"--f\"], True),\n        (False, [], False),\n        # Boolean flags have a 3-states logic.\n        # See: https://github.com/pallets/click/issues/3024#issue-3285556668\n        (None, [\"--f\"], True),\n        (None, [], None),\n    ),\n)\ndef test_boolean_flag(runner, default, args, expect):\n    @click.command()\n    @click.option(\"--f\", is_flag=True, default=default)\n    def cli(f):\n        return f\n\n    result = runner.invoke(cli, args, standalone_mode=False)\n    assert result.return_value is expect\n\n\n@pytest.mark.parametrize(\n    (\"value\", \"expect\"),\n    chain(\n        ((x, \"True\") for x in (\"1\", \"true\", \"t\", \"yes\", \"y\", \"on\")),\n        ((x, \"False\") for x in (\"0\", \"false\", \"f\", \"no\", \"n\", \"off\")),\n    ),\n)\ndef test_boolean_conversion(runner, value, expect):\n    @click.command()\n    @click.option(\"--flag\", type=bool)\n    def cli(flag):\n        click.echo(flag, nl=False)\n\n    result = runner.invoke(cli, [\"--flag\", value])\n    assert result.output == expect\n\n    result = runner.invoke(cli, [\"--flag\", value.title()])\n    assert result.output == expect\n\n\n@pytest.mark.parametrize(\n    (\"default\", \"args\", \"expected\"),\n    # These test cases are similar to the ones in\n    # tests/test_options.py::test_default_dual_option_callback, so keep them in sync.\n    (\n        # Each option is returning its own flag_value, whatever the default is.\n        (True, [\"--upper\"], \"upper\"),\n        (True, [\"--lower\"], \"lower\"),\n        (False, [\"--upper\"], \"upper\"),\n        (False, [\"--lower\"], \"lower\"),\n        (None, [\"--upper\"], \"upper\"),\n        (None, [\"--lower\"], \"lower\"),\n        (UNSET, [\"--upper\"], \"upper\"),\n        (UNSET, [\"--lower\"], \"lower\"),\n        # Check that the last option wins when both are specified.\n        (True, [\"--upper\", \"--lower\"], \"lower\"),\n        (True, [\"--lower\", \"--upper\"], \"upper\"),\n        # Check that the default is returned as-is when no option is specified.\n        (\"upper\", [], \"upper\"),\n        (\"lower\", [], \"lower\"),\n        (\"uPPer\", [], \"uPPer\"),\n        (\"lOwEr\", [], \"lOwEr\"),\n        (\" ᕕ( ᐛ )ᕗ \", [], \" ᕕ( ᐛ )ᕗ \"),\n        (None, [], None),\n        # Default is normalized to None if it is UNSET.\n        (UNSET, [], None),\n        # Special case: if default=True and flag_value is set, the value returned is the\n        # flag_value, not the True Python value itself.\n        (True, [], \"upper\"),\n        # Non-string defaults are process as strings by the default Parameter's type.\n        (False, [], \"False\"),\n        (42, [], \"42\"),\n        (12.3, [], \"12.3\"),\n    ),\n)\ndef test_flag_value_dual_options(runner, default, args, expected):\n    \"\"\"Check how default is processed when options compete for the same variable name.\n\n    Covers the regression reported in\n    https://github.com/pallets/click/issues/3024#issuecomment-3146199461\n    \"\"\"\n\n    @click.command()\n    @click.option(\"--upper\", \"case\", flag_value=\"upper\", default=default)\n    @click.option(\"--lower\", \"case\", flag_value=\"lower\")\n    def cli(case):\n        click.echo(repr(case), nl=False)\n\n    result = runner.invoke(cli, args)\n    assert result.output == repr(expected)\n\n\ndef test_file_option(runner):\n    @click.command()\n    @click.option(\"--file\", type=click.File(\"w\"))\n    def input(file):\n        file.write(\"Hello World!\\n\")\n\n    @click.command()\n    @click.option(\"--file\", type=click.File(\"r\"))\n    def output(file):\n        click.echo(file.read())\n\n    with runner.isolated_filesystem():\n        result_in = runner.invoke(input, [\"--file=example.txt\"])\n        result_out = runner.invoke(output, [\"--file=example.txt\"])\n\n    assert not result_in.exception\n    assert result_in.output == \"\"\n    assert not result_out.exception\n    assert result_out.output == \"Hello World!\\n\\n\"\n\n\ndef test_file_lazy_mode(runner):\n    do_io = False\n\n    @click.command()\n    @click.option(\"--file\", type=click.File(\"w\"))\n    def input(file):\n        if do_io:\n            file.write(\"Hello World!\\n\")\n\n    @click.command()\n    @click.option(\"--file\", type=click.File(\"r\"))\n    def output(file):\n        pass\n\n    with runner.isolated_filesystem():\n        os.mkdir(\"example.txt\")\n\n        do_io = True\n        result_in = runner.invoke(input, [\"--file=example.txt\"])\n        assert result_in.exit_code == 1\n\n        do_io = False\n        result_in = runner.invoke(input, [\"--file=example.txt\"])\n        assert result_in.exit_code == 0\n\n        result_out = runner.invoke(output, [\"--file=example.txt\"])\n        assert result_out.exception\n\n    @click.command()\n    @click.option(\"--file\", type=click.File(\"w\", lazy=False))\n    def input_non_lazy(file):\n        file.write(\"Hello World!\\n\")\n\n    with runner.isolated_filesystem():\n        os.mkdir(\"example.txt\")\n        result_in = runner.invoke(input_non_lazy, [\"--file=example.txt\"])\n        assert result_in.exit_code == 2\n        assert \"Invalid value for '--file': 'example.txt'\" in result_in.output\n\n\ndef test_path_option(runner):\n    @click.command()\n    @click.option(\"-O\", type=click.Path(file_okay=False, exists=True, writable=True))\n    def write_to_dir(o):\n        with open(os.path.join(o, \"foo.txt\"), \"wb\") as f:\n            f.write(b\"meh\\n\")\n\n    with runner.isolated_filesystem():\n        os.mkdir(\"test\")\n\n        result = runner.invoke(write_to_dir, [\"-O\", \"test\"])\n        assert not result.exception\n\n        with open(\"test/foo.txt\", \"rb\") as f:\n            assert f.read() == b\"meh\\n\"\n\n        result = runner.invoke(write_to_dir, [\"-O\", \"test/foo.txt\"])\n        assert \"is a file\" in result.output\n\n    @click.command()\n    @click.option(\"-f\", type=click.Path(exists=True))\n    def showtype(f):\n        click.echo(f\"is_file={os.path.isfile(f)}\")\n        click.echo(f\"is_dir={os.path.isdir(f)}\")\n\n    with runner.isolated_filesystem():\n        result = runner.invoke(showtype, [\"-f\", \"xxx\"])\n        assert \"does not exist\" in result.output\n\n        result = runner.invoke(showtype, [\"-f\", \".\"])\n        assert \"is_file=False\" in result.output\n        assert \"is_dir=True\" in result.output\n\n    @click.command()\n    @click.option(\"-f\", type=click.Path())\n    def exists(f):\n        click.echo(f\"exists={os.path.exists(f)}\")\n\n    with runner.isolated_filesystem():\n        result = runner.invoke(exists, [\"-f\", \"xxx\"])\n        assert \"exists=False\" in result.output\n\n        result = runner.invoke(exists, [\"-f\", \".\"])\n        assert \"exists=True\" in result.output\n\n\ndef test_choice_option(runner):\n    @click.command()\n    @click.option(\"--method\", type=click.Choice([\"foo\", \"bar\", \"baz\"]))\n    def cli(method):\n        click.echo(method)\n\n    result = runner.invoke(cli, [\"--method=foo\"])\n    assert not result.exception\n    assert result.output == \"foo\\n\"\n\n    result = runner.invoke(cli, [\"--method=meh\"])\n    assert result.exit_code == 2\n    assert (\n        \"Invalid value for '--method': 'meh' is not one of 'foo', 'bar', 'baz'.\"\n        in result.output\n    )\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert \"--method [foo|bar|baz]\" in result.output\n\n\ndef test_choice_argument(runner):\n    @click.command()\n    @click.argument(\"method\", type=click.Choice([\"foo\", \"bar\", \"baz\"]))\n    def cli(method):\n        click.echo(method)\n\n    result = runner.invoke(cli, [\"foo\"])\n    assert not result.exception\n    assert result.output == \"foo\\n\"\n\n    result = runner.invoke(cli, [\"meh\"])\n    assert result.exit_code == 2\n    assert (\n        \"Invalid value for '{foo|bar|baz}': 'meh' is not one of 'foo',\"\n        \" 'bar', 'baz'.\" in result.output\n    )\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert \"{foo|bar|baz}\" in result.output\n\n\ndef test_choice_argument_enum(runner):\n    class MyEnum(str, enum.Enum):\n        FOO = \"foo-value\"\n        BAR = \"bar-value\"\n        BAZ = \"baz-value\"\n\n    @click.command()\n    @click.argument(\"method\", type=click.Choice(MyEnum, case_sensitive=False))\n    def cli(method: MyEnum):\n        assert isinstance(method, MyEnum)\n        click.echo(method)\n\n    result = runner.invoke(cli, [\"foo\"])\n    assert result.output == \"foo-value\\n\"\n    assert not result.exception\n\n    result = runner.invoke(cli, [\"meh\"])\n    assert result.exit_code == 2\n    assert (\n        \"Invalid value for '{foo|bar|baz}': 'meh' is not one of 'foo',\"\n        \" 'bar', 'baz'.\" in result.output\n    )\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert \"{foo|bar|baz}\" in result.output\n\n\ndef test_choice_argument_custom_type(runner):\n    class MyClass:\n        def __init__(self, value: str) -> None:\n            self.value = value\n\n        def __str__(self) -> str:\n            return self.value\n\n    @click.command()\n    @click.argument(\n        \"method\", type=click.Choice([MyClass(\"foo\"), MyClass(\"bar\"), MyClass(\"baz\")])\n    )\n    def cli(method: MyClass):\n        assert isinstance(method, MyClass)\n        click.echo(method)\n\n    result = runner.invoke(cli, [\"foo\"])\n    assert not result.exception\n    assert result.output == \"foo\\n\"\n\n    result = runner.invoke(cli, [\"meh\"])\n    assert result.exit_code == 2\n    assert (\n        \"Invalid value for '{foo|bar|baz}': 'meh' is not one of 'foo',\"\n        \" 'bar', 'baz'.\" in result.output\n    )\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert \"{foo|bar|baz}\" in result.output\n\n\ndef test_choice_argument_none(runner):\n    @click.command()\n    @click.argument(\n        \"method\", type=click.Choice([\"not-none\", None], case_sensitive=False)\n    )\n    def cli(method: str | None):\n        assert isinstance(method, str) or method is None\n        click.echo(repr(method), nl=False)\n\n    result = runner.invoke(cli, [\"not-none\"])\n    assert not result.exception\n    assert result.output == repr(\"not-none\")\n\n    result = runner.invoke(cli, [\"none\"])\n    assert not result.exception\n    assert result.output == repr(None)\n\n    result = runner.invoke(cli, [])\n    assert result.exception\n    assert (\n        \"Error: Missing argument '{not-none|none}'. \"\n        \"Choose from:\\n\\tnot-none,\\n\\tnone\\n\" in result.stderr\n    )\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert result.output.startswith(\"Usage: cli [OPTIONS] {not-none|none}\\n\")\n\n\ndef test_datetime_option_default(runner):\n    @click.command()\n    @click.option(\"--start_date\", type=click.DateTime())\n    def cli(start_date):\n        click.echo(start_date.strftime(\"%Y-%m-%dT%H:%M:%S\"))\n\n    result = runner.invoke(cli, [\"--start_date=2015-09-29\"])\n    assert not result.exception\n    assert result.output == \"2015-09-29T00:00:00\\n\"\n\n    result = runner.invoke(cli, [\"--start_date=2015-09-29T09:11:22\"])\n    assert not result.exception\n    assert result.output == \"2015-09-29T09:11:22\\n\"\n\n    result = runner.invoke(cli, [\"--start_date=2015-09\"])\n    assert result.exit_code == 2\n    assert (\n        \"Invalid value for '--start_date': '2015-09' does not match the formats\"\n        \" '%Y-%m-%d', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S'.\"\n    ) in result.output\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert (\n        \"--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S]\" in result.output\n    )\n\n\ndef test_datetime_option_custom(runner):\n    @click.command()\n    @click.option(\"--start_date\", type=click.DateTime(formats=[\"%A %B %d, %Y\"]))\n    def cli(start_date):\n        click.echo(start_date.strftime(\"%Y-%m-%dT%H:%M:%S\"))\n\n    result = runner.invoke(cli, [\"--start_date=Wednesday June 05, 2010\"])\n    assert not result.exception\n    assert result.output == \"2010-06-05T00:00:00\\n\"\n\n\ndef test_required_option(runner):\n    @click.command()\n    @click.option(\"--foo\", required=True)\n    def cli(foo):\n        click.echo(foo)\n\n    result = runner.invoke(cli, [])\n    assert result.exit_code == 2\n    assert \"Missing option '--foo'\" in result.output\n\n\ndef test_evaluation_order(runner):\n    called = []\n\n    def memo(ctx, param, value):\n        called.append(value)\n        return value\n\n    @click.command()\n    @click.option(\"--missing\", default=\"missing\", is_eager=False, callback=memo)\n    @click.option(\"--eager-flag1\", flag_value=\"eager1\", is_eager=True, callback=memo)\n    @click.option(\"--eager-flag2\", flag_value=\"eager2\", is_eager=True, callback=memo)\n    @click.option(\"--eager-flag3\", flag_value=\"eager3\", is_eager=True, callback=memo)\n    @click.option(\"--normal-flag1\", flag_value=\"normal1\", is_eager=False, callback=memo)\n    @click.option(\"--normal-flag2\", flag_value=\"normal2\", is_eager=False, callback=memo)\n    @click.option(\"--normal-flag3\", flag_value=\"normal3\", is_eager=False, callback=memo)\n    def cli(**x):\n        pass\n\n    result = runner.invoke(\n        cli,\n        [\n            \"--eager-flag2\",\n            \"--eager-flag1\",\n            \"--normal-flag2\",\n            \"--eager-flag3\",\n            \"--normal-flag3\",\n            \"--normal-flag3\",\n            \"--normal-flag1\",\n            \"--normal-flag1\",\n        ],\n    )\n    assert not result.exception\n    assert called == [\n        \"eager2\",\n        \"eager1\",\n        \"eager3\",\n        \"normal2\",\n        \"normal3\",\n        \"normal1\",\n        \"missing\",\n    ]\n\n\ndef test_hidden_option(runner):\n    @click.command()\n    @click.option(\"--nope\", hidden=True)\n    def cli(nope):\n        click.echo(nope)\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert result.exit_code == 0\n    assert \"--nope\" not in result.output\n\n\ndef test_hidden_command(runner):\n    @click.group()\n    def cli():\n        pass\n\n    @cli.command(hidden=True)\n    def nope():\n        pass\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert result.exit_code == 0\n    assert \"nope\" not in result.output\n\n\ndef test_hidden_group(runner):\n    @click.group()\n    def cli():\n        pass\n\n    @cli.group(hidden=True)\n    def subgroup():\n        pass\n\n    @subgroup.command()\n    def nope():\n        pass\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert result.exit_code == 0\n    assert \"subgroup\" not in result.output\n    assert \"nope\" not in result.output\n\n\ndef test_summary_line(runner):\n    @click.group()\n    def cli():\n        pass\n\n    @cli.command()\n    def cmd():\n        \"\"\"\n        Summary line without period\n\n        Here is a sentence. And here too.\n        \"\"\"\n        pass\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert \"Summary line without period\" in result.output\n    assert \"Here is a sentence.\" not in result.output\n\n\ndef test_help_invalid_default(runner):\n    cli = click.Command(\n        \"cli\",\n        params=[\n            click.Option(\n                [\"-a\"],\n                type=click.Path(exists=True),\n                default=\"not found\",\n                show_default=True,\n            ),\n        ],\n    )\n    result = runner.invoke(cli, [\"--help\"])\n    assert result.exit_code == 0\n    assert \"default: not found\" in result.output\n"
  },
  {
    "path": "tests/test_chain.py",
    "content": "import sys\n\nimport pytest\n\nimport click\n\n\ndef debug():\n    click.echo(\n        f\"{sys._getframe(1).f_code.co_name}\"\n        f\"={'|'.join(click.get_current_context().args)}\"\n    )\n\n\ndef test_basic_chaining(runner):\n    @click.group(chain=True)\n    def cli():\n        pass\n\n    @cli.command(\"sdist\")\n    def sdist():\n        click.echo(\"sdist called\")\n\n    @cli.command(\"bdist\")\n    def bdist():\n        click.echo(\"bdist called\")\n\n    result = runner.invoke(cli, [\"bdist\", \"sdist\", \"bdist\"])\n    assert not result.exception\n    assert result.output.splitlines() == [\n        \"bdist called\",\n        \"sdist called\",\n        \"bdist called\",\n    ]\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"expect\"),\n    [\n        ([\"--help\"], \"COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...\"),\n        ([\"--help\"], \"ROOT HELP\"),\n        ([\"sdist\", \"--help\"], \"SDIST HELP\"),\n        ([\"bdist\", \"--help\"], \"BDIST HELP\"),\n        ([\"bdist\", \"sdist\", \"--help\"], \"SDIST HELP\"),\n    ],\n)\ndef test_chaining_help(runner, args, expect):\n    @click.group(chain=True)\n    def cli():\n        \"\"\"ROOT HELP\"\"\"\n        pass\n\n    @cli.command(\"sdist\")\n    def sdist():\n        \"\"\"SDIST HELP\"\"\"\n        click.echo(\"sdist called\")\n\n    @cli.command(\"bdist\")\n    def bdist():\n        \"\"\"BDIST HELP\"\"\"\n        click.echo(\"bdist called\")\n\n    result = runner.invoke(cli, args)\n    assert not result.exception\n    assert expect in result.output\n\n\ndef test_chaining_with_options(runner):\n    @click.group(chain=True)\n    def cli():\n        pass\n\n    @cli.command(\"sdist\")\n    @click.option(\"--format\")\n    def sdist(format):\n        click.echo(f\"sdist called {format}\")\n\n    @cli.command(\"bdist\")\n    @click.option(\"--format\")\n    def bdist(format):\n        click.echo(f\"bdist called {format}\")\n\n    result = runner.invoke(cli, [\"bdist\", \"--format=1\", \"sdist\", \"--format=2\"])\n    assert not result.exception\n    assert result.output.splitlines() == [\"bdist called 1\", \"sdist called 2\"]\n\n\n@pytest.mark.parametrize((\"chain\", \"expect\"), [(False, \"1\"), (True, \"[]\")])\ndef test_no_command_result_callback(runner, chain, expect):\n    \"\"\"When a group has ``invoke_without_command=True``, the result\n    callback is always invoked. A regular group invokes it with\n    its return value, a chained group with ``[]``.\n    \"\"\"\n\n    @click.group(invoke_without_command=True, chain=chain)\n    def cli():\n        return 1\n\n    @cli.result_callback()\n    def process_result(result):\n        click.echo(result, nl=False)\n\n    result = runner.invoke(cli, [])\n    assert result.output == expect\n\n\ndef test_chaining_with_arguments(runner):\n    @click.group(chain=True)\n    def cli():\n        pass\n\n    @cli.command(\"sdist\")\n    @click.argument(\"format\")\n    def sdist(format):\n        click.echo(f\"sdist called {format}\")\n\n    @cli.command(\"bdist\")\n    @click.argument(\"format\")\n    def bdist(format):\n        click.echo(f\"bdist called {format}\")\n\n    result = runner.invoke(cli, [\"bdist\", \"1\", \"sdist\", \"2\"])\n    assert not result.exception\n    assert result.output.splitlines() == [\"bdist called 1\", \"sdist called 2\"]\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"input\", \"expect\"),\n    [\n        ([\"-f\", \"-\"], \"foo\\nbar\", [\"foo\", \"bar\"]),\n        ([\"-f\", \"-\", \"strip\"], \"foo \\n bar\", [\"foo\", \"bar\"]),\n        ([\"-f\", \"-\", \"strip\", \"uppercase\"], \"foo \\n bar\", [\"FOO\", \"BAR\"]),\n    ],\n)\ndef test_pipeline(runner, args, input, expect):\n    @click.group(chain=True, invoke_without_command=True)\n    @click.option(\"-f\", type=click.File(\"r\"))\n    def cli(f):\n        pass\n\n    @cli.result_callback()\n    def process_pipeline(processors, f):\n        iterator = (x.rstrip(\"\\r\\n\") for x in f)\n        for processor in processors:\n            iterator = processor(iterator)\n        for item in iterator:\n            click.echo(item)\n\n    @cli.command(\"uppercase\")\n    def make_uppercase():\n        def processor(iterator):\n            for line in iterator:\n                yield line.upper()\n\n        return processor\n\n    @cli.command(\"strip\")\n    def make_strip():\n        def processor(iterator):\n            for line in iterator:\n                yield line.strip()\n\n        return processor\n\n    result = runner.invoke(cli, args, input=input)\n    assert not result.exception\n    assert result.output.splitlines() == expect\n\n\ndef test_args_and_chain(runner):\n    @click.group(chain=True)\n    def cli():\n        debug()\n\n    @cli.command()\n    def a():\n        debug()\n\n    @cli.command()\n    def b():\n        debug()\n\n    @cli.command()\n    def c():\n        debug()\n\n    result = runner.invoke(cli, [\"a\", \"b\", \"c\"])\n    assert not result.exception\n    assert result.output.splitlines() == [\"cli=\", \"a=\", \"b=\", \"c=\"]\n\n\ndef test_group_arg_behavior(runner):\n    with pytest.raises(RuntimeError):\n\n        @click.group(chain=True)\n        @click.argument(\"forbidden\", required=False)\n        def bad_cli():\n            pass\n\n    with pytest.raises(RuntimeError):\n\n        @click.group(chain=True)\n        @click.argument(\"forbidden\", nargs=-1)\n        def bad_cli2():\n            pass\n\n    @click.group(chain=True)\n    @click.argument(\"arg\")\n    def cli(arg):\n        click.echo(f\"cli:{arg}\")\n\n    @cli.command()\n    def a():\n        click.echo(\"a\")\n\n    result = runner.invoke(cli, [\"foo\", \"a\"])\n    assert not result.exception\n    assert result.output.splitlines() == [\"cli:foo\", \"a\"]\n\n\n@pytest.mark.xfail\ndef test_group_chaining(runner):\n    @click.group(chain=True)\n    def cli():\n        debug()\n\n    @cli.group()\n    def l1a():\n        debug()\n\n    @l1a.command()\n    def l2a():\n        debug()\n\n    @l1a.command()\n    def l2b():\n        debug()\n\n    @cli.command()\n    def l1b():\n        debug()\n\n    result = runner.invoke(cli, [\"l1a\", \"l2a\", \"l1b\"])\n    assert not result.exception\n    assert result.output.splitlines() == [\"cli=\", \"l1a=\", \"l2a=\", \"l1b=\"]\n"
  },
  {
    "path": "tests/test_command_decorators.py",
    "content": "import pytest\n\nimport click\n\n\ndef test_command_no_parens(runner):\n    @click.command\n    def cli():\n        click.echo(\"hello\")\n\n    result = runner.invoke(cli)\n    assert result.exception is None\n    assert result.output == \"hello\\n\"\n\n\ndef test_custom_command_no_parens(runner):\n    class CustomCommand(click.Command):\n        pass\n\n    class CustomGroup(click.Group):\n        command_class = CustomCommand\n\n    @click.group(cls=CustomGroup)\n    def grp():\n        pass\n\n    @grp.command\n    def cli():\n        click.echo(\"hello custom command class\")\n\n    result = runner.invoke(cli)\n    assert result.exception is None\n    assert result.output == \"hello custom command class\\n\"\n\n\ndef test_group_no_parens(runner):\n    @click.group\n    def grp():\n        click.echo(\"grp1\")\n\n    @grp.command\n    def cmd1():\n        click.echo(\"cmd1\")\n\n    @grp.group\n    def grp2():\n        click.echo(\"grp2\")\n\n    @grp2.command\n    def cmd2():\n        click.echo(\"cmd2\")\n\n    result = runner.invoke(grp, [\"cmd1\"])\n    assert result.exception is None\n    assert result.output == \"grp1\\ncmd1\\n\"\n\n    result = runner.invoke(grp, [\"grp2\", \"cmd2\"])\n    assert result.exception is None\n    assert result.output == \"grp1\\ngrp2\\ncmd2\\n\"\n\n\ndef test_params_argument(runner):\n    opt = click.Argument([\"a\"])\n\n    @click.command(params=[opt])\n    @click.argument(\"b\")\n    def cli(a, b):\n        click.echo(f\"{a} {b}\")\n\n    assert cli.params[0].name == \"a\"\n    assert cli.params[1].name == \"b\"\n    result = runner.invoke(cli, [\"1\", \"2\"])\n    assert result.output == \"1 2\\n\"\n\n\n@pytest.mark.parametrize(\n    \"name\",\n    [\n        \"init_data\",\n        \"init_data_command\",\n        \"init_data_cmd\",\n        \"init_data_group\",\n        \"init_data_grp\",\n    ],\n)\ndef test_generate_name(name: str) -> None:\n    def f():\n        pass\n\n    f.__name__ = name\n    f = click.command(f)\n    assert f.name == \"init-data\"\n"
  },
  {
    "path": "tests/test_commands.py",
    "content": "import re\n\nimport pytest\n\nimport click\n\n\ndef test_other_command_invoke(runner):\n    @click.command()\n    @click.pass_context\n    def cli(ctx):\n        return ctx.invoke(other_cmd, arg=42)\n\n    @click.command()\n    @click.argument(\"arg\", type=click.INT)\n    def other_cmd(arg):\n        click.echo(arg)\n\n    result = runner.invoke(cli, [])\n    assert not result.exception\n    assert result.output == \"42\\n\"\n\n\ndef test_other_command_forward(runner):\n    cli = click.Group()\n\n    @cli.command()\n    @click.option(\"--count\", default=1)\n    def test(count):\n        click.echo(f\"Count: {count:d}\")\n\n    @cli.command()\n    @click.option(\"--count\", default=1)\n    @click.pass_context\n    def dist(ctx, count):\n        ctx.forward(test)\n        ctx.invoke(test, count=42)\n\n    result = runner.invoke(cli, [\"dist\"])\n    assert not result.exception\n    assert result.output == \"Count: 1\\nCount: 42\\n\"\n\n\ndef test_forwarded_params_consistency(runner):\n    cli = click.Group()\n\n    @cli.command()\n    @click.option(\"-a\")\n    @click.pass_context\n    def first(ctx, **kwargs):\n        click.echo(f\"{ctx.params}\")\n\n    @cli.command()\n    @click.option(\"-a\")\n    @click.option(\"-b\")\n    @click.pass_context\n    def second(ctx, **kwargs):\n        click.echo(f\"{ctx.params}\")\n        ctx.forward(first)\n\n    result = runner.invoke(cli, [\"second\", \"-a\", \"foo\", \"-b\", \"bar\"])\n    assert not result.exception\n    assert result.output == \"{'a': 'foo', 'b': 'bar'}\\n{'a': 'foo', 'b': 'bar'}\\n\"\n\n\ndef test_auto_shorthelp(runner):\n    @click.group()\n    def cli():\n        pass\n\n    @cli.command()\n    def short():\n        \"\"\"This is a short text.\"\"\"\n\n    @cli.command()\n    def special_chars():\n        \"\"\"Login and store the token in ~/.netrc.\"\"\"\n\n    @cli.command()\n    def long():\n        \"\"\"This is a long text that is too long to show as short help\n        and will be truncated instead.\"\"\"\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert (\n        re.search(\n            r\"Commands:\\n\\s+\"\n            r\"long\\s+This is a long text that is too long to show as short help\"\n            r\"\\.\\.\\.\\n\\s+\"\n            r\"short\\s+This is a short text\\.\\n\\s+\"\n            r\"special-chars\\s+Login and store the token in ~/.netrc\\.\\s*\",\n            result.output,\n        )\n        is not None\n    )\n\n\ndef test_command_no_args_is_help(runner):\n    result = runner.invoke(click.Command(\"test\", no_args_is_help=True))\n    assert result.exit_code == 2\n    assert \"Show this message and exit.\" in result.output\n\n\ndef test_default_maps(runner):\n    @click.group()\n    def cli():\n        pass\n\n    @cli.command()\n    @click.option(\"--name\", default=\"normal\")\n    def foo(name):\n        click.echo(name)\n\n    result = runner.invoke(cli, [\"foo\"], default_map={\"foo\": {\"name\": \"changed\"}})\n\n    assert not result.exception\n    assert result.output == \"changed\\n\"\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"exit_code\", \"expect\"),\n    [\n        ([\"obj1\"], 2, \"Error: Missing command.\"),\n        ([\"obj1\", \"--help\"], 0, \"Show this message and exit.\"),\n        ([\"obj1\", \"move\"], 0, \"obj=obj1\\nmove\\n\"),\n        ([], 2, \"Show this message and exit.\"),\n    ],\n)\ndef test_group_with_args(runner, args, exit_code, expect):\n    @click.group()\n    @click.argument(\"obj\")\n    def cli(obj):\n        click.echo(f\"obj={obj}\")\n\n    @cli.command()\n    def move():\n        click.echo(\"move\")\n\n    result = runner.invoke(cli, args)\n    assert result.exit_code == exit_code\n    assert expect in result.output\n\n\ndef test_custom_parser(runner):\n    import optparse\n\n    @click.group()\n    def cli():\n        pass\n\n    class OptParseCommand(click.Command):\n        def __init__(self, name, parser, callback):\n            super().__init__(name)\n            self.parser = parser\n            self.callback = callback\n\n        def parse_args(self, ctx, args):\n            try:\n                opts, args = parser.parse_args(args)\n            except Exception as e:\n                ctx.fail(str(e))\n            ctx.args = args\n            ctx.params = vars(opts)\n\n        def get_usage(self, ctx):\n            return self.parser.get_usage()\n\n        def get_help(self, ctx):\n            return self.parser.format_help()\n\n        def invoke(self, ctx):\n            ctx.invoke(self.callback, ctx.args, **ctx.params)\n\n    parser = optparse.OptionParser(usage=\"Usage: foo test [OPTIONS]\")\n    parser.add_option(\n        \"-f\", \"--file\", dest=\"filename\", help=\"write report to FILE\", metavar=\"FILE\"\n    )\n    parser.add_option(\n        \"-q\",\n        \"--quiet\",\n        action=\"store_false\",\n        dest=\"verbose\",\n        default=True,\n        help=\"don't print status messages to stdout\",\n    )\n\n    def test_callback(args, filename, verbose):\n        click.echo(\" \".join(args))\n        click.echo(filename)\n        click.echo(verbose)\n\n    cli.add_command(OptParseCommand(\"test\", parser, test_callback))\n\n    result = runner.invoke(cli, [\"test\", \"-f\", \"f.txt\", \"-q\", \"q1.txt\", \"q2.txt\"])\n    assert result.exception is None\n    assert result.output.splitlines() == [\"q1.txt q2.txt\", \"f.txt\", \"False\"]\n\n    result = runner.invoke(cli, [\"test\", \"--help\"])\n    assert result.exception is None\n    assert result.output.splitlines() == [\n        \"Usage: foo test [OPTIONS]\",\n        \"\",\n        \"Options:\",\n        \"  -h, --help            show this help message and exit\",\n        \"  -f FILE, --file=FILE  write report to FILE\",\n        \"  -q, --quiet           don't print status messages to stdout\",\n    ]\n\n\ndef test_object_propagation(runner):\n    for chain in False, True:\n\n        @click.group(chain=chain)\n        @click.option(\"--debug/--no-debug\", default=False)\n        @click.pass_context\n        def cli(ctx, debug):\n            if ctx.obj is None:\n                ctx.obj = {}\n            ctx.obj[\"DEBUG\"] = debug\n\n        @cli.command()\n        @click.pass_context\n        def sync(ctx):\n            click.echo(f\"Debug is {'on' if ctx.obj['DEBUG'] else 'off'}\")\n\n        result = runner.invoke(cli, [\"sync\"])\n        assert result.exception is None\n        assert result.output == \"Debug is off\\n\"\n\n\n@pytest.mark.parametrize(\n    (\"opt_params\", \"expected\"),\n    (\n        # Original tests.\n        ({\"type\": click.INT, \"default\": 42}, 42),\n        ({\"type\": click.INT, \"default\": \"15\"}, 15),\n        ({\"multiple\": True}, ()),\n        # SENTINEL value tests.\n        ({\"default\": None}, None),\n        ({\"type\": click.STRING}, None),  # No default specified, should be None.\n        ({\"type\": click.BOOL, \"default\": False}, False),\n        ({\"type\": click.BOOL, \"default\": True}, True),\n        ({\"type\": click.FLOAT, \"default\": 3.14}, 3.14),\n        # Multiple with default.\n        ({\"multiple\": True, \"default\": [1, 2, 3]}, (1, 2, 3)),\n        ({\"multiple\": True, \"default\": ()}, ()),\n        # Required option without value should use SENTINEL behavior.\n        ({\"required\": False}, None),\n        # Choice type with default.\n        ({\"type\": click.Choice([\"a\", \"b\", \"c\"]), \"default\": \"b\"}, \"b\"),\n        # Path type with default.\n        ({\"type\": click.Path(), \"default\": \"/tmp\"}, \"/tmp\"),\n        # Flag options.\n        ({\"is_flag\": True, \"default\": False}, False),\n        ({\"is_flag\": True, \"default\": True}, True),\n        # Count option.\n        ({\"count\": True}, 0),\n        # Hidden option.\n        ({\"hidden\": True, \"default\": \"secret\"}, \"secret\"),\n    ),\n)\ndef test_other_command_invoke_with_defaults(runner, opt_params, expected):\n    @click.command()\n    @click.pass_context\n    def cli(ctx):\n        return ctx.invoke(other_cmd)\n\n    @click.command()\n    @click.option(\"-a\", **opt_params)\n    @click.pass_context\n    def other_cmd(ctx, a):\n        return ctx.info_name, a\n\n    result = runner.invoke(cli, standalone_mode=False)\n\n    assert result.return_value == (\"other\", expected)\n\n\ndef test_invoked_subcommand(runner):\n    @click.group(invoke_without_command=True)\n    @click.pass_context\n    def cli(ctx):\n        if ctx.invoked_subcommand is None:\n            click.echo(\"no subcommand, use default\")\n            ctx.invoke(sync)\n        else:\n            click.echo(\"invoke subcommand\")\n\n    @cli.command()\n    def sync():\n        click.echo(\"in subcommand\")\n\n    result = runner.invoke(cli, [\"sync\"])\n    assert not result.exception\n    assert result.output == \"invoke subcommand\\nin subcommand\\n\"\n\n    result = runner.invoke(cli)\n    assert not result.exception\n    assert result.output == \"no subcommand, use default\\nin subcommand\\n\"\n\n\ndef test_aliased_command_canonical_name(runner):\n    class AliasedGroup(click.Group):\n        def get_command(self, ctx, cmd_name):\n            return push\n\n        def resolve_command(self, ctx, args):\n            _, command, args = super().resolve_command(ctx, args)\n            return command.name, command, args\n\n    cli = AliasedGroup()\n\n    @cli.command()\n    def push():\n        click.echo(\"push command\")\n\n    result = runner.invoke(cli, [\"pu\", \"--help\"])\n    assert not result.exception\n    assert result.output.startswith(\"Usage: root push [OPTIONS]\")\n\n\ndef test_group_add_command_name(runner):\n    cli = click.Group(\"cli\")\n    cmd = click.Command(\"a\", params=[click.Option([\"-x\"], required=True)])\n    cli.add_command(cmd, \"b\")\n    # Check that the command is accessed through the registered name,\n    # not the original name.\n    result = runner.invoke(cli, [\"b\"], default_map={\"b\": {\"x\": 3}})\n    assert result.exit_code == 0\n\n\n@pytest.mark.parametrize(\n    (\"invocation_order\", \"declaration_order\", \"expected_order\"),\n    [\n        # Non-eager options.\n        ([], [\"-a\"], [\"-a\"]),\n        ([\"-a\"], [\"-a\"], [\"-a\"]),\n        ([], [\"-a\", \"-c\"], [\"-a\", \"-c\"]),\n        ([\"-a\"], [\"-a\", \"-c\"], [\"-a\", \"-c\"]),\n        ([\"-c\"], [\"-a\", \"-c\"], [\"-c\", \"-a\"]),\n        ([], [\"-c\", \"-a\"], [\"-c\", \"-a\"]),\n        ([\"-a\"], [\"-c\", \"-a\"], [\"-a\", \"-c\"]),\n        ([\"-c\"], [\"-c\", \"-a\"], [\"-c\", \"-a\"]),\n        ([\"-a\", \"-c\"], [\"-a\", \"-c\"], [\"-a\", \"-c\"]),\n        ([\"-c\", \"-a\"], [\"-a\", \"-c\"], [\"-c\", \"-a\"]),\n        # Eager options.\n        ([], [\"-b\"], [\"-b\"]),\n        ([\"-b\"], [\"-b\"], [\"-b\"]),\n        ([], [\"-b\", \"-d\"], [\"-b\", \"-d\"]),\n        ([\"-b\"], [\"-b\", \"-d\"], [\"-b\", \"-d\"]),\n        ([\"-d\"], [\"-b\", \"-d\"], [\"-d\", \"-b\"]),\n        ([], [\"-d\", \"-b\"], [\"-d\", \"-b\"]),\n        ([\"-b\"], [\"-d\", \"-b\"], [\"-b\", \"-d\"]),\n        ([\"-d\"], [\"-d\", \"-b\"], [\"-d\", \"-b\"]),\n        ([\"-b\", \"-d\"], [\"-b\", \"-d\"], [\"-b\", \"-d\"]),\n        ([\"-d\", \"-b\"], [\"-b\", \"-d\"], [\"-d\", \"-b\"]),\n        # Mixed options.\n        ([], [\"-a\", \"-b\", \"-c\", \"-d\"], [\"-b\", \"-d\", \"-a\", \"-c\"]),\n        ([\"-a\"], [\"-a\", \"-b\", \"-c\", \"-d\"], [\"-b\", \"-d\", \"-a\", \"-c\"]),\n        ([\"-b\"], [\"-a\", \"-b\", \"-c\", \"-d\"], [\"-b\", \"-d\", \"-a\", \"-c\"]),\n        ([\"-c\"], [\"-a\", \"-b\", \"-c\", \"-d\"], [\"-b\", \"-d\", \"-c\", \"-a\"]),\n        ([\"-d\"], [\"-a\", \"-b\", \"-c\", \"-d\"], [\"-d\", \"-b\", \"-a\", \"-c\"]),\n        ([\"-a\", \"-b\"], [\"-a\", \"-b\", \"-c\", \"-d\"], [\"-b\", \"-d\", \"-a\", \"-c\"]),\n        ([\"-b\", \"-a\"], [\"-a\", \"-b\", \"-c\", \"-d\"], [\"-b\", \"-d\", \"-a\", \"-c\"]),\n        ([\"-d\", \"-c\"], [\"-a\", \"-b\", \"-c\", \"-d\"], [\"-d\", \"-b\", \"-c\", \"-a\"]),\n        ([\"-c\", \"-d\"], [\"-a\", \"-b\", \"-c\", \"-d\"], [\"-d\", \"-b\", \"-c\", \"-a\"]),\n        ([\"-a\", \"-b\", \"-c\", \"-d\"], [\"-a\", \"-b\", \"-c\", \"-d\"], [\"-b\", \"-d\", \"-a\", \"-c\"]),\n        ([\"-b\", \"-d\", \"-a\", \"-c\"], [\"-a\", \"-b\", \"-c\", \"-d\"], [\"-b\", \"-d\", \"-a\", \"-c\"]),\n        ([], [\"-b\", \"-d\", \"-e\", \"-a\", \"-c\"], [\"-b\", \"-d\", \"-e\", \"-a\", \"-c\"]),\n        ([\"-a\", \"-d\"], [\"-b\", \"-d\", \"-e\", \"-a\", \"-c\"], [\"-d\", \"-b\", \"-e\", \"-a\", \"-c\"]),\n        ([\"-c\", \"-d\"], [\"-b\", \"-d\", \"-e\", \"-a\", \"-c\"], [\"-d\", \"-b\", \"-e\", \"-c\", \"-a\"]),\n    ],\n)\ndef test_iter_params_for_processing(\n    invocation_order, declaration_order, expected_order\n):\n    parameters = {\n        \"-a\": click.Option([\"-a\"]),\n        \"-b\": click.Option([\"-b\"], is_eager=True),\n        \"-c\": click.Option([\"-c\"]),\n        \"-d\": click.Option([\"-d\"], is_eager=True),\n        \"-e\": click.Option([\"-e\"], is_eager=True),\n    }\n\n    invocation_params = [parameters[opt_id] for opt_id in invocation_order]\n    declaration_params = [parameters[opt_id] for opt_id in declaration_order]\n    expected_params = [parameters[opt_id] for opt_id in expected_order]\n\n    assert (\n        click.core.iter_params_for_processing(invocation_params, declaration_params)\n        == expected_params\n    )\n\n\ndef test_help_param_priority(runner):\n    \"\"\"Cover the edge-case in which the eagerness of help option was not\n    respected, because it was internally generated multiple times.\n\n    See: https://github.com/pallets/click/pull/2811\n    \"\"\"\n\n    def print_and_exit(ctx, param, value):\n        if value:\n            click.echo(f\"Value of {param.name} is: {value}\")\n            ctx.exit()\n\n    @click.command(context_settings={\"help_option_names\": (\"--my-help\",)})\n    @click.option(\"-a\", is_flag=True, expose_value=False, callback=print_and_exit)\n    @click.option(\n        \"-b\", is_flag=True, expose_value=False, callback=print_and_exit, is_eager=True\n    )\n    def cli():\n        pass\n\n    # --my-help is properly called and stop execution.\n    result = runner.invoke(cli, [\"--my-help\"])\n    assert \"Value of a is: True\" not in result.stdout\n    assert \"Value of b is: True\" not in result.stdout\n    assert \"--my-help\" in result.stdout\n    assert result.exit_code == 0\n\n    # -a is properly called and stop execution.\n    result = runner.invoke(cli, [\"-a\"])\n    assert \"Value of a is: True\" in result.stdout\n    assert \"Value of b is: True\" not in result.stdout\n    assert \"--my-help\" not in result.stdout\n    assert result.exit_code == 0\n\n    # -a takes precedence over -b and stop execution.\n    result = runner.invoke(cli, [\"-a\", \"-b\"])\n    assert \"Value of a is: True\" not in result.stdout\n    assert \"Value of b is: True\" in result.stdout\n    assert \"--my-help\" not in result.stdout\n    assert result.exit_code == 0\n\n    # --my-help is eager by default so takes precedence over -a and stop\n    # execution, whatever the order.\n    for args in [[\"-a\", \"--my-help\"], [\"--my-help\", \"-a\"]]:\n        result = runner.invoke(cli, args)\n        assert \"Value of a is: True\" not in result.stdout\n        assert \"Value of b is: True\" not in result.stdout\n        assert \"--my-help\" in result.stdout\n        assert result.exit_code == 0\n\n    # Both -b and --my-help are eager so they're called in the order they're\n    # invoked by the user.\n    result = runner.invoke(cli, [\"-b\", \"--my-help\"])\n    assert \"Value of a is: True\" not in result.stdout\n    assert \"Value of b is: True\" in result.stdout\n    assert \"--my-help\" not in result.stdout\n    assert result.exit_code == 0\n\n    # But there was a bug when --my-help is called before -b, because the\n    # --my-help option created by click via help_option_names is internally\n    # created twice and is not the same object, breaking the priority order\n    # produced by iter_params_for_processing.\n    result = runner.invoke(cli, [\"--my-help\", \"-b\"])\n    assert \"Value of a is: True\" not in result.stdout\n    assert \"Value of b is: True\" not in result.stdout\n    assert \"--my-help\" in result.stdout\n    assert result.exit_code == 0\n\n\ndef test_unprocessed_options(runner):\n    @click.command(context_settings=dict(ignore_unknown_options=True))\n    @click.argument(\"args\", nargs=-1, type=click.UNPROCESSED)\n    @click.option(\"--verbose\", \"-v\", count=True)\n    def cli(verbose, args):\n        click.echo(f\"Verbosity: {verbose}\")\n        click.echo(f\"Args: {'|'.join(args)}\")\n\n    result = runner.invoke(cli, [\"-foo\", \"-vvvvx\", \"--muhaha\", \"x\", \"y\", \"-x\"])\n    assert not result.exception\n    assert result.output.splitlines() == [\n        \"Verbosity: 4\",\n        \"Args: -foo|-x|--muhaha|x|y|-x\",\n    ]\n\n\n@pytest.mark.parametrize(\"doc\", [\"CLI HELP\", None])\n@pytest.mark.parametrize(\"deprecated\", [True, \"USE OTHER COMMAND INSTEAD\"])\ndef test_deprecated_in_help_messages(runner, doc, deprecated):\n    @click.command(deprecated=deprecated, help=doc)\n    def cli():\n        pass\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert \"(DEPRECATED\" in result.output\n\n    if isinstance(deprecated, str):\n        assert deprecated in result.output\n\n\n@pytest.mark.parametrize(\"deprecated\", [True, \"USE OTHER COMMAND INSTEAD\"])\ndef test_deprecated_in_invocation(runner, deprecated):\n    @click.command(deprecated=deprecated)\n    def deprecated_cmd():\n        pass\n\n    result = runner.invoke(deprecated_cmd)\n    assert \"DeprecationWarning:\" in result.output\n\n    if isinstance(deprecated, str):\n        assert deprecated in result.output\n\n\ndef test_command_parse_args_collects_option_prefixes():\n    @click.command()\n    @click.option(\"+p\", is_flag=True)\n    @click.option(\"!e\", is_flag=True)\n    def test(p, e):\n        pass\n\n    ctx = click.Context(test)\n    test.parse_args(ctx, [])\n\n    assert ctx._opt_prefixes == {\"-\", \"--\", \"+\", \"!\"}\n\n\ndef test_group_parse_args_collects_base_option_prefixes():\n    @click.group()\n    @click.option(\"~t\", is_flag=True)\n    def group(t):\n        pass\n\n    @group.command()\n    @click.option(\"+p\", is_flag=True)\n    def command1(p):\n        pass\n\n    @group.command()\n    @click.option(\"!e\", is_flag=True)\n    def command2(e):\n        pass\n\n    ctx = click.Context(group)\n    group.parse_args(ctx, [\"command1\", \"+p\"])\n\n    assert ctx._opt_prefixes == {\"-\", \"--\", \"~\"}\n\n\ndef test_group_invoke_collects_used_option_prefixes(runner):\n    opt_prefixes = set()\n\n    @click.group()\n    @click.option(\"~t\", is_flag=True)\n    def group(t):\n        pass\n\n    @group.command()\n    @click.option(\"+p\", is_flag=True)\n    @click.pass_context\n    def command1(ctx, p):\n        nonlocal opt_prefixes\n        opt_prefixes = ctx._opt_prefixes\n\n    @group.command()\n    @click.option(\"!e\", is_flag=True)\n    def command2(e):\n        pass\n\n    runner.invoke(group, [\"command1\"])\n    assert opt_prefixes == {\"-\", \"--\", \"~\", \"+\"}\n\n\n@pytest.mark.parametrize(\"exc\", (EOFError, KeyboardInterrupt))\ndef test_abort_exceptions_with_disabled_standalone_mode(runner, exc):\n    @click.command()\n    def cli():\n        raise exc(\"catch me!\")\n\n    rv = runner.invoke(cli, standalone_mode=False)\n    assert rv.exit_code == 1\n    assert isinstance(rv.exception.__cause__, exc)\n    assert rv.exception.__cause__.args == (\"catch me!\",)\n"
  },
  {
    "path": "tests/test_compat.py",
    "content": "from click._compat import should_strip_ansi\n\n\ndef test_is_jupyter_kernel_output():\n    class JupyterKernelFakeStream:\n        pass\n\n    # implementation detail, aka cheapskate test\n    JupyterKernelFakeStream.__module__ = \"ipykernel.faked\"\n    assert not should_strip_ansi(stream=JupyterKernelFakeStream())\n"
  },
  {
    "path": "tests/test_context.py",
    "content": "import logging\nfrom contextlib import AbstractContextManager\nfrom contextlib import contextmanager\nfrom types import TracebackType\n\nimport pytest\n\nimport click\nfrom click import Context\nfrom click import Option\nfrom click import Parameter\nfrom click.core import ParameterSource\nfrom click.decorators import help_option\nfrom click.decorators import pass_meta_key\n\n\ndef test_ensure_context_objects(runner):\n    class Foo:\n        def __init__(self):\n            self.title = \"default\"\n\n    pass_foo = click.make_pass_decorator(Foo, ensure=True)\n\n    @click.group()\n    @pass_foo\n    def cli(foo):\n        pass\n\n    @cli.command()\n    @pass_foo\n    def test(foo):\n        click.echo(foo.title)\n\n    result = runner.invoke(cli, [\"test\"])\n    assert not result.exception\n    assert result.output == \"default\\n\"\n\n\ndef test_get_context_objects(runner):\n    class Foo:\n        def __init__(self):\n            self.title = \"default\"\n\n    pass_foo = click.make_pass_decorator(Foo, ensure=True)\n\n    @click.group()\n    @click.pass_context\n    def cli(ctx):\n        ctx.obj = Foo()\n        ctx.obj.title = \"test\"\n\n    @cli.command()\n    @pass_foo\n    def test(foo):\n        click.echo(foo.title)\n\n    result = runner.invoke(cli, [\"test\"])\n    assert not result.exception\n    assert result.output == \"test\\n\"\n\n\ndef test_get_context_objects_no_ensuring(runner):\n    class Foo:\n        def __init__(self):\n            self.title = \"default\"\n\n    pass_foo = click.make_pass_decorator(Foo)\n\n    @click.group()\n    @click.pass_context\n    def cli(ctx):\n        ctx.obj = Foo()\n        ctx.obj.title = \"test\"\n\n    @cli.command()\n    @pass_foo\n    def test(foo):\n        click.echo(foo.title)\n\n    result = runner.invoke(cli, [\"test\"])\n    assert not result.exception\n    assert result.output == \"test\\n\"\n\n\ndef test_get_context_objects_missing(runner):\n    class Foo:\n        pass\n\n    pass_foo = click.make_pass_decorator(Foo)\n\n    @click.group()\n    @click.pass_context\n    def cli(ctx):\n        pass\n\n    @cli.command()\n    @pass_foo\n    def test(foo):\n        click.echo(foo.title)\n\n    result = runner.invoke(cli, [\"test\"])\n    assert result.exception is not None\n    assert isinstance(result.exception, RuntimeError)\n    assert (\n        \"Managed to invoke callback without a context object of type\"\n        \" 'Foo' existing\" in str(result.exception)\n    )\n\n\ndef test_multi_enter(runner):\n    called = []\n\n    @click.command()\n    @click.pass_context\n    def cli(ctx):\n        def callback():\n            called.append(True)\n\n        ctx.call_on_close(callback)\n\n        with ctx:\n            pass\n        assert not called\n\n    result = runner.invoke(cli, [])\n    assert result.exception is None\n    assert called == [True]\n\n\ndef test_global_context_object(runner):\n    @click.command()\n    @click.pass_context\n    def cli(ctx):\n        assert click.get_current_context() is ctx\n        ctx.obj = \"FOOBAR\"\n        assert click.get_current_context().obj == \"FOOBAR\"\n\n    assert click.get_current_context(silent=True) is None\n    runner.invoke(cli, [], catch_exceptions=False)\n    assert click.get_current_context(silent=True) is None\n\n\ndef test_context_meta(runner):\n    LANG_KEY = f\"{__name__}.lang\"\n\n    def set_language(value):\n        click.get_current_context().meta[LANG_KEY] = value\n\n    def get_language():\n        return click.get_current_context().meta.get(LANG_KEY, \"en_US\")\n\n    @click.command()\n    @click.pass_context\n    def cli(ctx):\n        assert get_language() == \"en_US\"\n        set_language(\"de_DE\")\n        assert get_language() == \"de_DE\"\n\n    runner.invoke(cli, [], catch_exceptions=False)\n\n\ndef test_make_pass_meta_decorator(runner):\n    @click.group()\n    @click.pass_context\n    def cli(ctx):\n        ctx.meta[\"value\"] = \"good\"\n\n    @cli.command()\n    @pass_meta_key(\"value\")\n    def show(value):\n        return value\n\n    result = runner.invoke(cli, [\"show\"], standalone_mode=False)\n    assert result.return_value == \"good\"\n\n\ndef test_make_pass_meta_decorator_doc():\n    pass_value = pass_meta_key(\"value\")\n    assert \"the 'value' key from :attr:`click.Context.meta`\" in pass_value.__doc__\n    pass_value = pass_meta_key(\"value\", doc_description=\"the test value\")\n    assert \"passes the test value\" in pass_value.__doc__\n\n\ndef test_hiding_of_unset_sentinel_in_callbacks():\n    \"\"\"Fix: https://github.com/pallets/click/issues/3136\"\"\"\n\n    def inspect_other_params(ctx, param, value):\n        \"\"\"A callback that inspects other parameters' values via the context.\"\"\"\n        assert click.get_current_context() is ctx\n        click.echo(f\"callback.my_arg: {ctx.params.get('my_arg')!r}\")\n        click.echo(f\"callback.my_opt: {ctx.params.get('my_opt')!r}\")\n        click.echo(f\"callback.my_callback: {ctx.params.get('my_callback')!r}\")\n\n        click.echo(f\"callback.param: {param!r}\")\n        click.echo(f\"callback.value: {value!r}\")\n\n        return \"hard-coded\"\n\n    class ParameterInternalCheck(Option):\n        \"\"\"An option that checks internal state during processing.\"\"\"\n\n        def process_value(self, ctx, value):\n            \"\"\"Check that UNSET values are hidden as None in ctx.params within the\n            callback, and then properly restored afterwards.\n            \"\"\"\n            assert click.get_current_context() is ctx\n            click.echo(f\"before_process.my_arg: {ctx.params.get('my_arg')!r}\")\n            click.echo(f\"before_process.my_opt: {ctx.params.get('my_opt')!r}\")\n            click.echo(f\"before_process.my_callback: {ctx.params.get('my_callback')!r}\")\n\n            value = super().process_value(ctx, value)\n\n            assert click.get_current_context() is ctx\n            click.echo(f\"after_process.my_arg: {ctx.params.get('my_arg')!r}\")\n            click.echo(f\"after_process.my_opt: {ctx.params.get('my_opt')!r}\")\n            click.echo(f\"after_process.my_callback: {ctx.params.get('my_callback')!r}\")\n\n            return value\n\n    def change_other_params(ctx, param, value):\n        \"\"\"A callback that modifies other parameters' values via the context.\"\"\"\n        assert click.get_current_context() is ctx\n        click.echo(f\"before_change.my_arg: {ctx.params.get('my_arg')!r}\")\n        click.echo(f\"before_change.my_opt: {ctx.params.get('my_opt')!r}\")\n        click.echo(f\"before_change.my_callback: {ctx.params.get('my_callback')!r}\")\n\n        click.echo(f\"before_change.param: {param!r}\")\n        click.echo(f\"before_change.value: {value!r}\")\n\n        ctx.params[\"my_arg\"] = \"changed\"\n        # Reset to None parameters that where not UNSET to see they are not forced back\n        # to UNSET.\n        ctx.params[\"my_callback\"] = None\n\n        return value\n\n    @click.command\n    @click.argument(\"my-arg\", required=False)\n    @click.option(\"--my-opt\")\n    @click.option(\"--my-callback\", callback=inspect_other_params)\n    @click.option(\"--check-internal\", cls=ParameterInternalCheck)\n    @click.option(\n        \"--modifying-callback\", cls=ParameterInternalCheck, callback=change_other_params\n    )\n    @click.pass_context\n    def cli(ctx, my_arg, my_opt, my_callback, check_internal, modifying_callback):\n        click.echo(f\"cli.my_arg: {my_arg!r}\")\n        click.echo(f\"cli.my_opt: {my_opt!r}\")\n        click.echo(f\"cli.my_callback: {my_callback!r}\")\n        click.echo(f\"cli.check_internal: {check_internal!r}\")\n        click.echo(f\"cli.modifying_callback: {modifying_callback!r}\")\n\n    runner = click.testing.CliRunner()\n    result = runner.invoke(cli)\n\n    assert result.stdout.splitlines() == [\n        # Values of other parameters within the callback are None, not UNSET.\n        \"callback.my_arg: None\",\n        \"callback.my_opt: None\",\n        \"callback.my_callback: None\",\n        \"callback.param: <Option my_callback>\",\n        \"callback.value: None\",\n        # Previous UNSET values were not altered by the callback.\n        \"before_process.my_arg: Sentinel.UNSET\",\n        \"before_process.my_opt: Sentinel.UNSET\",\n        \"before_process.my_callback: 'hard-coded'\",\n        \"after_process.my_arg: Sentinel.UNSET\",\n        \"after_process.my_opt: Sentinel.UNSET\",\n        \"after_process.my_callback: 'hard-coded'\",\n        # Changes on other parameters within the callback are restored afterwards.\n        \"before_process.my_arg: Sentinel.UNSET\",\n        \"before_process.my_opt: Sentinel.UNSET\",\n        \"before_process.my_callback: 'hard-coded'\",\n        \"before_change.my_arg: None\",\n        \"before_change.my_opt: None\",\n        \"before_change.my_callback: 'hard-coded'\",\n        \"before_change.param: <ParameterInternalCheck modifying_callback>\",\n        \"before_change.value: None\",\n        \"after_process.my_arg: 'changed'\",\n        \"after_process.my_opt: Sentinel.UNSET\",\n        \"after_process.my_callback: None\",\n        # Unset values within the main command are UNSET, but hidden as None.\n        \"cli.my_arg: 'changed'\",\n        \"cli.my_opt: None\",\n        \"cli.my_callback: None\",\n        \"cli.check_internal: None\",\n        \"cli.modifying_callback: None\",\n    ]\n    assert not result.stderr\n    assert not result.exception\n    assert result.exit_code == 0\n\n\ndef test_context_pushing():\n    rv = []\n\n    @click.command()\n    def cli():\n        pass\n\n    ctx = click.Context(cli)\n\n    @ctx.call_on_close\n    def test_callback():\n        rv.append(42)\n\n    with ctx.scope(cleanup=False):\n        # Internal\n        assert ctx._depth == 2\n\n    assert rv == []\n\n    with ctx.scope():\n        # Internal\n        assert ctx._depth == 1\n\n    assert rv == [42]\n\n\ndef test_pass_obj(runner):\n    @click.group()\n    @click.pass_context\n    def cli(ctx):\n        ctx.obj = \"test\"\n\n    @cli.command()\n    @click.pass_obj\n    def test(obj):\n        click.echo(obj)\n\n    result = runner.invoke(cli, [\"test\"])\n    assert not result.exception\n    assert result.output == \"test\\n\"\n\n\ndef test_close_before_pop(runner):\n    called = []\n\n    @click.command()\n    @click.pass_context\n    def cli(ctx):\n        ctx.obj = \"test\"\n\n        @ctx.call_on_close\n        def foo():\n            assert click.get_current_context().obj == \"test\"\n            called.append(True)\n\n        click.echo(\"aha!\")\n\n    result = runner.invoke(cli, [])\n    assert not result.exception\n    assert result.output == \"aha!\\n\"\n    assert called == [True]\n\n\ndef test_close_before_exit(runner):\n    called = []\n\n    @click.command()\n    @click.pass_context\n    def cli(ctx):\n        ctx.obj = \"test\"\n\n        @ctx.call_on_close\n        def foo():\n            assert click.get_current_context().obj == \"test\"\n            called.append(True)\n\n        ctx.exit()\n\n        click.echo(\"aha!\")\n\n    result = runner.invoke(cli, [])\n    assert not result.exception\n    assert not result.output\n    assert called == [True]\n\n\n@pytest.mark.parametrize(\n    (\"cli_args\", \"expect\"),\n    [\n        pytest.param(\n            (\"--option-with-callback\", \"--force-exit\"),\n            [\"ExitingOption\", \"NonExitingOption\"],\n            id=\"natural_order\",\n        ),\n        pytest.param(\n            (\"--force-exit\", \"--option-with-callback\"),\n            [\"ExitingOption\"],\n            id=\"eagerness_precedence\",\n        ),\n    ],\n)\ndef test_multiple_eager_callbacks(runner, cli_args, expect):\n    \"\"\"Checks all callbacks are called on exit, even the nasty ones hidden within\n    callbacks.\n\n    Also checks the order in which they're called.\n    \"\"\"\n    # Keeps track of callback calls.\n    called = []\n\n    class NonExitingOption(Option):\n        def reset_state(self):\n            called.append(self.__class__.__name__)\n\n        def set_state(self, ctx: Context, param: Parameter, value: str) -> str:\n            ctx.call_on_close(self.reset_state)\n            return value\n\n        def __init__(self, *args, **kwargs) -> None:\n            kwargs.setdefault(\"expose_value\", False)\n            kwargs.setdefault(\"callback\", self.set_state)\n            super().__init__(*args, **kwargs)\n\n    class ExitingOption(NonExitingOption):\n        def set_state(self, ctx: Context, param: Parameter, value: str) -> str:\n            value = super().set_state(ctx, param, value)\n            ctx.exit()\n            return value\n\n    @click.command()\n    @click.option(\"--option-with-callback\", is_eager=True, cls=NonExitingOption)\n    @click.option(\"--force-exit\", is_eager=True, cls=ExitingOption)\n    def cli():\n        click.echo(\"This will never be printed as we forced exit via --force-exit\")\n\n    result = runner.invoke(cli, cli_args)\n    assert not result.exception\n    assert not result.output\n\n    assert called == expect\n\n\ndef test_no_state_leaks(runner):\n    \"\"\"Demonstrate state leaks with a specific case of the generic test above.\n\n    Use a logger as a real-world example of a common fixture which, due to its global\n    nature, can leak state if not clean-up properly in a callback.\n    \"\"\"\n    # Keeps track of callback calls.\n    called = []\n\n    class DebugLoggerOption(Option):\n        \"\"\"A custom option to set the name of the debug logger.\"\"\"\n\n        logger_name: str\n        \"\"\"The ID of the logger to use.\"\"\"\n\n        def reset_loggers(self):\n            \"\"\"Forces logger managed by the option to be reset to the default level.\"\"\"\n            logger = logging.getLogger(self.logger_name)\n            logger.setLevel(logging.NOTSET)\n\n            # Logger has been properly reset to its initial state.\n            assert logger.level == logging.NOTSET\n            assert logger.getEffectiveLevel() == logging.WARNING\n\n            called.append(True)\n\n        def set_level(self, ctx: Context, param: Parameter, value: str) -> None:\n            \"\"\"Set the logger to DEBUG level.\"\"\"\n            # Keep the logger name around so we can reset it later when winding down\n            # the option.\n            self.logger_name = value\n\n            # Get the global logger object.\n            logger = logging.getLogger(self.logger_name)\n\n            # Check pre-conditions: new logger is not set, but inherits its level from\n            # default <root> logger. That's the exact same state we are expecting our\n            # logger to be in after being messed with by the CLI.\n            assert logger.level == logging.NOTSET\n            assert logger.getEffectiveLevel() == logging.WARNING\n\n            logger.setLevel(logging.DEBUG)\n            ctx.call_on_close(self.reset_loggers)\n            return value\n\n        def __init__(self, *args, **kwargs) -> None:\n            kwargs.setdefault(\"callback\", self.set_level)\n            super().__init__(*args, **kwargs)\n\n    @click.command()\n    @click.option(\"--debug-logger-name\", is_eager=True, cls=DebugLoggerOption)\n    @help_option()\n    @click.pass_context\n    def messing_with_logger(ctx, debug_logger_name):\n        # Introspect context to make sure logger name are aligned.\n        assert debug_logger_name == ctx.command.params[0].logger_name\n\n        logger = logging.getLogger(debug_logger_name)\n\n        # Logger's level has been properly set to DEBUG by DebugLoggerOption.\n        assert logger.level == logging.DEBUG\n        assert logger.getEffectiveLevel() == logging.DEBUG\n\n        logger.debug(\"Blah blah blah\")\n\n        ctx.exit()\n\n        click.echo(\"This will never be printed as we exited early\")\n\n    # Call the CLI to mess with the custom logger.\n    result = runner.invoke(\n        messing_with_logger, [\"--debug-logger-name\", \"my_logger\", \"--help\"]\n    )\n\n    assert called == [True]\n\n    # Check the custom logger has been reverted to it initial state by the option\n    # callback after being messed with by the CLI.\n    logger = logging.getLogger(\"my_logger\")\n    assert logger.level == logging.NOTSET\n    assert logger.getEffectiveLevel() == logging.WARNING\n\n    assert not result.exception\n    assert result.output.startswith(\"Usage: messing-with-logger [OPTIONS]\")\n\n\ndef test_with_resource():\n    @contextmanager\n    def manager():\n        val = [1]\n        yield val\n        val[0] = 0\n\n    ctx = click.Context(click.Command(\"test\"))\n\n    with ctx.scope():\n        rv = ctx.with_resource(manager())\n        assert rv[0] == 1\n\n    assert rv == [0]\n\n\ndef test_with_resource_exception() -> None:\n    class TestContext(AbstractContextManager[list[int]]):\n        _handle_exception: bool\n        _base_val: int\n        val: list[int]\n\n        def __init__(self, base_val: int = 1, *, handle_exception: bool = True) -> None:\n            self._handle_exception = handle_exception\n            self._base_val = base_val\n\n        def __enter__(self) -> list[int]:\n            self.val = [self._base_val]\n            return self.val\n\n        def __exit__(\n            self,\n            exc_type: type[BaseException] | None,\n            exc_value: BaseException | None,\n            traceback: TracebackType | None,\n        ) -> bool | None:\n            if not exc_type:\n                self.val[0] = self._base_val - 1\n                return None\n\n            self.val[0] = self._base_val + 1\n            return self._handle_exception\n\n    class TestException(Exception):\n        pass\n\n    ctx = click.Context(click.Command(\"test\"))\n\n    base_val = 1\n\n    with ctx.scope():\n        rv = ctx.with_resource(TestContext(base_val=base_val))\n        assert rv[0] == base_val\n\n    assert rv == [base_val - 1]\n\n    with ctx.scope():\n        rv = ctx.with_resource(TestContext(base_val=base_val))\n        raise TestException()\n\n    assert rv == [base_val + 1]\n\n    with pytest.raises(TestException):\n        with ctx.scope():\n            rv = ctx.with_resource(\n                TestContext(base_val=base_val, handle_exception=False)\n            )\n            raise TestException()\n\n\ndef test_with_resource_nested_exception() -> None:\n    class TestContext(AbstractContextManager[list[int]]):\n        _handle_exception: bool\n        _base_val: int\n        val: list[int]\n\n        def __init__(self, base_val: int = 1, *, handle_exception: bool = True) -> None:\n            self._handle_exception = handle_exception\n            self._base_val = base_val\n\n        def __enter__(self) -> list[int]:\n            self.val = [self._base_val]\n            return self.val\n\n        def __exit__(\n            self,\n            exc_type: type[BaseException] | None,\n            exc_value: BaseException | None,\n            traceback: TracebackType | None,\n        ) -> bool | None:\n            if not exc_type:\n                self.val[0] = self._base_val - 1\n                return None\n\n            self.val[0] = self._base_val + 1\n            return self._handle_exception\n\n    class TestException(Exception):\n        pass\n\n    ctx = click.Context(click.Command(\"test\"))\n    base_val = 1\n    base_val_nested = 11\n\n    with ctx.scope():\n        rv = ctx.with_resource(TestContext(base_val=base_val))\n        rv_nested = ctx.with_resource(TestContext(base_val=base_val_nested))\n        assert rv[0] == base_val\n        assert rv_nested[0] == base_val_nested\n\n    assert rv == [base_val - 1]\n    assert rv_nested == [base_val_nested - 1]\n\n    with ctx.scope():\n        rv = ctx.with_resource(TestContext(base_val=base_val))\n        rv_nested = ctx.with_resource(TestContext(base_val=base_val_nested))\n        raise TestException()\n\n    # If one of the context \"eats\" the exceptions they will not be forwarded to other\n    # parts. This is due to how ExitStack unwinding works\n    assert rv_nested == [base_val_nested + 1]\n    assert rv == [base_val - 1]\n\n    with ctx.scope():\n        rv = ctx.with_resource(TestContext(base_val=base_val))\n        rv_nested = ctx.with_resource(\n            TestContext(base_val=base_val_nested, handle_exception=False)\n        )\n        raise TestException()\n\n    assert rv_nested == [base_val_nested + 1]\n    assert rv == [base_val + 1]\n\n    with pytest.raises(TestException):\n        rv = ctx.with_resource(TestContext(base_val=base_val, handle_exception=False))\n        rv_nested = ctx.with_resource(\n            TestContext(base_val=base_val_nested, handle_exception=False)\n        )\n        raise TestException()\n\n\ndef test_make_pass_decorator_args(runner):\n    \"\"\"\n    Test to check that make_pass_decorator doesn't consume arguments based on\n    invocation order.\n    \"\"\"\n\n    class Foo:\n        title = \"foocmd\"\n\n    pass_foo = click.make_pass_decorator(Foo)\n\n    @click.group()\n    @click.pass_context\n    def cli(ctx):\n        ctx.obj = Foo()\n\n    @cli.command()\n    @click.pass_context\n    @pass_foo\n    def test1(foo, ctx):\n        click.echo(foo.title)\n\n    @cli.command()\n    @pass_foo\n    @click.pass_context\n    def test2(ctx, foo):\n        click.echo(foo.title)\n\n    result = runner.invoke(cli, [\"test1\"])\n    assert not result.exception\n    assert result.output == \"foocmd\\n\"\n\n    result = runner.invoke(cli, [\"test2\"])\n    assert not result.exception\n    assert result.output == \"foocmd\\n\"\n\n\ndef test_propagate_show_default_setting(runner):\n    \"\"\"A context's ``show_default`` setting defaults to the value from\n    the parent context.\n    \"\"\"\n    group = click.Group(\n        commands={\n            \"sub\": click.Command(\"sub\", params=[click.Option([\"-a\"], default=\"a\")]),\n        },\n        context_settings={\"show_default\": True},\n    )\n    result = runner.invoke(group, [\"sub\", \"--help\"])\n    assert \"[default: a]\" in result.output\n\n\ndef test_exit_not_standalone():\n    @click.command()\n    @click.pass_context\n    def cli(ctx):\n        ctx.exit(1)\n\n    assert cli.main([], \"test_exit_not_standalone\", standalone_mode=False) == 1\n\n    @click.command()\n    @click.pass_context\n    def cli(ctx):\n        ctx.exit(0)\n\n    assert cli.main([], \"test_exit_not_standalone\", standalone_mode=False) == 0\n\n\n@pytest.mark.parametrize(\n    (\"option_args\", \"invoke_args\", \"expect\"),\n    [\n        pytest.param({}, {}, ParameterSource.DEFAULT, id=\"default\"),\n        pytest.param(\n            {},\n            {\"default_map\": {\"option\": 1}},\n            ParameterSource.DEFAULT_MAP,\n            id=\"default_map\",\n        ),\n        pytest.param(\n            {},\n            {\"args\": [\"-o\", \"1\"]},\n            ParameterSource.COMMANDLINE,\n            id=\"commandline short\",\n        ),\n        pytest.param(\n            {},\n            {\"args\": [\"--option\", \"1\"]},\n            ParameterSource.COMMANDLINE,\n            id=\"commandline long\",\n        ),\n        pytest.param(\n            {},\n            {\"auto_envvar_prefix\": \"TEST\", \"env\": {\"TEST_OPTION\": \"1\"}},\n            ParameterSource.ENVIRONMENT,\n            id=\"environment auto\",\n        ),\n        pytest.param(\n            {\"envvar\": \"NAME\"},\n            {\"env\": {\"NAME\": \"1\"}},\n            ParameterSource.ENVIRONMENT,\n            id=\"environment manual\",\n        ),\n    ],\n)\ndef test_parameter_source(runner, option_args, invoke_args, expect):\n    @click.command()\n    @click.pass_context\n    @click.option(\"-o\", \"--option\", default=1, **option_args)\n    def cli(ctx, option):\n        return ctx.get_parameter_source(\"option\")\n\n    rv = runner.invoke(cli, standalone_mode=False, **invoke_args)\n    assert rv.return_value == expect\n\n\ndef test_propagate_opt_prefixes():\n    parent = click.Context(click.Command(\"test\"))\n    parent._opt_prefixes = {\"-\", \"--\", \"!\"}\n    ctx = click.Context(click.Command(\"test2\"), parent=parent)\n\n    assert ctx._opt_prefixes == {\"-\", \"--\", \"!\"}\n"
  },
  {
    "path": "tests/test_custom_classes.py",
    "content": "import click\n\n\ndef test_command_context_class():\n    \"\"\"A command with a custom ``context_class`` should produce a\n    context using that type.\n    \"\"\"\n\n    class CustomContext(click.Context):\n        pass\n\n    class CustomCommand(click.Command):\n        context_class = CustomContext\n\n    command = CustomCommand(\"test\")\n    context = command.make_context(\"test\", [])\n    assert isinstance(context, CustomContext)\n\n\ndef test_context_invoke_type(runner):\n    \"\"\"A command invoked from a custom context should have a new\n    context with the same type.\n    \"\"\"\n\n    class CustomContext(click.Context):\n        pass\n\n    class CustomCommand(click.Command):\n        context_class = CustomContext\n\n    @click.command()\n    @click.argument(\"first_id\", type=int)\n    @click.pass_context\n    def second(ctx, first_id):\n        assert isinstance(ctx, CustomContext)\n        assert id(ctx) != first_id\n\n    @click.command(cls=CustomCommand)\n    @click.pass_context\n    def first(ctx):\n        assert isinstance(ctx, CustomContext)\n        ctx.invoke(second, first_id=id(ctx))\n\n    assert not runner.invoke(first).exception\n\n\ndef test_context_formatter_class():\n    \"\"\"A context with a custom ``formatter_class`` should format help\n    using that type.\n    \"\"\"\n\n    class CustomFormatter(click.HelpFormatter):\n        def write_heading(self, heading):\n            heading = click.style(heading, fg=\"yellow\")\n            return super().write_heading(heading)\n\n    class CustomContext(click.Context):\n        formatter_class = CustomFormatter\n\n    context = CustomContext(\n        click.Command(\"test\", params=[click.Option([\"--value\"])]), color=True\n    )\n    assert \"\\x1b[33mOptions\\x1b[0m:\" in context.get_help()\n\n\ndef test_group_command_class(runner):\n    \"\"\"A group with a custom ``command_class`` should create subcommands\n    of that type by default.\n    \"\"\"\n\n    class CustomCommand(click.Command):\n        pass\n\n    class CustomGroup(click.Group):\n        command_class = CustomCommand\n\n    group = CustomGroup()\n    subcommand = group.command()(lambda: None)\n    assert type(subcommand) is CustomCommand\n    subcommand = group.command(cls=click.Command)(lambda: None)\n    assert type(subcommand) is click.Command\n\n\ndef test_group_group_class(runner):\n    \"\"\"A group with a custom ``group_class`` should create subgroups\n    of that type by default.\n    \"\"\"\n\n    class CustomSubGroup(click.Group):\n        pass\n\n    class CustomGroup(click.Group):\n        group_class = CustomSubGroup\n\n    group = CustomGroup()\n    subgroup = group.group()(lambda: None)\n    assert type(subgroup) is CustomSubGroup\n    subgroup = group.command(cls=click.Group)(lambda: None)\n    assert type(subgroup) is click.Group\n\n\ndef test_group_group_class_self(runner):\n    \"\"\"A group with ``group_class = type`` should create subgroups of\n    the same type as itself.\n    \"\"\"\n\n    class CustomGroup(click.Group):\n        group_class = type\n\n    group = CustomGroup()\n    subgroup = group.group()(lambda: None)\n    assert type(subgroup) is CustomGroup\n"
  },
  {
    "path": "tests/test_defaults.py",
    "content": "import click\n\n\ndef test_basic_defaults(runner):\n    @click.command()\n    @click.option(\"--foo\", default=42, type=click.FLOAT)\n    def cli(foo):\n        assert isinstance(foo, float)\n        click.echo(f\"FOO:[{foo}]\")\n\n    result = runner.invoke(cli, [])\n    assert not result.exception\n    assert \"FOO:[42.0]\" in result.output\n\n\ndef test_multiple_defaults(runner):\n    @click.command()\n    @click.option(\"--foo\", default=[23, 42], type=click.FLOAT, multiple=True)\n    def cli(foo):\n        for item in foo:\n            assert isinstance(item, float)\n            click.echo(item)\n\n    result = runner.invoke(cli, [])\n    assert not result.exception\n    assert result.output.splitlines() == [\"23.0\", \"42.0\"]\n\n\ndef test_nargs_plus_multiple(runner):\n    @click.command()\n    @click.option(\n        \"--arg\", default=((1, 2), (3, 4)), nargs=2, multiple=True, type=click.INT\n    )\n    def cli(arg):\n        for a, b in arg:\n            click.echo(f\"<{a:d}|{b:d}>\")\n\n    result = runner.invoke(cli, [])\n    assert not result.exception\n    assert result.output.splitlines() == [\"<1|2>\", \"<3|4>\"]\n\n\ndef test_multiple_flag_default(runner):\n    \"\"\"Default default for flags when multiple=True should be empty tuple.\"\"\"\n\n    @click.command\n    # flag due to secondary token\n    @click.option(\"-y/-n\", multiple=True)\n    # flag due to is_flag\n    @click.option(\"-f\", is_flag=True, multiple=True)\n    # flag due to flag_value\n    @click.option(\"-v\", \"v\", flag_value=1, multiple=True)\n    @click.option(\"-q\", \"v\", flag_value=-1, multiple=True)\n    def cli(y, f, v):\n        return y, f, v\n\n    result = runner.invoke(cli, standalone_mode=False)\n    assert result.return_value == ((), (), ())\n\n    result = runner.invoke(cli, [\"-y\", \"-n\", \"-f\", \"-v\", \"-q\"], standalone_mode=False)\n    assert result.return_value == ((True, False), (True,), (1, -1))\n\n\ndef test_flag_default_map(runner):\n    \"\"\"test flag with default map\"\"\"\n\n    @click.group()\n    def cli():\n        pass\n\n    @cli.command()\n    @click.option(\"--name/--no-name\", is_flag=True, show_default=True, help=\"name flag\")\n    def foo(name):\n        click.echo(name)\n\n    result = runner.invoke(cli, [\"foo\"])\n    assert \"False\" in result.output\n\n    result = runner.invoke(cli, [\"foo\", \"--help\"])\n    assert \"default: no-name\" in result.output\n\n    result = runner.invoke(cli, [\"foo\"], default_map={\"foo\": {\"name\": True}})\n    assert \"True\" in result.output\n\n    result = runner.invoke(cli, [\"foo\", \"--help\"], default_map={\"foo\": {\"name\": True}})\n    assert \"default: name\" in result.output\n\n\ndef test_shared_param_prefers_first_default(runner):\n    \"\"\"test that the first default is chosen when multiple flags share a param name\"\"\"\n\n    @click.command\n    @click.option(\"--red\", \"color\", flag_value=\"red\")\n    @click.option(\"--green\", \"color\", flag_value=\"green\", default=True)\n    def prefers_green(color):\n        click.echo(color)\n\n    @click.command\n    @click.option(\"--red\", \"color\", flag_value=\"red\", default=True)\n    @click.option(\"--green\", \"color\", flag_value=\"green\")\n    def prefers_red(color):\n        click.echo(color)\n\n    result = runner.invoke(prefers_green, [])\n    assert \"green\" in result.output\n    result = runner.invoke(prefers_green, [\"--red\"])\n    assert \"red\" in result.output\n\n    result = runner.invoke(prefers_red, [])\n    assert \"red\" in result.output\n    result = runner.invoke(prefers_red, [\"--green\"])\n    assert \"green\" in result.output\n"
  },
  {
    "path": "tests/test_formatting.py",
    "content": "import click\n\n\ndef test_basic_functionality(runner):\n    @click.command()\n    def cli():\n        \"\"\"First paragraph.\n\n        This is a very long second\n        paragraph and not correctly\n        wrapped but it will be rewrapped.\n\n        \\b\n        This is\n        a paragraph\n        without rewrapping.\n\n        \\b\n        1\n         2\n          3\n\n        And this is a paragraph\n        that will be rewrapped again.\n        \"\"\"\n\n    result = runner.invoke(cli, [\"--help\"], terminal_width=60)\n    assert not result.exception\n    assert result.output.splitlines() == [\n        \"Usage: cli [OPTIONS]\",\n        \"\",\n        \"  First paragraph.\",\n        \"\",\n        \"  This is a very long second paragraph and not correctly\",\n        \"  wrapped but it will be rewrapped.\",\n        \"\",\n        \"  This is\",\n        \"  a paragraph\",\n        \"  without rewrapping.\",\n        \"\",\n        \"  1\",\n        \"   2\",\n        \"    3\",\n        \"\",\n        \"  And this is a paragraph that will be rewrapped again.\",\n        \"\",\n        \"Options:\",\n        \"  --help  Show this message and exit.\",\n    ]\n\n\ndef test_wrapping_long_options_strings(runner):\n    @click.group()\n    def cli():\n        \"\"\"Top level command\"\"\"\n\n    @cli.group()\n    def a_very_long():\n        \"\"\"Second level\"\"\"\n\n    @a_very_long.command()\n    @click.argument(\"first\")\n    @click.argument(\"second\")\n    @click.argument(\"third\")\n    @click.argument(\"fourth\")\n    @click.argument(\"fifth\")\n    @click.argument(\"sixth\")\n    def command():\n        \"\"\"A command.\"\"\"\n\n    # 54 is chosen as a length where the second line is one character\n    # longer than the maximum length.\n    result = runner.invoke(cli, [\"a-very-long\", \"command\", \"--help\"], terminal_width=54)\n    assert not result.exception\n    assert result.output.splitlines() == [\n        \"Usage: cli a-very-long command [OPTIONS] FIRST SECOND\",\n        \"                               THIRD FOURTH FIFTH\",\n        \"                               SIXTH\",\n        \"\",\n        \"  A command.\",\n        \"\",\n        \"Options:\",\n        \"  --help  Show this message and exit.\",\n    ]\n\n\ndef test_wrapping_long_command_name(runner):\n    @click.group()\n    def cli():\n        \"\"\"Top level command\"\"\"\n\n    @cli.group()\n    def a_very_very_very_long():\n        \"\"\"Second level\"\"\"\n\n    @a_very_very_very_long.command()\n    @click.argument(\"first\")\n    @click.argument(\"second\")\n    @click.argument(\"third\")\n    @click.argument(\"fourth\")\n    @click.argument(\"fifth\")\n    @click.argument(\"sixth\")\n    def command():\n        \"\"\"A command.\"\"\"\n\n    result = runner.invoke(\n        cli, [\"a-very-very-very-long\", \"command\", \"--help\"], terminal_width=54\n    )\n    assert not result.exception\n    assert result.output.splitlines() == [\n        \"Usage: cli a-very-very-very-long command \",\n        \"           [OPTIONS] FIRST SECOND THIRD FOURTH FIFTH\",\n        \"           SIXTH\",\n        \"\",\n        \"  A command.\",\n        \"\",\n        \"Options:\",\n        \"  --help  Show this message and exit.\",\n    ]\n\n\ndef test_formatting_empty_help_lines(runner):\n    @click.command()\n    def cli():\n        # fmt: off\n        \"\"\"Top level command\n\n        \"\"\"\n        # fmt: on\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert not result.exception\n    assert result.output.splitlines() == [\n        \"Usage: cli [OPTIONS]\",\n        \"\",\n        \"  Top level command\",\n        \"\",\n        \"\",\n        \"\",\n        \"Options:\",\n        \"  --help  Show this message and exit.\",\n    ]\n\n\ndef test_formatting_usage_error(runner):\n    @click.command()\n    @click.argument(\"arg\")\n    def cmd(arg):\n        click.echo(f\"arg:{arg}\")\n\n    result = runner.invoke(cmd, [])\n    assert result.exit_code == 2\n    assert result.output.splitlines() == [\n        \"Usage: cmd [OPTIONS] ARG\",\n        \"Try 'cmd --help' for help.\",\n        \"\",\n        \"Error: Missing argument 'ARG'.\",\n    ]\n\n\ndef test_formatting_usage_error_metavar_missing_arg(runner):\n    \"\"\"\n    :author: @r-m-n\n    Including attribution to #612\n    \"\"\"\n\n    @click.command()\n    @click.argument(\"arg\", metavar=\"metavar\")\n    def cmd(arg):\n        pass\n\n    result = runner.invoke(cmd, [])\n    assert result.exit_code == 2\n    assert result.output.splitlines() == [\n        \"Usage: cmd [OPTIONS] metavar\",\n        \"Try 'cmd --help' for help.\",\n        \"\",\n        \"Error: Missing argument 'metavar'.\",\n    ]\n\n\ndef test_formatting_usage_error_metavar_bad_arg(runner):\n    @click.command()\n    @click.argument(\"arg\", type=click.INT, metavar=\"metavar\")\n    def cmd(arg):\n        pass\n\n    result = runner.invoke(cmd, [\"3.14\"])\n    assert result.exit_code == 2\n    assert result.output.splitlines() == [\n        \"Usage: cmd [OPTIONS] metavar\",\n        \"Try 'cmd --help' for help.\",\n        \"\",\n        \"Error: Invalid value for 'metavar': '3.14' is not a valid integer.\",\n    ]\n\n\ndef test_formatting_usage_error_nested(runner):\n    @click.group()\n    def cmd():\n        pass\n\n    @cmd.command()\n    @click.argument(\"bar\")\n    def foo(bar):\n        click.echo(f\"foo:{bar}\")\n\n    result = runner.invoke(cmd, [\"foo\"])\n    assert result.exit_code == 2\n    assert result.output.splitlines() == [\n        \"Usage: cmd foo [OPTIONS] BAR\",\n        \"Try 'cmd foo --help' for help.\",\n        \"\",\n        \"Error: Missing argument 'BAR'.\",\n    ]\n\n\ndef test_formatting_usage_error_no_help(runner):\n    @click.command(add_help_option=False)\n    @click.argument(\"arg\")\n    def cmd(arg):\n        click.echo(f\"arg:{arg}\")\n\n    result = runner.invoke(cmd, [])\n    assert result.exit_code == 2\n    assert result.output.splitlines() == [\n        \"Usage: cmd [OPTIONS] ARG\",\n        \"\",\n        \"Error: Missing argument 'ARG'.\",\n    ]\n\n\ndef test_formatting_usage_custom_help(runner):\n    @click.command(context_settings=dict(help_option_names=[\"--man\"]))\n    @click.argument(\"arg\")\n    def cmd(arg):\n        click.echo(f\"arg:{arg}\")\n\n    result = runner.invoke(cmd, [])\n    assert result.exit_code == 2\n    assert result.output.splitlines() == [\n        \"Usage: cmd [OPTIONS] ARG\",\n        \"Try 'cmd --man' for help.\",\n        \"\",\n        \"Error: Missing argument 'ARG'.\",\n    ]\n\n\ndef test_formatting_custom_type_metavar(runner):\n    class MyType(click.ParamType):\n        def get_metavar(self, param: click.Parameter, ctx: click.Context):\n            return \"MY_TYPE\"\n\n    @click.command(\"foo\")\n    @click.help_option()\n    @click.argument(\"param\", type=MyType())\n    def cmd(param):\n        pass\n\n    result = runner.invoke(cmd, \"--help\")\n    assert not result.exception\n    assert result.output.splitlines() == [\n        \"Usage: foo [OPTIONS] MY_TYPE\",\n        \"\",\n        \"Options:\",\n        \"  --help  Show this message and exit.\",\n    ]\n\n\ndef test_truncating_docstring(runner):\n    @click.command()\n    @click.pass_context\n    def cli(ctx):\n        \"\"\"First paragraph.\n\n        This is a very long second\n        paragraph and not correctly\n        wrapped but it will be rewrapped.\n        \\f\n\n        :param click.core.Context ctx: Click context.\n        \"\"\"\n\n    result = runner.invoke(cli, [\"--help\"], terminal_width=60)\n    assert not result.exception\n    assert result.output.splitlines() == [\n        \"Usage: cli [OPTIONS]\",\n        \"\",\n        \"  First paragraph.\",\n        \"\",\n        \"  This is a very long second paragraph and not correctly\",\n        \"  wrapped but it will be rewrapped.\",\n        \"\",\n        \"Options:\",\n        \"  --help  Show this message and exit.\",\n    ]\n\n\ndef test_truncating_docstring_no_help(runner):\n    @click.command()\n    @click.pass_context\n    def cli(ctx):\n        \"\"\"\n        \\f\n\n        This text should be truncated.\n        \"\"\"\n\n    result = runner.invoke(cli, [\"--help\"], terminal_width=60)\n    assert not result.exception\n    assert result.output.splitlines() == [\n        \"Usage: cli [OPTIONS]\",\n        \"\",\n        \"Options:\",\n        \"  --help  Show this message and exit.\",\n    ]\n\n\ndef test_removing_multiline_marker(runner):\n    @click.group()\n    def cli():\n        pass\n\n    @cli.command()\n    def cmd1():\n        \"\"\"\\b\n        This is command with a multiline help text\n        which should not be rewrapped.\n        The output of the short help text should\n        not contain the multiline marker.\n        \"\"\"\n        pass\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert \"\\b\" not in result.output\n\n\ndef test_global_show_default(runner):\n    @click.command(context_settings=dict(show_default=True))\n    @click.option(\"-f\", \"in_file\", default=\"out.txt\", help=\"Output file name\")\n    def cli():\n        pass\n\n    result = runner.invoke(cli, [\"--help\"])\n    # the default to \"--help\" is not shown because it is False\n    assert result.output.splitlines() == [\n        \"Usage: cli [OPTIONS]\",\n        \"\",\n        \"Options:\",\n        \"  -f TEXT  Output file name  [default: out.txt]\",\n        \"  --help   Show this message and exit.\",\n    ]\n\n\ndef test_formatting_with_options_metavar_empty(runner):\n    cli = click.Command(\"cli\", options_metavar=\"\", params=[click.Argument([\"var\"])])\n    result = runner.invoke(cli, [\"--help\"])\n    assert \"Usage: cli VAR\\n\" in result.output\n\n\ndef test_help_formatter_write_text():\n    text = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit\"\n    formatter = click.HelpFormatter(width=len(\"  Lorem ipsum dolor sit amet,\"))\n    formatter.current_indent = 2\n    formatter.write_text(text)\n    actual = formatter.getvalue()\n    expected = \"  Lorem ipsum dolor sit amet,\\n  consectetur adipiscing elit\\n\"\n    assert actual == expected\n"
  },
  {
    "path": "tests/test_imports.py",
    "content": "import json\nimport subprocess\nimport sys\n\nfrom click._compat import WIN\n\nIMPORT_TEST = b\"\"\"\\\nimport builtins\n\nfound_imports = set()\nreal_import = builtins.__import__\nimport sys\n\ndef tracking_import(module, locals=None, globals=None, fromlist=None,\n                    level=0):\n    rv = real_import(module, locals, globals, fromlist, level)\n    if globals and globals['__name__'].startswith('click') and level == 0:\n        found_imports.add(module)\n    return rv\nbuiltins.__import__ = tracking_import\n\nimport click\nrv = list(found_imports)\nimport json\nclick.echo(json.dumps(rv))\n\"\"\"\n\nALLOWED_IMPORTS = {\n    \"__future__\",\n    \"codecs\",\n    \"collections\",\n    \"collections.abc\",\n    \"configparser\",\n    \"contextlib\",\n    \"datetime\",\n    \"enum\",\n    \"errno\",\n    \"fcntl\",\n    \"functools\",\n    \"gettext\",\n    \"inspect\",\n    \"io\",\n    \"itertools\",\n    \"os\",\n    \"re\",\n    \"stat\",\n    \"struct\",\n    \"sys\",\n    \"threading\",\n    \"types\",\n    \"typing\",\n    \"weakref\",\n}\n\nif WIN:\n    ALLOWED_IMPORTS.update([\"ctypes\", \"ctypes.wintypes\", \"msvcrt\", \"time\"])\n\n\ndef test_light_imports():\n    c = subprocess.Popen(\n        [sys.executable, \"-\"], stdin=subprocess.PIPE, stdout=subprocess.PIPE\n    )\n    rv = c.communicate(IMPORT_TEST)[0]\n    rv = rv.decode(\"utf-8\")\n    imported = json.loads(rv)\n\n    for module in imported:\n        if module == \"click\" or module.startswith(\"click.\"):\n            continue\n        assert module in ALLOWED_IMPORTS\n"
  },
  {
    "path": "tests/test_info_dict.py",
    "content": "import pytest\n\nimport click.types\n\n# Common (obj, expect) pairs used to construct multiple tests.\nSTRING_PARAM_TYPE = (click.STRING, {\"param_type\": \"String\", \"name\": \"text\"})\nINT_PARAM_TYPE = (click.INT, {\"param_type\": \"Int\", \"name\": \"integer\"})\nBOOL_PARAM_TYPE = (click.BOOL, {\"param_type\": \"Bool\", \"name\": \"boolean\"})\nHELP_OPTION = (\n    None,\n    {\n        \"name\": \"help\",\n        \"param_type_name\": \"option\",\n        \"opts\": [\"--help\"],\n        \"secondary_opts\": [],\n        \"type\": BOOL_PARAM_TYPE[1],\n        \"required\": False,\n        \"nargs\": 1,\n        \"multiple\": False,\n        \"default\": False,\n        \"envvar\": None,\n        \"help\": \"Show this message and exit.\",\n        \"prompt\": None,\n        \"is_flag\": True,\n        \"flag_value\": True,\n        \"count\": False,\n        \"hidden\": False,\n    },\n)\nNAME_ARGUMENT = (\n    click.Argument([\"name\"]),\n    {\n        \"name\": \"name\",\n        \"param_type_name\": \"argument\",\n        \"opts\": [\"name\"],\n        \"secondary_opts\": [],\n        \"type\": STRING_PARAM_TYPE[1],\n        \"required\": True,\n        \"nargs\": 1,\n        \"multiple\": False,\n        \"default\": None,\n        \"envvar\": None,\n    },\n)\nNUMBER_OPTION = (\n    click.Option([\"-c\", \"--count\", \"number\"], default=1),\n    {\n        \"name\": \"number\",\n        \"param_type_name\": \"option\",\n        \"opts\": [\"-c\", \"--count\"],\n        \"secondary_opts\": [],\n        \"type\": INT_PARAM_TYPE[1],\n        \"required\": False,\n        \"nargs\": 1,\n        \"multiple\": False,\n        \"default\": 1,\n        \"envvar\": None,\n        \"help\": None,\n        \"prompt\": None,\n        \"is_flag\": False,\n        \"flag_value\": None,\n        \"count\": False,\n        \"hidden\": False,\n    },\n)\nHELLO_COMMAND = (\n    click.Command(\"hello\", params=[NUMBER_OPTION[0]]),\n    {\n        \"name\": \"hello\",\n        \"params\": [NUMBER_OPTION[1], HELP_OPTION[1]],\n        \"help\": None,\n        \"epilog\": None,\n        \"short_help\": None,\n        \"hidden\": False,\n        \"deprecated\": False,\n    },\n)\nHELLO_GROUP = (\n    click.Group(\"cli\", [HELLO_COMMAND[0]]),\n    {\n        \"name\": \"cli\",\n        \"params\": [HELP_OPTION[1]],\n        \"help\": None,\n        \"epilog\": None,\n        \"short_help\": None,\n        \"hidden\": False,\n        \"deprecated\": False,\n        \"commands\": {\"hello\": HELLO_COMMAND[1]},\n        \"chain\": False,\n    },\n)\n\n\n@pytest.mark.parametrize(\n    (\"obj\", \"expect\"),\n    [\n        pytest.param(\n            click.types.FuncParamType(range),\n            {\"param_type\": \"Func\", \"name\": \"range\", \"func\": range},\n            id=\"Func ParamType\",\n        ),\n        pytest.param(\n            click.UNPROCESSED,\n            {\"param_type\": \"Unprocessed\", \"name\": \"text\"},\n            id=\"UNPROCESSED ParamType\",\n        ),\n        pytest.param(*STRING_PARAM_TYPE, id=\"STRING ParamType\"),\n        pytest.param(\n            click.Choice((\"a\", \"b\")),\n            {\n                \"param_type\": \"Choice\",\n                \"name\": \"choice\",\n                \"choices\": (\"a\", \"b\"),\n                \"case_sensitive\": True,\n            },\n            id=\"Choice ParamType\",\n        ),\n        pytest.param(\n            click.DateTime([\"%Y-%m-%d\"]),\n            {\"param_type\": \"DateTime\", \"name\": \"datetime\", \"formats\": [\"%Y-%m-%d\"]},\n            id=\"DateTime ParamType\",\n        ),\n        pytest.param(*INT_PARAM_TYPE, id=\"INT ParamType\"),\n        pytest.param(\n            click.IntRange(0, 10, clamp=True),\n            {\n                \"param_type\": \"IntRange\",\n                \"name\": \"integer range\",\n                \"min\": 0,\n                \"max\": 10,\n                \"min_open\": False,\n                \"max_open\": False,\n                \"clamp\": True,\n            },\n            id=\"IntRange ParamType\",\n        ),\n        pytest.param(\n            click.FLOAT, {\"param_type\": \"Float\", \"name\": \"float\"}, id=\"FLOAT ParamType\"\n        ),\n        pytest.param(\n            click.FloatRange(-0.5, 0.5),\n            {\n                \"param_type\": \"FloatRange\",\n                \"name\": \"float range\",\n                \"min\": -0.5,\n                \"max\": 0.5,\n                \"min_open\": False,\n                \"max_open\": False,\n                \"clamp\": False,\n            },\n            id=\"FloatRange ParamType\",\n        ),\n        pytest.param(*BOOL_PARAM_TYPE, id=\"Bool ParamType\"),\n        pytest.param(\n            click.UUID, {\"param_type\": \"UUID\", \"name\": \"uuid\"}, id=\"UUID ParamType\"\n        ),\n        pytest.param(\n            click.File(),\n            {\"param_type\": \"File\", \"name\": \"filename\", \"mode\": \"r\", \"encoding\": None},\n            id=\"File ParamType\",\n        ),\n        pytest.param(\n            click.Path(),\n            {\n                \"param_type\": \"Path\",\n                \"name\": \"path\",\n                \"exists\": False,\n                \"file_okay\": True,\n                \"dir_okay\": True,\n                \"writable\": False,\n                \"readable\": True,\n                \"allow_dash\": False,\n            },\n            id=\"Path ParamType\",\n        ),\n        pytest.param(\n            click.Tuple((click.STRING, click.INT)),\n            {\n                \"param_type\": \"Tuple\",\n                \"name\": \"<text integer>\",\n                \"types\": [STRING_PARAM_TYPE[1], INT_PARAM_TYPE[1]],\n            },\n            id=\"Tuple ParamType\",\n        ),\n        pytest.param(*NUMBER_OPTION, id=\"Option\"),\n        pytest.param(\n            click.Option([\"--cache/--no-cache\", \"-c/-u\"]),\n            {\n                \"name\": \"cache\",\n                \"param_type_name\": \"option\",\n                \"opts\": [\"--cache\", \"-c\"],\n                \"secondary_opts\": [\"--no-cache\", \"-u\"],\n                \"type\": BOOL_PARAM_TYPE[1],\n                \"required\": False,\n                \"nargs\": 1,\n                \"multiple\": False,\n                \"default\": False,\n                \"envvar\": None,\n                \"help\": None,\n                \"prompt\": None,\n                \"is_flag\": True,\n                \"flag_value\": True,\n                \"count\": False,\n                \"hidden\": False,\n            },\n            id=\"Flag Option\",\n        ),\n        pytest.param(*NAME_ARGUMENT, id=\"Argument\"),\n    ],\n)\ndef test_parameter(obj, expect):\n    out = obj.to_info_dict()\n    assert out == expect\n\n\n@pytest.mark.parametrize(\n    (\"obj\", \"expect\"),\n    [\n        pytest.param(*HELLO_COMMAND, id=\"Command\"),\n        pytest.param(*HELLO_GROUP, id=\"Group\"),\n        pytest.param(\n            click.Group(\n                \"base\",\n                [click.Command(\"test\", params=[NAME_ARGUMENT[0]]), HELLO_GROUP[0]],\n            ),\n            {\n                \"name\": \"base\",\n                \"params\": [HELP_OPTION[1]],\n                \"help\": None,\n                \"epilog\": None,\n                \"short_help\": None,\n                \"hidden\": False,\n                \"deprecated\": False,\n                \"commands\": {\n                    \"cli\": HELLO_GROUP[1],\n                    \"test\": {\n                        \"name\": \"test\",\n                        \"params\": [NAME_ARGUMENT[1], HELP_OPTION[1]],\n                        \"help\": None,\n                        \"epilog\": None,\n                        \"short_help\": None,\n                        \"hidden\": False,\n                        \"deprecated\": False,\n                    },\n                },\n                \"chain\": False,\n            },\n            id=\"Nested Group\",\n        ),\n    ],\n)\ndef test_command(obj, expect):\n    ctx = click.Context(obj)\n    out = obj.to_info_dict(ctx)\n    assert out == expect\n\n\ndef test_context():\n    ctx = click.Context(HELLO_COMMAND[0])\n    out = ctx.to_info_dict()\n    assert out == {\n        \"command\": HELLO_COMMAND[1],\n        \"info_name\": None,\n        \"allow_extra_args\": False,\n        \"allow_interspersed_args\": True,\n        \"ignore_unknown_options\": False,\n        \"auto_envvar_prefix\": None,\n    }\n\n\ndef test_paramtype_no_name():\n    class TestType(click.ParamType):\n        pass\n\n    assert TestType().to_info_dict()[\"name\"] == \"TestType\"\n"
  },
  {
    "path": "tests/test_normalization.py",
    "content": "import click\n\nCONTEXT_SETTINGS = dict(token_normalize_func=lambda x: x.lower())\n\n\ndef test_option_normalization(runner):\n    @click.command(context_settings=CONTEXT_SETTINGS)\n    @click.option(\"--foo\")\n    @click.option(\"-x\")\n    def cli(foo, x):\n        click.echo(foo)\n        click.echo(x)\n\n    result = runner.invoke(cli, [\"--FOO\", \"42\", \"-X\", 23])\n    assert result.output == \"42\\n23\\n\"\n\n\ndef test_choice_normalization(runner):\n    @click.command(context_settings=CONTEXT_SETTINGS)\n    @click.option(\n        \"--method\",\n        type=click.Choice(\n            [\"SCREAMING_SNAKE_CASE\", \"snake_case\", \"PascalCase\", \"kebab-case\"],\n            case_sensitive=False,\n        ),\n    )\n    def cli(method):\n        click.echo(method)\n\n    result = runner.invoke(cli, [\"--METHOD=snake_case\"])\n    assert not result.exception, result.output\n    assert result.output == \"snake_case\\n\"\n\n    # Even though it's case sensitive, the choice's original value is preserved\n    result = runner.invoke(cli, [\"--method=pascalcase\"])\n    assert not result.exception, result.output\n    assert result.output == \"PascalCase\\n\"\n\n    result = runner.invoke(cli, [\"--method=meh\"])\n    assert result.exit_code == 2\n    assert (\n        \"Invalid value for '--method': 'meh' is not one of \"\n        \"'screaming_snake_case', 'snake_case', 'pascalcase', 'kebab-case'.\"\n    ) in result.output\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert (\n        \"--method [screaming_snake_case|snake_case|pascalcase|kebab-case]\"\n        in result.output\n    )\n\n\ndef test_command_normalization(runner):\n    @click.group(context_settings=CONTEXT_SETTINGS)\n    def cli():\n        pass\n\n    @cli.command()\n    def foo():\n        click.echo(\"here!\")\n\n    result = runner.invoke(cli, [\"FOO\"])\n    assert result.output == \"here!\\n\"\n"
  },
  {
    "path": "tests/test_options.py",
    "content": "import enum\nimport os\nimport re\nimport sys\nimport tempfile\nfrom contextlib import nullcontext\nfrom typing import Literal\n\nif sys.version_info < (3, 11):\n    enum.StrEnum = enum.Enum  # type: ignore[assignment]\n\nimport pytest\n\nimport click\nfrom click import Option\nfrom click import UNPROCESSED\nfrom click._utils import UNSET\nfrom click.testing import CliRunner\n\n\ndef test_prefixes(runner):\n    @click.command()\n    @click.option(\"++foo\", is_flag=True, help=\"das foo\")\n    @click.option(\"--bar\", is_flag=True, help=\"das bar\")\n    def cli(foo, bar):\n        click.echo(f\"foo={foo} bar={bar}\")\n\n    result = runner.invoke(cli, [\"++foo\", \"--bar\"])\n    assert not result.exception\n    assert result.output == \"foo=True bar=True\\n\"\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert re.search(r\"\\+\\+foo\\s+das foo\", result.output) is not None\n    assert re.search(r\"--bar\\s+das bar\", result.output) is not None\n\n\ndef test_invalid_option(runner):\n    with pytest.raises(TypeError, match=\"name was passed\") as exc_info:\n        click.Option([\"foo\"])\n\n    message = str(exc_info.value)\n    assert \"name was passed (foo)\" in message\n    assert \"declare an argument\" in message\n    assert \"'--foo'\" in message\n\n\n@pytest.mark.parametrize(\"deprecated\", [True, \"USE B INSTEAD\"])\ndef test_deprecated_usage(runner, deprecated):\n    @click.command()\n    @click.option(\"--foo\", default=\"bar\", deprecated=deprecated)\n    def cmd(foo):\n        click.echo(foo)\n\n    result = runner.invoke(cmd, [\"--help\"])\n    assert \"(DEPRECATED\" in result.output\n\n    if isinstance(deprecated, str):\n        assert deprecated in result.output\n\n\n@pytest.mark.parametrize(\"deprecated\", [True, \"USE B INSTEAD\"])\ndef test_deprecated_warning(runner, deprecated):\n    @click.command()\n    @click.option(\n        \"--my-option\", required=False, deprecated=deprecated, default=\"default option\"\n    )\n    def cli(my_option: str):\n        click.echo(f\"{my_option}\")\n\n    # defaults should not give a deprecated warning\n    result = runner.invoke(cli, [])\n    assert result.exit_code == 0, result.output\n    assert \"is deprecated\" not in result.output\n\n    result = runner.invoke(cli, [\"--my-option\", \"hello\"])\n    assert result.exit_code == 0, result.output\n    assert \"option 'my_option' is deprecated\" in result.output\n\n    if isinstance(deprecated, str):\n        assert deprecated in result.output\n\n\ndef test_deprecated_required(runner):\n    with pytest.raises(ValueError, match=\"is deprecated and still required\"):\n        click.Option([\"--a\"], required=True, deprecated=True)\n\n\ndef test_deprecated_prompt(runner):\n    with pytest.raises(ValueError, match=\"`deprecated` options cannot use `prompt`\"):\n        click.Option([\"--a\"], prompt=True, deprecated=True)\n\n\ndef test_invalid_nargs(runner):\n    with pytest.raises(TypeError, match=\"nargs=-1 is not supported for options.\"):\n\n        @click.command()\n        @click.option(\"--foo\", nargs=-1)\n        def cli(foo):\n            pass\n\n\ndef test_nargs_tup_composite_mult(runner):\n    @click.command()\n    @click.option(\"--item\", type=(str, int), multiple=True)\n    def copy(item):\n        for name, id in item:\n            click.echo(f\"name={name} id={id:d}\")\n\n    result = runner.invoke(copy, [\"--item\", \"peter\", \"1\", \"--item\", \"max\", \"2\"])\n    assert not result.exception\n    assert result.output.splitlines() == [\"name=peter id=1\", \"name=max id=2\"]\n\n\ndef test_counting(runner):\n    @click.command()\n    @click.option(\"-v\", count=True, help=\"Verbosity\", type=click.IntRange(0, 3))\n    def cli(v):\n        click.echo(f\"verbosity={v:d}\")\n\n    result = runner.invoke(cli, [\"-vvv\"])\n    assert not result.exception\n    assert result.output == \"verbosity=3\\n\"\n\n    result = runner.invoke(cli, [\"-vvvv\"])\n    assert result.exception\n    assert \"Invalid value for '-v': 4 is not in the range 0<=x<=3.\" in result.output\n\n    result = runner.invoke(cli, [])\n    assert not result.exception\n    assert result.output == \"verbosity=0\\n\"\n\n    result = runner.invoke(cli, [\"--help\"])\n    assert re.search(r\"-v\\s+Verbosity\", result.output) is not None\n\n\n@pytest.mark.parametrize(\"unknown_flag\", [\"--foo\", \"-f\"])\ndef test_unknown_options(runner, unknown_flag):\n    @click.command()\n    def cli():\n        pass\n\n    result = runner.invoke(cli, [unknown_flag])\n    assert result.exception\n    assert f\"No such option: {unknown_flag}\" in result.output\n\n\n@pytest.mark.parametrize(\n    (\"value\", \"expect\"),\n    [\n        (\"--cat\", \"Did you mean --count?\"),\n        (\"--bounds\", \"(Possible options: --bound, --count)\"),\n        (\"--bount\", \"(Possible options: --bound, --count)\"),\n    ],\n)\ndef test_suggest_possible_options(runner, value, expect):\n    cli = click.Command(\n        \"cli\", params=[click.Option([\"--bound\"]), click.Option([\"--count\"])]\n    )\n    result = runner.invoke(cli, [value])\n    assert expect in result.output\n\n\ndef test_multiple_required(runner):\n    @click.command()\n    @click.option(\"-m\", \"--message\", multiple=True, required=True)\n    def cli(message):\n        click.echo(\"\\n\".join(message))\n\n    result = runner.invoke(cli, [\"-m\", \"foo\", \"-mbar\"])\n    assert not result.exception\n    assert result.output == \"foo\\nbar\\n\"\n\n    result = runner.invoke(cli, [])\n    assert result.exception\n    assert \"Error: Missing option '-m' / '--message'.\" in result.output\n\n\n@pytest.mark.parametrize(\n    (\"multiple\", \"nargs\", \"default\", \"expected\"),\n    [\n        # If multiple values are allowed, defaults should be iterable.\n        (True, 1, [], ()),\n        (True, 1, (), ()),\n        (True, 1, tuple(), ()),\n        (True, 1, set(), ()),\n        (True, 1, frozenset(), ()),\n        (True, 1, {}, ()),\n        # Special values.\n        (True, 1, None, ()),\n        (True, 1, UNSET, ()),\n        # Number of values are kept as-is in the default.\n        (True, 1, [1], (1,)),\n        (True, 1, [1, 2], (1, 2)),\n        (True, 1, [1, 2, 3], (1, 2, 3)),\n        (True, 1, [1.1, 2.2], (1.1, 2.2)),\n        (True, 1, [\"1\", \"2\"], (\"1\", \"2\")),\n        (True, 1, [None, None], (None, None)),\n        # Contrary to list or tuples, native Python types not supported by Click are\n        # not recognized and are converted to the default format: tuple of strings.\n        # Refs: https://github.com/pallets/click/issues/3036\n        (True, 1, {1, 2}, (\"1\", \"2\")),\n        (True, 1, frozenset([1, 2]), (\"1\", \"2\")),\n        (True, 1, {1: \"a\", 2: \"b\"}, (\"1\", \"2\")),\n        # Multiple values with nargs > 1.\n        (True, 2, [], ()),\n        (True, 2, (), ()),\n        (True, 2, tuple(), ()),\n        (True, 2, set(), ()),\n        (True, 2, frozenset(), ()),\n        (True, 2, {}, ()),\n        (True, 2, None, ()),\n        (True, 2, UNSET, ()),\n        (True, 2, [[1, 2]], ((1, 2),)),\n        (True, 2, [[1, 2], [3, 4]], ((1, 2), (3, 4))),\n        (True, 2, [[1, 2], [3, 4], [5, 6]], ((1, 2), (3, 4), (5, 6))),\n        (True, 2, [[1.1, 2.2], [3.3, 4.4]], ((1.1, 2.2), (3.3, 4.4))),\n        (True, 2, [[\"1\", \"2\"], [\"3\", \"4\"]], ((\"1\", \"2\"), (\"3\", \"4\"))),\n        (True, 2, [[None, None], [None, None]], ((None, None), (None, None))),\n        (True, 2, [[1, 2.2], [\"3\", None]], ((1, 2.2), (3, None))),\n        (True, 2, [[1, 2.2], None], ((1, 2.2), None)),\n        # Default of the right length works for non-multiples.\n        (False, 2, [1, 2], (1, 2)),\n    ],\n)\ndef test_good_defaults_for_multiple(runner, multiple, nargs, default, expected):\n    @click.command()\n    @click.option(\"-a\", multiple=multiple, nargs=nargs, default=default)\n    def cmd(a):\n        click.echo(repr(a), nl=False)\n\n    result = runner.invoke(cmd)\n    assert result.output == repr(expected)\n\n\n@pytest.mark.parametrize(\n    (\"multiple\", \"nargs\", \"default\", \"exception\", \"message\"),\n    [\n        # Non-iterables defaults.\n        (\n            True,\n            1,\n            \"Yo\",\n            None,\n            \"Error: Invalid value for '-a': Value must be an iterable.\",\n        ),\n        (\n            True,\n            1,\n            \"\",\n            None,\n            \"Error: Invalid value for '-a': Value must be an iterable.\",\n        ),\n        (\n            True,\n            1,\n            True,\n            None,\n            \"Error: Invalid value for '-a': Value must be an iterable.\",\n        ),\n        (\n            True,\n            1,\n            False,\n            None,\n            \"Error: Invalid value for '-a': Value must be an iterable.\",\n        ),\n        (\n            True,\n            1,\n            12,\n            None,\n            \"Error: Invalid value for '-a': Value must be an iterable.\",\n        ),\n        (\n            True,\n            1,\n            7.9,\n            None,\n            \"Error: Invalid value for '-a': Value must be an iterable.\",\n        ),\n        #\n        (\n            False,\n            2,\n            42,\n            None,\n            \"Error: Invalid value for '-a': Value must be an iterable.\",\n        ),\n        (\n            True,\n            2,\n            [\"test string which is not a list in the list\"],\n            None,\n            \"Error: Invalid value for '-a': Value must be an iterable.\",\n        ),\n        # Multiple options, each with 2 args, but with wrong length.\n        (\n            True,\n            2,\n            (1,),\n            None,\n            \"Error: Invalid value for '-a': Value must be an iterable.\",\n        ),\n        (\n            True,\n            2,\n            (1, 2, 3),\n            None,\n            \"Error: Invalid value for '-a': Value must be an iterable.\",\n        ),\n        (\n            True,\n            2,\n            [tuple()],\n            ValueError,\n            r\"'nargs' must be 0 \\(or None\\) for type <click\\.types\\.Tuple object at \"\n            r\"0x[0-9A-Fa-f]+>, but it was 2\\.\",\n        ),\n        (\n            True,\n            2,\n            [(1,)],\n            ValueError,\n            r\"'nargs' must be 1 \\(or None\\) for type <click\\.types\\.Tuple object at \"\n            r\"0x[0-9A-Fa-f]+>, but it was 2\\.\",\n        ),\n        (\n            True,\n            2,\n            [(1, 2, 3)],\n            ValueError,\n            r\"'nargs' must be 3 \\(or None\\) for type <click\\.types\\.Tuple object at \"\n            r\"0x[0-9A-Fa-f]+>, but it was 2\\.\",\n        ),\n        # A mix of valid and invalid defaults.\n        (\n            True,\n            2,\n            [[1, 2.2], []],\n            None,\n            \"Error: Invalid value for '-a': 2 values are required, but 0 were given.\",\n        ),\n        # Default values that are iterable but not of the right length.\n        (\n            False,\n            2,\n            [1],\n            None,\n            \"Error: Invalid value for '-a': Takes 2 values but 1 was given.\",\n        ),\n        (\n            True,\n            2,\n            [[1]],\n            ValueError,\n            r\"'nargs' must be 1 \\(or None\\) for type <click\\.types\\.Tuple object at \"\n            r\"0x[0-9A-Fa-f]+>, but it was 2\\.\",\n        ),\n    ],\n)\ndef test_bad_defaults_for_multiple(\n    runner, multiple, nargs, default, exception, message\n):\n    if exception:\n        assert issubclass(exception, Exception)\n    else:\n        assert exception is None\n\n    with (\n        pytest.raises(exception, match=re.compile(message))\n        if exception\n        else nullcontext()\n    ):\n\n        @click.command()\n        @click.option(\"-a\", multiple=multiple, nargs=nargs, default=default)\n        def cmd(a):\n            click.echo(repr(a))\n\n        result = runner.invoke(cmd)\n        assert message in result.stderr\n\n\n@pytest.mark.parametrize(\"env_key\", [\"MYPATH\", \"AUTO_MYPATH\"])\ndef test_empty_envvar(runner, env_key):\n    @click.command()\n    @click.option(\"--mypath\", type=click.Path(exists=True), envvar=\"MYPATH\")\n    def cli(mypath):\n        click.echo(f\"mypath: {mypath}\")\n\n    result = runner.invoke(cli, env={env_key: \"\"}, auto_envvar_prefix=\"AUTO\")\n    assert result.exception is None\n    assert result.output == \"mypath: None\\n\"\n\n\ndef test_multiple_envvar(runner):\n    @click.command()\n    @click.option(\"--arg\", multiple=True)\n    def cmd(arg):\n        click.echo(\"|\".join(arg))\n\n    result = runner.invoke(\n        cmd, [], auto_envvar_prefix=\"TEST\", env={\"TEST_ARG\": \"foo bar baz\"}\n    )\n    assert not result.exception\n    assert result.output == \"foo|bar|baz\\n\"\n\n    @click.command()\n    @click.option(\"--arg\", multiple=True, envvar=\"X\")\n    def cmd(arg):\n        click.echo(\"|\".join(arg))\n\n    result = runner.invoke(cmd, [], env={\"X\": \"foo bar baz\"})\n    assert not result.exception\n    assert result.output == \"foo|bar|baz\\n\"\n\n    @click.command()\n    @click.option(\"--arg\", multiple=True, type=click.Path())\n    def cmd(arg):\n        click.echo(\"|\".join(arg))\n\n    result = runner.invoke(\n        cmd,\n        [],\n        auto_envvar_prefix=\"TEST\",\n        env={\"TEST_ARG\": f\"foo{os.path.pathsep}bar\"},\n    )\n    assert not result.exception\n    assert result.output == \"foo|bar\\n\"\n\n\n@pytest.mark.parametrize(\n    (\"envvar_name\", \"envvar_value\", \"expected\"),\n    (\n        # Lower-cased variations of value.\n        (\"SHOUT\", \"true\", True),\n        (\"SHOUT\", \"false\", False),\n        # Title-cased variations of value.\n        (\"SHOUT\", \"True\", True),\n        (\"SHOUT\", \"False\", False),\n        # Upper-cased variations of value.\n        (\"SHOUT\", \"TRUE\", True),\n        (\"SHOUT\", \"FALSE\", False),\n        # Random-cased variations of value.\n        (\"SHOUT\", \"TruE\", True),\n        (\"SHOUT\", \"truE\", True),\n        (\"SHOUT\", \"FaLsE\", False),\n        (\"SHOUT\", \"falsE\", False),\n        # Extra spaces around the value.\n        (\"SHOUT\", \"true    \", True),\n        (\"SHOUT\", \"  true  \", True),\n        (\"SHOUT\", \"    true\", True),\n        (\"SHOUT\", \"false   \", False),\n        (\"SHOUT\", \"  false \", False),\n        (\"SHOUT\", \"   false\", False),\n        # Integer variations.\n        (\"SHOUT\", \"1\", True),\n        (\"SHOUT\", \"0\", False),\n        # Alternative states.\n        (\"SHOUT\", \"t\", True),\n        (\"SHOUT\", \"T\", True),\n        (\"SHOUT\", \"  T  \", True),\n        (\"SHOUT\", \"f\", False),\n        (\"SHOUT\", \"y\", True),\n        (\"SHOUT\", \"n\", False),\n        (\"SHOUT\", \"yes\", True),\n        (\"SHOUT\", \"no\", False),\n        (\"SHOUT\", \"on\", True),\n        (\"SHOUT\", \"off\", False),\n        # Blank value variations.\n        (\"SHOUT\", None, False),\n        (\"SHOUT\", \"\", False),\n        (\"SHOUT\", \" \", False),\n        (\"SHOUT\", \"       \", False),\n        # Variable names are not stripped of spaces and so don't match the\n        # flag, which then naturraly takes its default value.\n        (\"SHOUT    \", \"True\", False),\n        (\"SHOUT    \", \"False\", False),\n        (\"  SHOUT  \", \"True\", False),\n        (\"  SHOUT  \", \"False\", False),\n        (\"    SHOUT\", \"True\", False),\n        (\"    SHOUT\", \"False\", False),\n        (\"SH    OUT\", \"True\", False),\n        (\"SH    OUT\", \"False\", False),\n        # Same for random and reverse environment variable names: they are not\n        # recognized by the option.\n        (\"RANDOM\", \"True\", False),\n        (\"NO_SHOUT\", \"True\", False),\n        (\"NO_SHOUT\", \"False\", False),\n        (\"NOSHOUT\", \"True\", False),\n        (\"NOSHOUT\", \"False\", False),\n    ),\n)\ndef test_boolean_flag_envvar(runner, envvar_name, envvar_value, expected):\n    assert isinstance(envvar_name, str)\n    assert isinstance(envvar_value, str) or envvar_value is None\n\n    @click.command()\n    @click.option(\"--shout/--no-shout\", envvar=\"SHOUT\")\n    def cli(shout):\n        click.echo(repr(shout), nl=False)\n\n    result = runner.invoke(cli, [], env={envvar_name: envvar_value})\n    assert result.exit_code == 0\n    assert result.output == repr(expected)\n\n\n@pytest.mark.parametrize(\n    \"value\",\n    (\n        # Extra spaces inside the value.\n        \"tr ue\",\n        \"fa lse\",\n        # Numbers.\n        \"10\",\n        \"01\",\n        \"00\",\n        \"11\",\n        \"0.0\",\n        \"1.1\",\n        # Random strings.\n        \"randomstring\",\n        \"None\",\n        \"'None'\",\n        \"A B\",\n        \" 1 2 \",\n        \"9.3\",\n        \"a;n\",\n        \"x:y\",\n        \"i/o\",\n    ),\n)\ndef test_boolean_envvar_bad_values(runner, value):\n    @click.command()\n    @click.option(\"--shout/--no-shout\", envvar=\"SHOUT\")\n    def cli(shout):\n        click.echo(shout)\n\n    result = runner.invoke(cli, [], env={\"SHOUT\": value})\n    assert result.exit_code == 2\n    assert (\n        f\"Invalid value for '--shout': {value!r} is not a valid boolean.\"\n        in result.output\n    )\n\n\ndef test_multiple_default_help(runner):\n    @click.command()\n    @click.option(\"--arg1\", multiple=True, default=(\"foo\", \"bar\"), show_default=True)\n    @click.option(\"--arg2\", multiple=True, default=(1, 2), type=int, show_default=True)\n    def cmd(arg, arg2):\n        pass\n\n    result = runner.invoke(cmd, [\"--help\"])\n    assert not result.exception\n    assert \"foo, bar\" in result.output\n    assert \"1, 2\" in result.output\n\n\ndef test_show_default_default_map(runner):\n    @click.command()\n    @click.option(\"--arg\", default=\"a\", show_default=True)\n    def cmd(arg):\n        click.echo(arg)\n\n    result = runner.invoke(cmd, [\"--help\"], default_map={\"arg\": \"b\"})\n\n    assert not result.exception\n    assert \"[default: b]\" in result.output\n\n\ndef test_multiple_default_type():\n    opt = click.Option([\"-a\"], multiple=True, default=(1, 2))\n    assert opt.nargs == 1\n    assert opt.multiple\n    assert opt.type is click.INT\n    ctx = click.Context(click.Command(\"test\"))\n    assert opt.get_default(ctx) == (1, 2)\n\n\ndef test_multiple_default_composite_type():\n    opt = click.Option([\"-a\"], multiple=True, default=[(1, \"a\")])\n    assert opt.nargs == 2\n    assert opt.multiple\n    assert isinstance(opt.type, click.Tuple)\n    assert opt.type.types == [click.INT, click.STRING]\n    ctx = click.Context(click.Command(\"test\"))\n    assert opt.type_cast_value(ctx, opt.get_default(ctx)) == ((1, \"a\"),)\n\n\ndef test_parse_multiple_default_composite_type(runner):\n    @click.command()\n    @click.option(\"-a\", multiple=True, default=(\"a\", \"b\"))\n    @click.option(\"-b\", multiple=True, default=[(1, \"a\")])\n    def cmd(a, b):\n        click.echo(a)\n        click.echo(b)\n\n    # result = runner.invoke(cmd, \"-a c -a 1 -a d -b 2 two -b 4 four\".split())\n    # assert result.output == \"('c', '1', 'd')\\n((2, 'two'), (4, 'four'))\\n\"\n    result = runner.invoke(cmd)\n    assert result.output == \"('a', 'b')\\n((1, 'a'),)\\n\"\n\n\ndef test_dynamic_default_help_unset(runner):\n    @click.command()\n    @click.option(\n        \"--username\",\n        prompt=True,\n        default=lambda: os.environ.get(\"USER\", \"\"),\n        show_default=True,\n    )\n    def cmd(username):\n        print(\"Hello,\", username)\n\n    result = runner.invoke(cmd, [\"--help\"])\n    assert result.exit_code == 0\n    assert \"--username\" in result.output\n    assert \"lambda\" not in result.output\n    assert \"(dynamic)\" in result.output\n\n\ndef test_dynamic_default_help_text(runner):\n    @click.command()\n    @click.option(\n        \"--username\",\n        prompt=True,\n        default=lambda: os.environ.get(\"USER\", \"\"),\n        show_default=\"current user\",\n    )\n    def cmd(username):\n        print(\"Hello,\", username)\n\n    result = runner.invoke(cmd, [\"--help\"])\n    assert result.exit_code == 0\n    assert \"--username\" in result.output\n    assert \"lambda\" not in result.output\n    assert \"(current user)\" in result.output\n\n\ndef test_dynamic_default_help_special_method(runner):\n    class Value:\n        def __call__(self):\n            return 42\n\n        def __str__(self):\n            return \"special value\"\n\n    opt = click.Option([\"-a\"], default=Value(), show_default=True)\n    ctx = click.Context(click.Command(\"cli\"))\n    assert opt.get_help_extra(ctx) == {\"default\": \"special value\"}\n    assert \"special value\" in opt.get_help_record(ctx)[1]\n\n\n@pytest.mark.parametrize(\n    (\"type\", \"expect\"),\n    [\n        (click.IntRange(1, 32), \"1<=x<=32\"),\n        (click.IntRange(1, 32, min_open=True, max_open=True), \"1<x<32\"),\n        (click.IntRange(1), \"x>=1\"),\n        (click.IntRange(max=32), \"x<=32\"),\n    ],\n)\ndef test_intrange_default_help_text(type, expect):\n    option = click.Option([\"--num\"], type=type, show_default=True, default=2)\n    context = click.Context(click.Command(\"test\"))\n    assert option.get_help_extra(context) == {\"default\": \"2\", \"range\": expect}\n    result = option.get_help_record(context)[1]\n    assert expect in result\n\n\ndef test_count_default_type_help():\n    \"\"\"A count option with the default type should not show >=0 in help.\"\"\"\n    option = click.Option([\"--count\"], count=True, help=\"some words\")\n    context = click.Context(click.Command(\"test\"))\n    assert option.get_help_extra(context) == {}\n    result = option.get_help_record(context)[1]\n    assert result == \"some words\"\n\n\ndef test_file_type_help_default():\n    \"\"\"The default for a File type is a filename string. The string\n    should be displayed in help, not an open file object.\n\n    Type casting is only applied to defaults in processing, not when\n    getting the default value.\n    \"\"\"\n    option = click.Option(\n        [\"--in\"], type=click.File(), default=__file__, show_default=True\n    )\n    context = click.Context(click.Command(\"test\"))\n    assert option.get_help_extra(context) == {\"default\": __file__}\n    result = option.get_help_record(context)[1]\n    assert __file__ in result\n\n\ndef test_toupper_envvar_prefix(runner):\n    @click.command()\n    @click.option(\"--arg\")\n    def cmd(arg):\n        click.echo(arg)\n\n    result = runner.invoke(cmd, [], auto_envvar_prefix=\"test\", env={\"TEST_ARG\": \"foo\"})\n    assert not result.exception\n    assert result.output == \"foo\\n\"\n\n\ndef test_nargs_envvar(runner):\n    @click.command()\n    @click.option(\"--arg\", nargs=2)\n    def cmd(arg):\n        click.echo(\"|\".join(arg))\n\n    result = runner.invoke(\n        cmd, [], auto_envvar_prefix=\"TEST\", env={\"TEST_ARG\": \"foo bar\"}\n    )\n    assert not result.exception\n    assert result.output == \"foo|bar\\n\"\n\n    @click.command()\n    @click.option(\"--arg\", nargs=2, multiple=True)\n    def cmd(arg):\n        for item in arg:\n            click.echo(\"|\".join(item))\n\n    result = runner.invoke(\n        cmd, [], auto_envvar_prefix=\"TEST\", env={\"TEST_ARG\": \"x 1 y 2\"}\n    )\n    assert not result.exception\n    assert result.output == \"x|1\\ny|2\\n\"\n\n\ndef test_show_envvar(runner):\n    @click.command()\n    @click.option(\"--arg1\", envvar=\"ARG1\", show_envvar=True)\n    def cmd(arg):\n        pass\n\n    result = runner.invoke(cmd, [\"--help\"])\n    assert not result.exception\n    assert \"ARG1\" in result.output\n\n\ndef test_show_envvar_auto_prefix(runner):\n    @click.command()\n    @click.option(\"--arg1\", show_envvar=True)\n    def cmd(arg):\n        pass\n\n    result = runner.invoke(cmd, [\"--help\"], auto_envvar_prefix=\"TEST\")\n    assert not result.exception\n    assert \"TEST_ARG1\" in result.output\n\n\ndef test_show_envvar_auto_prefix_dash_in_command(runner):\n    @click.group()\n    def cli():\n        pass\n\n    @cli.command()\n    @click.option(\"--baz\", show_envvar=True)\n    def foo_bar(baz):\n        pass\n\n    result = runner.invoke(cli, [\"foo-bar\", \"--help\"], auto_envvar_prefix=\"TEST\")\n    assert not result.exception\n    assert \"TEST_FOO_BAR_BAZ\" in result.output\n\n\ndef test_custom_validation(runner):\n    def validate_pos_int(ctx, param, value):\n        if value < 0:\n            raise click.BadParameter(\"Value needs to be positive\")\n        return value\n\n    @click.command()\n    @click.option(\"--foo\", callback=validate_pos_int, default=1)\n    def cmd(foo):\n        click.echo(foo)\n\n    result = runner.invoke(cmd, [\"--foo\", \"-1\"])\n    assert \"Invalid value for '--foo': Value needs to be positive\" in result.output\n\n    result = runner.invoke(cmd, [\"--foo\", \"42\"])\n    assert result.output == \"42\\n\"\n\n\ndef test_callback_validates_prompt(runner, monkeypatch):\n    def validate(ctx, param, value):\n        if value < 0:\n            raise click.BadParameter(\"should be positive\")\n\n        return value\n\n    @click.command()\n    @click.option(\"-a\", type=int, callback=validate, prompt=True)\n    def cli(a):\n        click.echo(a)\n\n    result = runner.invoke(cli, input=\"-12\\n60\\n\")\n    assert result.output == \"A: -12\\nError: should be positive\\nA: 60\\n60\\n\"\n\n\ndef test_winstyle_options(runner):\n    @click.command()\n    @click.option(\"/debug;/no-debug\", help=\"Enables or disables debug mode.\")\n    def cmd(debug):\n        click.echo(debug)\n\n    result = runner.invoke(cmd, [\"/debug\"], help_option_names=[\"/?\"])\n    assert result.output == \"True\\n\"\n    result = runner.invoke(cmd, [\"/no-debug\"], help_option_names=[\"/?\"])\n    assert result.output == \"False\\n\"\n    result = runner.invoke(cmd, [], help_option_names=[\"/?\"])\n    assert result.output == \"False\\n\"\n    result = runner.invoke(cmd, [\"/?\"], help_option_names=[\"/?\"])\n    assert \"/debug; /no-debug  Enables or disables debug mode.\" in result.output\n    assert \"/?                 Show this message and exit.\" in result.output\n\n\ndef test_legacy_options(runner):\n    @click.command()\n    @click.option(\"-whatever\")\n    def cmd(whatever):\n        click.echo(whatever)\n\n    result = runner.invoke(cmd, [\"-whatever\", \"42\"])\n    assert result.output == \"42\\n\"\n    result = runner.invoke(cmd, [\"-whatever=23\"])\n    assert result.output == \"23\\n\"\n\n\n@pytest.mark.parametrize(\n    (\"value\", \"expect_missing\", \"processed_value\"),\n    [\n        (UNSET, True, None),\n        (None, False, None),\n        # Default type of the argument is str, so everything is processed as strings.\n        (\"\", False, \"\"),\n        (\"  \", False, \"  \"),\n        (\"foo\", False, \"foo\"),\n        (\"12\", False, \"12\"),\n        (12, False, \"12\"),\n        (12.1, False, \"12.1\"),\n        (list(), False, \"[]\"),\n        (tuple(), False, \"()\"),\n        (set(), False, \"set()\"),\n        (frozenset(), False, \"frozenset()\"),\n        (dict(), False, \"{}\"),\n    ],\n)\ndef test_required_option(value, expect_missing, processed_value):\n    ctx = click.Context(click.Command(\"\"))\n    argument = click.Option([\"-a\"], required=True)\n\n    if expect_missing:\n        with pytest.raises(click.MissingParameter) as excinfo:\n            argument.process_value(ctx, value)\n        assert str(excinfo.value) == \"Missing parameter: a\"\n\n    else:\n        value = argument.process_value(ctx, value)\n        assert value == processed_value\n\n\ndef test_missing_required_flag(runner):\n    cli = click.Command(\n        \"cli\", params=[click.Option([\"--on/--off\"], is_flag=True, required=True)]\n    )\n    result = runner.invoke(cli)\n    assert result.exit_code == 2\n    assert \"Error: Missing option '--on'.\" in result.output\n\n\ndef test_missing_choice(runner):\n    @click.command()\n    @click.option(\"--foo\", type=click.Choice([\"foo\", \"bar\"]), required=True)\n    def cmd(foo):\n        click.echo(foo)\n\n    result = runner.invoke(cmd)\n    assert result.exit_code == 2\n    error, separator, choices = result.output.partition(\"Choose from\")\n    assert \"Error: Missing option '--foo'. \" in error\n    assert \"Choose from\" in separator\n    assert \"foo\" in choices\n    assert \"bar\" in choices\n\n\ndef test_missing_envvar(runner):\n    cli = click.Command(\n        \"cli\", params=[click.Option([\"--foo\"], envvar=\"bar\", required=True)]\n    )\n    result = runner.invoke(cli)\n    assert result.exit_code == 2\n    assert \"Error: Missing option '--foo'.\" in result.output\n    cli = click.Command(\n        \"cli\",\n        params=[click.Option([\"--foo\"], envvar=\"bar\", show_envvar=True, required=True)],\n    )\n    result = runner.invoke(cli)\n    assert result.exit_code == 2\n    assert \"Error: Missing option '--foo' (env var: 'bar').\" in result.output\n\n    cli = click.Command(\n        \"cli\", params=[click.Option([\"--foo\"], show_envvar=True, required=True)]\n    )\n    result = runner.invoke(cli)\n    assert result.exit_code == 2\n    assert \"Error: Missing option '--foo'.\" in result.output\n\n\ndef test_case_insensitive_choice(runner):\n    @click.command()\n    @click.option(\"--foo\", type=click.Choice([\"Orange\", \"Apple\"], case_sensitive=False))\n    def cmd(foo):\n        click.echo(foo)\n\n    result = runner.invoke(cmd, [\"--foo\", \"apple\"])\n    assert result.exit_code == 0\n    assert result.output == \"Apple\\n\"\n\n    result = runner.invoke(cmd, [\"--foo\", \"oRANGe\"])\n    assert result.exit_code == 0\n    assert result.output == \"Orange\\n\"\n\n    result = runner.invoke(cmd, [\"--foo\", \"Apple\"])\n    assert result.exit_code == 0\n    assert result.output == \"Apple\\n\"\n\n    @click.command()\n    @click.option(\"--foo\", type=click.Choice([\"Orange\", \"Apple\"]))\n    def cmd2(foo):\n        click.echo(foo)\n\n    result = runner.invoke(cmd2, [\"--foo\", \"apple\"])\n    assert result.exit_code == 2\n\n    result = runner.invoke(cmd2, [\"--foo\", \"oRANGe\"])\n    assert result.exit_code == 2\n\n    result = runner.invoke(cmd2, [\"--foo\", \"Apple\"])\n    assert result.exit_code == 0\n\n\ndef test_case_insensitive_choice_returned_exactly(runner):\n    @click.command()\n    @click.option(\"--foo\", type=click.Choice([\"Orange\", \"Apple\"], case_sensitive=False))\n    def cmd(foo):\n        click.echo(foo)\n\n    result = runner.invoke(cmd, [\"--foo\", \"apple\"])\n    assert result.exit_code == 0\n    assert result.output == \"Apple\\n\"\n\n\ndef test_option_help_preserve_paragraphs(runner):\n    @click.command()\n    @click.option(\n        \"-C\",\n        \"--config\",\n        type=click.Path(),\n        help=\"\"\"Configuration file to use.\n\n        If not given, the environment variable CONFIG_FILE is consulted\n        and used if set. If neither are given, a default configuration\n        file is loaded.\"\"\",\n    )\n    def cmd(config):\n        pass\n\n    result = runner.invoke(cmd, [\"--help\"])\n    assert result.exit_code == 0\n    i = \" \" * 21\n    assert (\n        \"  -C, --config PATH  Configuration file to use.\\n\"\n        f\"{i}\\n\"\n        f\"{i}If not given, the environment variable CONFIG_FILE is\\n\"\n        f\"{i}consulted and used if set. If neither are given, a default\\n\"\n        f\"{i}configuration file is loaded.\"\n    ) in result.output\n\n\ndef test_argument_custom_class(runner):\n    class CustomArgument(click.Argument):\n        def get_default(self, ctx, call=True):\n            \"\"\"a dumb override of a default value for testing\"\"\"\n            return \"I am a default\"\n\n    @click.command()\n    @click.argument(\"testarg\", cls=CustomArgument, default=\"you wont see me\")\n    def cmd(testarg):\n        click.echo(testarg)\n\n    result = runner.invoke(cmd)\n    assert \"I am a default\" in result.output\n    assert \"you wont see me\" not in result.output\n\n\ndef test_option_custom_class(runner):\n    class CustomOption(click.Option):\n        def get_help_record(self, ctx):\n            \"\"\"a dumb override of a help text for testing\"\"\"\n            return (\"--help\", \"I am a help text\")\n\n    @click.command()\n    @click.option(\"--testoption\", cls=CustomOption, help=\"you wont see me\")\n    def cmd(testoption):\n        click.echo(testoption)\n\n    result = runner.invoke(cmd, [\"--help\"])\n    assert \"I am a help text\" in result.output\n    assert \"you wont see me\" not in result.output\n\n\n@pytest.mark.parametrize(\n    (\"param_decl\", \"option_kwargs\", \"pass_argv\"),\n    (\n        # there is a large potential parameter space to explore here\n        # this is just a very small sample of it\n        (\"--opt\", {}, []),\n        (\"--opt\", {\"multiple\": True}, []),\n        (\"--opt\", {\"is_flag\": True}, []),\n        (\"--opt/--no-opt\", {\"is_flag\": True, \"default\": None}, []),\n        (\"--req\", {\"is_flag\": True, \"required\": True}, [\"--req\"]),\n    ),\n)\ndef test_option_custom_class_can_override_type_cast_value_and_never_sees_unset(\n    runner, param_decl, option_kwargs, pass_argv\n):\n    \"\"\"\n    Test that overriding type_cast_value is supported\n\n    In particular, the option is never passed an UNSET sentinel value.\n    \"\"\"\n\n    class CustomOption(click.Option):\n        def type_cast_value(self, ctx, value):\n            assert value is not UNSET\n            return value\n\n    @click.command()\n    @click.option(\"myparam\", param_decl, **option_kwargs, cls=CustomOption)\n    def cmd(myparam):\n        click.echo(\"ok\")\n\n    result = runner.invoke(cmd, pass_argv)\n    assert not result.exception\n    assert result.exit_code == 0\n\n\ndef test_option_custom_class_reusable(runner):\n    \"\"\"Ensure we can reuse a custom class option. See Issue #926\"\"\"\n\n    class CustomOption(click.Option):\n        def get_help_record(self, ctx):\n            \"\"\"a dumb override of a help text for testing\"\"\"\n            return (\"--help\", \"I am a help text\")\n\n    # Assign to a variable to re-use the decorator.\n    testoption = click.option(\"--testoption\", cls=CustomOption, help=\"you wont see me\")\n\n    @click.command()\n    @testoption\n    def cmd1(testoption):\n        click.echo(testoption)\n\n    @click.command()\n    @testoption\n    def cmd2(testoption):\n        click.echo(testoption)\n\n    # Both of the commands should have the --help option now.\n    for cmd in (cmd1, cmd2):\n        result = runner.invoke(cmd, [\"--help\"])\n        assert \"I am a help text\" in result.output\n        assert \"you wont see me\" not in result.output\n\n\n@pytest.mark.parametrize(\"custom_class\", (True, False))\n@pytest.mark.parametrize(\n    (\"name_specs\", \"expected\"),\n    (\n        (\n            (\"-h\", \"--help\"),\n            \"  -h, --help  Show this message and exit.\\n\",\n        ),\n        (\n            (\"-h\",),\n            \"  -h      Show this message and exit.\\n\"\n            \"  --help  Show this message and exit.\\n\",\n        ),\n        (\n            (\"--help\",),\n            \"  --help  Show this message and exit.\\n\",\n        ),\n    ),\n)\ndef test_help_option_custom_names_and_class(runner, custom_class, name_specs, expected):\n    class CustomHelpOption(click.Option):\n        pass\n\n    option_attrs = {}\n    if custom_class:\n        option_attrs[\"cls\"] = CustomHelpOption\n\n    @click.command()\n    @click.help_option(*name_specs, **option_attrs)\n    def cmd():\n        pass\n\n    for arg in name_specs:\n        result = runner.invoke(cmd, [arg])\n        assert not result.exception\n        assert result.exit_code == 0\n        assert expected in result.output\n\n\ndef test_bool_flag_with_type(runner):\n    @click.command()\n    @click.option(\"--shout/--no-shout\", default=False, type=bool)\n    def cmd(shout):\n        pass\n\n    result = runner.invoke(cmd)\n    assert not result.exception\n\n\ndef test_aliases_for_flags(runner):\n    @click.command()\n    @click.option(\"--warnings/--no-warnings\", \" /-W\", default=True)\n    def cli(warnings):\n        click.echo(warnings)\n\n    result = runner.invoke(cli, [\"--warnings\"])\n    assert result.output == \"True\\n\"\n    result = runner.invoke(cli, [\"--no-warnings\"])\n    assert result.output == \"False\\n\"\n    result = runner.invoke(cli, [\"-W\"])\n    assert result.output == \"False\\n\"\n\n    @click.command()\n    @click.option(\"--warnings/--no-warnings\", \"-w\", default=True)\n    def cli_alt(warnings):\n        click.echo(warnings)\n\n    result = runner.invoke(cli_alt, [\"--warnings\"])\n    assert result.output == \"True\\n\"\n    result = runner.invoke(cli_alt, [\"--no-warnings\"])\n    assert result.output == \"False\\n\"\n    result = runner.invoke(cli_alt, [\"-w\"])\n    assert result.output == \"True\\n\"\n\n\n@pytest.mark.parametrize(\n    \"option_args,expected\",\n    [\n        ([\"--aggressive\", \"--all\", \"-a\"], \"aggressive\"),\n        ([\"--first\", \"--second\", \"--third\", \"-a\", \"-b\", \"-c\"], \"first\"),\n        ([\"--apple\", \"--banana\", \"--cantaloupe\", \"-a\", \"-b\", \"-c\"], \"apple\"),\n        ([\"--cantaloupe\", \"--banana\", \"--apple\", \"-c\", \"-b\", \"-a\"], \"cantaloupe\"),\n        ([\"-a\", \"-b\", \"-c\"], \"a\"),\n        ([\"-c\", \"-b\", \"-a\"], \"c\"),\n        ([\"-a\", \"--apple\", \"-b\", \"--banana\", \"-c\", \"--cantaloupe\"], \"apple\"),\n        ([\"-c\", \"-a\", \"--cantaloupe\", \"-b\", \"--banana\", \"--apple\"], \"cantaloupe\"),\n        ([\"--from\", \"-f\", \"_from\"], \"_from\"),\n        ([\"--return\", \"-r\", \"_ret\"], \"_ret\"),\n    ],\n)\ndef test_option_names(runner, option_args, expected):\n    @click.command()\n    @click.option(*option_args, is_flag=True)\n    def cmd(**kwargs):\n        click.echo(str(kwargs[expected]))\n\n    assert cmd.params[0].name == expected\n\n    for form in option_args:\n        if form.startswith(\"-\"):\n            result = runner.invoke(cmd, [form])\n            assert result.output == \"True\\n\"\n\n\ndef test_flag_duplicate_names(runner):\n    with pytest.raises(ValueError, match=\"cannot use the same flag for true/false\"):\n        click.Option([\"--foo/--foo\"], default=False)\n\n\n@pytest.mark.parametrize((\"default\", \"expect\"), [(False, \"no-cache\"), (True, \"cache\")])\ndef test_show_default_boolean_flag_name(runner, default, expect):\n    \"\"\"When a boolean flag has distinct True/False opts, it should show\n    the default opt name instead of the default value. It should only\n    show one name even if multiple are declared.\n    \"\"\"\n    opt = click.Option(\n        (\"--cache/--no-cache\", \"--c/--nc\"),\n        default=default,\n        show_default=True,\n        help=\"Enable/Disable the cache.\",\n    )\n    ctx = click.Context(click.Command(\"test\"))\n    assert opt.get_help_extra(ctx) == {\"default\": expect}\n    message = opt.get_help_record(ctx)[1]\n    assert f\"[default: {expect}]\" in message\n\n\ndef test_show_true_default_boolean_flag_value(runner):\n    \"\"\"When a boolean flag only has one opt and its default is True,\n    it will show the default value, not the opt name.\n    \"\"\"\n    opt = click.Option(\n        (\"--cache\",),\n        is_flag=True,\n        show_default=True,\n        default=True,\n        help=\"Enable the cache.\",\n    )\n    ctx = click.Context(click.Command(\"test\"))\n    assert opt.get_help_extra(ctx) == {\"default\": \"True\"}\n    message = opt.get_help_record(ctx)[1]\n    assert \"[default: True]\" in message\n\n\n@pytest.mark.parametrize(\"default\", [False, None])\ndef test_hide_false_default_boolean_flag_value(runner, default):\n    \"\"\"When a boolean flag only has one opt and its default is False or\n    None, it will not show the default\n    \"\"\"\n    opt = click.Option(\n        (\"--cache\",),\n        is_flag=True,\n        show_default=True,\n        default=default,\n        help=\"Enable the cache.\",\n    )\n    ctx = click.Context(click.Command(\"test\"))\n    assert opt.get_help_extra(ctx) == {}\n    message = opt.get_help_record(ctx)[1]\n    assert \"[default: \" not in message\n\n\ndef test_show_default_string(runner):\n    \"\"\"When show_default is a string show that value as default.\"\"\"\n    opt = click.Option([\"--limit\"], show_default=\"unlimited\")\n    ctx = click.Context(click.Command(\"cli\"))\n    assert opt.get_help_extra(ctx) == {\"default\": \"(unlimited)\"}\n    message = opt.get_help_record(ctx)[1]\n    assert \"[default: (unlimited)]\" in message\n\n\ndef test_show_default_with_empty_string(runner):\n    \"\"\"When show_default is True and default is set to an empty string.\"\"\"\n    opt = click.Option([\"--limit\"], default=\"\", show_default=True)\n    ctx = click.Context(click.Command(\"cli\"))\n    message = opt.get_help_record(ctx)[1]\n    assert '[default: \"\"]' in message\n\n\ndef test_do_not_show_no_default(runner):\n    \"\"\"When show_default is True and no default is set do not show None.\"\"\"\n    opt = click.Option([\"--limit\"], show_default=True)\n    ctx = click.Context(click.Command(\"cli\"))\n    assert opt.get_help_extra(ctx) == {}\n    message = opt.get_help_record(ctx)[1]\n    assert \"[default: None]\" not in message\n\n\ndef test_do_not_show_default_empty_multiple():\n    \"\"\"When show_default is True and multiple=True is set, it should not\n    print empty default value in --help output.\n    \"\"\"\n    opt = click.Option([\"-a\"], multiple=True, help=\"values\", show_default=True)\n    ctx = click.Context(click.Command(\"cli\"))\n    assert opt.get_help_extra(ctx) == {}\n    message = opt.get_help_record(ctx)[1]\n    assert message == \"values\"\n\n\n@pytest.mark.parametrize(\n    (\"ctx_value\", \"opt_value\", \"extra_value\", \"expect\"),\n    [\n        (None, None, {}, False),\n        (None, False, {}, False),\n        (None, True, {\"default\": \"1\"}, True),\n        (False, None, {}, False),\n        (False, False, {}, False),\n        (False, True, {\"default\": \"1\"}, True),\n        (True, None, {\"default\": \"1\"}, True),\n        (True, False, {}, False),\n        (True, True, {\"default\": \"1\"}, True),\n        (False, \"one\", {\"default\": \"(one)\"}, True),\n    ],\n)\ndef test_show_default_precedence(ctx_value, opt_value, extra_value, expect):\n    ctx = click.Context(click.Command(\"test\"), show_default=ctx_value)\n    opt = click.Option(\"-a\", default=1, help=\"value\", show_default=opt_value)\n    assert opt.get_help_extra(ctx) == extra_value\n    help = opt.get_help_record(ctx)[1]\n    assert (\"default:\" in help) is expect\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"expect\"),\n    [\n        (None, (None, None, ())),\n        ([\"--opt\"], (\"flag\", None, ())),\n        ([\"--opt\", \"-a\", 42], (\"flag\", \"42\", ())),\n        ([\"--opt\", \"test\", \"-a\", 42], (\"test\", \"42\", ())),\n        ([\"--opt=test\", \"-a\", 42], (\"test\", \"42\", ())),\n        ([\"-o\"], (\"flag\", None, ())),\n        ([\"-o\", \"-a\", 42], (\"flag\", \"42\", ())),\n        ([\"-o\", \"test\", \"-a\", 42], (\"test\", \"42\", ())),\n        ([\"-otest\", \"-a\", 42], (\"test\", \"42\", ())),\n        ([\"a\", \"b\", \"c\"], (None, None, (\"a\", \"b\", \"c\"))),\n        ([\"--opt\", \"a\", \"b\", \"c\"], (\"a\", None, (\"b\", \"c\"))),\n        ([\"--opt\", \"test\"], (\"test\", None, ())),\n        ([\"-otest\", \"a\", \"b\", \"c\"], (\"test\", None, (\"a\", \"b\", \"c\"))),\n        ([\"--opt=test\", \"a\", \"b\", \"c\"], (\"test\", None, (\"a\", \"b\", \"c\"))),\n    ],\n)\ndef test_option_with_optional_value(runner, args, expect):\n    @click.command()\n    @click.option(\"-o\", \"--opt\", is_flag=False, flag_value=\"flag\")\n    @click.option(\"-a\")\n    @click.argument(\"b\", nargs=-1)\n    def cli(opt, a, b):\n        return opt, a, b\n\n    result = runner.invoke(cli, args, standalone_mode=False, catch_exceptions=False)\n    assert result.return_value == expect\n\n\ndef test_multiple_option_with_optional_value(runner):\n    cli = click.Command(\n        \"cli\",\n        params=[\n            click.Option([\"-f\"], is_flag=False, flag_value=\"flag\", multiple=True),\n            click.Option([\"-a\"]),\n            click.Argument([\"b\"], nargs=-1),\n        ],\n        callback=lambda **kwargs: kwargs,\n    )\n    result = runner.invoke(\n        cli,\n        [\"-f\", \"-f\", \"other\", \"-f\", \"-a\", \"1\", \"a\", \"b\"],\n        standalone_mode=False,\n        catch_exceptions=False,\n    )\n    assert result.return_value == {\n        \"f\": (\"flag\", \"other\", \"flag\"),\n        \"a\": \"1\",\n        \"b\": (\"a\", \"b\"),\n    }\n\n\ndef test_type_from_flag_value():\n    param = click.Option([\"-a\", \"x\"], default=True, flag_value=4)\n    assert param.type is click.INT\n    param = click.Option([\"-b\", \"x\"], flag_value=8)\n    assert param.type is click.INT\n\n\n@pytest.mark.parametrize(\n    (\"opt_params\", \"args\", \"expected\"),\n    [\n        # The type passed to the option is responsible to converting the value, whether\n        # we pass the option flag or not.\n        ({\"type\": bool}, [], False),\n        ({\"type\": bool}, [\"--foo\"], True),\n        ({\"type\": click.BOOL}, [], False),\n        ({\"type\": click.BOOL}, [\"--foo\"], True),\n        ({\"type\": str}, [], None),\n        ({\"type\": str}, [\"--foo\"], \"True\"),\n        # Default value is given as-is to the --foo option when it is not passed,\n        # whatever the type. Now if --foo is passed, the value is always True, whatever\n        # the type. In both case the type of the option is responsible for the\n        # conversion of the value.\n        ({\"type\": bool, \"default\": True}, [], True),\n        ({\"type\": bool, \"default\": True}, [\"--foo\"], True),\n        ({\"type\": bool, \"default\": False}, [], False),\n        ({\"type\": bool, \"default\": False}, [\"--foo\"], True),\n        # ({\"type\": bool, \"default\": \"foo\"}, [], \"foo\"),\n        ({\"type\": bool, \"default\": \"foo\"}, [\"--foo\"], True),\n        ({\"type\": bool, \"default\": None}, [], None),\n        ({\"type\": bool, \"default\": None}, [\"--foo\"], True),\n        ({\"type\": click.BOOL, \"default\": True}, [], True),\n        ({\"type\": click.BOOL, \"default\": True}, [\"--foo\"], True),\n        ({\"type\": click.BOOL, \"default\": False}, [], False),\n        ({\"type\": click.BOOL, \"default\": False}, [\"--foo\"], True),\n        # ({\"type\": click.BOOL, \"default\": \"foo\"}, [], \"foo\"),\n        ({\"type\": click.BOOL, \"default\": \"foo\"}, [\"--foo\"], True),\n        ({\"type\": click.BOOL, \"default\": None}, [], None),\n        ({\"type\": click.BOOL, \"default\": None}, [\"--foo\"], True),\n        ({\"type\": str, \"default\": True}, [], \"True\"),\n        ({\"type\": str, \"default\": True}, [\"--foo\"], \"True\"),\n        ({\"type\": str, \"default\": False}, [], \"False\"),\n        ({\"type\": str, \"default\": False}, [\"--foo\"], \"True\"),\n        ({\"type\": str, \"default\": \"foo\"}, [], \"foo\"),\n        ({\"type\": str, \"default\": \"foo\"}, [\"--foo\"], \"True\"),\n        ({\"type\": str, \"default\": None}, [], None),\n        ({\"type\": str, \"default\": None}, [\"--foo\"], \"True\"),\n        # Flag value is given as-is to the --foo option when it is passed, then\n        # converted by the option type.\n        ({\"type\": bool, \"flag_value\": True}, [], False),\n        ({\"type\": bool, \"flag_value\": True}, [\"--foo\"], True),\n        ({\"type\": bool, \"flag_value\": False}, [], False),\n        ({\"type\": bool, \"flag_value\": False}, [\"--foo\"], False),\n        ({\"type\": bool, \"flag_value\": None}, [], False),\n        ({\"type\": bool, \"flag_value\": None}, [\"--foo\"], None),\n        ({\"type\": click.BOOL, \"flag_value\": True}, [], False),\n        ({\"type\": click.BOOL, \"flag_value\": True}, [\"--foo\"], True),\n        ({\"type\": click.BOOL, \"flag_value\": False}, [], False),\n        ({\"type\": click.BOOL, \"flag_value\": False}, [\"--foo\"], False),\n        ({\"type\": click.BOOL, \"flag_value\": None}, [], False),\n        ({\"type\": click.BOOL, \"flag_value\": None}, [\"--foo\"], None),\n        ({\"type\": str, \"flag_value\": True}, [], None),\n        ({\"type\": str, \"flag_value\": True}, [\"--foo\"], \"True\"),\n        ({\"type\": str, \"flag_value\": False}, [], None),\n        ({\"type\": str, \"flag_value\": False}, [\"--foo\"], \"False\"),\n        ({\"type\": str, \"flag_value\": \"foo\"}, [], None),\n        ({\"type\": str, \"flag_value\": \"foo\"}, [\"--foo\"], \"foo\"),\n        ({\"type\": str, \"flag_value\": None}, [], None),\n        ({\"type\": str, \"flag_value\": None}, [\"--foo\"], None),\n        # Not passing --foo returns the default value as-is, in its Python type, then\n        # converted by the option type.\n        ({\"type\": bool, \"default\": True, \"flag_value\": True}, [], True),\n        ({\"type\": bool, \"default\": True, \"flag_value\": False}, [], False),\n        ({\"type\": bool, \"default\": False, \"flag_value\": True}, [], False),\n        ({\"type\": bool, \"default\": False, \"flag_value\": False}, [], False),\n        ({\"type\": bool, \"default\": None, \"flag_value\": True}, [], None),\n        ({\"type\": bool, \"default\": None, \"flag_value\": False}, [], None),\n        ({\"type\": str, \"default\": True, \"flag_value\": True}, [], \"True\"),\n        ({\"type\": str, \"default\": True, \"flag_value\": False}, [], \"False\"),\n        ({\"type\": str, \"default\": False, \"flag_value\": True}, [], \"False\"),\n        ({\"type\": str, \"default\": False, \"flag_value\": False}, [], \"False\"),\n        ({\"type\": str, \"default\": \"foo\", \"flag_value\": True}, [], \"foo\"),\n        ({\"type\": str, \"default\": \"foo\", \"flag_value\": False}, [], \"foo\"),\n        ({\"type\": str, \"default\": \"foo\", \"flag_value\": \"bar\"}, [], \"foo\"),\n        ({\"type\": str, \"default\": \"foo\", \"flag_value\": None}, [], \"foo\"),\n        ({\"type\": str, \"default\": None, \"flag_value\": True}, [], None),\n        ({\"type\": str, \"default\": None, \"flag_value\": False}, [], None),\n        # Passing --foo returns the flag_value that was explicitly set by the user,\n        # with its Python type, but still converted by the option type.\n        ({\"type\": bool, \"default\": True, \"flag_value\": True}, [\"--foo\"], True),\n        ({\"type\": bool, \"default\": True, \"flag_value\": False}, [\"--foo\"], False),\n        ({\"type\": bool, \"default\": False, \"flag_value\": True}, [\"--foo\"], True),\n        ({\"type\": bool, \"default\": False, \"flag_value\": False}, [\"--foo\"], False),\n        ({\"type\": bool, \"default\": None, \"flag_value\": True}, [\"--foo\"], True),\n        ({\"type\": bool, \"default\": None, \"flag_value\": False}, [\"--foo\"], False),\n        ({\"type\": str, \"default\": True, \"flag_value\": True}, [\"--foo\"], \"True\"),\n        ({\"type\": str, \"default\": True, \"flag_value\": False}, [\"--foo\"], \"False\"),\n        ({\"type\": str, \"default\": False, \"flag_value\": True}, [\"--foo\"], \"True\"),\n        ({\"type\": str, \"default\": False, \"flag_value\": False}, [\"--foo\"], \"False\"),\n        ({\"type\": str, \"default\": \"foo\", \"flag_value\": True}, [\"--foo\"], \"True\"),\n        ({\"type\": str, \"default\": \"foo\", \"flag_value\": False}, [\"--foo\"], \"False\"),\n        ({\"type\": str, \"default\": \"foo\", \"flag_value\": \"bar\"}, [\"--foo\"], \"bar\"),\n        ({\"type\": str, \"default\": \"foo\", \"flag_value\": None}, [\"--foo\"], None),\n        ({\"type\": str, \"default\": None, \"flag_value\": True}, [\"--foo\"], \"True\"),\n        ({\"type\": str, \"default\": None, \"flag_value\": False}, [\"--foo\"], \"False\"),\n    ],\n)\ndef test_flag_value_and_default(runner, opt_params, args, expected):\n    @click.command()\n    @click.option(\"--foo\", is_flag=True, **opt_params)\n    def cmd(foo):\n        click.echo(repr(foo), nl=False)\n\n    result = runner.invoke(cmd, args)\n    assert result.output == repr(expected)\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"opts\"),\n    [\n        ([], {\"type\": bool, \"default\": \"foo\"}),\n        ([], {\"type\": click.BOOL, \"default\": \"foo\"}),\n        ([\"--foo\"], {\"type\": bool, \"flag_value\": \"foo\"}),\n        ([\"--foo\"], {\"type\": click.BOOL, \"flag_value\": \"foo\"}),\n    ],\n)\ndef test_invalid_flag_definition(runner, args, opts):\n    @click.command()\n    @click.option(\"--foo\", is_flag=True, **opts)\n    def cmd(foo):\n        click.echo(foo)\n\n    result = runner.invoke(cmd, args)\n    assert (\n        \"Error: Invalid value for '--foo': 'foo' is not a valid boolean\"\n        in result.output\n    )\n\n\n@pytest.mark.parametrize(\n    (\"default\", \"args\", \"expected\"),\n    # These test cases are similar to the ones in\n    # tests/test_basic.py::test_flag_value_dual_options, so keep them in sync.\n    (\n        # Each option is returning its own flag_value, whatever the default is.\n        (True, [\"--js\"], \"js\"),\n        (True, [\"--xml\"], \"xml\"),\n        (False, [\"--js\"], \"js\"),\n        (False, [\"--xml\"], \"xml\"),\n        (None, [\"--js\"], \"js\"),\n        (None, [\"--xml\"], \"xml\"),\n        (UNSET, [\"--js\"], \"js\"),\n        (UNSET, [\"--xml\"], \"xml\"),\n        # Check that the last option wins when both are specified.\n        (True, [\"--js\", \"--xml\"], \"xml\"),\n        (True, [\"--xml\", \"--js\"], \"js\"),\n        # Check that the default is returned as-is when no option is specified.\n        (\"js\", [], \"js\"),\n        (\"xml\", [], \"xml\"),\n        (\"jS\", [], \"jS\"),\n        (\"xMl\", [], \"xMl\"),\n        (\" ᕕ( ᐛ )ᕗ \", [], \" ᕕ( ᐛ )ᕗ \"),\n        (None, [], None),\n        # Special case: UNSET is not provided as-is to the callback, but normalized to\n        # None.\n        (UNSET, [], None),\n        # Special case: if default=True and flag_value is set, the value returned is the\n        # flag_value, not the True Python value itself.\n        (True, [], \"js\"),\n        # Non-string defaults are process as strings by the default Parameter's type.\n        (False, [], \"False\"),\n        (42, [], \"42\"),\n        (12.3, [], \"12.3\"),\n    ),\n)\ndef test_default_dual_option_callback(runner, default, args, expected):\n    \"\"\"Check how default is processed by the callback when options compete for the same\n    variable name.\n\n    Reproduction of the issue reported in\n    https://github.com/pallets/click/pull/3030#discussion_r2271571819\n    \"\"\"\n\n    def _my_func(ctx, param, value):\n        # Print the value received by the callback as-is, so we can check for it.\n        return f\"Callback value: {value!r}\"\n\n    @click.command()\n    @click.option(\"--js\", \"fmt\", flag_value=\"js\", callback=_my_func, default=default)\n    @click.option(\"--xml\", \"fmt\", flag_value=\"xml\", callback=_my_func)\n    def main(fmt):\n        click.secho(fmt, nl=False)\n\n    result = runner.invoke(main, args)\n    assert result.output == f\"Callback value: {expected!r}\"\n    assert result.exit_code == 0\n\n\n@pytest.mark.parametrize(\n    (\"flag_value\", \"envvar_value\", \"expected\"),\n    [\n        # The envvar match exactly the flag value and is case-sensitive.\n        (\"bar\", \"bar\", \"bar\"),\n        (\"BAR\", \"BAR\", \"BAR\"),\n        (\" bar \", \" bar \", \" bar \"),\n        (\"42\", \"42\", \"42\"),\n        (\"None\", \"None\", \"None\"),\n        (\"True\", \"True\", \"True\"),\n        (\"False\", \"False\", \"False\"),\n        (\"true\", \"true\", \"true\"),\n        (\"false\", \"false\", \"false\"),\n        (\"A B\", \"A B\", \"A B\"),\n        (\" 1 2 \", \" 1 2 \", \" 1 2 \"),\n        (\"9.3\", \"9.3\", \"9.3\"),\n        (\"a;n\", \"a;n\", \"a;n\"),\n        (\"x:y\", \"x:y\", \"x:y\"),\n        (\"i/o\", \"i/o\", \"i/o\"),\n        # Empty or absent envvar is consider unset, so the flag default value is\n        # returned, which is None in this case.\n        (\"bar\", \"\", None),\n        (\"bar\", None, None),\n        (\"BAR\", \"\", None),\n        (\"BAR\", None, None),\n        (\" bar \", \"\", None),\n        (\" bar \", None, None),\n        (42, \"\", None),\n        (42, None, None),\n        (\"42\", \"\", None),\n        (\"42\", None, None),\n        (None, \"\", None),\n        (None, None, None),\n        (\"None\", \"\", None),\n        (\"None\", None, None),\n        (True, \"\", None),\n        (True, None, None),\n        (\"True\", \"\", None),\n        (\"True\", None, None),\n        (\"true\", \"\", None),\n        (\"true\", None, None),\n        (False, \"\", None),\n        (False, None, None),\n        (\"False\", \"\", None),\n        (\"False\", None, None),\n        (\"false\", \"\", None),\n        (\"false\", None, None),\n        # Activate the flag with a value recognized as True by the envvar, which\n        # returns the flag_value.\n        (\"bar\", \"True\", \"bar\"),\n        (\"bar\", \"true\", \"bar\"),\n        (\"bar\", \"trUe\", \"bar\"),\n        (\"bar\", \"  TRUE  \", \"bar\"),\n        (\"bar\", \"1\", \"bar\"),\n        (\"bar\", \"yes\", \"bar\"),\n        (\"bar\", \"on\", \"bar\"),\n        (\"bar\", \"t\", \"bar\"),\n        (\"bar\", \"y\", \"bar\"),\n        # Deactivating the flag with a value recognized as False by the envvar, which\n        # explicitly return the 'False' as the option is explicitly declared as a\n        # string.\n        (\"bar\", \"False\", \"False\"),\n        (\"bar\", \"false\", \"False\"),\n        (\"bar\", \"faLse\", \"False\"),\n        (\"bar\", \"  FALSE  \", \"False\"),\n        (\"bar\", \"0\", \"False\"),\n        (\"bar\", \"no\", \"False\"),\n        (\"bar\", \"off\", \"False\"),\n        (\"bar\", \"f\", \"False\"),\n        (\"bar\", \"n\", \"False\"),\n        # Any other value than the flag_value, or a recogned True or False value, fails\n        # to explicitly activate or deactivate the flag. So the flag default value is\n        # returned, which is None in this case.\n        (\"bar\", \" bar \", None),\n        (\"bar\", \"BAR\", None),\n        (\"bar\", \"random\", None),\n        (\"bar\", \"bar random\", None),\n        (\"bar\", \"random bar\", None),\n        (\"BAR\", \"bar\", None),\n        (\" bar \", \"bar\", None),\n        (\" bar \", \"BAR\", None),\n        (42, \"42\", None),\n        (42, \"foo\", None),\n        (None, \"foo\", None),\n        (\"None\", \"foo\", None),\n    ],\n)\ndef test_envvar_string_flag_value(runner, flag_value, envvar_value, expected):\n    \"\"\"Ensure that flag_value is recognized by the envvar.\"\"\"\n\n    @click.command()\n    @click.option(\"--upper\", type=str, flag_value=flag_value, envvar=\"UPPER\")\n    def cmd(upper):\n        click.echo(repr(upper), nl=False)\n\n    result = runner.invoke(cmd, env={\"UPPER\": envvar_value})\n    assert result.exit_code == 0\n    assert result.output == repr(expected)\n\n\n@pytest.mark.parametrize(\n    (\"opt_decls\", \"opt_params\", \"expect_is_flag\", \"expect_is_bool_flag\"),\n    [\n        # Not boolean flags.\n        (\"-a\", {\"type\": int}, False, False),\n        (\"-a\", {\"type\": bool}, False, False),\n        (\"-a\", {\"default\": True}, False, False),\n        (\"-a\", {\"default\": False}, False, False),\n        (\"-a\", {\"flag_value\": 1}, True, False),\n        # Boolean flags.\n        (\"-a\", {\"is_flag\": True}, True, True),\n        (\"-a/-A\", {}, True, True),\n        (\"-a\", {\"flag_value\": True}, True, True),\n        # Non-flag with flag_value.\n        (\"-a\", {\"is_flag\": False, \"flag_value\": 1}, False, False),\n    ],\n)\ndef test_flag_auto_detection(\n    opt_decls, opt_params, expect_is_flag, expect_is_bool_flag\n):\n    option = Option([opt_decls], **opt_params)\n    assert option.is_flag is expect_is_flag\n    assert option.is_bool_flag is expect_is_bool_flag\n\n\n@pytest.mark.parametrize(\n    (\"kwargs\", \"message\"),\n    [\n        ({\"count\": True, \"multiple\": True}, \"'count' is not valid with 'multiple'.\"),\n        ({\"count\": True, \"is_flag\": True}, \"'count' is not valid with 'is_flag'.\"),\n    ],\n)\ndef test_invalid_flag_combinations(runner, kwargs, message):\n    with pytest.raises(TypeError) as e:\n        click.Option([\"-a\"], **kwargs)\n\n    assert message in str(e.value)\n\n\ndef test_non_flag_with_non_negatable_default(runner):\n    class NonNegatable:\n        def __bool__(self):\n            raise ValueError(\"Cannot negate this object\")\n\n    @click.command()\n    @click.option(\"--foo\", default=NonNegatable())\n    def cmd(foo):\n        pass\n\n    result = runner.invoke(cmd)\n    assert result.exit_code == 0\n\n\nclass HashType(enum.Enum):\n    MD5 = \"MD5\"\n    SHA1 = \"SHA1\"\n    SHA256 = \"SHA-256\"\n\n\nclass Number(enum.IntEnum):\n    ONE = enum.auto()\n    TWO = enum.auto()\n\n\nclass Letter(enum.StrEnum):\n    NAME_1 = \"Value-1\"\n    NAME_2 = \"Value_2\"\n    NAME_3 = \"42_value\"\n\n\nclass Color(enum.Flag):\n    RED = enum.auto()\n    GREEN = enum.auto()\n    BLUE = enum.auto()\n\n\nclass ColorInt(enum.IntFlag):\n    RED = enum.auto()\n    GREEN = enum.auto()\n    BLUE = enum.auto()\n\n\n@pytest.mark.parametrize(\n    (\"choices\", \"metavar\"),\n    [\n        ([\"foo\", \"bar\"], \"[TEXT]\"),\n        ([1, 2], \"[INTEGER]\"),\n        ([1.0, 2.0], \"[FLOAT]\"),\n        ([True, False], \"[BOOLEAN]\"),\n        ([\"foo\", 1], \"[TEXT|INTEGER]\"),\n        (HashType, \"[HASHTYPE]\"),\n        (Number, \"[NUMBER]\"),\n        (Letter, \"[LETTER]\"),\n        (Color, \"[COLOR]\"),\n        (ColorInt, \"[COLORINT]\"),\n    ],\n)\ndef test_choice_usage_rendering(runner, choices, metavar):\n    \"\"\"BY default ``--help`` prints choice's values in the usage message.\n\n    But ``show_choices=False`` makes ``--help`` prints choice's METAVAR instead of\n    values.\n\n    Also check that usage error message always suggests the actual values.\n    \"\"\"\n\n    @click.command()\n    @click.option(\"-g\", type=click.Choice(choices))\n    def cli_with_choices(g):\n        pass\n\n    @click.command()\n    @click.option(\"-g\", type=click.Choice(choices), show_choices=False)\n    def cli_without_choices(g):\n        pass\n\n    display_values = tuple(\n        i.name if isinstance(i, enum.Enum) else str(i) for i in choices\n    )\n\n    # Check that the choices values are rendered as-is in the usage message.\n    result = runner.invoke(cli_with_choices, [\"--help\"])\n    assert f\"[{'|'.join(display_values)}]\" in result.stdout\n    assert not result.stderr\n    assert result.exit_code == 0\n\n    # Check that the metavar is rendered instead of the choices values themselves.\n    result = runner.invoke(cli_without_choices, [\"--help\"])\n    assert metavar in result.stdout\n    assert not result.stderr\n    assert result.exit_code == 0\n\n    # Check the usage error message suggests the actual accepted values.\n    for cli in (cli_with_choices, cli_without_choices):\n        result = runner.invoke(cli, [\"-g\", \"random\"])\n        assert (\n            \"\\n\\nError: Invalid value for '-g': 'random' is not one of \"\n            f\"{', '.join(map(repr, display_values))}.\\n\" in result.stderr\n        )\n        assert not result.stdout\n        assert result.exit_code == 2\n\n\n@pytest.mark.parametrize(\n    (\"choices\", \"default\", \"default_string\"),\n    [\n        ([\"foo\", \"bar\"], \"bar\", \"bar\"),\n        # The default value is not enforced to be in the choices.\n        ([\"foo\", \"bar\"], \"random\", \"random\"),\n        # None cannot be a default value as-is: it left the default value as unset.\n        ([\"foo\", \"bar\"], None, None),\n        ([0, 1], 0, \"0\"),\n        # Values are not coerced to the type of the choice, even if equivalent.\n        ([0, 1], 0.0, \"0.0\"),\n        ([1, 2], 2, \"2\"),\n        ([1.0, 2.0], 2, \"2\"),\n        ([1.0, 2.0], 2.0, \"2.0\"),\n        ([True, False], True, \"True\"),\n        ([True, False], False, \"False\"),\n        ([\"foo\", 1], \"foo\", \"foo\"),\n        ([\"foo\", 1], 1, \"1\"),\n        # Enum choices are rendered as their names, not values.\n        # See: https://github.com/pallets/click/issues/2911\n        (HashType, HashType.SHA1, \"SHA1\"),\n        # Enum choices allow defaults strings that are their names.\n        (HashType, HashType.SHA256, \"SHA256\"),\n        (HashType, \"SHA256\", \"SHA256\"),\n        (Number, Number.TWO, \"TWO\"),\n        (Number, \"TWO\", \"TWO\"),\n        (Letter, Letter.NAME_1, \"NAME_1\"),\n        (Letter, Letter.NAME_2, \"NAME_2\"),\n        (Letter, Letter.NAME_3, \"NAME_3\"),\n        (Letter, \"NAME_1\", \"NAME_1\"),\n        (Letter, \"NAME_2\", \"NAME_2\"),\n        (Letter, \"NAME_3\", \"NAME_3\"),\n        (Color, Color.GREEN, \"GREEN\"),\n        (Color, \"GREEN\", \"GREEN\"),\n        (ColorInt, ColorInt.GREEN, \"GREEN\"),\n        (ColorInt, \"GREEN\", \"GREEN\"),\n    ],\n)\ndef test_choice_default_rendering(runner, choices, default, default_string):\n    @click.command()\n    @click.option(\"-g\", type=click.Choice(choices), default=default, show_default=True)\n    def cli_with_choices(g):\n        pass\n\n    # Check that the default value is kept normalized to the type of the choice.\n    assert cli_with_choices.params[0].default == default\n\n    result = runner.invoke(cli_with_choices, [\"--help\"])\n    extra_usage = f\"[default: {default_string}]\"\n    if default_string is None:\n        assert extra_usage not in result.output\n    else:\n        assert extra_usage in result.output\n\n\n@pytest.mark.parametrize(\n    \"opts_one,opts_two\",\n    [\n        # No duplicate shortnames\n        (\n            (\"-a\", \"--aardvark\"),\n            (\"-a\", \"--avocado\"),\n        ),\n        # No duplicate long names\n        (\n            (\"-a\", \"--aardvark\"),\n            (\"-b\", \"--aardvark\"),\n        ),\n    ],\n)\ndef test_duplicate_names_warning(runner, opts_one, opts_two):\n    @click.command()\n    @click.option(*opts_one)\n    @click.option(*opts_two)\n    def cli(one, two):\n        pass\n\n    with pytest.warns(UserWarning):\n        runner.invoke(cli, [])\n\n\nOBJECT_SENTINEL = object()\n\"\"\"An object-based sentinel value.\"\"\"\n\n\nclass EnumSentinel(enum.Enum):\n    FALSY_SENTINEL = object()\n\n    def __bool__(self) -> Literal[False]:\n        \"\"\"Force the sentinel to be falsy to make sure it is not caught by Click\n        internal implementation.\n\n        Falsy sentinels have been discussed in:\n        https://github.com/pallets/click/pull/3030#pullrequestreview-3106604795\n        https://github.com/pallets/click/pull/3030#pullrequestreview-3108471552\n        \"\"\"\n        return False\n\n\n# Any kind of sentinel value is recognized by Click as a valid flag value.\n@pytest.mark.parametrize(\"sentinel\", (OBJECT_SENTINEL, EnumSentinel.FALSY_SENTINEL))\n@pytest.mark.parametrize(\n    (\"args\", \"expected\"),\n    (\n        # Option default to None.\n        ([], None),\n        # Config has no default value, and no flag value, so it requires an argument.\n        (\n            [\"--config\"],\n            re.compile(re.escape(\"Error: Option '--config' requires an argument.\\n\")),\n        ),\n        (\n            [\"--no-config\", \"--config\"],\n            re.compile(re.escape(\"Error: Option '--config' requires an argument.\\n\")),\n        ),\n        # Passing --no-config defaults to the sentinel value because of the flag_value,\n        # and then the custom type receives that sentinel and returns a message.\n        ([\"--no-config\"], \"No configuration file provided.\"),\n        # Passing --config with an argument returns the file path.\n        ([\"--config\", \"foo.conf\"], \"foo.conf\"),\n        # An argument is not allowed for --no-config, so it raises an error.\n        (\n            [\"--no-config\", \"foo.conf\"],\n            re.compile(\n                r\"Usage: main \\[OPTIONS\\]\\n\"\n                r\"Try 'main --help' for help.\\n\"\n                r\"\\n\"\n                r\"Error: Got unexpected extra argument (.+)\\n\"\n            ),\n        ),\n        # Passing --config with an argument that does not exist raises an error.\n        (\n            [\"--config\", \"random-file.conf\"],\n            re.compile(\n                r\"Usage: main \\[OPTIONS\\]\\n\"\n                r\"Try 'main --help' for help.\\n\"\n                r\"\\n\"\n                r\"Error: Invalid value for '-c' / '--config': \"\n                r\"File 'random-file.conf' does not exist.\\n\"\n            ),\n        ),\n        (\n            [\"--config\", \"--no-config\"],\n            re.compile(\n                r\"Usage: main \\[OPTIONS\\]\\n\"\n                r\"Try 'main --help' for help.\\n\"\n                r\"\\n\"\n                r\"Error: Invalid value for '-c' / '--config': \"\n                r\"File '--no-config' does not exist.\\n\"\n            ),\n        ),\n        (\n            [\"--config\", \"--no-config\", \"foo.conf\"],\n            re.compile(\n                r\"Usage: main \\[OPTIONS\\]\\n\"\n                r\"Try 'main --help' for help.\\n\"\n                r\"\\n\"\n                r\"Error: Invalid value for '-c' / '--config': \"\n                r\"File '--no-config' does not exist.\\n\"\n            ),\n        ),\n        # --config is passed last and overrides the --no-config option.\n        ([\"--no-config\", \"--config\", \"foo.conf\"], \"foo.conf\"),\n    ),\n)\ndef test_dual_options_custom_type_sentinel_flag_value(runner, sentinel, args, expected):\n    \"\"\"Check that an object-based sentinel, used as a flag value, is returned as-is\n    to a custom type that is shared by two options, competing for the same variable\n    name.\n\n    A reproduction of\n    https://github.com/pallets/click/issues/3024#issuecomment-3146511356\n    \"\"\"\n\n    class ConfigParamType(click.ParamType):\n        \"\"\"A custom type that accepts a file path or a sentinel value.\"\"\"\n\n        def convert(self, value, param, ctx):\n            if value is sentinel:\n                return \"No configuration file provided.\"\n            else:\n                return click.Path(exists=True, dir_okay=False).convert(\n                    value, param, ctx\n                )\n\n    @click.command()\n    @click.option(\"-c\", \"--config\", type=ConfigParamType())\n    @click.option(\"--no-config\", \"config\", flag_value=sentinel, type=ConfigParamType())\n    def main(config):\n        click.echo(repr(config), nl=False)\n\n    with tempfile.NamedTemporaryFile(mode=\"w\") as named_tempfile:\n        if \"foo.conf\" in args:\n            named_tempfile.write(\"Blah blah\")\n            named_tempfile.flush()\n            args = [named_tempfile.name if a == \"foo.conf\" else a for a in args]\n\n        result = runner.invoke(main, args)\n\n        if isinstance(expected, re.Pattern):\n            assert re.match(expected, result.output)\n        else:\n            assert result.output == repr(\n                named_tempfile.name if expected == \"foo.conf\" else expected\n            )\n\n\nclass EngineType(enum.Enum):\n    OSS = enum.auto()\n    PRO = enum.auto()\n    MAX = enum.auto()\n\n\nclass Class1:\n    pass\n\n\nclass Class2:\n    pass\n\n\n@pytest.mark.parametrize(\n    (\"opt_params\", \"args\", \"expected\"),\n    [\n        # Check that the flag value is returned as-is when the option is passed, and\n        # not normalized to a boolean, even if it is explicitly declared as a flag.\n        ({\"type\": EngineType, \"flag_value\": None}, [\"--pro\"], None),\n        (\n            {\"type\": EngineType, \"is_flag\": True, \"flag_value\": None},\n            [\"--pro\"],\n            None,\n        ),\n        ({\"type\": EngineType, \"flag_value\": EngineType.OSS}, [\"--pro\"], EngineType.OSS),\n        (\n            {\"type\": EngineType, \"is_flag\": True, \"flag_value\": EngineType.OSS},\n            [\"--pro\"],\n            EngineType.OSS,\n        ),\n        (\n            {\"type\": EngineType, \"is_flag\": True, \"default\": EngineType.OSS},\n            [\"--pro\"],\n            EngineType.OSS,\n        ),\n        # The default value is returned as-is when the option is not passed, whatever\n        # the flag value.\n        ({\"type\": EngineType, \"flag_value\": None}, [], None),\n        ({\"type\": EngineType, \"is_flag\": True, \"flag_value\": None}, [], None),\n        ({\"type\": EngineType, \"flag_value\": EngineType.OSS}, [], None),\n        (\n            {\"type\": EngineType, \"is_flag\": True, \"flag_value\": EngineType.OSS},\n            [],\n            None,\n        ),\n        (\n            {\"type\": EngineType, \"is_flag\": True, \"default\": EngineType.OSS},\n            [],\n            EngineType.OSS,\n        ),\n        # The option has not enough parameters to be detected as flag-like, so it\n        # requires an argument.\n        (\n            {\"type\": EngineType, \"default\": EngineType.OSS},\n            [\"--pro\"],\n            re.compile(re.escape(\"Error: Option '--pro' requires an argument.\\n\")),\n        ),\n        ({\"type\": EngineType, \"default\": EngineType.OSS}, [], EngineType.OSS),\n        # If a flag value is set, it is returned instead of the default value.\n        (\n            {\"type\": EngineType, \"flag_value\": EngineType.OSS, \"default\": True},\n            [\"--pro\"],\n            EngineType.OSS,\n        ),\n        (\n            {\"type\": EngineType, \"flag_value\": EngineType.OSS, \"default\": True},\n            [],\n            EngineType.OSS,\n        ),\n        # Type is not specified and default to string, so the default value is\n        # returned as a string, even if it is a boolean. Also, defaults to the\n        # flag_value instead of the default value to support legacy behavior.\n        ({\"flag_value\": \"1\", \"default\": True}, [], \"1\"),\n        ({\"flag_value\": \"1\", \"default\": 42}, [], \"42\"),\n        ({\"flag_value\": EngineType.OSS, \"default\": True}, [], \"EngineType.OSS\"),\n        ({\"flag_value\": EngineType.OSS, \"default\": 42}, [], \"42\"),\n        # See: the result is the same if we force the type to be str.\n        ({\"type\": str, \"flag_value\": 1, \"default\": True}, [], \"1\"),\n        ({\"type\": str, \"flag_value\": 1, \"default\": 42}, [], \"42\"),\n        ({\"type\": str, \"flag_value\": \"1\", \"default\": True}, [], \"1\"),\n        ({\"type\": str, \"flag_value\": \"1\", \"default\": 42}, [], \"42\"),\n        (\n            {\"type\": str, \"flag_value\": EngineType.OSS, \"default\": True},\n            [],\n            \"EngineType.OSS\",\n        ),\n        ({\"type\": str, \"flag_value\": EngineType.OSS, \"default\": 42}, [], \"42\"),\n        # But having the flag value set to integer is automaticcally recognized by\n        # Click.\n        ({\"flag_value\": 1, \"default\": True}, [], 1),\n        ({\"flag_value\": 1, \"default\": 42}, [], 42),\n        ({\"type\": int, \"flag_value\": 1, \"default\": True}, [], 1),\n        ({\"type\": int, \"flag_value\": 1, \"default\": 42}, [], 42),\n        ({\"type\": int, \"flag_value\": \"1\", \"default\": True}, [], 1),\n        ({\"type\": int, \"flag_value\": \"1\", \"default\": 42}, [], 42),\n    ],\n)\ndef test_custom_type_flag_value_standalone_option(runner, opt_params, args, expected):\n    \"\"\"Test how the type and flag_value influence the returned value.\n\n    Cover cases reported in:\n    https://github.com/pallets/click/issues/3024#issuecomment-3146480714\n    https://github.com/pallets/click/issues/2012#issuecomment-892437060\n    \"\"\"\n\n    @click.command()\n    @click.option(\"--pro\", **opt_params)\n    def scan(pro):\n        click.echo(repr(pro), nl=False)\n\n    result = runner.invoke(scan, args)\n    if isinstance(expected, re.Pattern):\n        assert re.match(expected, result.output)\n    else:\n        assert result.output == repr(expected)\n\n\n@pytest.mark.parametrize(\n    (\"opt1_params\", \"opt2_params\", \"args\", \"expected\"),\n    [\n        # Dual options sharing the same variable name, are not competitive, and the\n        # flag value is returned as-is. Especially when the type is force to be\n        # unprocessed.\n        (\n            {\"flag_value\": EngineType.OSS, \"type\": UNPROCESSED},\n            {\"flag_value\": EngineType.PRO, \"type\": UNPROCESSED},\n            [],\n            None,\n        ),\n        (\n            {\"flag_value\": EngineType.OSS, \"type\": UNPROCESSED},\n            {\"flag_value\": EngineType.PRO, \"type\": UNPROCESSED},\n            [\"--opt1\"],\n            EngineType.OSS,\n        ),\n        (\n            {\"flag_value\": EngineType.OSS, \"type\": UNPROCESSED},\n            {\"flag_value\": EngineType.PRO, \"type\": UNPROCESSED},\n            [\"--opt2\"],\n            EngineType.PRO,\n        ),\n        # Check that passing exotic flag values like classes is supported, but are\n        # rendered to strings when the type is not specified.\n        (\n            {\"flag_value\": Class1, \"default\": True},\n            {\"flag_value\": Class2},\n            [],\n            re.compile(r\"'<test_options.Class1 object at 0x[0-9A-Fa-f]+>'\"),\n        ),\n        (\n            {\"flag_value\": Class1, \"default\": True},\n            {\"flag_value\": Class2},\n            [\"--opt1\"],\n            \"<class 'test_options.Class1'>\",\n        ),\n        (\n            {\"flag_value\": Class1, \"default\": True},\n            {\"flag_value\": Class2},\n            [\"--opt2\"],\n            \"<class 'test_options.Class2'>\",\n        ),\n        # Even the default is processed as a string.\n        ({\"flag_value\": Class1, \"default\": \"True\"}, {\"flag_value\": Class2}, [], \"True\"),\n        ({\"flag_value\": Class1, \"default\": None}, {\"flag_value\": Class2}, [], None),\n        # To get the classes as-is, we need to specify the type as UNPROCESSED.\n        (\n            {\"flag_value\": Class1, \"type\": UNPROCESSED, \"default\": True},\n            {\"flag_value\": Class2, \"type\": UNPROCESSED},\n            [],\n            re.compile(r\"<test_options.Class1 object at 0x[0-9A-Fa-f]+>\"),\n        ),\n        (\n            {\"flag_value\": Class1, \"type\": UNPROCESSED, \"default\": True},\n            {\"flag_value\": Class2, \"type\": UNPROCESSED},\n            [\"--opt1\"],\n            Class1,\n        ),\n        (\n            {\"flag_value\": Class1, \"type\": UNPROCESSED, \"default\": True},\n            {\"flag_value\": Class2, \"type\": UNPROCESSED},\n            [\"--opt2\"],\n            Class2,\n        ),\n        # Setting the default to a class, an instance of the class is returned instead\n        # of the class itself, because the default is allowed to be callable (and\n        # consummd). And this happens whatever the type is.\n        (\n            {\"flag_value\": Class1, \"default\": Class1},\n            {\"flag_value\": Class2},\n            [],\n            re.compile(r\"'<test_options.Class1 object at 0x[0-9A-Fa-f]+>'\"),\n        ),\n        (\n            {\"flag_value\": Class1, \"default\": Class2},\n            {\"flag_value\": Class2},\n            [],\n            re.compile(r\"'<test_options.Class2 object at 0x[0-9A-Fa-f]+>'\"),\n        ),\n        (\n            {\"flag_value\": Class1, \"type\": UNPROCESSED, \"default\": Class1},\n            {\"flag_value\": Class2, \"type\": UNPROCESSED},\n            [],\n            re.compile(r\"<test_options.Class1 object at 0x[0-9A-Fa-f]+>\"),\n        ),\n        (\n            {\"flag_value\": Class1, \"type\": UNPROCESSED, \"default\": Class2},\n            {\"flag_value\": Class2, \"type\": UNPROCESSED},\n            [],\n            re.compile(r\"<test_options.Class2 object at 0x[0-9A-Fa-f]+>\"),\n        ),\n        # Having the flag value set to integer is automaticcally recognized by Click.\n        (\n            {\"flag_value\": 1, \"default\": True},\n            {\"flag_value\": \"1\"},\n            [],\n            1,\n        ),\n        (\n            {\"flag_value\": 1, \"type\": int, \"default\": True},\n            {\"flag_value\": \"1\", \"type\": int},\n            [],\n            1,\n        ),\n    ],\n)\ndef test_custom_type_flag_value_dual_options(\n    runner, opt1_params, opt2_params, args, expected\n):\n    \"\"\"Test how flag values are processed with dual options competing for the same\n    variable name.\n\n    Reproduce issues reported in:\n    https://github.com/pallets/click/issues/3024#issuecomment-3146508536\n    https://github.com/pallets/click/issues/2012#issue-946471049\n    https://github.com/pallets/click/issues/2012#issuecomment-892437060\n    \"\"\"\n\n    @click.command()\n    @click.option(\"--opt1\", \"dual_option\", **opt1_params)\n    @click.option(\"--opt2\", \"dual_option\", **opt2_params)\n    def cli(dual_option):\n        click.echo(repr(dual_option), nl=False)\n\n    result = runner.invoke(cli, args)\n    if isinstance(expected, re.Pattern):\n        assert re.match(expected, result.output)\n    else:\n        assert result.output == repr(expected)\n\n\ndef test_custom_type_frozenset_flag_value(runner):\n    \"\"\"Check that frozenset is correctly handled as a type, a flag value and a default.\n\n    Reproduces https://github.com/pallets/click/issues/2610\n    \"\"\"\n\n    @click.command()\n    @click.option(\n        \"--without-scm-ignore-files\",\n        \"scm_ignore_files\",\n        is_flag=True,\n        type=frozenset,\n        flag_value=frozenset(),\n        default=frozenset([\"git\"]),\n    )\n    def rcli(scm_ignore_files):\n        click.echo(repr(scm_ignore_files), nl=False)\n\n    result = runner.invoke(rcli)\n    assert result.stdout == \"frozenset({'git'})\"\n    assert result.exit_code == 0\n\n    result = runner.invoke(rcli, [\"--without-scm-ignore-files\"])\n    assert result.stdout == \"frozenset()\"\n    assert result.exit_code == 0\n\n\n@pytest.mark.parametrize(\n    (\"flag_type\", \"args\", \"expect_output\"),\n    [\n        (str, [], \"Default\\n\"),\n        (str, [\"--theflag\"], \"FlagValue\\n\"),\n        (str, [\"--theflag\", \"value\"], \"value\\n\"),\n        (int, [], \"0\\n\"),\n        (int, [\"--theflag\"], \"1\\n\"),\n        (int, [\"--theflag\", \"2\"], \"2\\n\"),\n    ],\n)\ndef test_flag_value_on_option_with_zero_or_one_args(flag_type, args, expect_output):\n    \"\"\"An option with flag_value and is_flag=False can be\n    omitted or used with 0 or 1 args.\n\n    Regression test for https://github.com/pallets/click/issues/3084\n    \"\"\"\n    if flag_type is str:\n        flagopt = click.option(\n            \"--theflag\",\n            type=str,\n            is_flag=False,\n            flag_value=\"FlagValue\",\n            default=\"Default\",\n        )\n    elif flag_type is int:\n        flagopt = click.option(\n            \"--theflag\", type=int, is_flag=False, flag_value=1, default=0\n        )\n    else:\n        raise NotImplementedError(flag_type)\n\n    @click.command()\n    @flagopt\n    def cmd(theflag):\n        click.echo(theflag)\n\n    runner = CliRunner()\n    result = runner.invoke(cmd, args)\n    assert result.exit_code == 0\n    assert result.output == expect_output\n"
  },
  {
    "path": "tests/test_parser.py",
    "content": "import pytest\n\nimport click\nfrom click.parser import _OptionParser\nfrom click.shell_completion import split_arg_string\n\n\n@pytest.mark.parametrize(\n    (\"value\", \"expect\"),\n    [\n        (\"cli a b c\", [\"cli\", \"a\", \"b\", \"c\"]),\n        (\"cli 'my file\", [\"cli\", \"my file\"]),\n        (\"cli 'my file'\", [\"cli\", \"my file\"]),\n        (\"cli my\\\\\", [\"cli\", \"my\"]),\n        (\"cli my\\\\ file\", [\"cli\", \"my file\"]),\n    ],\n)\ndef test_split_arg_string(value, expect):\n    assert split_arg_string(value) == expect\n\n\ndef test_parser_default_prefixes():\n    parser = _OptionParser()\n    assert parser._opt_prefixes == {\"-\", \"--\"}\n\n\ndef test_parser_collects_prefixes():\n    ctx = click.Context(click.Command(\"test\"))\n    parser = _OptionParser(ctx)\n    click.Option(\"+p\", is_flag=True).add_to_parser(parser, ctx)\n    click.Option(\"!e\", is_flag=True).add_to_parser(parser, ctx)\n    assert parser._opt_prefixes == {\"-\", \"--\", \"+\", \"!\"}\n"
  },
  {
    "path": "tests/test_shell_completion.py",
    "content": "import textwrap\nimport warnings\nfrom collections.abc import Mapping\n\nimport pytest\n\nimport click.shell_completion\nfrom click.core import Argument\nfrom click.core import Command\nfrom click.core import Group\nfrom click.core import Option\nfrom click.shell_completion import add_completion_class\nfrom click.shell_completion import CompletionItem\nfrom click.shell_completion import ShellComplete\nfrom click.types import Choice\nfrom click.types import File\nfrom click.types import Path\n\n\ndef _get_completions(cli, args, incomplete):\n    comp = ShellComplete(cli, {}, cli.name, \"_CLICK_COMPLETE\")\n    return comp.get_completions(args, incomplete)\n\n\ndef _get_words(cli, args, incomplete):\n    return [c.value for c in _get_completions(cli, args, incomplete)]\n\n\ndef test_command():\n    cli = Command(\"cli\", params=[Option([\"-t\", \"--test\"])])\n    assert _get_words(cli, [], \"\") == []\n    assert _get_words(cli, [], \"-\") == [\"-t\", \"--test\", \"--help\"]\n    assert _get_words(cli, [], \"--\") == [\"--test\", \"--help\"]\n    assert _get_words(cli, [], \"--t\") == [\"--test\"]\n    # -t has been seen, so --test isn't suggested\n    assert _get_words(cli, [\"-t\", \"a\"], \"-\") == [\"--help\"]\n\n\ndef test_group():\n    cli = Group(\"cli\", params=[Option([\"-a\"])], commands=[Command(\"x\"), Command(\"y\")])\n    assert _get_words(cli, [], \"\") == [\"x\", \"y\"]\n    assert _get_words(cli, [], \"-\") == [\"-a\", \"--help\"]\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"word\", \"expect\"),\n    [\n        ([], \"\", [\"get\"]),\n        ([\"get\"], \"\", [\"full\"]),\n        ([\"get\", \"full\"], \"\", [\"data\"]),\n        ([\"get\", \"full\"], \"-\", [\"--verbose\", \"--help\"]),\n        ([\"get\", \"full\", \"data\"], \"\", []),\n        ([\"get\", \"full\", \"data\"], \"-\", [\"-a\", \"--help\"]),\n    ],\n)\ndef test_nested_group(args: list[str], word: str, expect: list[str]) -> None:\n    cli = Group(\n        \"cli\",\n        commands=[\n            Group(\n                \"get\",\n                commands=[\n                    Group(\n                        \"full\",\n                        params=[Option([\"--verbose\"])],\n                        commands=[Command(\"data\", params=[Option([\"-a\"])])],\n                    )\n                ],\n            )\n        ],\n    )\n    assert _get_words(cli, args, word) == expect\n\n\ndef test_group_command_same_option():\n    cli = Group(\n        \"cli\", params=[Option([\"-a\"])], commands=[Command(\"x\", params=[Option([\"-a\"])])]\n    )\n    assert _get_words(cli, [], \"-\") == [\"-a\", \"--help\"]\n    assert _get_words(cli, [\"-a\", \"a\"], \"-\") == [\"--help\"]\n    assert _get_words(cli, [\"-a\", \"a\", \"x\"], \"-\") == [\"-a\", \"--help\"]\n    assert _get_words(cli, [\"-a\", \"a\", \"x\", \"-a\", \"a\"], \"-\") == [\"--help\"]\n\n\ndef test_chained():\n    cli = Group(\n        \"cli\",\n        chain=True,\n        commands=[\n            Command(\"set\", params=[Option([\"-y\"])]),\n            Command(\"start\"),\n            Group(\"get\", commands=[Command(\"full\")]),\n        ],\n    )\n    assert _get_words(cli, [], \"\") == [\"get\", \"set\", \"start\"]\n    assert _get_words(cli, [], \"s\") == [\"set\", \"start\"]\n    assert _get_words(cli, [\"set\", \"start\"], \"\") == [\"get\"]\n    # subcommands and parent subcommands\n    assert _get_words(cli, [\"get\"], \"\") == [\"full\", \"set\", \"start\"]\n    assert _get_words(cli, [\"get\", \"full\"], \"\") == [\"set\", \"start\"]\n    assert _get_words(cli, [\"get\"], \"s\") == [\"set\", \"start\"]\n\n\ndef test_help_option():\n    cli = Group(\"cli\", commands=[Command(\"with\"), Command(\"no\", add_help_option=False)])\n    assert _get_words(cli, [\"with\"], \"--\") == [\"--help\"]\n    assert _get_words(cli, [\"no\"], \"--\") == []\n\n\ndef test_argument_order():\n    cli = Command(\n        \"cli\",\n        params=[\n            Argument([\"plain\"]),\n            Argument([\"c1\"], type=Choice([\"a1\", \"a2\", \"b\"])),\n            Argument([\"c2\"], type=Choice([\"c1\", \"c2\", \"d\"])),\n        ],\n    )\n    # first argument has no completions\n    assert _get_words(cli, [], \"\") == []\n    assert _get_words(cli, [], \"a\") == []\n    # first argument filled, now completion can happen\n    assert _get_words(cli, [\"x\"], \"a\") == [\"a1\", \"a2\"]\n    assert _get_words(cli, [\"x\", \"b\"], \"d\") == [\"d\"]\n\n\ndef test_argument_default():\n    cli = Command(\n        \"cli\",\n        add_help_option=False,\n        params=[\n            Argument([\"a\"], type=Choice([\"a\"]), default=\"a\"),\n            Argument([\"b\"], type=Choice([\"b\"]), default=\"b\"),\n        ],\n    )\n    assert _get_words(cli, [], \"\") == [\"a\"]\n    assert _get_words(cli, [\"a\"], \"b\") == [\"b\"]\n    # ignore type validation\n    assert _get_words(cli, [\"x\"], \"b\") == [\"b\"]\n\n\ndef test_type_choice():\n    cli = Command(\"cli\", params=[Option([\"-c\"], type=Choice([\"a1\", \"a2\", \"b\"]))])\n    assert _get_words(cli, [\"-c\"], \"\") == [\"a1\", \"a2\", \"b\"]\n    assert _get_words(cli, [\"-c\"], \"a\") == [\"a1\", \"a2\"]\n    assert _get_words(cli, [\"-c\"], \"a2\") == [\"a2\"]\n\n\ndef test_choice_special_characters():\n    cli = Command(\"cli\", params=[Option([\"-c\"], type=Choice([\"!1\", \"!2\", \"+3\"]))])\n    assert _get_words(cli, [\"-c\"], \"\") == [\"!1\", \"!2\", \"+3\"]\n    assert _get_words(cli, [\"-c\"], \"!\") == [\"!1\", \"!2\"]\n    assert _get_words(cli, [\"-c\"], \"!2\") == [\"!2\"]\n\n\ndef test_choice_conflicting_prefix():\n    cli = Command(\n        \"cli\",\n        params=[\n            Option([\"-c\"], type=Choice([\"!1\", \"!2\", \"+3\"])),\n            Option([\"+p\"], is_flag=True),\n        ],\n    )\n    assert _get_words(cli, [\"-c\"], \"\") == [\"!1\", \"!2\", \"+3\"]\n    assert _get_words(cli, [\"-c\"], \"+\") == [\"+p\"]\n\n\ndef test_option_count():\n    cli = Command(\"cli\", params=[Option([\"-c\"], count=True)])\n    assert _get_words(cli, [\"-c\"], \"\") == []\n    assert _get_words(cli, [\"-c\"], \"-\") == [\"--help\"]\n\n\ndef test_option_optional():\n    cli = Command(\n        \"cli\",\n        add_help_option=False,\n        params=[\n            Option([\"--name\"], is_flag=False, flag_value=\"value\"),\n            Option([\"--flag\"], is_flag=True),\n        ],\n    )\n    assert _get_words(cli, [\"--name\"], \"\") == []\n    assert _get_words(cli, [\"--name\"], \"-\") == [\"--flag\"]\n    assert _get_words(cli, [\"--name\", \"--flag\"], \"-\") == []\n\n\n@pytest.mark.parametrize(\n    (\"type\", \"expect\"),\n    [(File(), \"file\"), (Path(), \"file\"), (Path(file_okay=False), \"dir\")],\n)\ndef test_path_types(type, expect):\n    cli = Command(\"cli\", params=[Option([\"-f\"], type=type)])\n    out = _get_completions(cli, [\"-f\"], \"ab\")\n    assert len(out) == 1\n    c = out[0]\n    assert c.value == \"ab\"\n    assert c.type == expect\n\n\ndef test_absolute_path():\n    cli = Command(\"cli\", params=[Option([\"-f\"], type=Path())])\n    out = _get_completions(cli, [\"-f\"], \"/ab\")\n    assert len(out) == 1\n    c = out[0]\n    assert c.value == \"/ab\"\n\n\ndef test_option_flag():\n    cli = Command(\n        \"cli\",\n        add_help_option=False,\n        params=[\n            Option([\"--on/--off\"]),\n            Argument([\"a\"], type=Choice([\"a1\", \"a2\", \"b\"])),\n        ],\n    )\n    assert _get_words(cli, [], \"--\") == [\"--on\", \"--off\"]\n    # flag option doesn't take value, use choice argument\n    assert _get_words(cli, [\"--on\"], \"a\") == [\"a1\", \"a2\"]\n\n\ndef test_flag_option_with_nargs_option():\n    cli = Command(\n        \"cli\",\n        add_help_option=False,\n        params=[\n            Argument([\"a\"], type=Choice([\"a1\", \"a2\", \"b\"])),\n            Option([\"--flag\"], is_flag=True),\n            Option([\"-c\"], type=Choice([\"p\", \"q\"]), nargs=2),\n        ],\n    )\n    assert _get_words(cli, [\"a1\", \"--flag\", \"-c\"], \"\") == [\"p\", \"q\"]\n\n\ndef test_option_custom():\n    def custom(ctx, param, incomplete):\n        return [incomplete.upper()]\n\n    cli = Command(\n        \"cli\",\n        params=[\n            Argument([\"x\"]),\n            Argument([\"y\"]),\n            Argument([\"z\"], shell_complete=custom),\n        ],\n    )\n    assert _get_words(cli, [\"a\", \"b\"], \"\") == [\"\"]\n    assert _get_words(cli, [\"a\", \"b\"], \"c\") == [\"C\"]\n\n\ndef test_option_multiple():\n    cli = Command(\n        \"type\",\n        params=[Option([\"-m\"], type=Choice([\"a\", \"b\"]), multiple=True), Option([\"-f\"])],\n    )\n    assert _get_words(cli, [\"-m\"], \"\") == [\"a\", \"b\"]\n    assert \"-m\" in _get_words(cli, [\"-m\", \"a\"], \"-\")\n    assert _get_words(cli, [\"-m\", \"a\", \"-m\"], \"\") == [\"a\", \"b\"]\n    # used single options aren't suggested again\n    assert \"-c\" not in _get_words(cli, [\"-c\", \"f\"], \"-\")\n\n\ndef test_option_nargs():\n    cli = Command(\"cli\", params=[Option([\"-c\"], type=Choice([\"a\", \"b\"]), nargs=2)])\n    assert _get_words(cli, [\"-c\"], \"\") == [\"a\", \"b\"]\n    assert _get_words(cli, [\"-c\", \"a\"], \"\") == [\"a\", \"b\"]\n    assert _get_words(cli, [\"-c\", \"a\", \"b\"], \"\") == []\n\n\ndef test_argument_nargs():\n    cli = Command(\n        \"cli\",\n        params=[\n            Argument([\"x\"], type=Choice([\"a\", \"b\"]), nargs=2),\n            Argument([\"y\"], type=Choice([\"c\", \"d\"]), nargs=-1),\n            Option([\"-z\"]),\n        ],\n    )\n    assert _get_words(cli, [], \"\") == [\"a\", \"b\"]\n    assert _get_words(cli, [\"a\"], \"\") == [\"a\", \"b\"]\n    assert _get_words(cli, [\"a\", \"b\"], \"\") == [\"c\", \"d\"]\n    assert _get_words(cli, [\"a\", \"b\", \"c\"], \"\") == [\"c\", \"d\"]\n    assert _get_words(cli, [\"a\", \"b\", \"c\", \"d\"], \"\") == [\"c\", \"d\"]\n    assert _get_words(cli, [\"a\", \"-z\", \"1\"], \"\") == [\"a\", \"b\"]\n    assert _get_words(cli, [\"a\", \"-z\", \"1\", \"b\"], \"\") == [\"c\", \"d\"]\n\n\ndef test_double_dash():\n    cli = Command(\n        \"cli\",\n        add_help_option=False,\n        params=[\n            Option([\"--opt\"]),\n            Argument([\"name\"], type=Choice([\"name\", \"--\", \"-o\", \"--opt\"])),\n        ],\n    )\n    assert _get_words(cli, [], \"-\") == [\"--opt\"]\n    assert _get_words(cli, [\"value\"], \"-\") == [\"--opt\"]\n    assert _get_words(cli, [], \"\") == [\"name\", \"--\", \"-o\", \"--opt\"]\n    assert _get_words(cli, [\"--\"], \"\") == [\"name\", \"--\", \"-o\", \"--opt\"]\n\n\ndef test_hidden():\n    cli = Group(\n        \"cli\",\n        commands=[\n            Command(\n                \"hidden\",\n                add_help_option=False,\n                hidden=True,\n                params=[\n                    Option([\"-a\"]),\n                    Option([\"-b\"], type=Choice([\"a\", \"b\"]), hidden=True),\n                ],\n            )\n        ],\n    )\n    assert \"hidden\" not in _get_words(cli, [], \"\")\n    assert \"hidden\" not in _get_words(cli, [], \"hidden\")\n    assert _get_words(cli, [\"hidden\"], \"-\") == [\"-a\"]\n    assert _get_words(cli, [\"hidden\", \"-b\"], \"\") == [\"a\", \"b\"]\n\n\ndef test_add_different_name():\n    cli = Group(\"cli\", commands={\"renamed\": Command(\"original\")})\n    words = _get_words(cli, [], \"\")\n    assert \"renamed\" in words\n    assert \"original\" not in words\n\n\ndef test_completion_item_data():\n    c = CompletionItem(\"test\", a=1)\n    assert c.a == 1\n    assert c.b is None\n\n\n@pytest.fixture()\ndef _patch_for_completion(monkeypatch):\n    monkeypatch.setattr(\n        \"click.shell_completion.BashComplete._check_version\", lambda self: True\n    )\n\n\n@pytest.mark.parametrize(\"shell\", [\"bash\", \"zsh\", \"fish\"])\n@pytest.mark.usefixtures(\"_patch_for_completion\")\ndef test_full_source(runner, shell):\n    cli = Group(\"cli\", commands=[Command(\"a\"), Command(\"b\")])\n    result = runner.invoke(cli, env={\"_CLI_COMPLETE\": f\"{shell}_source\"})\n    assert f\"_CLI_COMPLETE={shell}_complete\" in result.output\n\n\n@pytest.mark.parametrize(\n    (\"shell\", \"env\", \"expect\"),\n    [\n        (\"bash\", {\"COMP_WORDS\": \"\", \"COMP_CWORD\": \"0\"}, \"plain,a\\nplain,b\\n\"),\n        (\"bash\", {\"COMP_WORDS\": \"a b\", \"COMP_CWORD\": \"1\"}, \"plain,b\\n\"),\n        (\"zsh\", {\"COMP_WORDS\": \"\", \"COMP_CWORD\": \"0\"}, \"plain\\na\\n_\\nplain\\nb\\nbee\\n\"),\n        (\"zsh\", {\"COMP_WORDS\": \"a b\", \"COMP_CWORD\": \"1\"}, \"plain\\nb\\nbee\\n\"),\n        (\"fish\", {\"COMP_WORDS\": \"\", \"COMP_CWORD\": \"\"}, \"plain,a\\nplain,b\\tbee\\n\"),\n        (\"fish\", {\"COMP_WORDS\": \"a b\", \"COMP_CWORD\": \"b\"}, \"plain,b\\tbee\\n\"),\n        (\"fish\", {\"COMP_WORDS\": 'a \"b', \"COMP_CWORD\": '\"b'}, \"plain,b\\tbee\\n\"),\n    ],\n)\n@pytest.mark.usefixtures(\"_patch_for_completion\")\ndef test_full_complete(runner, shell, env, expect):\n    cli = Group(\"cli\", commands=[Command(\"a\"), Command(\"b\", help=\"bee\")])\n    env[\"_CLI_COMPLETE\"] = f\"{shell}_complete\"\n    result = runner.invoke(cli, env=env)\n    assert result.output == expect\n\n\n@pytest.mark.parametrize(\n    (\"env\", \"expect\"),\n    [\n        (\n            {\"COMP_WORDS\": \"\", \"COMP_CWORD\": \"0\"},\n            textwrap.dedent(\n                \"\"\"\\\n                    plain\n                    a\n                    _\n                    plain\n                    b\n                    bee\n                    plain\n                    c\\\\:d\n                    cee:dee\n                    plain\n                    c:e\n                    _\n                \"\"\"\n            ),\n        ),\n        (\n            {\"COMP_WORDS\": \"a c\", \"COMP_CWORD\": \"1\"},\n            textwrap.dedent(\n                \"\"\"\\\n                    plain\n                    c\\\\:d\n                    cee:dee\n                    plain\n                    c:e\n                    _\n                \"\"\"\n            ),\n        ),\n        (\n            {\"COMP_WORDS\": \"a c:\", \"COMP_CWORD\": \"1\"},\n            textwrap.dedent(\n                \"\"\"\\\n                    plain\n                    c\\\\:d\n                    cee:dee\n                    plain\n                    c:e\n                    _\n                \"\"\"\n            ),\n        ),\n    ],\n)\n@pytest.mark.usefixtures(\"_patch_for_completion\")\ndef test_zsh_full_complete_with_colons(\n    runner, env: Mapping[str, str], expect: str\n) -> None:\n    cli = Group(\n        \"cli\",\n        commands=[\n            Command(\"a\"),\n            Command(\"b\", help=\"bee\"),\n            Command(\"c:d\", help=\"cee:dee\"),\n            Command(\"c:e\"),\n        ],\n    )\n    result = runner.invoke(\n        cli,\n        env={\n            **env,\n            \"_CLI_COMPLETE\": \"zsh_complete\",\n        },\n    )\n    assert result.output == expect\n\n\n@pytest.mark.usefixtures(\"_patch_for_completion\")\ndef test_context_settings(runner):\n    def complete(ctx, param, incomplete):\n        return ctx.obj[\"choices\"]\n\n    cli = Command(\"cli\", params=[Argument(\"x\", shell_complete=complete)])\n    result = runner.invoke(\n        cli,\n        obj={\"choices\": [\"a\", \"b\"]},\n        env={\"COMP_WORDS\": \"\", \"COMP_CWORD\": \"0\", \"_CLI_COMPLETE\": \"bash_complete\"},\n    )\n    assert result.output == \"plain,a\\nplain,b\\n\"\n\n\n@pytest.mark.parametrize((\"value\", \"expect\"), [(False, [\"Au\", \"al\"]), (True, [\"al\"])])\ndef test_choice_case_sensitive(value, expect):\n    cli = Command(\n        \"cli\",\n        params=[Option([\"-a\"], type=Choice([\"Au\", \"al\", \"Bc\"], case_sensitive=value))],\n    )\n    completions = _get_words(cli, [\"-a\"], \"a\")\n    assert completions == expect\n\n\n@pytest.fixture()\ndef _restore_available_shells(tmpdir):\n    prev_available_shells = click.shell_completion._available_shells.copy()\n    click.shell_completion._available_shells.clear()\n    yield\n    click.shell_completion._available_shells.clear()\n    click.shell_completion._available_shells.update(prev_available_shells)\n\n\n@pytest.mark.usefixtures(\"_restore_available_shells\")\ndef test_add_completion_class():\n    # At first, \"mysh\" is not in available shells\n    assert \"mysh\" not in click.shell_completion._available_shells\n\n    class MyshComplete(ShellComplete):\n        name = \"mysh\"\n        source_template = \"dummy source\"\n\n    # \"mysh\" still not in available shells because it is not registered\n    assert \"mysh\" not in click.shell_completion._available_shells\n\n    # Adding a completion class should return that class\n    assert add_completion_class(MyshComplete) is MyshComplete\n\n    # Now, \"mysh\" is finally in available shells\n    assert \"mysh\" in click.shell_completion._available_shells\n    assert click.shell_completion._available_shells[\"mysh\"] is MyshComplete\n\n\n@pytest.mark.usefixtures(\"_restore_available_shells\")\ndef test_add_completion_class_with_name():\n    # At first, \"mysh\" is not in available shells\n    assert \"mysh\" not in click.shell_completion._available_shells\n    assert \"not_mysh\" not in click.shell_completion._available_shells\n\n    class MyshComplete(ShellComplete):\n        name = \"not_mysh\"\n        source_template = \"dummy source\"\n\n    # \"mysh\" and \"not_mysh\" are still not in available shells because\n    # it is not registered yet\n    assert \"mysh\" not in click.shell_completion._available_shells\n    assert \"not_mysh\" not in click.shell_completion._available_shells\n\n    # Adding a completion class should return that class.\n    # Because we are using the \"name\" parameter, the name isn't taken\n    # from the class.\n    assert add_completion_class(MyshComplete, name=\"mysh\") is MyshComplete\n\n    # Now, \"mysh\" is finally in available shells\n    assert \"mysh\" in click.shell_completion._available_shells\n    assert \"not_mysh\" not in click.shell_completion._available_shells\n    assert click.shell_completion._available_shells[\"mysh\"] is MyshComplete\n\n\n@pytest.mark.usefixtures(\"_restore_available_shells\")\ndef test_add_completion_class_decorator():\n    # At first, \"mysh\" is not in available shells\n    assert \"mysh\" not in click.shell_completion._available_shells\n\n    @add_completion_class\n    class MyshComplete(ShellComplete):\n        name = \"mysh\"\n        source_template = \"dummy source\"\n\n    # Using `add_completion_class` as a decorator adds the new shell immediately\n    assert \"mysh\" in click.shell_completion._available_shells\n    assert click.shell_completion._available_shells[\"mysh\"] is MyshComplete\n\n\n# Don't make the ResourceWarning give an error\n@pytest.mark.filterwarnings(\"default\")\ndef test_files_closed(runner) -> None:\n    with runner.isolated_filesystem():\n        config_file = \"foo.txt\"\n        with open(config_file, \"w\") as f:\n            f.write(\"bar\")\n\n        @click.group()\n        @click.option(\n            \"--config-file\",\n            default=config_file,\n            type=click.File(mode=\"r\"),\n        )\n        @click.pass_context\n        def cli(ctx, config_file):\n            pass\n\n        with warnings.catch_warnings(record=True) as current_warnings:\n            assert not current_warnings, \"There should be no warnings to start\"\n            _get_completions(cli, args=[], incomplete=\"\")\n            assert not current_warnings, \"There should be no warnings after either\"\n"
  },
  {
    "path": "tests/test_termui.py",
    "content": "import platform\nimport tempfile\nimport time\n\nimport pytest\n\nimport click._termui_impl\nfrom click._compat import WIN\nfrom click.exceptions import BadParameter\nfrom click.exceptions import MissingParameter\n\n\nclass FakeClock:\n    def __init__(self):\n        self.now = time.time()\n\n    def advance_time(self, seconds=1):\n        self.now += seconds\n\n    def time(self):\n        return self.now\n\n\ndef _create_progress(length=10, **kwargs):\n    progress = click.progressbar(tuple(range(length)))\n    for key, value in kwargs.items():\n        setattr(progress, key, value)\n    return progress\n\n\ndef test_progressbar_strip_regression(runner, monkeypatch):\n    label = \"    padded line\"\n\n    @click.command()\n    def cli():\n        with _create_progress(label=label) as progress:\n            for _ in progress:\n                pass\n\n    monkeypatch.setattr(click._termui_impl, \"isatty\", lambda _: True)\n    assert (\n        label\n        in runner.invoke(cli, [], standalone_mode=False, catch_exceptions=False).output\n    )\n\n\ndef test_progressbar_length_hint(runner, monkeypatch):\n    class Hinted:\n        def __init__(self, n):\n            self.items = list(range(n))\n\n        def __length_hint__(self):\n            return len(self.items)\n\n        def __iter__(self):\n            return self\n\n        def __next__(self):\n            if self.items:\n                return self.items.pop()\n            else:\n                raise StopIteration\n\n        next = __next__\n\n    @click.command()\n    def cli():\n        with click.progressbar(Hinted(10), label=\"test\") as progress:\n            for _ in progress:\n                pass\n\n    monkeypatch.setattr(click._termui_impl, \"isatty\", lambda _: True)\n    result = runner.invoke(cli, [])\n    assert result.exception is None\n\n\ndef test_progressbar_no_tty(runner, monkeypatch):\n    @click.command()\n    def cli():\n        with _create_progress(label=\"working\") as progress:\n            for _ in progress:\n                pass\n\n    monkeypatch.setattr(click._termui_impl, \"isatty\", lambda _: False)\n    assert runner.invoke(cli, []).output == \"working\\n\"\n\n\ndef test_progressbar_hidden_manual(runner, monkeypatch):\n    @click.command()\n    def cli():\n        with _create_progress(label=\"see nothing\", hidden=True) as progress:\n            for _ in progress:\n                pass\n\n    monkeypatch.setattr(click._termui_impl, \"isatty\", lambda _: True)\n    assert runner.invoke(cli, []).output == \"\"\n\n\n@pytest.mark.parametrize(\"avg, expected\", [([], 0.0), ([1, 4], 2.5)])\ndef test_progressbar_time_per_iteration(runner, avg, expected):\n    with _create_progress(2, avg=avg) as progress:\n        assert progress.time_per_iteration == expected\n\n\n@pytest.mark.parametrize(\"finished, expected\", [(False, 5), (True, 0)])\ndef test_progressbar_eta(runner, finished, expected):\n    with _create_progress(2, finished=finished, avg=[1, 4]) as progress:\n        assert progress.eta == expected\n\n\n@pytest.mark.parametrize(\n    \"eta, expected\",\n    [\n        (0, \"00:00:00\"),\n        (30, \"00:00:30\"),\n        (90, \"00:01:30\"),\n        (900, \"00:15:00\"),\n        (9000, \"02:30:00\"),\n        (99999999999, \"1157407d 09:46:39\"),\n        (None, \"\"),\n    ],\n)\ndef test_progressbar_format_eta(runner, eta, expected):\n    with _create_progress(1, eta_known=eta is not None, avg=[eta]) as progress:\n        assert progress.format_eta() == expected\n\n\n@pytest.mark.parametrize(\"pos, length\", [(0, 5), (-1, 1), (5, 5), (6, 5), (4, 0)])\ndef test_progressbar_format_pos(runner, pos, length):\n    with _create_progress(length, pos=pos) as progress:\n        result = progress.format_pos()\n        assert result == f\"{pos}/{length}\"\n\n\n@pytest.mark.parametrize(\n    \"length, finished, pos, avg, expected\",\n    [\n        (8, False, 7, 0, \"#######-\"),\n        (0, True, 8, 0, \"########\"),\n    ],\n)\ndef test_progressbar_format_bar(runner, length, finished, pos, avg, expected):\n    with _create_progress(\n        length, width=8, pos=pos, finished=finished, avg=[avg]\n    ) as progress:\n        assert progress.format_bar() == expected\n\n\n@pytest.mark.parametrize(\n    \"length, show_percent, show_pos, pos, expected\",\n    [\n        (0, True, True, 0, \"  [--------]  0/0    0%\"),\n        (0, False, True, 0, \"  [--------]  0/0\"),\n        (0, False, False, 0, \"  [--------]\"),\n        (0, False, False, 0, \"  [--------]\"),\n        (8, True, True, 8, \"  [########]  8/8  100%\"),\n    ],\n)\ndef test_progressbar_format_progress_line(\n    runner, length, show_percent, show_pos, pos, expected\n):\n    with _create_progress(\n        length,\n        width=8,\n        show_percent=show_percent,\n        pos=pos,\n        show_pos=show_pos,\n    ) as progress:\n        assert progress.format_progress_line() == expected\n\n\n@pytest.mark.parametrize(\"test_item\", [\"test\", None])\ndef test_progressbar_format_progress_line_with_show_func(runner, test_item):\n    def item_show_func(item):\n        return item\n\n    with _create_progress(\n        item_show_func=item_show_func, current_item=test_item\n    ) as progress:\n        if test_item:\n            assert progress.format_progress_line().endswith(test_item)\n        else:\n            assert progress.format_progress_line().endswith(progress.format_pct())\n\n\ndef test_progressbar_init_exceptions(runner):\n    with pytest.raises(TypeError, match=\"iterable or length is required\"):\n        click.progressbar()\n\n\ndef test_progressbar_iter_outside_with_exceptions(runner):\n    progress = click.progressbar(length=2)\n\n    with pytest.raises(RuntimeError, match=\"with block\"):\n        iter(progress)\n\n\ndef test_progressbar_is_iterator(runner, monkeypatch):\n    @click.command()\n    def cli():\n        with click.progressbar(range(10), label=\"test\") as progress:\n            while True:\n                try:\n                    next(progress)\n                except StopIteration:\n                    break\n\n    monkeypatch.setattr(click._termui_impl, \"isatty\", lambda _: True)\n    result = runner.invoke(cli, [])\n    assert result.exception is None\n\n\ndef test_choices_list_in_prompt(runner, monkeypatch):\n    @click.command()\n    @click.option(\n        \"-g\", type=click.Choice([\"none\", \"day\", \"week\", \"month\"]), prompt=True\n    )\n    def cli_with_choices(g):\n        pass\n\n    @click.command()\n    @click.option(\n        \"-g\",\n        type=click.Choice([\"none\", \"day\", \"week\", \"month\"]),\n        prompt=True,\n        show_choices=False,\n    )\n    def cli_without_choices(g):\n        pass\n\n    result = runner.invoke(cli_with_choices, [], input=\"none\")\n    assert \"(none, day, week, month)\" in result.output\n\n    result = runner.invoke(cli_without_choices, [], input=\"none\")\n    assert \"(none, day, week, month)\" not in result.output\n\n\n@pytest.mark.parametrize(\n    \"file_kwargs\", [{\"mode\": \"rt\"}, {\"mode\": \"rb\"}, {\"lazy\": True}]\n)\ndef test_file_prompt_default_format(runner, file_kwargs):\n    @click.command()\n    @click.option(\"-f\", default=__file__, prompt=\"file\", type=click.File(**file_kwargs))\n    def cli(f):\n        click.echo(f.name)\n\n    result = runner.invoke(cli, input=\"\\n\")\n    assert result.output == f\"file [{__file__}]: \\n{__file__}\\n\"\n\n\ndef test_secho(runner):\n    with runner.isolation() as outstreams:\n        click.secho(None, nl=False)\n        bytes = outstreams[0].getvalue()\n        assert bytes == b\"\"\n\n\n@pytest.mark.skipif(platform.system() == \"Windows\", reason=\"No style on Windows.\")\n@pytest.mark.parametrize(\n    (\"value\", \"expect\"), [(123, b\"\\x1b[45m123\\x1b[0m\"), (b\"test\", b\"test\")]\n)\ndef test_secho_non_text(runner, value, expect):\n    with runner.isolation() as (out, _, _):\n        click.secho(value, nl=False, color=True, bg=\"magenta\")\n        result = out.getvalue()\n        assert result == expect\n\n\ndef test_progressbar_yields_all_items(runner):\n    with click.progressbar(range(3)) as progress:\n        assert len(list(progress)) == 3\n\n\ndef test_progressbar_update(runner, monkeypatch):\n    fake_clock = FakeClock()\n\n    @click.command()\n    def cli():\n        with click.progressbar(range(4)) as progress:\n            for _ in progress:\n                fake_clock.advance_time()\n                print(\"\")\n\n    monkeypatch.setattr(time, \"time\", fake_clock.time)\n    monkeypatch.setattr(click._termui_impl, \"isatty\", lambda _: True)\n    output = runner.invoke(cli, []).output\n\n    lines = [line for line in output.split(\"\\n\") if \"[\" in line]\n\n    assert \"  0%\" in lines[0]\n    assert \" 25%  00:00:03\" in lines[1]\n    assert \" 50%  00:00:02\" in lines[2]\n    assert \" 75%  00:00:01\" in lines[3]\n    assert \"100%          \" in lines[4]\n\n\ndef test_progressbar_item_show_func(runner, monkeypatch):\n    \"\"\"item_show_func should show the current item being yielded.\"\"\"\n\n    @click.command()\n    def cli():\n        with click.progressbar(range(3), item_show_func=lambda x: str(x)) as progress:\n            for item in progress:\n                click.echo(f\" item {item}\")\n\n    monkeypatch.setattr(click._termui_impl, \"isatty\", lambda _: True)\n    lines = runner.invoke(cli).output.splitlines()\n\n    for i, line in enumerate(x for x in lines if \"item\" in x):\n        assert f\"{i}    item {i}\" in line\n\n\ndef test_progressbar_update_with_item_show_func(runner, monkeypatch):\n    @click.command()\n    def cli():\n        with click.progressbar(\n            length=6, item_show_func=lambda x: f\"Custom {x}\"\n        ) as progress:\n            while not progress.finished:\n                progress.update(2, progress.pos)\n                click.echo()\n\n    monkeypatch.setattr(click._termui_impl, \"isatty\", lambda _: True)\n    output = runner.invoke(cli, []).output\n\n    lines = [line for line in output.split(\"\\n\") if \"[\" in line]\n\n    assert \"Custom 0\" in lines[0]\n    assert \"Custom 2\" in lines[1]\n    assert \"Custom 4\" in lines[2]\n\n\ndef test_progress_bar_update_min_steps(runner):\n    bar = _create_progress(update_min_steps=5)\n    bar.update(3)\n    assert bar._completed_intervals == 3\n    assert bar.pos == 0\n    bar.update(2)\n    assert bar._completed_intervals == 0\n    assert bar.pos == 5\n\n\n@pytest.mark.parametrize(\"key_char\", (\"h\", \"H\", \"é\", \"À\", \" \", \"字\", \"àH\", \"àR\"))\n@pytest.mark.parametrize(\"echo\", [True, False])\n@pytest.mark.skipif(not WIN, reason=\"Tests user-input using the msvcrt module.\")\ndef test_getchar_windows(runner, monkeypatch, key_char, echo):\n    monkeypatch.setattr(click._termui_impl.msvcrt, \"getwche\", lambda: key_char)\n    monkeypatch.setattr(click._termui_impl.msvcrt, \"getwch\", lambda: key_char)\n    monkeypatch.setattr(click.termui, \"_getchar\", None)\n    assert click.getchar(echo) == key_char\n\n\n@pytest.mark.parametrize(\n    \"special_key_char, key_char\", [(\"\\x00\", \"a\"), (\"\\x00\", \"b\"), (\"\\xe0\", \"c\")]\n)\n@pytest.mark.skipif(\n    not WIN, reason=\"Tests special character inputs using the msvcrt module.\"\n)\ndef test_getchar_special_key_windows(runner, monkeypatch, special_key_char, key_char):\n    ordered_inputs = [key_char, special_key_char]\n    monkeypatch.setattr(\n        click._termui_impl.msvcrt, \"getwch\", lambda: ordered_inputs.pop()\n    )\n    monkeypatch.setattr(click.termui, \"_getchar\", None)\n    assert click.getchar() == f\"{special_key_char}{key_char}\"\n\n\n@pytest.mark.parametrize(\n    (\"key_char\", \"exc\"), [(\"\\x03\", KeyboardInterrupt), (\"\\x1a\", EOFError)]\n)\n@pytest.mark.skipif(not WIN, reason=\"Tests user-input using the msvcrt module.\")\ndef test_getchar_windows_exceptions(runner, monkeypatch, key_char, exc):\n    monkeypatch.setattr(click._termui_impl.msvcrt, \"getwch\", lambda: key_char)\n    monkeypatch.setattr(click.termui, \"_getchar\", None)\n\n    with pytest.raises(exc):\n        click.getchar()\n\n\n@pytest.mark.skipif(platform.system() == \"Windows\", reason=\"No sed on Windows.\")\ndef test_fast_edit(runner):\n    result = click.edit(\"a\\nb\", editor=\"sed -i~ 's/$/Test/'\")\n    assert result == \"aTest\\nbTest\\n\"\n\n\n@pytest.mark.skipif(platform.system() == \"Windows\", reason=\"No sed on Windows.\")\ndef test_edit(runner):\n    with tempfile.NamedTemporaryFile(mode=\"w\") as named_tempfile:\n        named_tempfile.write(\"a\\nb\\n\")\n        named_tempfile.flush()\n\n        result = click.edit(filename=named_tempfile.name, editor=\"sed -i~ 's/$/Test/'\")\n        assert result is None\n\n        # We need ot reopen the file as it becomes unreadable after the edit.\n        with open(named_tempfile.name) as reopened_file:\n            # POSIX says that when sed writes a pattern space to output then it\n            # is immediately followed by a newline and so the expected result\n            # should contain the newline.  However, some sed implementations\n            # (e.g. GNU sed) does not terminate the last line in the output\n            # with the newline in a case the input data missed newline at the\n            # end of last line.  Hence the input data (see above) should be\n            # terminated by newline too.\n            assert reopened_file.read() == \"aTest\\nbTest\\n\"\n\n\n@pytest.mark.parametrize(\n    (\"prompt_required\", \"required\", \"args\", \"expect\"),\n    [\n        (True, False, None, \"prompt\"),\n        (True, False, [\"-v\"], \"Option '-v' requires an argument.\"),\n        (False, True, None, \"prompt\"),\n        (False, True, [\"-v\"], \"prompt\"),\n    ],\n)\ndef test_prompt_required_with_required(runner, prompt_required, required, args, expect):\n    @click.command()\n    @click.option(\"-v\", prompt=True, prompt_required=prompt_required, required=required)\n    def cli(v):\n        click.echo(str(v))\n\n    result = runner.invoke(cli, args, input=\"prompt\")\n    assert expect in result.output\n\n\n@pytest.mark.parametrize(\n    (\"args\", \"expect\"),\n    [\n        # Flag not passed, don't prompt.\n        pytest.param(None, None, id=\"no flag\"),\n        # Flag and value passed, don't prompt.\n        pytest.param([\"-v\", \"value\"], \"value\", id=\"short sep value\"),\n        pytest.param([\"--value\", \"value\"], \"value\", id=\"long sep value\"),\n        pytest.param([\"-vvalue\"], \"value\", id=\"short join value\"),\n        pytest.param([\"--value=value\"], \"value\", id=\"long join value\"),\n        # Flag without value passed, prompt.\n        pytest.param([\"-v\"], \"prompt\", id=\"short no value\"),\n        pytest.param([\"--value\"], \"prompt\", id=\"long no value\"),\n        # Don't use next option flag as value.\n        pytest.param([\"-v\", \"-o\", \"42\"], (\"prompt\", \"42\"), id=\"no value opt\"),\n    ],\n)\ndef test_prompt_required_false(runner, args, expect):\n    @click.command()\n    @click.option(\"-v\", \"--value\", prompt=True, prompt_required=False)\n    @click.option(\"-o\")\n    def cli(value, o):\n        if o is not None:\n            return value, o\n\n        return value\n\n    result = runner.invoke(cli, args=args, input=\"prompt\", standalone_mode=False)\n    assert result.exception is None\n    assert result.return_value == expect\n\n\n@pytest.mark.parametrize(\n    (\"prompt\", \"input\", \"default\", \"expect\"),\n    [\n        (True, \"password\\npassword\", None, \"password\"),\n        (\"Confirm Password\", \"password\\npassword\\n\", None, \"password\"),\n        (True, \"\\n\\n\", \"\", \"\"),\n        (False, None, None, None),\n    ],\n)\ndef test_confirmation_prompt(runner, prompt, input, default, expect):\n    @click.command()\n    @click.option(\n        \"--password\",\n        prompt=prompt,\n        hide_input=True,\n        default=default,\n        confirmation_prompt=prompt,\n    )\n    def cli(password):\n        return password\n\n    result = runner.invoke(cli, input=input, standalone_mode=False)\n    assert result.exception is None\n    assert result.return_value == expect\n\n    if prompt == \"Confirm Password\":\n        assert \"Confirm Password: \" in result.output\n\n\ndef test_false_show_default_cause_no_default_display_in_prompt(runner):\n    @click.command()\n    @click.option(\"--arg1\", show_default=False, prompt=True, default=\"my-default-value\")\n    def cmd(arg1):\n        pass\n\n    # Confirm that the default value is not included in the output when `show_default`\n    # is False\n    result = runner.invoke(cmd, input=\"my-input\", standalone_mode=False)\n    assert \"my-default-value\" not in result.output\n\n\nREPEAT = object()\n\"\"\"Sentinel value to indicate that the prompt is expected to be repeated.\n\nI.e. the value provided by the user is not satisfactory and need to be re-prompted.\n\"\"\"\n\nINVALID = object()\n\"\"\"Sentinel value to indicate that the prompt is expected to be invalid.\n\nOn invalid input, Click will output an error message and re-prompt the user.\n\"\"\"\n\nBOOLEAN_FLAG_PROMPT_CASES = [\n    ###\n    ### Test cases with prompt=True explicitly enabled for the flag.\n    ###\n    # Prompt is allowed and the flag has no default, so it prompts.\n    ({\"prompt\": True}, [], \"[y/N]\", \"y\", True),\n    ({\"prompt\": True}, [], \"[y/N]\", \"n\", False),\n    # Empty input default to False.\n    ({\"prompt\": True}, [], \"[y/N]\", \"\", False),\n    # Changing the default to True, makes the prompt change to [Y/n].\n    ({\"prompt\": True, \"default\": True}, [], \"[Y/n]\", \"\", True),\n    ({\"prompt\": True, \"default\": True}, [], \"[Y/n]\", \"y\", True),\n    ({\"prompt\": True, \"default\": True}, [], \"[Y/n]\", \"n\", False),\n    # False is the default's default, so it prompts with [y/N].\n    ({\"prompt\": True, \"default\": False}, [], \"[y/N]\", \"\", False),\n    ({\"prompt\": True, \"default\": False}, [], \"[y/N]\", \"y\", True),\n    ({\"prompt\": True, \"default\": False}, [], \"[y/N]\", \"n\", False),\n    # Defaulting to None, prompts with [y/n], which makes the user explicitly choose\n    # between True or False.\n    ({\"prompt\": True, \"default\": None}, [], \"[y/n]\", \"y\", True),\n    ({\"prompt\": True, \"default\": None}, [], \"[y/n]\", \"n\", False),\n    # Random string default is treated as a truthy value, so it prompts with [Y/n].\n    ({\"prompt\": True, \"default\": \"foo\"}, [], \"[Y/n]\", \"\", True),\n    ({\"prompt\": True, \"default\": \"foo\"}, [], \"[Y/n]\", \"y\", True),\n    ({\"prompt\": True, \"default\": \"foo\"}, [], \"[Y/n]\", \"n\", False),\n    ###\n    ### Test cases with required=True explicitly enabled for the flag.\n    ###\n    # A required flag just raises an error unless a default is set.\n    ({\"required\": True}, [], None, None, MissingParameter),\n    ({\"required\": True, \"default\": True}, [], None, None, True),\n    ({\"required\": True, \"default\": False}, [], None, None, False),\n    ({\"required\": True, \"default\": None}, [], None, None, None),\n    ({\"required\": True, \"default\": \"on\"}, [], None, None, True),\n    ({\"required\": True, \"default\": \"off\"}, [], None, None, False),\n    ({\"required\": True, \"default\": \"foo\"}, [], None, None, BadParameter),\n    ###\n    ### Explicitly passing the flag to the CLI bypass any prompt, whatever the\n    ### configuration of the flag.\n    ###\n    # Flag allowing a prompt.\n    ({\"prompt\": True}, [\"--flag\"], None, None, True),\n    ({\"prompt\": True}, [\"--no-flag\"], None, None, False),\n    ({\"prompt\": True, \"default\": None}, [\"--flag\"], None, None, True),\n    ({\"prompt\": True, \"default\": None}, [\"--no-flag\"], None, None, False),\n    ({\"prompt\": True, \"default\": True}, [\"--flag\"], None, None, True),\n    ({\"prompt\": True, \"default\": True}, [\"--no-flag\"], None, None, False),\n    ({\"prompt\": True, \"default\": False}, [\"--flag\"], None, None, True),\n    ({\"prompt\": True, \"default\": False}, [\"--no-flag\"], None, None, False),\n    ({\"prompt\": True, \"default\": \"foo\"}, [\"--flag\"], None, None, True),\n    ({\"prompt\": True, \"default\": \"foo\"}, [\"--no-flag\"], None, None, False),\n    # Required flag.\n    ({\"required\": True}, [\"--flag\"], None, None, True),\n    ({\"required\": True}, [\"--no-flag\"], None, None, False),\n    ({\"required\": True, \"default\": None}, [\"--flag\"], None, None, True),\n    ({\"required\": True, \"default\": None}, [\"--no-flag\"], None, None, False),\n    ({\"required\": True, \"default\": True}, [\"--flag\"], None, None, True),\n    ({\"required\": True, \"default\": True}, [\"--no-flag\"], None, None, False),\n    ({\"required\": True, \"default\": False}, [\"--flag\"], None, None, True),\n    ({\"required\": True, \"default\": False}, [\"--no-flag\"], None, None, False),\n    ({\"required\": True, \"default\": \"foo\"}, [\"--flag\"], None, None, True),\n    ({\"required\": True, \"default\": \"foo\"}, [\"--no-flag\"], None, None, False),\n]\n\nFLAG_VALUE_PROMPT_CASES = [\n    ###\n    ### Test cases with prompt=True explicitly enabled for the flag.\n    ###\n    # Prompt is allowed and the flag has no default, so it prompts.\n    # But the flag_value is not set, so it defaults to a string.\n    ({\"prompt\": True}, [], \"\", \"\", REPEAT),\n    ({\"prompt\": True}, [], \"\", \"y\", \"y\"),\n    ({\"prompt\": True}, [], \"\", \"n\", \"n\"),\n    ({\"prompt\": True}, [], \"\", \"foo\", \"foo\"),\n    # This time we provide a boolean flag_value, which makes the flag behave like a\n    # boolean flag, and use the appropriate variation of [y/n].\n    ({\"prompt\": True, \"flag_value\": True}, [], \"[y/N]\", \"\", False),\n    ({\"prompt\": True, \"flag_value\": True}, [], \"[y/N]\", \"y\", True),\n    ({\"prompt\": True, \"flag_value\": True}, [], \"[y/N]\", \"n\", False),\n    ({\"prompt\": True, \"flag_value\": False}, [], \"[y/N]\", \"\", False),\n    ({\"prompt\": True, \"flag_value\": False}, [], \"[y/N]\", \"y\", True),\n    ({\"prompt\": True, \"flag_value\": False}, [], \"[y/N]\", \"n\", False),\n    # Other flag values changes the auto-detection of the flag type.\n    ({\"prompt\": True, \"flag_value\": None}, [], \"\", \"\", REPEAT),\n    ({\"prompt\": True, \"flag_value\": None}, [], \"\", \"y\", \"y\"),\n    ({\"prompt\": True, \"flag_value\": None}, [], \"\", \"n\", \"n\"),\n    ({\"prompt\": True, \"flag_value\": \"foo\"}, [], \"\", \"\", REPEAT),\n    ({\"prompt\": True, \"flag_value\": \"foo\"}, [], \"\", \"y\", \"y\"),\n    ({\"prompt\": True, \"flag_value\": \"foo\"}, [], \"\", \"n\", \"n\"),\n    ###\n    ### Test cases with a flag_value and a default.\n    ###\n    # default=True\n    ({\"prompt\": True, \"default\": True, \"flag_value\": True}, [], \"[Y/n]\", \"\", True),\n    ({\"prompt\": True, \"default\": True, \"flag_value\": True}, [], \"[Y/n]\", \"y\", True),\n    ({\"prompt\": True, \"default\": True, \"flag_value\": True}, [], \"[Y/n]\", \"n\", False),\n    ({\"prompt\": True, \"default\": True, \"flag_value\": False}, [], \"[y/N]\", \"\", False),\n    ({\"prompt\": True, \"default\": True, \"flag_value\": False}, [], \"[y/N]\", \"y\", True),\n    ({\"prompt\": True, \"default\": True, \"flag_value\": False}, [], \"[y/N]\", \"n\", False),\n    # default=False\n    ({\"prompt\": True, \"default\": False, \"flag_value\": True}, [], \"[y/N]\", \"\", False),\n    ({\"prompt\": True, \"default\": False, \"flag_value\": True}, [], \"[y/N]\", \"y\", True),\n    ({\"prompt\": True, \"default\": False, \"flag_value\": True}, [], \"[y/N]\", \"n\", False),\n    ({\"prompt\": True, \"default\": False, \"flag_value\": False}, [], \"[y/N]\", \"\", False),\n    ({\"prompt\": True, \"default\": False, \"flag_value\": False}, [], \"[y/N]\", \"y\", True),\n    ({\"prompt\": True, \"default\": False, \"flag_value\": False}, [], \"[y/N]\", \"n\", False),\n    # default=None\n    (\n        {\"prompt\": True, \"default\": None, \"flag_value\": True},\n        [],\n        \"[y/n]\",\n        \"\",\n        INVALID,\n    ),\n    ({\"prompt\": True, \"default\": None, \"flag_value\": True}, [], \"[y/n]\", \"y\", True),\n    ({\"prompt\": True, \"default\": None, \"flag_value\": True}, [], \"[y/n]\", \"n\", False),\n    (\n        {\"prompt\": True, \"default\": None, \"flag_value\": False},\n        [],\n        \"[y/n]\",\n        \"\",\n        INVALID,\n    ),\n    ({\"prompt\": True, \"default\": None, \"flag_value\": False}, [], \"[y/n]\", \"y\", True),\n    ({\"prompt\": True, \"default\": None, \"flag_value\": False}, [], \"[y/n]\", \"n\", False),\n    # If the flag_value is None, the flag behave like a string flag, whatever the\n    # default is.\n    ({\"prompt\": True, \"default\": True, \"flag_value\": None}, [], \"\", \"\", REPEAT),\n    ({\"prompt\": True, \"default\": True, \"flag_value\": None}, [], \"\", \"y\", \"y\"),\n    ({\"prompt\": True, \"default\": True, \"flag_value\": None}, [], \"\", \"n\", \"n\"),\n    (\n        {\"prompt\": True, \"default\": False, \"flag_value\": None},\n        [],\n        \"[False]\",\n        \"\",\n        \"False\",\n    ),\n    ({\"prompt\": True, \"default\": False, \"flag_value\": None}, [], \"[False]\", \"y\", \"y\"),\n    ({\"prompt\": True, \"default\": False, \"flag_value\": None}, [], \"[False]\", \"n\", \"n\"),\n    ({\"prompt\": True, \"default\": None, \"flag_value\": None}, [], \"\", \"\", REPEAT),\n    ({\"prompt\": True, \"default\": None, \"flag_value\": None}, [], \"\", \"y\", \"y\"),\n    ({\"prompt\": True, \"default\": None, \"flag_value\": None}, [], \"\", \"n\", \"n\"),\n]\n\n\n@pytest.mark.parametrize(\n    (\"opt_decls\", \"opt_params\", \"args\", \"prompt\", \"input\", \"expected\"),\n    # Boolean flag prompt cases.\n    [(\"--flag/--no-flag\", *case_params) for case_params in BOOLEAN_FLAG_PROMPT_CASES]\n    # Non-boolean flag prompt cases.\n    + [(\"--flag\", *case_params) for case_params in FLAG_VALUE_PROMPT_CASES],\n)\ndef test_flag_value_prompt(\n    runner, opt_decls, opt_params, args, prompt, input, expected\n):\n    \"\"\"Check how flag value are prompted and handled by all combinations of\n    ``prompt``, ``default``, and ``flag_value`` parameters.\n\n    Covers concerns raised in issue https://github.com/pallets/click/issues/1992.\n    \"\"\"\n\n    @click.command()\n    @click.option(opt_decls, **opt_params)\n    def cli(flag):\n        click.echo(repr(flag))\n\n    invoke_options = {\"standalone_mode\": False}\n    if input is not None:\n        assert isinstance(input, str)\n        invoke_options[\"input\"] = f\"{input}\\n\"\n\n    result = runner.invoke(cli, args, **invoke_options)\n\n    if expected in (MissingParameter, BadParameter):\n        assert isinstance(result.exception, expected)\n        assert not result.output\n        assert result.exit_code == 1\n\n    else:\n        expected_output = \"\"\n        if prompt is not None:\n            # Build the expected prompt.\n            assert isinstance(prompt, str)\n            expected_prompt = f\"Flag {prompt}: \" if prompt else \"Flag: \"\n\n            # Add the user input to the expected output.\n            assert isinstance(input, str)\n            expected_output += f\"{expected_prompt}{input}\\n\"\n\n            if expected is INVALID:\n                expected_output += \"Error: invalid input\\n\"\n\n            # The prompt is expected to be repeated.\n            if expected in (REPEAT, INVALID):\n                expected_output += expected_prompt\n\n        if expected not in (REPEAT, INVALID):\n            expected_output += f\"{expected!r}\\n\"\n\n        assert result.output == expected_output\n        assert not result.stderr\n        assert result.exit_code == 0 if expected not in (REPEAT, INVALID) else 1\n"
  },
  {
    "path": "tests/test_testing.py",
    "content": "import os\nimport sys\nfrom io import BytesIO\n\nimport pytest\n\nimport click\nfrom click.exceptions import ClickException\nfrom click.testing import CliRunner\n\n\ndef test_runner():\n    @click.command()\n    def test():\n        i = click.get_binary_stream(\"stdin\")\n        o = click.get_binary_stream(\"stdout\")\n        while True:\n            chunk = i.read(4096)\n            if not chunk:\n                break\n            o.write(chunk)\n            o.flush()\n\n    runner = CliRunner()\n    result = runner.invoke(test, input=\"Hello World!\\n\")\n    assert not result.exception\n    assert result.output == \"Hello World!\\n\"\n\n\ndef test_echo_stdin_stream():\n    @click.command()\n    def test():\n        i = click.get_binary_stream(\"stdin\")\n        o = click.get_binary_stream(\"stdout\")\n        while True:\n            chunk = i.read(4096)\n            if not chunk:\n                break\n            o.write(chunk)\n            o.flush()\n\n    runner = CliRunner(echo_stdin=True)\n    result = runner.invoke(test, input=\"Hello World!\\n\")\n    assert not result.exception\n    assert result.output == \"Hello World!\\nHello World!\\n\"\n\n\ndef test_echo_stdin_prompts():\n    @click.command()\n    def test_python_input():\n        foo = input(\"Foo: \")\n        click.echo(f\"foo={foo}\")\n\n    runner = CliRunner(echo_stdin=True)\n    result = runner.invoke(test_python_input, input=\"bar bar\\n\")\n    assert not result.exception\n    assert result.output == \"Foo: bar bar\\nfoo=bar bar\\n\"\n\n    @click.command()\n    @click.option(\"--foo\", prompt=True)\n    def test_prompt(foo):\n        click.echo(f\"foo={foo}\")\n\n    result = runner.invoke(test_prompt, input=\"bar bar\\n\")\n    assert not result.exception\n    assert result.output == \"Foo: bar bar\\nfoo=bar bar\\n\"\n\n    @click.command()\n    @click.option(\"--foo\", prompt=True, hide_input=True)\n    def test_hidden_prompt(foo):\n        click.echo(f\"foo={foo}\")\n\n    result = runner.invoke(test_hidden_prompt, input=\"bar bar\\n\")\n    assert not result.exception\n    assert result.output == \"Foo: \\nfoo=bar bar\\n\"\n\n    @click.command()\n    @click.option(\"--foo\", prompt=True)\n    @click.option(\"--bar\", prompt=True)\n    def test_multiple_prompts(foo, bar):\n        click.echo(f\"foo={foo}, bar={bar}\")\n\n    result = runner.invoke(test_multiple_prompts, input=\"one\\ntwo\\n\")\n    assert not result.exception\n    assert result.output == \"Foo: one\\nBar: two\\nfoo=one, bar=two\\n\"\n\n\ndef test_runner_with_stream():\n    @click.command()\n    def test():\n        i = click.get_binary_stream(\"stdin\")\n        o = click.get_binary_stream(\"stdout\")\n        while True:\n            chunk = i.read(4096)\n            if not chunk:\n                break\n            o.write(chunk)\n            o.flush()\n\n    runner = CliRunner()\n    result = runner.invoke(test, input=BytesIO(b\"Hello World!\\n\"))\n    assert not result.exception\n    assert result.output == \"Hello World!\\n\"\n\n    runner = CliRunner(echo_stdin=True)\n    result = runner.invoke(test, input=BytesIO(b\"Hello World!\\n\"))\n    assert not result.exception\n    assert result.output == \"Hello World!\\nHello World!\\n\"\n\n\ndef test_prompts():\n    @click.command()\n    @click.option(\"--foo\", prompt=True)\n    def test(foo):\n        click.echo(f\"foo={foo}\")\n\n    runner = CliRunner()\n    result = runner.invoke(test, input=\"wau wau\\n\")\n    assert not result.exception\n    assert result.output == \"Foo: wau wau\\nfoo=wau wau\\n\"\n\n    @click.command()\n    @click.option(\"--foo\", prompt=True, hide_input=True)\n    def test(foo):\n        click.echo(f\"foo={foo}\")\n\n    runner = CliRunner()\n    result = runner.invoke(test, input=\"wau wau\\n\")\n    assert not result.exception\n    assert result.output == \"Foo: \\nfoo=wau wau\\n\"\n\n\ndef test_getchar():\n    @click.command()\n    def continue_it():\n        click.echo(click.getchar())\n\n    runner = CliRunner()\n    result = runner.invoke(continue_it, input=\"y\")\n    assert not result.exception\n    assert result.output == \"y\\n\"\n\n    runner = CliRunner(echo_stdin=True)\n    result = runner.invoke(continue_it, input=\"y\")\n    assert not result.exception\n    assert result.output == \"y\\n\"\n\n    @click.command()\n    def getchar_echo():\n        click.echo(click.getchar(echo=True))\n\n    runner = CliRunner()\n    result = runner.invoke(getchar_echo, input=\"y\")\n    assert not result.exception\n    assert result.output == \"yy\\n\"\n\n    runner = CliRunner(echo_stdin=True)\n    result = runner.invoke(getchar_echo, input=\"y\")\n    assert not result.exception\n    assert result.output == \"yy\\n\"\n\n\ndef test_catch_exceptions():\n    class CustomError(Exception):\n        pass\n\n    @click.command()\n    def cli():\n        raise CustomError(1)\n\n    runner = CliRunner()\n\n    result = runner.invoke(cli)\n    assert isinstance(result.exception, CustomError)\n    assert type(result.exc_info) is tuple\n    assert len(result.exc_info) == 3\n\n    with pytest.raises(CustomError):\n        runner.invoke(cli, catch_exceptions=False)\n\n    CustomError = SystemExit\n\n    result = runner.invoke(cli)\n    assert result.exit_code == 1\n\n\ndef test_catch_exceptions_cli_runner():\n    \"\"\"Test that invoke `catch_exceptions` takes the value from CliRunner if not set\n    explicitly.\"\"\"\n\n    class CustomError(Exception):\n        pass\n\n    @click.command()\n    def cli():\n        raise CustomError(1)\n\n    runner = CliRunner(catch_exceptions=False)\n\n    result = runner.invoke(cli, catch_exceptions=True)\n    assert isinstance(result.exception, CustomError)\n    assert type(result.exc_info) is tuple\n    assert len(result.exc_info) == 3\n\n    with pytest.raises(CustomError):\n        runner.invoke(cli)\n\n\ndef test_with_color():\n    @click.command()\n    def cli():\n        click.secho(\"hello world\", fg=\"blue\")\n\n    runner = CliRunner()\n\n    result = runner.invoke(cli)\n    assert result.output == \"hello world\\n\"\n    assert not result.exception\n\n    result = runner.invoke(cli, color=True)\n    assert result.output == f\"{click.style('hello world', fg='blue')}\\n\"\n    assert not result.exception\n\n\ndef test_with_color_errors():\n    class CLIError(ClickException):\n        def format_message(self) -> str:\n            return click.style(self.message, fg=\"red\")\n\n    @click.command()\n    def cli():\n        raise CLIError(\"Red error\")\n\n    runner = CliRunner()\n\n    result = runner.invoke(cli)\n    assert result.output == \"Error: Red error\\n\"\n    assert result.exception\n\n    result = runner.invoke(cli, color=True)\n    assert result.output == f\"Error: {click.style('Red error', fg='red')}\\n\"\n    assert result.exception\n\n\ndef test_with_color_but_pause_not_blocking():\n    @click.command()\n    def cli():\n        click.pause()\n\n    runner = CliRunner()\n    result = runner.invoke(cli, color=True)\n    assert not result.exception\n    assert result.output == \"\"\n\n\ndef test_exit_code_and_output_from_sys_exit():\n    # See issue #362\n    @click.command()\n    def cli_string():\n        click.echo(\"hello world\")\n        sys.exit(\"error\")\n\n    @click.command()\n    @click.pass_context\n    def cli_string_ctx_exit(ctx):\n        click.echo(\"hello world\")\n        ctx.exit(\"error\")\n\n    @click.command()\n    def cli_int():\n        click.echo(\"hello world\")\n        sys.exit(1)\n\n    @click.command()\n    @click.pass_context\n    def cli_int_ctx_exit(ctx):\n        click.echo(\"hello world\")\n        ctx.exit(1)\n\n    @click.command()\n    def cli_float():\n        click.echo(\"hello world\")\n        sys.exit(1.0)\n\n    @click.command()\n    @click.pass_context\n    def cli_float_ctx_exit(ctx):\n        click.echo(\"hello world\")\n        ctx.exit(1.0)\n\n    @click.command()\n    def cli_no_error():\n        click.echo(\"hello world\")\n\n    runner = CliRunner()\n\n    result = runner.invoke(cli_string)\n    assert result.exit_code == 1\n    assert result.output == \"hello world\\nerror\\n\"\n\n    result = runner.invoke(cli_string_ctx_exit)\n    assert result.exit_code == 1\n    assert result.output == \"hello world\\nerror\\n\"\n\n    result = runner.invoke(cli_int)\n    assert result.exit_code == 1\n    assert result.output == \"hello world\\n\"\n\n    result = runner.invoke(cli_int_ctx_exit)\n    assert result.exit_code == 1\n    assert result.output == \"hello world\\n\"\n\n    result = runner.invoke(cli_float)\n    assert result.exit_code == 1\n    assert result.output == \"hello world\\n1.0\\n\"\n\n    result = runner.invoke(cli_float_ctx_exit)\n    assert result.exit_code == 1\n    assert result.output == \"hello world\\n1.0\\n\"\n\n    result = runner.invoke(cli_no_error)\n    assert result.exit_code == 0\n    assert result.output == \"hello world\\n\"\n\n\ndef test_env():\n    @click.command()\n    def cli_env():\n        click.echo(f\"ENV={os.environ['TEST_CLICK_ENV']}\")\n\n    runner = CliRunner()\n\n    env_orig = dict(os.environ)\n    env = dict(env_orig)\n    assert \"TEST_CLICK_ENV\" not in env\n    env[\"TEST_CLICK_ENV\"] = \"some_value\"\n    result = runner.invoke(cli_env, env=env)\n    assert result.exit_code == 0\n    assert result.output == \"ENV=some_value\\n\"\n\n    assert os.environ == env_orig\n\n\ndef test_stderr():\n    @click.command()\n    def cli_stderr():\n        click.echo(\"1 - stdout\")\n        click.echo(\"2 - stderr\", err=True)\n        click.echo(\"3 - stdout\")\n        click.echo(\"4 - stderr\", err=True)\n\n    runner_mix = CliRunner()\n    result_mix = runner_mix.invoke(cli_stderr)\n\n    assert result_mix.output == \"1 - stdout\\n2 - stderr\\n3 - stdout\\n4 - stderr\\n\"\n    assert result_mix.stdout == \"1 - stdout\\n3 - stdout\\n\"\n    assert result_mix.stderr == \"2 - stderr\\n4 - stderr\\n\"\n\n    @click.command()\n    def cli_empty_stderr():\n        click.echo(\"stdout\")\n\n    runner = CliRunner()\n    result = runner.invoke(cli_empty_stderr)\n\n    assert result.output == \"stdout\\n\"\n    assert result.stdout == \"stdout\\n\"\n    assert result.stderr == \"\"\n\n\n@pytest.mark.parametrize(\n    \"args, expected_output\",\n    [\n        (None, \"bar\\n\"),\n        ([], \"bar\\n\"),\n        (\"\", \"bar\\n\"),\n        ([\"--foo\", \"one two\"], \"one two\\n\"),\n        ('--foo \"one two\"', \"one two\\n\"),\n    ],\n)\ndef test_args(args, expected_output):\n    @click.command()\n    @click.option(\"--foo\", default=\"bar\")\n    def cli_args(foo):\n        click.echo(foo)\n\n    runner = CliRunner()\n    result = runner.invoke(cli_args, args=args)\n    assert result.exit_code == 0\n    assert result.output == expected_output\n\n\ndef test_setting_prog_name_in_extra():\n    @click.command()\n    def cli():\n        click.echo(\"ok\")\n\n    runner = CliRunner()\n    result = runner.invoke(cli, prog_name=\"foobar\")\n    assert not result.exception\n    assert result.output == \"ok\\n\"\n\n\ndef test_command_standalone_mode_returns_value():\n    @click.command()\n    def cli():\n        click.echo(\"ok\")\n        return \"Hello, World!\"\n\n    runner = CliRunner()\n    result = runner.invoke(cli, standalone_mode=False)\n    assert result.output == \"ok\\n\"\n    assert result.return_value == \"Hello, World!\"\n    assert result.exit_code == 0\n\n\ndef test_file_stdin_attrs(runner):\n    @click.command()\n    @click.argument(\"f\", type=click.File())\n    def cli(f):\n        click.echo(f.name)\n        click.echo(f.mode, nl=False)\n\n    result = runner.invoke(cli, [\"-\"])\n    assert result.output == \"<stdin>\\nr\"\n\n\ndef test_isolated_runner(runner):\n    with runner.isolated_filesystem() as d:\n        assert os.path.exists(d)\n\n    assert not os.path.exists(d)\n\n\ndef test_isolated_runner_custom_tempdir(runner, tmp_path):\n    with runner.isolated_filesystem(temp_dir=tmp_path) as d:\n        assert os.path.exists(d)\n\n    assert os.path.exists(d)\n    os.rmdir(d)\n\n\ndef test_isolation_stderr_errors():\n    \"\"\"Writing to stderr should escape invalid characters instead of\n    raising a UnicodeEncodeError.\n    \"\"\"\n    runner = CliRunner()\n\n    with runner.isolation() as (_, err, _):\n        click.echo(\"\\udce2\", err=True, nl=False)\n        assert err.getvalue() == b\"\\\\udce2\"\n\n\ndef test_isolation_flushes_unflushed_stderr():\n    \"\"\"An un-flushed write to stderr, as with `print(..., file=sys.stderr)`, will end up\n    flushed by the runner at end of invocation.\n    \"\"\"\n    runner = CliRunner()\n\n    with runner.isolation() as (_, err, _):\n        click.echo(\"\\udce2\", err=True, nl=False)\n        assert err.getvalue() == b\"\\\\udce2\"\n\n    @click.command()\n    def cli():\n        # set end=\"\", flush=False so that it's totally clear that we won't get any\n        # auto-flush behaviors\n        print(\"gyarados gyarados gyarados\", file=sys.stderr, end=\"\", flush=False)\n\n    result = runner.invoke(cli)\n    assert result.stderr == \"gyarados gyarados gyarados\"\n"
  },
  {
    "path": "tests/test_types.py",
    "content": "import os.path\nimport pathlib\nimport platform\nimport tempfile\n\nimport pytest\n\nimport click\nfrom click import FileError\n\n\n@pytest.mark.parametrize(\n    (\"type\", \"value\", \"expect\"),\n    [\n        (click.IntRange(0, 5), \"3\", 3),\n        (click.IntRange(5), \"5\", 5),\n        (click.IntRange(5), \"100\", 100),\n        (click.IntRange(max=5), \"5\", 5),\n        (click.IntRange(max=5), \"-100\", -100),\n        (click.IntRange(0, clamp=True), \"-1\", 0),\n        (click.IntRange(max=5, clamp=True), \"6\", 5),\n        (click.IntRange(0, min_open=True, clamp=True), \"0\", 1),\n        (click.IntRange(max=5, max_open=True, clamp=True), \"5\", 4),\n        (click.FloatRange(0.5, 1.5), \"1.2\", 1.2),\n        (click.FloatRange(0.5, min_open=True), \"0.51\", 0.51),\n        (click.FloatRange(max=1.5, max_open=True), \"1.49\", 1.49),\n        (click.FloatRange(0.5, clamp=True), \"-0.0\", 0.5),\n        (click.FloatRange(max=1.5, clamp=True), \"inf\", 1.5),\n    ],\n)\ndef test_range(type, value, expect):\n    assert type.convert(value, None, None) == expect\n\n\n@pytest.mark.parametrize(\n    (\"type\", \"value\", \"expect\"),\n    [\n        (click.IntRange(0, 5), \"6\", \"6 is not in the range 0<=x<=5.\"),\n        (click.IntRange(5), \"4\", \"4 is not in the range x>=5.\"),\n        (click.IntRange(max=5), \"6\", \"6 is not in the range x<=5.\"),\n        (click.IntRange(0, 5, min_open=True), 0, \"0<x<=5\"),\n        (click.IntRange(0, 5, max_open=True), 5, \"0<=x<5\"),\n        (click.FloatRange(0.5, min_open=True), 0.5, \"x>0.5\"),\n        (click.FloatRange(max=1.5, max_open=True), 1.5, \"x<1.5\"),\n    ],\n)\ndef test_range_fail(type, value, expect):\n    with pytest.raises(click.BadParameter) as exc_info:\n        type.convert(value, None, None)\n\n    assert expect in exc_info.value.message\n\n\ndef test_float_range_no_clamp_open():\n    with pytest.raises(TypeError):\n        click.FloatRange(0, 1, max_open=True, clamp=True)\n\n    sneaky = click.FloatRange(0, 1, max_open=True)\n    sneaky.clamp = True\n\n    with pytest.raises(RuntimeError):\n        sneaky.convert(\"1.5\", None, None)\n\n\n@pytest.mark.parametrize(\n    (\"nargs\", \"multiple\", \"default\", \"expect\"),\n    [\n        (2, False, None, None),\n        (2, False, (None, None), (None, None)),\n        (None, True, None, ()),\n        (None, True, (None, None), (None, None)),\n        (2, True, None, ()),\n        (2, True, [(None, None)], ((None, None),)),\n        (-1, None, None, ()),\n    ],\n)\ndef test_cast_multi_default(runner, nargs, multiple, default, expect):\n    if nargs == -1:\n        param = click.Argument([\"a\"], nargs=nargs, default=default)\n    else:\n        param = click.Option([\"-a\"], nargs=nargs, multiple=multiple, default=default)\n\n    cli = click.Command(\"cli\", params=[param], callback=lambda a: a)\n    result = runner.invoke(cli, standalone_mode=False)\n    assert result.exception is None\n    assert result.return_value == expect\n\n\n@pytest.mark.parametrize(\n    (\"cls\", \"expect\"),\n    [\n        (None, \"a/b/c.txt\"),\n        (str, \"a/b/c.txt\"),\n        (bytes, b\"a/b/c.txt\"),\n        (pathlib.Path, pathlib.Path(\"a\", \"b\", \"c.txt\")),\n    ],\n)\ndef test_path_type(runner, cls, expect):\n    cli = click.Command(\n        \"cli\",\n        params=[click.Argument([\"p\"], type=click.Path(path_type=cls))],\n        callback=lambda p: p,\n    )\n    result = runner.invoke(cli, [\"a/b/c.txt\"], standalone_mode=False)\n    assert result.exception is None\n    assert result.return_value == expect\n\n\ndef _symlinks_supported():\n    with tempfile.TemporaryDirectory(prefix=\"click-pytest-\") as tempdir:\n        target = os.path.join(tempdir, \"target\")\n        open(target, \"w\").close()\n        link = os.path.join(tempdir, \"link\")\n\n        try:\n            os.symlink(target, link)\n            return True\n        except OSError:\n            return False\n\n\n@pytest.mark.skipif(\n    not _symlinks_supported(), reason=\"The current OS or FS doesn't support symlinks.\"\n)\ndef test_path_resolve_symlink(tmp_path, runner):\n    test_file = tmp_path / \"file\"\n    test_file_str = os.fspath(test_file)\n    test_file.write_text(\"\")\n\n    path_type = click.Path(resolve_path=True)\n    param = click.Argument([\"a\"], type=path_type)\n    ctx = click.Context(click.Command(\"cli\", params=[param]))\n\n    test_dir = tmp_path / \"dir\"\n    test_dir.mkdir()\n\n    abs_link = test_dir / \"abs\"\n    abs_link.symlink_to(test_file)\n    abs_rv = path_type.convert(os.fspath(abs_link), param, ctx)\n    assert abs_rv == test_file_str\n\n    rel_link = test_dir / \"rel\"\n    rel_link.symlink_to(pathlib.Path(\"..\") / \"file\")\n    rel_rv = path_type.convert(os.fspath(rel_link), param, ctx)\n    assert rel_rv == test_file_str\n\n\ndef _non_utf8_filenames_supported():\n    with tempfile.TemporaryDirectory(prefix=\"click-pytest-\") as tempdir:\n        try:\n            f = open(os.path.join(tempdir, \"\\udcff\"), \"w\")\n        except OSError:\n            return False\n\n        f.close()\n        return True\n\n\n@pytest.mark.skipif(\n    not _non_utf8_filenames_supported(),\n    reason=\"The current OS or FS doesn't support non-UTF-8 filenames.\",\n)\ndef test_path_surrogates(tmp_path, monkeypatch):\n    monkeypatch.chdir(tmp_path)\n    type = click.Path(exists=True)\n    path = pathlib.Path(\"\\udcff\")\n\n    with pytest.raises(click.BadParameter, match=\"'�' does not exist\"):\n        type.convert(path, None, None)\n\n    type = click.Path(file_okay=False)\n    path.touch()\n\n    with pytest.raises(click.BadParameter, match=\"'�' is a file\"):\n        type.convert(path, None, None)\n\n    path.unlink()\n    type = click.Path(dir_okay=False)\n    path.mkdir()\n\n    with pytest.raises(click.BadParameter, match=\"'�' is a directory\"):\n        type.convert(path, None, None)\n\n    path.rmdir()\n\n    def no_access(*args, **kwargs):\n        \"\"\"Test environments may be running as root, so we have to fake the result of\n        the access tests that use os.access\n        \"\"\"\n        p = args[0]\n        assert p == path, f\"unexpected os.access call on file not under test: {p!r}\"\n        return False\n\n    path.touch()\n    type = click.Path(readable=True)\n\n    with pytest.raises(click.BadParameter, match=\"'�' is not readable\"):\n        with monkeypatch.context() as m:\n            m.setattr(os, \"access\", no_access)\n            type.convert(path, None, None)\n\n    type = click.Path(readable=False, writable=True)\n\n    with pytest.raises(click.BadParameter, match=\"'�' is not writable\"):\n        with monkeypatch.context() as m:\n            m.setattr(os, \"access\", no_access)\n            type.convert(path, None, None)\n\n    type = click.Path(readable=False, executable=True)\n\n    with pytest.raises(click.BadParameter, match=\"'�' is not executable\"):\n        with monkeypatch.context() as m:\n            m.setattr(os, \"access\", no_access)\n            type.convert(path, None, None)\n\n    path.unlink()\n\n\n@pytest.mark.parametrize(\n    \"type\",\n    [\n        click.File(mode=\"r\"),\n        click.File(mode=\"r\", lazy=True),\n    ],\n)\ndef test_file_surrogates(type, tmp_path):\n    path = tmp_path / \"\\udcff\"\n\n    # - common case: �': No such file or directory\n    # - special case: Illegal byte sequence\n    # The spacial case is seen with rootless Podman. The root cause is most\n    # likely that the path is handled by a user-space program (FUSE).\n    match = r\"(�': No such file or directory|Illegal byte sequence)\"\n    with pytest.raises(click.BadParameter, match=match):\n        type.convert(path, None, None)\n\n\ndef test_file_error_surrogates():\n    message = FileError(filename=\"\\udcff\").format_message()\n    assert message == \"Could not open file '�': unknown error\"\n\n\n@pytest.mark.skipif(\n    platform.system() == \"Windows\", reason=\"Filepath syntax differences.\"\n)\ndef test_invalid_path_with_esc_sequence():\n    with pytest.raises(click.BadParameter) as exc_info:\n        with tempfile.TemporaryDirectory(prefix=\"my\\ndir\") as tempdir:\n            click.Path(dir_okay=False).convert(tempdir, None, None)\n\n    assert \"my\\\\ndir\" in exc_info.value.message\n\n\ndef test_choice_get_invalid_choice_message():\n    choice = click.Choice([\"a\", \"b\", \"c\"])\n    message = choice.get_invalid_choice_message(\"d\", ctx=None)\n    assert message == \"'d' is not one of 'a', 'b', 'c'.\"\n"
  },
  {
    "path": "tests/test_utils.py",
    "content": "import os\nimport pathlib\nimport stat\nimport subprocess\nimport sys\nfrom collections import namedtuple\nfrom contextlib import nullcontext\nfrom decimal import Decimal\nfrom fractions import Fraction\nfrom functools import partial\nfrom io import StringIO\nfrom pathlib import Path\nfrom tempfile import tempdir\nfrom unittest.mock import patch\n\nimport pytest\n\nimport click._termui_impl\nimport click.utils\nfrom click._compat import WIN\nfrom click._utils import UNSET\n\n\ndef test_unset_sentinel():\n    value = UNSET\n\n    assert value\n    assert value is UNSET\n    assert value == UNSET\n    assert repr(value) == \"Sentinel.UNSET\"\n    assert str(value) == \"Sentinel.UNSET\"\n    assert bool(value) is True\n\n    # Try all native Python values that can be falsy or truthy.\n    # See: https://docs.python.org/3/library/stdtypes.html#truth-value-testing\n    real_values = (\n        None,\n        True,\n        False,\n        0,\n        1,\n        0.0,\n        1.0,\n        0j,\n        1j,\n        Decimal(0),\n        Decimal(1),\n        Fraction(0, 1),\n        Fraction(1, 1),\n        \"\",\n        \"a\",\n        \"UNSET\",\n        \"Sentinel.UNSET\",\n        [1],\n        (1),\n        {1: \"a\"},\n        set(),\n        set([1]),\n        frozenset(),\n        frozenset([1]),\n        range(0),\n        range(1),\n    )\n\n    for real_value in real_values:\n        assert value != real_value\n        assert value is not real_value\n\n    assert value not in real_values\n\n\ndef test_echo(runner):\n    with runner.isolation() as outstreams:\n        click.echo(\"\\N{SNOWMAN}\")\n        click.echo(b\"\\x44\\x44\")\n        click.echo(42, nl=False)\n        click.echo(b\"a\", nl=False)\n        click.echo(\"\\x1b[31mx\\x1b[39m\", nl=False)\n        bytes = outstreams[0].getvalue().replace(b\"\\r\\n\", b\"\\n\")\n        assert bytes == b\"\\xe2\\x98\\x83\\nDD\\n42ax\"\n\n    # if wrapped, we expect bytes to survive.\n    @click.command()\n    def cli():\n        click.echo(b\"\\xf6\")\n\n    result = runner.invoke(cli, [])\n    assert result.stdout_bytes == b\"\\xf6\\n\"\n\n    # Ensure we do not strip for bytes.\n    with runner.isolation() as outstreams:\n        click.echo(bytearray(b\"\\x1b[31mx\\x1b[39m\"), nl=False)\n        assert outstreams[0].getvalue() == b\"\\x1b[31mx\\x1b[39m\"\n\n\ndef test_echo_custom_file():\n    f = StringIO()\n    click.echo(\"hello\", file=f)\n    assert f.getvalue() == \"hello\\n\"\n\n\ndef test_echo_no_streams(monkeypatch, runner):\n    \"\"\"echo should not fail when stdout and stderr are None with pythonw on Windows.\"\"\"\n    with runner.isolation():\n        sys.stdout = None\n        sys.stderr = None\n        click.echo(\"test\")\n        click.echo(\"test\", err=True)\n\n\n@pytest.mark.parametrize(\n    (\"styles\", \"ref\"),\n    [\n        ({\"fg\": \"black\"}, \"\\x1b[30mx y\\x1b[0m\"),\n        ({\"fg\": \"red\"}, \"\\x1b[31mx y\\x1b[0m\"),\n        ({\"fg\": \"green\"}, \"\\x1b[32mx y\\x1b[0m\"),\n        ({\"fg\": \"yellow\"}, \"\\x1b[33mx y\\x1b[0m\"),\n        ({\"fg\": \"blue\"}, \"\\x1b[34mx y\\x1b[0m\"),\n        ({\"fg\": \"magenta\"}, \"\\x1b[35mx y\\x1b[0m\"),\n        ({\"fg\": \"cyan\"}, \"\\x1b[36mx y\\x1b[0m\"),\n        ({\"fg\": \"white\"}, \"\\x1b[37mx y\\x1b[0m\"),\n        ({\"bg\": \"black\"}, \"\\x1b[40mx y\\x1b[0m\"),\n        ({\"bg\": \"red\"}, \"\\x1b[41mx y\\x1b[0m\"),\n        ({\"bg\": \"green\"}, \"\\x1b[42mx y\\x1b[0m\"),\n        ({\"bg\": \"yellow\"}, \"\\x1b[43mx y\\x1b[0m\"),\n        ({\"bg\": \"blue\"}, \"\\x1b[44mx y\\x1b[0m\"),\n        ({\"bg\": \"magenta\"}, \"\\x1b[45mx y\\x1b[0m\"),\n        ({\"bg\": \"cyan\"}, \"\\x1b[46mx y\\x1b[0m\"),\n        ({\"bg\": \"white\"}, \"\\x1b[47mx y\\x1b[0m\"),\n        ({\"bg\": 91}, \"\\x1b[48;5;91mx y\\x1b[0m\"),\n        ({\"bg\": (135, 0, 175)}, \"\\x1b[48;2;135;0;175mx y\\x1b[0m\"),\n        ({\"bold\": True}, \"\\x1b[1mx y\\x1b[0m\"),\n        ({\"dim\": True}, \"\\x1b[2mx y\\x1b[0m\"),\n        ({\"underline\": True}, \"\\x1b[4mx y\\x1b[0m\"),\n        ({\"overline\": True}, \"\\x1b[53mx y\\x1b[0m\"),\n        ({\"italic\": True}, \"\\x1b[3mx y\\x1b[0m\"),\n        ({\"blink\": True}, \"\\x1b[5mx y\\x1b[0m\"),\n        ({\"reverse\": True}, \"\\x1b[7mx y\\x1b[0m\"),\n        ({\"strikethrough\": True}, \"\\x1b[9mx y\\x1b[0m\"),\n        ({\"bold\": False}, \"\\x1b[22mx y\\x1b[0m\"),\n        ({\"dim\": False}, \"\\x1b[22mx y\\x1b[0m\"),\n        ({\"underline\": False}, \"\\x1b[24mx y\\x1b[0m\"),\n        ({\"overline\": False}, \"\\x1b[55mx y\\x1b[0m\"),\n        ({\"italic\": False}, \"\\x1b[23mx y\\x1b[0m\"),\n        ({\"blink\": False}, \"\\x1b[25mx y\\x1b[0m\"),\n        ({\"reverse\": False}, \"\\x1b[27mx y\\x1b[0m\"),\n        ({\"strikethrough\": False}, \"\\x1b[29mx y\\x1b[0m\"),\n        ({\"fg\": \"black\", \"reset\": False}, \"\\x1b[30mx y\"),\n    ],\n)\ndef test_styling(styles, ref):\n    assert click.style(\"x y\", **styles) == ref\n    assert click.unstyle(ref) == \"x y\"\n\n\n@pytest.mark.parametrize((\"text\", \"expect\"), [(\"\\x1b[?25lx y\\x1b[?25h\", \"x y\")])\ndef test_unstyle_other_ansi(text, expect):\n    assert click.unstyle(text) == expect\n\n\ndef test_filename_formatting():\n    assert click.format_filename(b\"foo.txt\") == \"foo.txt\"\n    assert click.format_filename(b\"/x/foo.txt\") == \"/x/foo.txt\"\n    assert click.format_filename(\"/x/foo.txt\") == \"/x/foo.txt\"\n    assert click.format_filename(\"/x/foo.txt\", shorten=True) == \"foo.txt\"\n    assert click.format_filename(\"/x/\\ufffd.txt\", shorten=True) == \"�.txt\"\n\n\ndef test_prompts(runner):\n    @click.command()\n    def test():\n        if click.confirm(\"Foo\"):\n            click.echo(\"yes!\")\n        else:\n            click.echo(\"no :(\")\n\n    result = runner.invoke(test, input=\"y\\n\")\n    assert not result.exception\n    assert result.output == \"Foo [y/N]: y\\nyes!\\n\"\n\n    result = runner.invoke(test, input=\"\\n\")\n    assert not result.exception\n    assert result.output == \"Foo [y/N]: \\nno :(\\n\"\n\n    result = runner.invoke(test, input=\"n\\n\")\n    assert not result.exception\n    assert result.output == \"Foo [y/N]: n\\nno :(\\n\"\n\n    @click.command()\n    def test_no():\n        if click.confirm(\"Foo\", default=True):\n            click.echo(\"yes!\")\n        else:\n            click.echo(\"no :(\")\n\n    result = runner.invoke(test_no, input=\"y\\n\")\n    assert not result.exception\n    assert result.output == \"Foo [Y/n]: y\\nyes!\\n\"\n\n    result = runner.invoke(test_no, input=\"\\n\")\n    assert not result.exception\n    assert result.output == \"Foo [Y/n]: \\nyes!\\n\"\n\n    result = runner.invoke(test_no, input=\"n\\n\")\n    assert not result.exception\n    assert result.output == \"Foo [Y/n]: n\\nno :(\\n\"\n\n\ndef test_confirm_repeat(runner):\n    cli = click.Command(\n        \"cli\", params=[click.Option([\"--a/--no-a\"], default=None, prompt=True)]\n    )\n    result = runner.invoke(cli, input=\"\\ny\\n\")\n    assert result.output == \"A [y/n]: \\nError: invalid input\\nA [y/n]: y\\n\"\n\n\n@pytest.mark.skipif(WIN, reason=\"Different behavior on windows.\")\ndef test_prompts_abort(monkeypatch, capsys):\n    def f(_):\n        raise KeyboardInterrupt()\n\n    monkeypatch.setattr(\"click.termui.hidden_prompt_func\", f)\n\n    try:\n        click.prompt(\"Password\", hide_input=True)\n    except click.Abort:\n        click.echo(\"interrupted\")\n\n    out, err = capsys.readouterr()\n    assert out == \"Password:\\ninterrupted\\n\"\n\n\ndef test_prompts_eof(runner):\n    \"\"\"If too few lines of input are given, prompt should exit, not hang.\"\"\"\n\n    @click.command\n    def echo():\n        for _ in range(3):\n            click.echo(click.prompt(\"\", type=int))\n\n    # only provide two lines of input for three prompts\n    result = runner.invoke(echo, input=\"1\\n2\\n\")\n    assert result.exit_code == 1\n\n\ndef _test_gen_func():\n    yield \"a\"\n    yield \"b\"\n    yield \"c\"\n    yield \"abc\"\n\n\ndef _test_gen_func_fails():\n    yield \"test\"\n    raise RuntimeError(\"This is a test.\")\n\n\ndef _test_gen_func_echo(file=None):\n    yield \"test\"\n    click.echo(\"hello\", file=file)\n    yield \"test\"\n\n\ndef _test_simulate_keyboard_interrupt(file=None):\n    yield \"output_before_keyboard_interrupt\"\n    raise KeyboardInterrupt()\n\n\nEchoViaPagerTest = namedtuple(\n    \"EchoViaPagerTest\",\n    (\n        \"description\",\n        \"test_input\",\n        \"expected_pager\",\n        \"expected_stdout\",\n        \"expected_stderr\",\n        \"expected_error\",\n    ),\n)\n\n\n@pytest.mark.skipif(WIN, reason=\"Different behavior on windows.\")\n@pytest.mark.parametrize(\n    \"pager_cmd\", [\"cat\", \"cat \", \" cat \", \"less\", \" less\", \" less \"]\n)\n@pytest.mark.parametrize(\n    \"test\",\n    [\n        # We need to pass a parameter function instead of a plain param\n        # as pytest.mark.parametrize will reuse the parameters causing the\n        # generators to be used up so they will not yield anymore\n        EchoViaPagerTest(\n            description=\"Plain string argument\",\n            test_input=lambda: \"just text\",\n            expected_pager=\"just text\\n\",\n            expected_stdout=\"\",\n            expected_stderr=\"\",\n            expected_error=None,\n        ),\n        EchoViaPagerTest(\n            description=\"Iterable argument\",\n            test_input=lambda: [\"itera\", \"ble\"],\n            expected_pager=\"iterable\\n\",\n            expected_stdout=\"\",\n            expected_stderr=\"\",\n            expected_error=None,\n        ),\n        EchoViaPagerTest(\n            description=\"Generator function argument\",\n            test_input=lambda: _test_gen_func,\n            expected_pager=\"abcabc\\n\",\n            expected_stdout=\"\",\n            expected_stderr=\"\",\n            expected_error=None,\n        ),\n        EchoViaPagerTest(\n            description=\"String generator argument\",\n            test_input=lambda: _test_gen_func(),\n            expected_pager=\"abcabc\\n\",\n            expected_stdout=\"\",\n            expected_stderr=\"\",\n            expected_error=None,\n        ),\n        EchoViaPagerTest(\n            description=\"Number generator expression argument\",\n            test_input=lambda: (c for c in range(6)),\n            expected_pager=\"012345\\n\",\n            expected_stdout=\"\",\n            expected_stderr=\"\",\n            expected_error=None,\n        ),\n        EchoViaPagerTest(\n            description=\"Exception in generator function argument\",\n            test_input=lambda: _test_gen_func_fails,\n            # Because generator throws early on, the pager did not have\n            # a chance yet to write the file.\n            expected_pager=\"\",\n            expected_stdout=\"\",\n            expected_stderr=\"\",\n            expected_error=RuntimeError,\n        ),\n        EchoViaPagerTest(\n            description=\"Exception in generator argument\",\n            test_input=lambda: _test_gen_func_fails,\n            # Because generator throws early on, the pager did not have a\n            # chance yet to write the file.\n            expected_pager=\"\",\n            expected_stdout=\"\",\n            expected_stderr=\"\",\n            expected_error=RuntimeError,\n        ),\n        EchoViaPagerTest(\n            description=\"Keyboard interrupt should not terminate the pager\",\n            test_input=lambda: _test_simulate_keyboard_interrupt(),\n            # Due to the keyboard interrupt during pager execution, click program\n            # should abort, but the pager should stay open.\n            # This allows users to cancel the program and search in the pager\n            # output, before they decide to terminate the pager.\n            expected_pager=\"output_before_keyboard_interrupt\",\n            expected_stdout=\"\",\n            expected_stderr=\"\",\n            expected_error=KeyboardInterrupt,\n        ),\n        EchoViaPagerTest(\n            description=\"Writing to stdout during generator execution\",\n            test_input=lambda: _test_gen_func_echo(),\n            expected_pager=\"testtest\\n\",\n            expected_stdout=\"hello\\n\",\n            expected_stderr=\"\",\n            expected_error=None,\n        ),\n        EchoViaPagerTest(\n            description=\"Writing to stderr during generator execution\",\n            test_input=lambda: _test_gen_func_echo(file=sys.stderr),\n            expected_pager=\"testtest\\n\",\n            expected_stdout=\"\",\n            expected_stderr=\"hello\\n\",\n            expected_error=None,\n        ),\n    ],\n)\ndef test_echo_via_pager(monkeypatch, capfd, pager_cmd, test):\n    monkeypatch.setitem(os.environ, \"PAGER\", pager_cmd)\n    monkeypatch.setattr(click._termui_impl, \"isatty\", lambda x: True)\n\n    test_input = test.test_input()\n    expected_pager = test.expected_pager\n    expected_stdout = test.expected_stdout\n    expected_stderr = test.expected_stderr\n    expected_error = test.expected_error\n\n    check_raise = pytest.raises(expected_error) if expected_error else nullcontext()\n\n    pager_out_tmp = Path(tempdir) / \"pager_out.txt\"\n    pager_out_tmp.unlink(missing_ok=True)\n    with pager_out_tmp.open(\"w\") as f:\n        force_subprocess_stdout = patch.object(\n            subprocess,\n            \"Popen\",\n            partial(subprocess.Popen, stdout=f),\n        )\n        with force_subprocess_stdout:\n            with check_raise:\n                click.echo_via_pager(test_input)\n\n    out, err = capfd.readouterr()\n\n    pager = pager_out_tmp.read_text()\n\n    assert pager == expected_pager, (\n        f\"Unexpected pager output in test case '{test.description}'\"\n    )\n    assert out == expected_stdout, (\n        f\"Unexpected stdout in test case '{test.description}'\"\n    )\n    assert err == expected_stderr, (\n        f\"Unexpected stderr in test case '{test.description}'\"\n    )\n\n\ndef test_echo_color_flag(monkeypatch, capfd):\n    isatty = True\n    monkeypatch.setattr(click._compat, \"isatty\", lambda x: isatty)\n\n    text = \"foo\"\n    styled_text = click.style(text, fg=\"red\")\n    assert styled_text == \"\\x1b[31mfoo\\x1b[0m\"\n\n    click.echo(styled_text, color=False)\n    out, err = capfd.readouterr()\n    assert out == f\"{text}\\n\"\n\n    click.echo(styled_text, color=True)\n    out, err = capfd.readouterr()\n    assert out == f\"{styled_text}\\n\"\n\n    isatty = True\n    click.echo(styled_text)\n    out, err = capfd.readouterr()\n    assert out == f\"{styled_text}\\n\"\n\n    isatty = False\n    # Faking isatty() is not enough on Windows;\n    # the implementation caches the colorama wrapped stream\n    # so we have to use a new stream for each test\n    stream = StringIO()\n    click.echo(styled_text, file=stream)\n    assert stream.getvalue() == f\"{text}\\n\"\n\n    stream = StringIO()\n    click.echo(styled_text, file=stream, color=True)\n    assert stream.getvalue() == f\"{styled_text}\\n\"\n\n\ndef test_prompt_cast_default(capfd, monkeypatch):\n    monkeypatch.setattr(sys, \"stdin\", StringIO(\"\\n\"))\n    value = click.prompt(\"value\", default=\"100\", type=int)\n    capfd.readouterr()\n    assert isinstance(value, int)\n\n\n@pytest.mark.skipif(WIN, reason=\"Test too complex to make work windows.\")\ndef test_echo_writing_to_standard_error(capfd, monkeypatch):\n    def emulate_input(text):\n        \"\"\"Emulate keyboard input.\"\"\"\n        monkeypatch.setattr(sys, \"stdin\", StringIO(text))\n\n    click.echo(\"Echo to standard output\")\n    out, err = capfd.readouterr()\n    assert out == \"Echo to standard output\\n\"\n    assert err == \"\"\n\n    click.echo(\"Echo to standard error\", err=True)\n    out, err = capfd.readouterr()\n    assert out == \"\"\n    assert err == \"Echo to standard error\\n\"\n\n    emulate_input(\"asdlkj\\n\")\n    click.prompt(\"Prompt to stdin\")\n    out, err = capfd.readouterr()\n    assert out == \"Prompt to stdin: \"\n    assert err == \"\"\n\n    emulate_input(\"asdlkj\\n\")\n    click.prompt(\"Prompt to stdin with no suffix\", prompt_suffix=\"\")\n    out, err = capfd.readouterr()\n    assert out == \"Prompt to stdin with no suffix\"\n    assert err == \"\"\n\n    emulate_input(\"asdlkj\\n\")\n    click.prompt(\"Prompt to stderr\", err=True)\n    out, err = capfd.readouterr()\n    assert out == \" \"\n    assert err == \"Prompt to stderr:\"\n\n    emulate_input(\"asdlkj\\n\")\n    click.prompt(\"Prompt to stderr with no suffix\", prompt_suffix=\"\", err=True)\n    out, err = capfd.readouterr()\n    assert out == \"x\"\n    assert err == \"Prompt to stderr with no suffi\"\n\n    emulate_input(\"y\\n\")\n    click.confirm(\"Prompt to stdin\")\n    out, err = capfd.readouterr()\n    assert out == \"Prompt to stdin [y/N]: \"\n    assert err == \"\"\n\n    emulate_input(\"y\\n\")\n    click.confirm(\"Prompt to stdin with no suffix\", prompt_suffix=\"\")\n    out, err = capfd.readouterr()\n    assert out == \"Prompt to stdin with no suffix [y/N]\"\n    assert err == \"\"\n\n    emulate_input(\"y\\n\")\n    click.confirm(\"Prompt to stderr\", err=True)\n    out, err = capfd.readouterr()\n    assert out == \" \"\n    assert err == \"Prompt to stderr [y/N]:\"\n\n    emulate_input(\"y\\n\")\n    click.confirm(\"Prompt to stderr with no suffix\", prompt_suffix=\"\", err=True)\n    out, err = capfd.readouterr()\n    assert out == \"]\"\n    assert err == \"Prompt to stderr with no suffix [y/N\"\n\n    monkeypatch.setattr(click.termui, \"isatty\", lambda x: True)\n    monkeypatch.setattr(click.termui, \"getchar\", lambda: \" \")\n\n    click.pause(\"Pause to stdout\")\n    out, err = capfd.readouterr()\n    assert out == \"Pause to stdout\\n\"\n    assert err == \"\"\n\n    click.pause(\"Pause to stderr\", err=True)\n    out, err = capfd.readouterr()\n    assert out == \"\"\n    assert err == \"Pause to stderr\\n\"\n\n\ndef test_echo_with_capsys(capsys):\n    click.echo(\"Capture me.\")\n    out, err = capsys.readouterr()\n    assert out == \"Capture me.\\n\"\n\n\ndef test_open_file(runner):\n    @click.command()\n    @click.argument(\"filename\")\n    def cli(filename):\n        with click.open_file(filename) as f:\n            click.echo(f.read())\n\n        click.echo(\"meep\")\n\n    with runner.isolated_filesystem():\n        with open(\"hello.txt\", \"w\") as f:\n            f.write(\"Cool stuff\")\n\n        result = runner.invoke(cli, [\"hello.txt\"])\n        assert result.exception is None\n        assert result.output == \"Cool stuff\\nmeep\\n\"\n\n        result = runner.invoke(cli, [\"-\"], input=\"foobar\")\n        assert result.exception is None\n        assert result.output == \"foobar\\nmeep\\n\"\n\n\ndef test_open_file_pathlib_dash(runner):\n    @click.command()\n    @click.argument(\n        \"filename\", type=click.Path(allow_dash=True, path_type=pathlib.Path)\n    )\n    def cli(filename):\n        click.echo(str(type(filename)))\n\n        with click.open_file(filename) as f:\n            click.echo(f.read())\n\n        result = runner.invoke(cli, [\"-\"], input=\"value\")\n        assert result.exception is None\n        assert result.output == \"pathlib.Path\\nvalue\\n\"\n\n\ndef test_open_file_ignore_errors_stdin(runner):\n    @click.command()\n    @click.argument(\"filename\")\n    def cli(filename):\n        with click.open_file(filename, errors=\"ignore\") as f:\n            click.echo(f.read())\n\n    result = runner.invoke(cli, [\"-\"], input=os.urandom(16))\n    assert result.exception is None\n\n\ndef test_open_file_respects_ignore(runner):\n    with runner.isolated_filesystem():\n        with open(\"test.txt\", \"w\") as f:\n            f.write(\"Hello world!\")\n\n        with click.open_file(\"test.txt\", encoding=\"utf8\", errors=\"ignore\") as f:\n            assert f.errors == \"ignore\"\n\n\ndef test_open_file_ignore_invalid_utf8(runner):\n    with runner.isolated_filesystem():\n        with open(\"test.txt\", \"wb\") as f:\n            f.write(b\"\\xe2\\x28\\xa1\")\n\n        with click.open_file(\"test.txt\", encoding=\"utf8\", errors=\"ignore\") as f:\n            f.read()\n\n\ndef test_open_file_ignore_no_encoding(runner):\n    with runner.isolated_filesystem():\n        with open(\"test.bin\", \"wb\") as f:\n            f.write(os.urandom(16))\n\n        with click.open_file(\"test.bin\", errors=\"ignore\") as f:\n            f.read()\n\n\n@pytest.mark.skipif(WIN, reason=\"os.chmod() is not fully supported on Windows.\")\n@pytest.mark.parametrize(\"permissions\", [0o400, 0o444, 0o600, 0o644])\ndef test_open_file_atomic_permissions_existing_file(runner, permissions):\n    with runner.isolated_filesystem():\n        with open(\"existing.txt\", \"w\") as f:\n            f.write(\"content\")\n        os.chmod(\"existing.txt\", permissions)\n\n        @click.command()\n        @click.argument(\"filename\")\n        def cli(filename):\n            click.open_file(filename, \"w\", atomic=True).close()\n\n        result = runner.invoke(cli, [\"existing.txt\"])\n        assert result.exception is None\n        assert stat.S_IMODE(os.stat(\"existing.txt\").st_mode) == permissions\n\n\n@pytest.mark.skipif(WIN, reason=\"os.stat() is not fully supported on Windows.\")\ndef test_open_file_atomic_permissions_new_file(runner):\n    with runner.isolated_filesystem():\n\n        @click.command()\n        @click.argument(\"filename\")\n        def cli(filename):\n            click.open_file(filename, \"w\", atomic=True).close()\n\n        # Create a test file to get the expected permissions for new files\n        # according to the current umask.\n        with open(\"test.txt\", \"w\"):\n            pass\n        permissions = stat.S_IMODE(os.stat(\"test.txt\").st_mode)\n\n        result = runner.invoke(cli, [\"new.txt\"])\n        assert result.exception is None\n        assert stat.S_IMODE(os.stat(\"new.txt\").st_mode) == permissions\n\n\ndef test_iter_keepopenfile(tmpdir):\n    expected = list(map(str, range(10)))\n    p = tmpdir.mkdir(\"testdir\").join(\"testfile\")\n    p.write(\"\\n\".join(expected))\n    with p.open() as f:\n        for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f), strict=False):\n            assert e_line == a_line.strip()\n\n\ndef test_iter_lazyfile(tmpdir):\n    expected = list(map(str, range(10)))\n    p = tmpdir.mkdir(\"testdir\").join(\"testfile\")\n    p.write(\"\\n\".join(expected))\n    with p.open() as f:\n        with click.utils.LazyFile(f.name) as lf:\n            for e_line, a_line in zip(expected, lf, strict=False):\n                assert e_line == a_line.strip()\n\n\nclass MockMain:\n    __slots__ = \"__package__\"\n\n    def __init__(self, package_name):\n        self.__package__ = package_name\n\n\n@pytest.mark.parametrize(\n    (\"path\", \"main\", \"expected\"),\n    [\n        (\"example.py\", None, \"example.py\"),\n        (str(pathlib.Path(\"/foo/bar/example.py\")), None, \"example.py\"),\n        (\"example\", None, \"example\"),\n        (str(pathlib.Path(\"example/__main__.py\")), \"example\", \"python -m example\"),\n        (str(pathlib.Path(\"example/cli.py\")), \"example\", \"python -m example.cli\"),\n        (str(pathlib.Path(\"./example\")), \"\", \"example\"),\n    ],\n)\ndef test_detect_program_name(path, main, expected):\n    assert click.utils._detect_program_name(path, _main=MockMain(main)) == expected\n\n\ndef test_expand_args(monkeypatch):\n    user = os.path.expanduser(\"~\")\n    assert user in click.utils._expand_args([\"~\"])\n    monkeypatch.setenv(\"CLICK_TEST\", \"hello\")\n    assert \"hello\" in click.utils._expand_args([\"$CLICK_TEST\"])\n    assert \"pyproject.toml\" in click.utils._expand_args([\"*.toml\"])\n    assert os.path.join(\"tests\", \"conftest.py\") in click.utils._expand_args(\n        [\"**/conftest.py\"]\n    )\n    assert \"*.not-found\" in click.utils._expand_args([\"*.not-found\"])\n    # a bad glob pattern, such as a pytest identifier, should return itself\n    assert click.utils._expand_args([\"test.py::test_bad\"])[0] == \"test.py::test_bad\"\n\n\n@pytest.mark.parametrize(\n    (\"value\", \"max_length\", \"expect\"),\n    [\n        pytest.param(\"\", 10, \"\", id=\"empty\"),\n        pytest.param(\"123 567 90\", 10, \"123 567 90\", id=\"equal length, no dot\"),\n        pytest.param(\"123 567 9. aaaa bbb\", 10, \"123 567 9.\", id=\"sentence < max\"),\n        pytest.param(\"123 567\\n\\n 9. aaaa bbb\", 10, \"123 567\", id=\"paragraph < max\"),\n        pytest.param(\"123 567 90123.\", 10, \"123 567...\", id=\"truncate\"),\n        pytest.param(\"123 5678 xxxxxx\", 10, \"123...\", id=\"length includes suffix\"),\n        pytest.param(\n            \"token in ~/.netrc ciao ciao\",\n            20,\n            \"token in ~/.netrc...\",\n            id=\"ignore dot in word\",\n        ),\n    ],\n)\n@pytest.mark.parametrize(\n    \"alter\",\n    [\n        pytest.param(None, id=\"\"),\n        pytest.param(\n            lambda text: \"\\n\\b\\n\" + \"  \".join(text.split(\" \")) + \"\\n\", id=\"no-wrap mark\"\n        ),\n    ],\n)\ndef test_make_default_short_help(value, max_length, alter, expect):\n    assert len(expect) <= max_length\n\n    if alter:\n        value = alter(value)\n\n    out = click.utils.make_default_short_help(value, max_length)\n    assert out == expect\n"
  },
  {
    "path": "tests/typing/typing_aliased_group.py",
    "content": "\"\"\"Example from https://click.palletsprojects.com/en/stable/advanced/#command-aliases\"\"\"\n\nfrom __future__ import annotations\n\nfrom typing_extensions import assert_type\n\nimport click\n\n\nclass AliasedGroup(click.Group):\n    def get_command(self, ctx: click.Context, cmd_name: str) -> click.Command | None:\n        rv = click.Group.get_command(self, ctx, cmd_name)\n        if rv is not None:\n            return rv\n        matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name)]\n        if not matches:\n            return None\n        elif len(matches) == 1:\n            return click.Group.get_command(self, ctx, matches[0])\n        ctx.fail(f\"Too many matches: {', '.join(sorted(matches))}\")\n\n    def resolve_command(\n        self, ctx: click.Context, args: list[str]\n    ) -> tuple[str | None, click.Command, list[str]]:\n        # always return the full command name\n        _, cmd, args = super().resolve_command(ctx, args)\n        assert cmd is not None\n        return cmd.name, cmd, args\n\n\n@click.command(cls=AliasedGroup)\ndef cli() -> None:\n    pass\n\n\nassert_type(cli, AliasedGroup)\n\n\n@cli.command()\ndef push() -> None:\n    pass\n\n\n@cli.command()\ndef pop() -> None:\n    pass\n"
  },
  {
    "path": "tests/typing/typing_confirmation_option.py",
    "content": "\"\"\"From https://click.palletsprojects.com/en/stable/options/#yes-parameters\"\"\"\n\nfrom typing_extensions import assert_type\n\nimport click\n\n\n@click.command()\n@click.confirmation_option(prompt=\"Are you sure you want to drop the db?\")\ndef dropdb() -> None:\n    click.echo(\"Dropped all tables!\")\n\n\nassert_type(dropdb, click.Command)\n"
  },
  {
    "path": "tests/typing/typing_group_kw_options.py",
    "content": "from typing_extensions import assert_type\n\nimport click\n\n\n@click.group(context_settings={})\ndef hello() -> None:\n    pass\n\n\nassert_type(hello, click.Group)\n"
  },
  {
    "path": "tests/typing/typing_help_option.py",
    "content": "from typing_extensions import assert_type\n\nimport click\n\n\n@click.command()\n@click.help_option(\"-h\", \"--help\")\ndef hello() -> None:\n    \"\"\"Simple program that greets NAME for a total of COUNT times.\"\"\"\n    click.echo(\"Hello!\")\n\n\nassert_type(hello, click.Command)\n"
  },
  {
    "path": "tests/typing/typing_options.py",
    "content": "\"\"\"From https://click.palletsprojects.com/en/stable/quickstart/#adding-parameters\"\"\"\n\nfrom typing_extensions import assert_type\n\nimport click\n\n\n@click.command()\n@click.option(\"--count\", default=1, help=\"number of greetings\")\n@click.argument(\"name\")\ndef hello(count: int, name: str) -> None:\n    for _ in range(count):\n        click.echo(f\"Hello {name}!\")\n\n\nassert_type(hello, click.Command)\n"
  },
  {
    "path": "tests/typing/typing_password_option.py",
    "content": "import codecs\n\nfrom typing_extensions import assert_type\n\nimport click\n\n\n@click.command()\n@click.password_option()\ndef encrypt(password: str) -> None:\n    click.echo(f\"encoded: to {codecs.encode(password, 'rot13')}\")\n\n\nassert_type(encrypt, click.Command)\n"
  },
  {
    "path": "tests/typing/typing_progressbar.py",
    "content": "from __future__ import annotations\n\nfrom typing_extensions import assert_type\n\nfrom click import progressbar\nfrom click._termui_impl import ProgressBar\n\n\ndef test_length_is_int() -> None:\n    with progressbar(length=5) as bar:\n        assert_type(bar, ProgressBar[int])\n        for i in bar:\n            assert_type(i, int)\n\n\ndef it() -> tuple[str, ...]:\n    return (\"hello\", \"world\")\n\n\ndef test_generic_on_iterable() -> None:\n    with progressbar(it()) as bar:\n        assert_type(bar, ProgressBar[str])\n        for s in bar:\n            assert_type(s, str)\n"
  },
  {
    "path": "tests/typing/typing_simple_example.py",
    "content": "\"\"\"The simple example from https://github.com/pallets/click#a-simple-example.\"\"\"\n\nfrom typing_extensions import assert_type\n\nimport click\n\n\n@click.command()\n@click.option(\"--count\", default=1, help=\"Number of greetings.\")\n@click.option(\"--name\", prompt=\"Your name\", help=\"The person to greet.\")\ndef hello(count: int, name: str) -> None:\n    \"\"\"Simple program that greets NAME for a total of COUNT times.\"\"\"\n    for _ in range(count):\n        click.echo(f\"Hello, {name}!\")\n\n\nassert_type(hello, click.Command)\n"
  },
  {
    "path": "tests/typing/typing_version_option.py",
    "content": "\"\"\"\nFrom https://click.palletsprojects.com/en/stable/options/#callbacks-and-eager-options.\n\"\"\"\n\nfrom typing_extensions import assert_type\n\nimport click\n\n\n@click.command()\n@click.version_option(\"0.1\")\ndef hello() -> None:\n    click.echo(\"Hello World!\")\n\n\nassert_type(hello, click.Command)\n"
  }
]