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.

cli: better support for bash completion (#3230)

Myles Scolnickd7c0e43dc497
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index 5ae56b9130..14737de0fe 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -3,9 +3,9 @@ from __future__ import annotations
import json
import os
-import pathlib
import sys
import tempfile
+from pathlib import Path
from typing import Any, Optional
import click
@@ -286,7 +286,7 @@ edit_help_msg = "\n".join(
help=sandbox_message,
)
@click.option("--profile-dir", default=None, type=str, hidden=True)
[email protected]("name", required=False)
[email protected]("name", required=False, type=click.Path())
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
def edit(
port: Optional[int],
@@ -575,7 +575,7 @@ Example:
type=bool,
help=sandbox_message,
)
[email protected]("name", required=True)
[email protected]("name", required=True, type=click.Path())
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
def run(
port: Optional[int],
@@ -643,15 +643,12 @@ def run(
@main.command(help="Recover a marimo notebook from JSON.")
[email protected]("name", required=True)
+ "name",
+ required=True,
+ type=click.Path(exists=True, file_okay=True, dir_okay=False),
+)
def recover(name: str) -> None:
- path = pathlib.Path(name)
- if not os.path.exists(name):
- raise click.UsageError("Invalid NAME - %s does not exist" % name)
-
- if not path.is_file():
- raise click.UsageError("Invalid NAME - %s is not a file" % name)
-
click.echo(codegen.recover(name))
@@ -754,6 +751,54 @@ def env() -> None:
click.echo(json.dumps(get_system_info(), indent=2))
+ help="Install shell completions for marimo. Supports bash, zsh, fish, and elvish."
+)
+def shell_completion() -> None:
+ shell = os.environ.get("SHELL", "")
+ if not shell:
+ click.echo(
+ "Could not determine shell. Please set $SHELL environment variable.",
+ err=True,
+ )
+ return
+
+ shell_name = Path(shell).name
+
+ commands = {
+ "bash": (
+ 'eval "$(_MARIMO_COMPLETE=bash_source marimo)"',
+ ".bashrc",
+ ),
+ "zsh": (
+ 'eval "$(_MARIMO_COMPLETE=zsh_source marimo)"',
+ ".zshrc",
+ ),
+ "fish": (
+ "_MARIMO_COMPLETE=fish_source marimo | source",
+ ".config/fish/completions/marimo.fish",
+ ),
+ }
+
+ if shell_name not in commands:
+ supported = ", ".join(commands.keys())
+ click.echo(
+ f"Unsupported shell: {shell_name}. Supported shells: {supported}",
+ err=True,
+ )
+ return
+
+ cmd, rc_file = commands[shell_name]
+ click.secho("Run this command to enable completions:", fg="green")
+ click.secho(f"\n echo '{cmd}' >> ~/{rc_file}\n", fg="yellow")
+ click.secho(
+ "\nThen restart your shell or run 'source ~/"
+ + rc_file
+ + "' to enable completions",
+ fg="green",
+ )
+
+
main.command()(convert)
main.add_command(export)
main.add_command(config)
diff --git a/marimo/_cli/convert/commands.py b/marimo/_cli/convert/commands.py
index 19c8bf7265..d3bd11a097 100644
--- a/marimo/_cli/convert/commands.py
+++ b/marimo/_cli/convert/commands.py
@@ -16,7 +16,7 @@ from marimo._utils.paths import maybe_make_dirs
@click.option(
"-o",
"--output",
- type=str,
+ type=click.Path(),
default=None,
help=(
"Output file to save the converted notebook to. "
diff --git a/marimo/_cli/development/commands.py b/marimo/_cli/development/commands.py
index 10fbef596d..839e1db002 100644
--- a/marimo/_cli/development/commands.py
+++ b/marimo/_cli/development/commands.py
@@ -340,10 +340,12 @@ def killall() -> None:
@click.command(
help="Inline packages according to PEP 723", name="inline-packages"
)
[email protected]("name", required=True)
-def inline_packages(
- name: str,
-) -> None:
+ "name",
+ required=True,
+ type=click.Path(exists=True, file_okay=True, dir_okay=False),
+)
+def inline_packages(name: str) -> None:
"""
Example usage:
diff --git a/marimo/_cli/export/commands.py b/marimo/_cli/export/commands.py
index 319653b24c..4a0acc4997 100644
--- a/marimo/_cli/export/commands.py
+++ b/marimo/_cli/export/commands.py
@@ -7,7 +7,6 @@ from typing import TYPE_CHECKING, Callable, Literal, Optional
import click
-import marimo._cli.cli_validators as validators
from marimo._cli.parse_args import parse_args
from marimo._cli.print import echo, green
from marimo._dependencies.dependencies import DependencyManager
@@ -126,14 +125,18 @@ Optionally pass CLI args to the notebook:
@click.option(
"-o",
"--output",
- type=str,
+ type=click.Path(),
default=None,
help=(
"Output file to save the HTML to. "
"If not provided, the HTML will be printed to stdout."
),
)
[email protected]("name", required=True, callback=validators.is_file_path)
+ "name",
+ required=True,
+ type=click.Path(exists=True, file_okay=True, dir_okay=False),
+)
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
def html(
name: str,
@@ -183,14 +186,18 @@ Watch for changes and regenerate the script on modification:
@click.option(
"-o",
"--output",
- type=str,
+ type=click.Path(),
default=None,
help=(
"Output file to save the script to. "
"If not provided, the script will be printed to stdout."
),
)
[email protected]("name", required=True, callback=validators.is_file_path)
+ "name",
+ required=True,
+ type=click.Path(exists=True, file_okay=True, dir_okay=False),
+)
def script(
name: str,
output: str,
@@ -229,14 +236,18 @@ Watch for changes and regenerate the script on modification:
@click.option(
"-o",
"--output",
- type=str,
+ type=click.Path(),
default=None,
help=(
"Output file to save the markdown to. "
"If not provided, markdown will be printed to stdout."
),
)
[email protected]("name", required=True, callback=validators.is_file_path)
+ "name",
+ required=True,
+ type=click.Path(exists=True, file_okay=True, dir_okay=False),
+)
def md(
name: str,
output: str,
@@ -284,7 +295,7 @@ Requires nbformat to be installed.
@click.option(
"-o",
"--output",
- type=str,
+ type=click.Path(),
default=None,
help=(
"Output file to save the ipynb file to. "
@@ -298,7 +309,11 @@ Requires nbformat to be installed.
type=bool,
help="Run the notebook and include outputs in the exported ipynb file.",
)
[email protected]("name", required=True, callback=validators.is_file_path)
+ "name",
+ required=True,
+ type=click.Path(exists=True, file_okay=True, dir_okay=False),
+)
def ipynb(
name: str,
output: str,
@@ -348,7 +363,7 @@ and cannot be opened directly from the file system (e.g. file://).
@click.option(
"-o",
"--output",
- type=str,
+ type=click.Path(),
required=True,
help="Output directory to save the HTML to.",
)
@@ -366,7 +381,11 @@ and cannot be opened directly from the file system (e.g. file://).
show_default=True,
help="Whether to show code by default in the exported HTML file.",
)
[email protected]("name", required=True, callback=validators.is_file_path)
+ "name",
+ required=True,
+ type=click.Path(exists=True, file_okay=True, dir_okay=False),
+)
def html_wasm(
name: str,
output: str,
diff --git a/tests/_cli/test_cli.py b/tests/_cli/test_cli.py
index f22db96f73..d336b54295 100644
--- a/tests/_cli/test_cli.py
+++ b/tests/_cli/test_cli.py
@@ -798,6 +798,15 @@ def test_cli_run_sandbox_prompt_yes() -> None:
p.kill()
+def test_shell_completion() -> None:
+ p = subprocess.run(
+ ["marimo", "shell-completion"],
+ capture_output=True,
+ )
+ assert p.returncode == 0
+ assert p.stdout is not None
+
+
HAS_DOCKER = DependencyManager.which("docker")