From 0848a6f7dfa6415f331d9ef90cac14ae0c0e0589 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Thu, 7 Dec 2023 19:41:30 +0100 Subject: [PATCH] Add shell completion for account names --- toot/cli/auth.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/toot/cli/auth.py b/toot/cli/auth.py index 12d1a74..e5a7c8e 100644 --- a/toot/cli/auth.py +++ b/toot/cli/auth.py @@ -2,6 +2,9 @@ import click import platform import sys import webbrowser +from click.shell_completion import CompletionItem + +from click.types import StringParamType from toot import api, config, __version__ from toot.auth import get_or_create_app, login_auth_code, login_username_password @@ -19,6 +22,18 @@ instance_option = click.option( ) +class AccountParamType(StringParamType): + """Custom type to add shell completion for account names""" + + def shell_complete(self, ctx, param, incomplete: str): + accounts = config.load_config()["users"].keys() + return [ + CompletionItem(a) + for a in accounts + if a.lower().startswith(incomplete.lower()) + ] + + @cli.command() def auth(): """Show logged in accounts and instances""" @@ -101,7 +116,7 @@ def login(base_url: str): @cli.command() -@click.argument("account", required=False) +@click.argument("account", type=AccountParamType(), required=False) def logout(account: str): """Log out of ACCOUNT, delete stored access keys""" accounts = _get_accounts_list() @@ -119,7 +134,7 @@ def logout(account: str): @cli.command() -@click.argument("account", required=False) +@click.argument("account", type=AccountParamType(), required=False) def activate(account: str): """Switch to logged in ACCOUNT.""" accounts = _get_accounts_list()