From 79f58a47068989a8c960e527d9f7632c08779120 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Sat, 21 Feb 2026 11:36:53 -0500 Subject: [PATCH] accept on/off values for --use-keyring Accept on/off values for --use-keyring, keeping true/false, as well as treating "reset" as special. The motivation is uniformity in the interface. The only loss is that click does not display the value choices in the helpdoc, but they are already listed in the description. Here we also change the description to suggest on/off, while continuing to silently accept true/false. The reason for the change is that --ssl-mode came first, and --ssl-mode takes on/off. --- changelog.md | 1 + mycli/main.py | 10 ++++++--- test/test_main.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index c51d4e9c..360e5af8 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,7 @@ Features * Let the `--dsn` argument accept literal DSNs as well as aliases. * Accept `--character-set` as an alias for `--charset` at the CLI. * Add SSL/TLS version to `status` output. +* More liberally accept `on`/`off` values for `true`/`false`, and vice versa. Bug Fixes diff --git a/mycli/main.py b/mycli/main.py index a3899fe0..7209369f 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -1725,9 +1725,9 @@ def get_last_query(self) -> str | None: @click.option( '--use-keyring', 'use_keyring_cli_opt', - type=click.Choice(['true', 'false', 'reset']), + type=str, default=None, - help='Store and retrieve passwords from the system keyring: true/false/reset.', + help='Store and retrieve passwords from the system keyring: on/off/reset.', ) @click.option("--checkup", is_flag=True, help="Run a checkup on your config file.") @click.pass_context @@ -2070,7 +2070,11 @@ def get_password_from_file(password_file: str | None) -> str | None: use_keyring = str_to_bool(mycli.config['main'].get('use_keyring', 'False')) reset_keyring = False else: - use_keyring = str_to_bool(use_keyring_cli_opt) + try: + use_keyring = str_to_bool(use_keyring_cli_opt) + except ValueError: + click.secho('Unknown value for --use_keyring', err=True, fg='red') + sys.exit(1) reset_keyring = False # todo: removeme after a period of transition diff --git a/test/test_main.py b/test/test_main.py index 1415f598..daf0222c 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -930,6 +930,61 @@ def run_query(self, query, new_line=True): ) +def test_keyring_arg(monkeypatch): + # Setup classes to mock mycli.main.MyCli + class Formatter: + format_name = None + + class Logger: + def debug(self, *args, **args_dict): + pass + + def warning(self, *args, **args_dict): + pass + + class MockMyCli: + config = { + 'main': {}, + 'alias_dsn': {}, + } + + def __init__(self, **args): + self.logger = Logger() + self.destructive_warning = False + self.main_formatter = Formatter() + self.redirect_formatter = Formatter() + self.ssl_mode = 'auto' + self.my_cnf = {'client': {}, 'mysqld': {}} + + def connect(self, **args): + MockMyCli.connect_args = args + + def run_query(self, query, new_line=True): + pass + + import mycli.main + + monkeypatch.setattr(mycli.main, 'MyCli', MockMyCli) + runner = CliRunner() + + result = runner.invoke(mycli.main.cli, args=['--use-keyring', 'True']) + assert result.exit_code == 0, result.output + ' ' + str(result.exception) + assert MockMyCli.connect_args['use_keyring'] is True + + result = runner.invoke(mycli.main.cli, args=['--use-keyring', 'on']) + assert result.exit_code == 0, result.output + ' ' + str(result.exception) + assert MockMyCli.connect_args['use_keyring'] is True + + result = runner.invoke(mycli.main.cli, args=['--use-keyring', 'off']) + assert result.exit_code == 0, result.output + ' ' + str(result.exception) + assert MockMyCli.connect_args['use_keyring'] is False + + result = runner.invoke(mycli.main.cli, args=['--use-keyring', 'Reset']) + assert result.exit_code == 0, result.output + ' ' + str(result.exception) + assert MockMyCli.connect_args['use_keyring'] is True + assert MockMyCli.connect_args['reset_keyring'] is True + + def test_ssh_config(monkeypatch): # Setup classes to mock mycli.main.MyCli class Formatter: