From 155b0bfa23b877110b35414d0f5d818c0f5906b6 Mon Sep 17 00:00:00 2001 From: Matthias Andree Date: Sat, 24 Apr 2021 14:34:34 +0200 Subject: fetchmailconf: properly catch and report option parsing errors --- fetchmailconf.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'fetchmailconf.py') diff --git a/fetchmailconf.py b/fetchmailconf.py index 8722d48a..909975d1 100755 --- a/fetchmailconf.py +++ b/fetchmailconf.py @@ -35,12 +35,25 @@ except: # define a dummy class to inherit from class Frame: pass -VERSION = "1.63.4" +VERSION = "1.63.5" MIN_PY = (2, 7, 13) if sys.version_info < MIN_PY: sys.exit("fetchmailconf: Python %s.%s.%s or later is required.\n" % MIN_PY) +# +# Display usage information and pass status argument through to sys.exit() +# +def usage(status): + print(""" +Usage: fetchmailconf {[-d] [-f fetchmailrc]|-h|--help|-V|--version} + -d - dump configuration (for debugging) + -f fmrc - read alternate fetchmailrc file +--help, -h - print this help text and quit +--version, -V - print fetchmailconf version and quit +""") + sys.exit(status) + # # Define the data structures the GUIs will be tossing around # @@ -2185,7 +2198,12 @@ gUSiYASJpMEHhilJTEnhAlGoQqYAZQ1AiqEMZ0jDGtqQImhwwA13yMMevoQAGvGhEAWHGMOAAAA7 # # Process options - options, arguments = getopt.getopt(sys.argv[1:], "df:hV", ["help", "version"]) + try: + options, arguments = getopt.getopt(sys.argv[1:], "df:hV", ["help", "version"]) + except getopt.GetoptError as e: + usage(str(e)) + except Exception as e: + raise e dump = rcfile = None for (switch, val) in options: if switch == '-d': @@ -2193,14 +2211,7 @@ gUSiYASJpMEHhilJTEnhAlGoQqYAZQ1AiqEMZ0jDGtqQImhwwA13yMMevoQAGvGhEAWHGMOAAAA7 elif switch == '-f': rcfile = val elif switch == '-h' or switch == '--help': - print(""" -Usage: fetchmailconf {[-d] [-f fetchmailrc]|-h|--help|-V|--version} - -d - dump configuration (for debugging) - -f fmrc - read alternate fetchmailrc file ---help, -h - print this help text and quit ---version, -V - print fetchmailconf version and quit -""") - sys.exit(0) + usage(0) elif switch == '-V' or switch == '--version': print("fetchmailconf %s" % VERSION) print("Running on python", sys.version) @@ -2212,6 +2223,9 @@ welcome to redistribute it under certain conditions. Please see the file COPYING in the source or documentation directory for details.""") sys.exit(0) + if arguments: + usage("Extra arguments: '" + "' '".join(arguments) + "'") + if "DISPLAY" not in os.environ: sys.exit("fetchmailconf must be run under X") -- cgit v1.2.3