From de2e91fd01d02e538e41af8c6ec5a853755153e2 Mon Sep 17 00:00:00 2001 From: Thao Nguyen <13851786+beanallergy@users.noreply.github.com> Date: Sun, 30 Jun 2024 17:46:50 +0300 Subject: [PATCH] improvement: list available commands for cli --- spotify2ytmusic/__main__.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/spotify2ytmusic/__main__.py b/spotify2ytmusic/__main__.py index e9e5ea6..e9f715b 100644 --- a/spotify2ytmusic/__main__.py +++ b/spotify2ytmusic/__main__.py @@ -2,16 +2,26 @@ from . import cli import sys +import inspect + +def list_commands(module): + # include only functions defined in e.g. 'cli' module + commands = [name for name, obj in inspect.getmembers(module) if inspect.isfunction(obj)] + return commands + +available_commands = list_commands(cli) if len(sys.argv) < 2: print(f"usage: spotify2ytmusic [COMMAND] ") - print(" For example, try 'list_playlists'") + print("Available commands:", ", ".join(available_commands)) + print(" For example, try 'spotify2ytmusic list_playlists'") sys.exit(1) -if not hasattr(cli, sys.argv[1]): +if sys.argv[1] not in available_commands: print( - f"ERROR: Unknown command, see https://github.com/linsomniac/spotify_to_ytmusic" + f"ERROR: Unknown command '{sys.argv[1]}', see https://github.com/linsomniac/spotify_to_ytmusic" ) + print("Available commands: ", ", ".join(available_commands)) sys.exit(1) fn = getattr(cli, sys.argv[1])