From 85260ed99d9b6fe7b603a923ddd89a3596fc381d Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Sat, 24 Jun 2023 18:44:22 +0200 Subject: [PATCH] Apply command defaults from settings --- toot/console.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/toot/console.py b/toot/console.py index 39842c4..a280e52 100644 --- a/toot/console.py +++ b/toot/console.py @@ -10,6 +10,7 @@ from itertools import chain from toot import config, commands, CLIENT_NAME, CLIENT_WEBSITE, __version__ from toot.exceptions import ApiError, ConsoleError from toot.output import print_out, print_err +from toot.settings import get_setting VISIBILITY_CHOICES = ["public", "unlisted", "private", "direct"] VISIBILITY_CHOICES_STR = ", ".join(f"'{v}'" for v in VISIBILITY_CHOICES) @@ -882,12 +883,24 @@ def get_argument_parser(name, command): if command.require_auth: combined_args += common_auth_args + defaults = get_setting(f"commands.{name}", dict, {}) + for args, kwargs in combined_args: + # Set default value from settings if exists + default = get_default_value(defaults, args) + if default is not None: + kwargs["default"] = default parser.add_argument(*args, **kwargs) return parser +def get_default_value(defaults, args): + # Hacky way to determine command name from argparse args + name = args[-1].lstrip("-").replace("-", "_") + return defaults.get(name) + + def run_command(app, user, name, args): command = next((c for c in COMMANDS if c.name == name), None)