From cd420f6a19624d476a971a746bfa432792f0a760 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Sat, 21 Mar 2026 15:33:05 -0400 Subject: [PATCH] return helpdoc for single "-h" "-h" alone cannot be a hostname specification, so when it is the only item in the list of arguments, respond with the helpdoc, matching the behavior of "--help". Previously, if the user ran mycli -h the response was only the cryptic Error: Option '-h' requires an argument. --- changelog.md | 8 ++++++++ mycli/main.py | 9 ++++++++- test/test_main.py | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index ace0426e..4cb836d5 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,11 @@ +Upcoming (TBD) +============== + +Features +--------- +* Respond to `-h` alone with the helpdoc. + + 1.66.0 (2026/03/21) ============== diff --git a/mycli/main.py b/mycli/main.py index d5f2b403..fbe19746 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -2706,10 +2706,17 @@ def read_ssh_config(ssh_config_path: str): return ssh_config +def filtered_sys_argv() -> list[str]: + args = sys.argv[1:] + if args == ['-h']: + args = ['--help'] + return args + + def main() -> int | None: try: result = click_entrypoint.main( - sys.argv[1:], + filtered_sys_argv(), standalone_mode=False, # disable builtin exception handling prog_name='mycli', ) diff --git a/test/test_main.py b/test/test_main.py index f47e5beb..c593f817 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -1,7 +1,7 @@ # type: ignore from collections import namedtuple -from contextlib import redirect_stdout +from contextlib import redirect_stderr, redirect_stdout import csv import io import os @@ -153,6 +153,42 @@ def test_is_valid_connection_scheme_invalid(executor, capsys): assert not is_valid +def test_filtered_sys_argv_maps_single_dash_h_to_help(monkeypatch): + import mycli.main + + monkeypatch.setattr(mycli.main.sys, 'argv', ['mycli', '-h']) + + assert mycli.main.filtered_sys_argv() == ['--help'] + + +def test_filtered_sys_argv_preserves_host_option_usage(monkeypatch): + import mycli.main + + monkeypatch.setattr(mycli.main.sys, 'argv', ['mycli', '-h', 'example.com']) + + assert mycli.main.filtered_sys_argv() == ['-h', 'example.com'] + + +def test_main_dash_h_and_help_have_equivalent_output(monkeypatch): + import mycli.main + + def run_main(argv): + stdout = io.StringIO() + stderr = io.StringIO() + monkeypatch.setattr(mycli.main.sys, 'argv', argv) + with redirect_stdout(stdout), redirect_stderr(stderr): + result = mycli.main.main() + return result, stdout.getvalue(), stderr.getvalue() + + dash_h_result, dash_h_stdout, dash_h_stderr = run_main(['mycli', '-h']) + dash_help_result, dash_help_stdout, dash_help_stderr = run_main(['mycli', '--help']) + + assert dash_h_result == 0 + assert dash_help_result == 0 + assert dash_h_stdout == dash_help_stdout + assert dash_h_stderr == dash_help_stderr + + @dbtest def test_ssl_mode_on(executor, capsys): runner = CliRunner()