Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix argument splitting on '--' in the command line (#9368)

Guilherme Piresbde8b0590848
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index 8c815275d2..ffe750b559 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -27,7 +27,7 @@ from marimo._cli.errors import (
)
from marimo._cli.export.commands import export
from marimo._cli.files.file_path import validate_name
-from marimo._cli.help_formatter import ColoredGroup
+from marimo._cli.help_formatter import ColoredGroup, RunCommand
from marimo._cli.pair.commands import pair
from marimo._cli.parse_args import parse_args
from marimo._cli.parser_ux import show_compact_usage_error
@@ -828,8 +828,19 @@ class _CollectedRunFiles:
def _split_run_paths_and_args(
- name: str, args: tuple[str, ...]
+ name: str,
+ args: tuple[str, ...],
+ args_after_separator: tuple[str, ...] | None = None,
) -> tuple[list[str], tuple[str, ...]]:
+ if (
+ args_after_separator
+ and args[-len(args_after_separator) :] == args_after_separator
+ ):
+ return [
+ name,
+ *args[: -len(args_after_separator)],
+ ], args_after_separator
+
paths = [name]
for index, arg in enumerate(args):
if arg == "--":
@@ -937,6 +948,7 @@ def _create_run_workspace(
@main.command(
+ cls=RunCommand,
help="""Run a notebook as an app in read-only mode.
If NAME is a url, the notebook will be downloaded to a temporary file.
@@ -946,7 +958,7 @@ Example:
marimo run notebook.py
marimo run folder another_folder
marimo run app.py -- --arg value
-"""
+""",
)
@click.option(
"-p",
@@ -1123,7 +1135,18 @@ def run(
run_in_sandbox,
)
- paths, notebook_args = _split_run_paths_and_args(name, args)
+ # click consumes `--` as an option terminator and does not pass it
+ # through to `args`. `RunCommand` records the raw tail so splitting
+ # logic can preserve "args after --" semantics without reading process-
+ # global argv state.
+ args_after_separator = ctx.meta.get("marimo_run_args_after_separator")
+ paths, notebook_args = _split_run_paths_and_args(
+ name,
+ args,
+ args_after_separator
+ if isinstance(args_after_separator, tuple)
+ else None,
+ )
if len(paths) == 1 and prompt_run_in_docker_container(
paths[0], trusted=trusted
diff --git a/marimo/_cli/help_formatter.py b/marimo/_cli/help_formatter.py
index 22653512b8..d8a2da1d23 100644
--- a/marimo/_cli/help_formatter.py
+++ b/marimo/_cli/help_formatter.py
@@ -93,6 +93,18 @@ class ColoredCommand(click.Command):
formatter.write_dl(rows)
+class RunCommand(ColoredCommand):
+ """Command that records the raw tail after Click's `--` separator."""
+
+ def parse_args(self, ctx: click.Context, args: list[str]) -> list[str]:
+ if "--" in args:
+ separator_index = args.index("--")
+ ctx.meta["marimo_run_args_after_separator"] = tuple(
+ args[separator_index + 1 :]
+ )
+ return super().parse_args(ctx, args)
+
+
class ColoredGroup(click.Group):
"""Click Group with colored help output (cargo-style)."""
diff --git a/tests/_cli/test_cli.py b/tests/_cli/test_cli.py
index bd02b21a95..0ccd546c7f 100644
--- a/tests/_cli/test_cli.py
+++ b/tests/_cli/test_cli.py
@@ -22,6 +22,7 @@ from pathlib import Path
from typing import TYPE_CHECKING, Any
from unittest.mock import patch
+import click
import pytest
from click.testing import CliRunner
@@ -30,6 +31,7 @@ from marimo._ast.cell import CellConfig
from marimo._cli.cli import (
_collect_marimo_files,
_create_run_workspace,
+ _split_run_paths_and_args,
main as cli_main,
)
from marimo._config.manager import get_default_config_manager
@@ -1720,6 +1722,102 @@ def test_cli_run_sandbox_prompt_yes() -> None:
p.kill()
+ "tail_args",
+ [
+ ["experiment_name=my-exp", "epochs=25"],
+ ["--key", "value"],
+ ["--key"],
+ ["-key", "value"],
+ ],
+)
+def test_cli_run_double_dash_reaches_splitter(tail_args: list[str]) -> None:
+ runner = CliRunner()
+ captured: dict[str, Any] = {}
+
+ def _capture(
+ name: str,
+ args: tuple[str, ...],
+ args_after_separator: tuple[str, ...] | None = None,
+ ) -> tuple[list[str], tuple[str, ...]]:
+ captured["name"] = name
+ captured["args"] = args
+ captured["args_after_separator"] = args_after_separator
+ raise click.ClickException("stop after capture")
+
+ with patch(
+ "marimo._cli.cli._split_run_paths_and_args", side_effect=_capture
+ ):
+ result = runner.invoke(
+ cli_main,
+ ["run", "notebook.py", "--", *tail_args],
+ )
+
+ assert result.exit_code != 0
+ assert "stop after capture" in result.output
+ assert captured["name"] == "notebook.py"
+ assert captured["args"] == tuple(tail_args)
+ assert captured["args_after_separator"] == tuple(tail_args)
+
+
+ ("args", "args_after_separator", "expected"),
+ [
+ (
+ ("experiment_name=my-exp",),
+ ("experiment_name=my-exp",),
+ (["notebook.py"], ("experiment_name=my-exp",)),
+ ),
+ (
+ ("other.py", "arg=value"),
+ ("arg=value",),
+ (["notebook.py", "other.py"], ("arg=value",)),
+ ),
+ (("other.py",), None, (["notebook.py", "other.py"], ())),
+ ],
+)
+def test_split_run_paths_and_args_with_click_separator_state(
+ args: tuple[str, ...],
+ args_after_separator: tuple[str, ...] | None,
+ expected: tuple[list[str], tuple[str, ...]],
+) -> None:
+ assert (
+ _split_run_paths_and_args("notebook.py", args, args_after_separator)
+ == expected
+ )
+
+
+def test_cli_run_double_dash_passes_notebook_argv(tmp_path: Path) -> None:
+ runner = CliRunner()
+ notebook = tmp_path / "notebook.py"
+ notebook.write_text("")
+
+ with (
+ patch(
+ "marimo._cli.cli.prompt_run_in_docker_container",
+ return_value=False,
+ ),
+ patch("marimo._cli.cli.check_app_correctness"),
+ patch("marimo._cli.cli.start") as mock_start,
+ ):
+ result = runner.invoke(
+ cli_main,
+ [
+ "run",
+ str(notebook),
+ "--headless",
+ "--no-token",
+ "--",
+ "experiment_name=my-exp",
+ ],
+ )
+
+ assert result.exit_code == 0, result.output
+ mock_start.assert_called_once()
+ assert mock_start.call_args.kwargs["argv"] == ["experiment_name=my-exp"]
+ assert mock_start.call_args.kwargs["cli_args"] == {}
+
+
def test_cli_with_custom_pyproject_config(tmp_path: Path) -> None:
# Create a custom pyproject.toml with special marimo config
pyproject_path = tmp_path / "pyproject.toml"