Skip to content

Commit 974307a

Browse files
committed
Improve CLI help handling for run command
Updated the `run` command so that `--help` is passed to the subcommand if there is one.
1 parent 85f4329 commit 974307a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/dotenv/cli.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def unset(ctx: click.Context, key: Any) -> None:
156156
sys.exit(1)
157157

158158

159-
@cli.command(context_settings={"ignore_unknown_options": True})
159+
@cli.command(context_settings={"ignore_unknown_options": True}, add_help_option=False, no_args_is_help=True)
160160
@click.pass_context
161161
@click.option(
162162
"--override/--no-override",
@@ -180,6 +180,9 @@ def run(ctx: click.Context, override: bool, commandline: List[str]) -> None:
180180
if not commandline:
181181
click.echo("No command given.")
182182
sys.exit(1)
183+
if len(commandline) == 1 and commandline[0] in ["-h", "--help"]:
184+
click.echo(ctx.get_help())
185+
sys.exit(0)
183186
run_command(commandline, dotenv_as_dict)
184187

185188

tests/test_cli.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import os
2+
import subprocess
3+
import sys
24
from pathlib import Path
35
from typing import Optional
46

@@ -249,3 +251,19 @@ def test_run_with_version(cli):
249251

250252
assert result.exit_code == 0
251253
assert result.output.strip().endswith(__version__)
254+
255+
256+
def test_run_subcommand_with_help_uses_subcommand_help(cli, dotenv_path):
257+
dotenv_path.write_text("a=b")
258+
output = sh.dotenv("--file", dotenv_path, "run", "printenv", "--help")
259+
260+
assert "dotenv run" not in output
261+
expected_help_output = subprocess.check_output(["printenv", "--help"]).decode("utf-8")
262+
assert output == expected_help_output
263+
264+
265+
def test_run_with_just_help_show_help(cli, dotenv_path):
266+
dotenv_path.write_text("a=b")
267+
output = sh.dotenv("--file", dotenv_path, "run", "--help")
268+
269+
assert "dotenv run" in output

0 commit comments

Comments
 (0)