From 0bf1a00a5f47ad1a229a991f4d122d446820c801 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 11 Apr 2021 02:20:32 +0200 Subject: [PATCH] Added --help and --logging for command line use. --- CHANGELOG.md | 2 ++ TODO.md | 3 ++- pesterchum.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 419319d..fd0e33e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - Server prompt + interface for adding & removing servers. - Custom text for numerous themes. (Mostly just made things consistent, for example, "REPORT BUG" to "Report Bug" for Trollian.) - Added theme support for "Beep on Message", "Flash on Message", "Mute Notifications". +- "Usage:" for pesterchum.py when running from the command line. +- Made logging level configurable via command line arguments. ### Fixed - Fixed current mood icon not showing up. diff --git a/TODO.md b/TODO.md index 9c7ca11..1c78aa6 100644 --- a/TODO.md +++ b/TODO.md @@ -11,4 +11,5 @@ - No error message when Pesterchum fails to join a channel. (For example, when the channel name length is over CHANNELLEN) - Choose memo window doesn't get updated on theme change. - Right click menu's color doesn't get updated on theme change in memos. - - Closed windows sometimes stay open. \ No newline at end of file + - Closed windows sometimes stay open. + - help() causes console to crash...? \ No newline at end of file diff --git a/pesterchum.py b/pesterchum.py index 9a23e99..9bb9ca3 100644 --- a/pesterchum.py +++ b/pesterchum.py @@ -8,8 +8,71 @@ except NameError: if os.path.dirname(sys.argv[0]): os.chdir(os.path.dirname(sys.argv[0])) + +print("Usage: pesterchum.py [OPTIONS]\n") +print("Use -h/--help to see the available options.") +#print("sys.argv: " + str(sys.argv[1:]) + '\n') + +# Help +if ('--help' in sys.argv[1:]) or ('-h' in sys.argv[1:]): + print("Possible arguments:") + help_arguments = " -l, --logging\n Specify level of logging, possible values are:\n" + \ + " CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET.\n" + \ + " The default value is WARNING.\n" + \ + " (See https://docs.python.org/3/library/logging.html)\n\n" + \ + " -s, --server\n Specify server override. (legacy)\n\n" + \ + " -p, --port\n Specify port override. (legacy)\n\n" + \ + " --advanced\n Enable advanced.\n\n" + \ + " --no-honk\n Disable honking.\n" + print(help_arguments) + sys.exit() + import logging -logging.basicConfig(level=logging.DEBUG) + +if ('--logging' in sys.argv[1:]) or ('-l' in sys.argv[1:]) & (False == ('--logging' in sys.argv[1:]) and ('-l' in sys.argv[1:])): + try: + # If both are specified, this does not run. + if ('-l' in sys.argv[1:]): + loglevel = sys.argv[sys.argv.index('-l') + 1] + if ('--logging' in sys.argv[1:]): + loglevel = sys.argv[sys.argv.index('--logging') + 1] + + loglevel = loglevel.upper() + + if loglevel == "CRITICAL": + loglevel = 50 + print("Logging Level is CRITICAL") + elif loglevel == "ERROR": + loglevel = 40 + print("Logging Level is ERROR") + elif loglevel == "WARNING": + loglevel = 30 + print("Logging Level is WARNING") + elif loglevel == "INFO": + loglevel = 20 + print("Logging Level is INFO") + elif loglevel == "DEBUG": + loglevel = 10 + print("Logging Level is DEBUG") + elif loglevel == "NOTSET": + loglevel = 0 + print("Logging Level is NOTSET") + else: + raise Exception + logging.basicConfig(level=loglevel) + + # Remove from argv because the rest of the code can't handle it :/ + if ('-l' in sys.argv[1:]): + sys.argv.pop(sys.argv.index('-l') + 1) + sys.argv.pop(sys.argv.index('-l')) + if ('--logging' in sys.argv[1:]): + sys.argv.pop(sys.argv.index('--logging') + 1) + sys.argv.pop(sys.argv.index('--logging')) + except: + print("Invalid syntax.") + logging.basicConfig(level=30) # Warning +else: + logging.basicConfig(level=30) # Warning from datetime import * import random @@ -1108,6 +1171,7 @@ class PesterWindow(MovingWindow): self.portOverride = options["port"] if "honk" in options: self.honk = options["honk"] + else: self.honk = True self.modes = ""