Determine the command used to run the program, for use in help text. If a file or entry point was executed, the file name is returned. If ``python -m`` was used to execute a module or package, ``python -m name`` is returned. This doesn't try to be too precise, the goal is to give a
(
path: str | None = None, _main: ModuleType | None = None
)
| 547 | |
| 548 | |
| 549 | def _detect_program_name( |
| 550 | path: str | None = None, _main: ModuleType | None = None |
| 551 | ) -> str: |
| 552 | """Determine the command used to run the program, for use in help |
| 553 | text. If a file or entry point was executed, the file name is |
| 554 | returned. If ``python -m`` was used to execute a module or package, |
| 555 | ``python -m name`` is returned. |
| 556 | |
| 557 | This doesn't try to be too precise, the goal is to give a concise |
| 558 | name for help text. Files are only shown as their name without the |
| 559 | path. ``python`` is only shown for modules, and the full path to |
| 560 | ``sys.executable`` is not shown. |
| 561 | |
| 562 | :param path: The Python file being executed. Python puts this in |
| 563 | ``sys.argv[0]``, which is used by default. |
| 564 | :param _main: The ``__main__`` module. This should only be passed |
| 565 | during internal testing. |
| 566 | |
| 567 | .. versionadded:: 8.0 |
| 568 | Based on command args detection in the Werkzeug reloader. |
| 569 | |
| 570 | :meta private: |
| 571 | """ |
| 572 | if _main is None: |
| 573 | _main = sys.modules["__main__"] |
| 574 | |
| 575 | if not path: |
| 576 | path = sys.argv[0] |
| 577 | |
| 578 | # The value of __package__ indicates how Python was called. It may |
| 579 | # not exist if a setuptools script is installed as an egg. It may be |
| 580 | # set incorrectly for entry points created with pip on Windows. |
| 581 | # It is set to "" inside a Shiv or PEX zipapp. |
| 582 | if getattr(_main, "__package__", None) in {None, ""} or ( |
| 583 | os.name == "nt" |
| 584 | and _main.__package__ == "" |
| 585 | and not os.path.exists(path) |
| 586 | and os.path.exists(f"{path}.exe") |
| 587 | ): |
| 588 | # Executed a file, like "python app.py". |
| 589 | return os.path.basename(path) |
| 590 | |
| 591 | # Executed a module, like "python -m example". |
| 592 | # Rewritten by Python from "-m script" to "/path/to/script.py". |
| 593 | # Need to look at main module to determine how it was executed. |
| 594 | py_module = t.cast(str, _main.__package__) |
| 595 | name = os.path.splitext(os.path.basename(path))[0] |
| 596 | |
| 597 | # A submodule like "example.cli". |
| 598 | if name != "__main__": |
| 599 | py_module = f"{py_module}.{name}" |
| 600 | |
| 601 | return f"python -m {py_module.lstrip('.')}" |
| 602 | |
| 603 | |
| 604 | def _expand_args( |
no outgoing calls
no test coverage detected
searching dependent graphs…