From 8b69d9048aecaaff7e8b34255bf46fa9143c83ad Mon Sep 17 00:00:00 2001 From: Martin Miglio Date: Sat, 4 Nov 2023 14:46:09 -0400 Subject: [PATCH] add cli for starting bot after program opens (#323) --- src/pyclashbot/__main__.py | 13 +++++++++---- src/pyclashbot/utils/cli_config.py | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/pyclashbot/utils/cli_config.py diff --git a/src/pyclashbot/__main__.py b/src/pyclashbot/__main__.py index 96d20e0f0..bb16bd7d2 100644 --- a/src/pyclashbot/__main__.py +++ b/src/pyclashbot/__main__.py @@ -15,6 +15,7 @@ check_user_settings, read_user_settings, ) +from pyclashbot.utils.cli_config import arg_parser from pyclashbot.utils.logger import Logger, initalize_pylogging from pyclashbot.utils.thread import PausableThread, StoppableThread @@ -246,9 +247,9 @@ def start_button_event(logger: Logger, window, values) -> WorkerThread | None: # setup the main thread and start it - args = job_dictionary + thread_args = job_dictionary # args: tuple[list[str], int] = (jobs, acc_count) - thread = WorkerThread(logger, args) + thread = WorkerThread(logger, thread_args) thread.start() # enable the stop button after the thread is started @@ -340,7 +341,7 @@ def handle_thread_finished( return thread, logger -def main_gui(settings: None | dict[str, str] = None) -> None: +def main_gui(start_on_run=False, settings: None | dict[str, str] = None) -> None: """method for displaying the main gui""" # create gui window window = create_window() @@ -355,6 +356,9 @@ def main_gui(settings: None | dict[str, str] = None) -> None: # run the gui while True: event, values = read_window(window, timeout=10) + if start_on_run: + event = "Start" + start_on_run = False # on exit event, kill any existing thread if event in [sg.WIN_CLOSED, "Exit"]: @@ -419,4 +423,5 @@ def main_gui(settings: None | dict[str, str] = None) -> None: if __name__ == "__main__": - main_gui() + cli_args = arg_parser() + main_gui(start_on_run=cli_args.start) diff --git a/src/pyclashbot/utils/cli_config.py b/src/pyclashbot/utils/cli_config.py new file mode 100644 index 000000000..e54112f49 --- /dev/null +++ b/src/pyclashbot/utils/cli_config.py @@ -0,0 +1,19 @@ +"""Module to parse arguments from CLI""" +from argparse import ArgumentParser, Namespace + + +def arg_parser() -> Namespace: + """function to parse arguments + + Returns: + Namespace: populated namespace from arguments + """ + parser = ArgumentParser(description="Run py-clash-bot from CLI") + parser.add_argument( + "--start", + "-s", + dest="start", + action="store_true", + help="Start the bot when the program opens", + ) + return parser.parse_args()