From 29825d6b3c983eb5acdd7904247b697e09dfdcba Mon Sep 17 00:00:00 2001 From: rhpidfyre Date: Fri, 6 Jun 2025 14:42:48 -0400 Subject: [PATCH] Utilize `cli.py` module for handling both arguments and argv --- src/cli.py | 29 +++++++++++++++++++++++------ src/main.py | 22 ++++++++-------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/cli.py b/src/cli.py index f845e8a..02d08d5 100644 --- a/src/cli.py +++ b/src/cli.py @@ -1,9 +1,26 @@ import argparse +import sys -def interface(): - parser = argparse.ArgumentParser() - parser.add_argument("--cpu-temp-critical", type=int, default=80, help="cpu tempature crtitical. default = 80") - parser.add_argument("--cpu-temp-warning", type=int, default=70, help="cpu tempature warning. default = 70") - parser.add_argument("--update-rate", type=int, default=1, help="how often updates happen in seconds. default = 1") +from typing import Optional - return parser.parse_args() \ No newline at end of file +_ntfy_configure_prompt = """\033[4mPlease configure an ntfy url before starting.\033[0m +Examples: +\033[32mpython3 main.py 10.0.13.37:42069 +python3 main.py ntfy.domain.com\033[0m""" + +class Interface: + def __init__(self): + self.parser = argparse.ArgumentParser() + self.parser.add_argument("--cpu-temp-critical", type=int, default=80, help="cpu tempature crtitical. default = 80") + self.parser.add_argument("--cpu-temp-warning", type=int, default=70, help="cpu tempature warning. default = 70") + self.parser.add_argument("--update-rate", type=int, default=1, help="how often updates happen in seconds. default = 1") + + def parsed_args(self): + return self.parser.parse_args() + + def argv_1(self) -> Optional[str]: + if len(sys.argv) > 1: + return sys.argv[1] + else: + print(_ntfy_configure_prompt) + return None \ No newline at end of file diff --git a/src/main.py b/src/main.py index d324ad9..24a4edc 100644 --- a/src/main.py +++ b/src/main.py @@ -1,5 +1,4 @@ import time -import sys import package import cli @@ -7,11 +6,6 @@ import cpu from ntfy import Ntfy -_ntfy_configure_prompt = """\033[4mPlease configure an ntfy url before starting.\033[0m -Examples: -\033[32mpython3 main.py 10.0.13.37:42069 -python3 main.py ntfy.domain.com\033[0m""" - def start( cpu_critical_temp: int, cpu_warning_temp: int, @@ -30,13 +24,13 @@ def start( if __name__ == "__main__": if package.installed("lm-sensors"): - if len(sys.argv) > 1: - cli_args = cli.interface() + cli_args = cli.Interface() + parsed = cli_args.parsed_args() + ntfy_server = cli_args.argv_1() + if ntfy_server: start( - cli_args.cpu_temp_critical, - cli_args.cpu_temp_warning, - sys.argv[1], - cli_args.update_rate, + parsed.cpu_temp_critical, + parsed.cpu_temp_warning, + ntfy_server, + parsed.update_rate, ) - else: - print(_ntfy_configure_prompt) \ No newline at end of file