An error is now shown for incorrect arguments passed

This commit is contained in:
DevilXD committed 2022-02-15 20:31:12 +01:00
1 parent c8c6b7139d
commit 763bc0a536
2 files changed
+45 -6

No files matched your search

+9
View File
@@ -46,5 +46,14 @@
"console": "integratedTerminal", "console": "integratedTerminal",
"justMyCode": false "justMyCode": false
}, },
{
"name": "Test ArgParser",
"type": "python",
"request": "launch",
"program": "main.py",
"args": ["--wrong"],
"console": "integratedTerminal",
"justMyCode": false
},
] ]
} }
+36 -6
View File
@@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import io
import sys import sys
import ctypes import ctypes
import signal import signal
@@ -7,7 +8,10 @@ import asyncio
import logging import logging
import argparse import argparse
import traceback import traceback
from typing import Optional import tkinter as tk
from pathlib import Path
from tkinter import messagebox
from typing import Optional, NoReturn
from twitch import Twitch from twitch import Twitch
from version import __version__ from version import __version__
@@ -15,6 +19,28 @@ from exceptions import CaptchaRequired
from constants import FORMATTER, LOG_PATH, WINDOW_TITLE from constants import FORMATTER, LOG_PATH, WINDOW_TITLE
# we need an dummy invisible window for the parser
root = tk.Tk()
root.overrideredirect(True)
root.withdraw()
class Parser(argparse.ArgumentParser):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._message: io.StringIO = io.StringIO()
def _print_message(self, message: str, *args, **kwargs) -> None:
self._message.write(message)
# print(message, file=self._message)
def exit(self, *args, **kwargs) -> NoReturn:
try:
super().exit(*args, **kwargs)
finally:
messagebox.showerror("Argument Parser Error", self._message.getvalue())
class ParsedArgs(argparse.Namespace): class ParsedArgs(argparse.Namespace):
_verbose: int _verbose: int
_debug_ws: bool _debug_ws: bool
@@ -58,8 +84,8 @@ class ParsedArgs(argparse.Namespace):
# handle input parameters # handle input parameters
# NOTE: due to using pythonw to run the main script, CLI help via '-h' and generally any # NOTE: due to using pythonw to run the main script, CLI help via '-h' and generally any
# console output is not available. The input arguments still work though. # console output is not available. The input arguments still work though.
parser = argparse.ArgumentParser( parser = Parser(
"Twitch Drops Miner (by DevilXD).exe", Path(sys.argv[0]).name,
description="A program that allows you to mine timed drops on Twitch.", description="A program that allows you to mine timed drops on Twitch.",
) )
parser.add_argument("--version", action="version", version=f"v{__version__}") parser.add_argument("--version", action="version", version=f"v{__version__}")
@@ -68,10 +94,14 @@ parser.add_argument("-g", "--game", default=None)
parser.add_argument("--tray", action="store_true") parser.add_argument("--tray", action="store_true")
parser.add_argument("--log", action="store_true") parser.add_argument("--log", action="store_true")
# undocumented debug args # undocumented debug args
parser.add_argument("--no-run-check", dest="no_run_check", action="store_true") parser.add_argument(
parser.add_argument("--debug-ws", dest="_debug_ws", action="store_true") "--no-run-check", dest="no_run_check", action="store_true", help=argparse.SUPPRESS
parser.add_argument("--debug-gql", dest="_debug_gql", action="store_true") )
parser.add_argument("--debug-ws", dest="_debug_ws", action="store_true", help=argparse.SUPPRESS)
parser.add_argument("--debug-gql", dest="_debug_gql", action="store_true", help=argparse.SUPPRESS)
options: ParsedArgs = parser.parse_args(namespace=ParsedArgs()) options: ParsedArgs = parser.parse_args(namespace=ParsedArgs())
# dummy window isn't needed anymore
root.destroy()
# check if we're not already running # check if we're not already running
try: try:
exists = ctypes.windll.user32.FindWindowW(None, WINDOW_TITLE) exists = ctypes.windll.user32.FindWindowW(None, WINDOW_TITLE)