diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index c548aa39..1d5783be 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -156,7 +156,7 @@ def unset(ctx: click.Context, key: Any) -> None: sys.exit(1) -@cli.command(context_settings={"ignore_unknown_options": True}) +@cli.command(context_settings={"ignore_unknown_options": True}, add_help_option=False) @click.pass_context @click.option( "--override/--no-override", @@ -180,6 +180,9 @@ def run(ctx: click.Context, override: bool, commandline: List[str]) -> None: if not commandline: click.echo("No command given.") sys.exit(1) + if len(commandline) == 1 and commandline[0] in ["-h", "--help"]: + click.echo(ctx.get_help()) + sys.exit(0) run_command(commandline, dotenv_as_dict) diff --git a/tests/test_cli.py b/tests/test_cli.py index 343fdb23..8accfafc 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,4 +1,5 @@ import os +import subprocess from pathlib import Path from typing import Optional @@ -249,3 +250,21 @@ def test_run_with_version(cli): assert result.exit_code == 0 assert result.output.strip().endswith(__version__) + + +def test_run_subcommand_with_help_uses_subcommand_help(cli, dotenv_path): + dotenv_path.write_text("a=b") + output = sh.dotenv("--file", dotenv_path, "run", "printenv", "--help") + + assert "dotenv run" not in output + expected_help_output = subprocess.check_output(["printenv", "--help"]).decode( + "utf-8" + ) + assert output == expected_help_output + + +def test_run_with_just_help_show_help(cli, dotenv_path): + dotenv_path.write_text("a=b") + output = sh.dotenv("--file", dotenv_path, "run", "--help") + + assert "dotenv run" in output