refactor: follow more guidelines

This commit is contained in:
2025-04-03 17:32:44 -04:00
parent f360566824
commit ec31250153
20 changed files with 122 additions and 86 deletions

View File

@@ -8,7 +8,9 @@ import utils
class ArgumentParser:
def __init__(self, command, description):
self.parser = argparse.ArgumentParser(
command, description=description, exit_on_error=False
command,
description=description,
exit_on_error=False,
)
def print_help(self):
@@ -26,21 +28,20 @@ class ArgumentParser:
async def parse_args(self, message, tokens) -> argparse.Namespace | None:
try:
with contextlib.redirect_stdout(io.StringIO()):
args = self.parser.parse_args(tokens[1:])
return args
return self.parser.parse_args(tokens[1:])
except SystemExit:
await utils.reply(message, f"```\n{self.print_help()}```")
except Exception as e:
await utils.reply(message, f"`{e}`")
def range_type(string: str, min=0, max=100):
def range_type(string: str, lower=0, upper=100) -> int:
try:
value = int(string)
except ValueError:
raise argparse.ArgumentTypeError("value is not a valid integer")
except ValueError as e:
raise argparse.ArgumentTypeError("value is not a valid integer") from e
if min <= value <= max:
if lower <= value <= upper:
return value
else:
raise argparse.ArgumentTypeError(f"value is not in range {min}-{max}")
raise argparse.ArgumentTypeError(f"value is not in range {lower}-{upper}")