|
4 | 4 |
|
5 | 5 | from __future__ import annotations |
6 | 6 |
|
| 7 | +import dataclasses |
7 | 8 | import functools |
8 | 9 | import typing |
9 | 10 |
|
|
21 | 22 | Endpoint = typing.Tuple[str, int] |
22 | 23 |
|
23 | 24 |
|
| 25 | +@dataclasses.dataclass(frozen=True) |
| 26 | +class CliOptions: |
| 27 | + """Options that were passed to the debugpy CLI entry point.""" |
| 28 | + mode: typing.Literal["connect", "listen"] |
| 29 | + target_kind: typing.Literal["file", "module", "code", "pid"] |
| 30 | + address: Endpoint |
| 31 | + log_to: str | None = None |
| 32 | + log_to_stderr: bool = False |
| 33 | + target: str | None = None |
| 34 | + wait_for_client: bool = False |
| 35 | + adapter_access_token: str | None = None |
| 36 | + config: dict[str, object] = dataclasses.field(default_factory=dict) |
| 37 | + parent_session_pid: int | None = None |
| 38 | + |
| 39 | + |
24 | 40 | def _api(cancelable=False): |
25 | 41 | def apply(f): |
26 | 42 | @functools.wraps(f) |
@@ -196,4 +212,37 @@ def trace_this_thread(__should_trace: bool): |
196 | 212 | """ |
197 | 213 |
|
198 | 214 |
|
| 215 | +def get_cli_options() -> CliOptions | None: |
| 216 | + """Returns the CLI options that were processed by debugpy. |
| 217 | + |
| 218 | + These options are all the options after the CLI args and |
| 219 | + environment variables that were processed on startup. |
| 220 | + |
| 221 | + If the debugpy CLI entry point was not called in this process, the |
| 222 | + returned value is None. |
| 223 | + """ |
| 224 | + from debugpy.server import cli |
| 225 | + |
| 226 | + options = cli.options |
| 227 | + if options.mode is None or options.target_kind is None or options.address is None: |
| 228 | + # The CLI entrypoint was not called so there are no options present. |
| 229 | + return None |
| 230 | + |
| 231 | + # We don't return the actual options object because we don't want callers |
| 232 | + # to be able to mutate it. Instead we use a frozen dataclass as a snapshot |
| 233 | + # with richer type annotations. |
| 234 | + return CliOptions( |
| 235 | + mode=options.mode, |
| 236 | + target_kind=options.target_kind, |
| 237 | + address=options.address, |
| 238 | + log_to=options.log_to, |
| 239 | + log_to_stderr=options.log_to_stderr, |
| 240 | + target=options.target, |
| 241 | + wait_for_client=options.wait_for_client, |
| 242 | + adapter_access_token=options.adapter_access_token, |
| 243 | + config=options.config, |
| 244 | + parent_session_pid=options.parent_session_pid, |
| 245 | + ) |
| 246 | + |
| 247 | + |
199 | 248 | __version__: str = _version.get_versions()["version"] |
0 commit comments