From 879c0d0ddc0ee9c41bb7db7418d67a277372500f Mon Sep 17 00:00:00 2001 From: kalyani_gattu Date: Wed, 30 Sep 2026 21:49:04 +0530 Subject: [PATCH] Add project name option to monitor command --- codecarbon/cli/main.py | 5 +++++ tests/cli/test_cli_main.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/codecarbon/cli/main.py b/codecarbon/cli/main.py index f81fb302a..932954018 100644 --- a/codecarbon/cli/main.py +++ b/codecarbon/cli/main.py @@ -393,6 +393,10 @@ def _external_config() -> dict: ) def monitor( ctx: typer.Context, + project_name: Annotated[ + str, + typer.Option(help="Project name for the current experiment."), + ] = None, measure_power_secs: Annotated[ int, typer.Option(help="Interval between two measures."), @@ -427,6 +431,7 @@ def monitor( # Only the options actually given are forwarded: the others are left to the # tracker, which resolves them from the configuration file and environment. cli_defaults = ( + ("project_name", project_name), ("measure_power_secs", measure_power_secs), ("api_call_interval", api_call_interval), ("log_level", log_level), diff --git a/tests/cli/test_cli_main.py b/tests/cli/test_cli_main.py index 09585126f..1a23df156 100644 --- a/tests/cli/test_cli_main.py +++ b/tests/cli/test_cli_main.py @@ -122,6 +122,43 @@ def test_monitor_run_and_monitor(monkeypatch): assert "Hello, World!" in result.output +def test_monitor_accepts_tracker_options(monkeypatch): + captured = {} + + class FakeTracker: + def __init__(self, **kwargs): + captured.update(kwargs) + + def start(self): + pass + + def stop(self): + pass + + monkeypatch.setattr( + "codecarbon.emissions_tracker.EmissionsTracker", + FakeTracker, + ) + monkeypatch.setattr(cli_main, "get_existing_exp_id", lambda: "test-exp") + + runner = CliRunner() + result = runner.invoke( + cli_main.codecarbon, + [ + "monitor", + "--project-name", + "test-project", + "--no-api", + "--", + "echo", + "test", + ], + ) + + assert result.exit_code == 0, result.output + assert captured["project_name"] == "test-project" + + def test_show_config_handles_access_token_errors(monkeypatch, tmp_path, capsys): class FakeApiClient: def __init__(self, endpoint_url=None):