Skip to content

Commit a04d1cf

Browse files
committed
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.
1 parent a447c48 commit a04d1cf

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Features
1010
* Let `help <keyword>` list similar keywords when not found.
1111
* Optionally highlight fuzzy search previews.
1212
* Make `\edit` synonymous with the `\e` command.
13+
* More liberally accept `on`/`off` values for `true`/`false`, and vice versa.
1314

1415

1516
Bug Fixes

mycli/main.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,9 +1786,9 @@ def get_last_query(self) -> str | None:
17861786
@click.option(
17871787
'--use-keyring',
17881788
'use_keyring_cli_opt',
1789-
type=click.Choice(['true', 'false', 'reset']),
1789+
type=str,
17901790
default=None,
1791-
help='Store and retrieve passwords from the system keyring: true/false/reset.',
1791+
help='Store and retrieve passwords from the system keyring: on/off/reset.',
17921792
)
17931793
@click.option(
17941794
'--keepalive-ticks',
@@ -2158,7 +2158,11 @@ def get_password_from_file(password_file: str | None) -> str | None:
21582158
use_keyring = str_to_bool(mycli.config['main'].get('use_keyring', 'False'))
21592159
reset_keyring = False
21602160
else:
2161-
use_keyring = str_to_bool(use_keyring_cli_opt)
2161+
try:
2162+
use_keyring = str_to_bool(use_keyring_cli_opt)
2163+
except ValueError:
2164+
click.secho('Unknown value for --use_keyring', err=True, fg='red')
2165+
sys.exit(1)
21622166
reset_keyring = False
21632167

21642168
# todo: removeme after a period of transition

test/test_main.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,65 @@ def run_query(self, query, new_line=True):
10151015
assert MockMyCli.connect_args['character_set'] == 'utf8mb3'
10161016

10171017

1018+
def test_keyring_arg(monkeypatch):
1019+
# Setup classes to mock mycli.main.MyCli
1020+
class Formatter:
1021+
format_name = None
1022+
1023+
class Logger:
1024+
def debug(self, *args, **args_dict):
1025+
pass
1026+
1027+
def warning(self, *args, **args_dict):
1028+
pass
1029+
1030+
class MockMyCli:
1031+
config = {
1032+
'main': {},
1033+
'alias_dsn': {},
1034+
"connection": {
1035+
"default_keepalive_ticks": 0,
1036+
},
1037+
}
1038+
1039+
def __init__(self, **args):
1040+
self.logger = Logger()
1041+
self.destructive_warning = False
1042+
self.main_formatter = Formatter()
1043+
self.redirect_formatter = Formatter()
1044+
self.ssl_mode = 'auto'
1045+
self.my_cnf = {'client': {}, 'mysqld': {}}
1046+
self.default_keepalive_ticks = 0
1047+
1048+
def connect(self, **args):
1049+
MockMyCli.connect_args = args
1050+
1051+
def run_query(self, query, new_line=True):
1052+
pass
1053+
1054+
import mycli.main
1055+
1056+
monkeypatch.setattr(mycli.main, 'MyCli', MockMyCli)
1057+
runner = CliRunner()
1058+
1059+
result = runner.invoke(mycli.main.cli, args=['--use-keyring', 'True'])
1060+
assert result.exit_code == 0, result.output + ' ' + str(result.exception)
1061+
assert MockMyCli.connect_args['use_keyring'] is True
1062+
1063+
result = runner.invoke(mycli.main.cli, args=['--use-keyring', 'on'])
1064+
assert result.exit_code == 0, result.output + ' ' + str(result.exception)
1065+
assert MockMyCli.connect_args['use_keyring'] is True
1066+
1067+
result = runner.invoke(mycli.main.cli, args=['--use-keyring', 'off'])
1068+
assert result.exit_code == 0, result.output + ' ' + str(result.exception)
1069+
assert MockMyCli.connect_args['use_keyring'] is False
1070+
1071+
result = runner.invoke(mycli.main.cli, args=['--use-keyring', 'Reset'])
1072+
assert result.exit_code == 0, result.output + ' ' + str(result.exception)
1073+
assert MockMyCli.connect_args['use_keyring'] is True
1074+
assert MockMyCli.connect_args['reset_keyring'] is True
1075+
1076+
10181077
def test_ssh_config(monkeypatch):
10191078
# Setup classes to mock mycli.main.MyCli
10201079
class Formatter:

0 commit comments

Comments
 (0)