Skip to content

Commit

Permalink
add cli for starting bot after program opens (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmiglio authored Nov 4, 2023
1 parent 5a34cc8 commit 8b69d90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/pyclashbot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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"]:
Expand Down Expand Up @@ -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)
19 changes: 19 additions & 0 deletions src/pyclashbot/utils/cli_config.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 8b69d90

Please sign in to comment.