]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blobdiff - autorandr.py
Improve usage information with GetoptError
[deb_pkgs/autorandr.git] / autorandr.py
index 42a9dac3e96a931f8bf48e77a36f4f575fdf2ddf..285c751bda4294e4dd0e07846320d37c5deebd2f 100755 (executable)
@@ -37,6 +37,9 @@ from distutils.version import LooseVersion as Version
 from itertools import chain
 from collections import OrderedDict
 
+import posix
+
+
 virtual_profiles = [
     # (name, description, callback)
     ("common", "Clone all connected outputs at the largest common resolution", None),
@@ -97,7 +100,7 @@ class AutorandrException(Exception):
         if self.line:
             retval.append(" (line %d)" % self.line)
         if self.original_exception:
-            retval.append(":\n  " % self.line)
+            retval.append(":\n  ")
             retval.append(str(self.original_exception).replace("\n", "\n  "))
         if self.report_bug:
             retval.append("\nThis appears to be a bug. Please help improving autorandr by reporting it upstream."
@@ -606,8 +609,10 @@ def main(argv):
     try:
        options = dict(getopt.getopt(argv[1:], "s:l:d:cfh", [ "dry-run", "change", "default=", "save=", "load=", "force", "fingerprint", "config", "help" ])[0])
     except getopt.GetoptError as e:
-        print(str(e))
-        options = { "--help": True }
+        print("Failed to parse options: {0}.\n"
+              "Use --help to get usage information.".format(str(e)),
+              file=sys.stderr)
+        sys.exit(posix.EX_USAGE)
 
     profiles = {}
     try:
@@ -713,12 +718,12 @@ if __name__ == '__main__':
     try:
         main(sys.argv)
     except AutorandrException as e:
-        print(file=sys.stderr)
         print(e, file=sys.stderr)
         sys.exit(1)
     except Exception as e:
-        trace = sys.exc_info()[2]
-        while trace.tb_next:
-            trace = trace.tb_next
-            print("\nUnhandled exception in line %d. Please report this as a bug:\n  %s" % (trace.tb_lineno, "\n  ".join(str(e).split("\n")),), file=sys.stderr)
-        sys.exit(1)
+        if not len(str(e)):  # BdbQuit
+            print("Exception: {0}".format(e.__class__.__name__))
+            sys.exit(2)
+
+        print("Unhandled exception ({0}). Please report this as a bug.".format(e), file=sys.stderr)
+        raise