Expose CLI Options through public API (#1928)

* Expose CLI Options through public API

Expose a public API that can retrieve the processed CLI options for the
current process launched through the debugpy CLI. This enables code to
be able to retrieve options like the port and adapter access token to be
used for launching their own child process' that can be debugged.

* Fix test by sending dict not dataclass object
This commit is contained in:
Jordan Borean 2025-07-16 08:56:03 +10:00 committed by GitHub
parent ead90f6f71
commit 1aff9aa541
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 97 additions and 7 deletions

View file

@ -0,0 +1,35 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE in the project root
# for license information.
from tests import debug
def test_cli_options_with_no_debugger():
import debugpy
cli_options = debugpy.get_cli_options()
assert cli_options is None
def test_cli_options_under_file_connect(pyfile, target, run):
@pyfile
def code_to_debug():
import dataclasses
import debugpy
import debuggee
from debuggee import backchannel
debuggee.setup()
backchannel.send(dataclasses.asdict(debugpy.get_cli_options()))
with debug.Session() as session:
backchannel = session.open_backchannel()
with run(session, target(code_to_debug)):
pass
cli_options = backchannel.receive()
assert cli_options['mode'] == 'connect'
assert cli_options['target_kind'] == 'file'

View file

@ -630,7 +630,7 @@ def test_subprocess_with_parent_pid(pyfile, target, run):
import subprocess
import sys
from debugpy.server import cli as debugpy_cli
import debugpy
debuggee.setup()
@ -642,8 +642,11 @@ def test_subprocess_with_parent_pid(pyfile, target, run):
else:
argv = ["/bin/sh", "-c"]
host, port = debugpy_cli.options.address
access_token = debugpy_cli.options.adapter_access_token
cli_opts = debugpy.get_cli_options()
assert cli_opts, "No CLI options found"
host, port = cli_opts.address
access_token = cli_opts.adapter_access_token
shell_args = [
sys.executable,