src/eric7/eric7_trpreviewer.py

branch
eric7
changeset 10303
ee1aadab1215
parent 10238
9ea4634a697e
child 10439
21c28b0f9e41
--- a/src/eric7/eric7_trpreviewer.py	Sat Nov 11 10:13:29 2023 +0100
+++ b/src/eric7/eric7_trpreviewer.py	Sat Nov 11 12:44:51 2023 +0100
@@ -12,48 +12,91 @@
 of the integrated tr previewer.
 """
 
+import argparse
 import os
 import sys
 
 from PyQt6.QtGui import QGuiApplication
 
-for arg in sys.argv[:]:
-    if arg.startswith("--config="):
-        from eric7 import Globals
+
+def createArgparseNamespace():
+    """
+    Function to create an argument parser.
+
+    @return created argument parser object
+    @rtype argparse.ArgumentParser
+    """
+    from eric7.UI.Info import Version
+
+    # 1. create the argument parser
+    parser = argparse.ArgumentParser(
+        description="Translations file previewer of the eric tool suite.",
+        epilog="Copyright (c) 2004 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>.",
+    )
 
-        configDir = arg.replace("--config=", "")
-        Globals.setConfigDir(configDir)
-        sys.argv.remove(arg)
-    elif arg.startswith("--settings="):
-        from PyQt6.QtCore import QSettings
+    # 2. add the arguments
+    parser.add_argument(
+        "-V",
+        "--version",
+        action="version",
+        version="%(prog)s {0}".format(Version),
+        help="show version information and exit",
+    )
+    parser.add_argument(
+        "--config",
+        metavar="config_dir",
+        help="use the given directory as the one containing the config files",
+    )
+    parser.add_argument(
+        "--settings",
+        metavar="settings_dir",
+        help="use the given directory to store the settings files",
+    )
+    parser.add_argument(
+        "file",
+        nargs="*",
+        help="open a list of translation files for previewing",
+    )
 
-        settingsDir = os.path.expanduser(arg.replace("--settings=", ""))
-        if not os.path.isdir(settingsDir):
-            os.makedirs(settingsDir)
-        QSettings.setPath(
-            QSettings.Format.IniFormat, QSettings.Scope.UserScope, settingsDir
-        )
-        sys.argv.remove(arg)
+    # 3. create the Namespace object by parsing the command line
+    args = parser.parse_args()
+    return args
+
+
+args = createArgparseNamespace()
+if args.config:
+    from eric7 import Globals
+
+    Globals.setConfigDir(args.config)
+if args.settings:
+    from PyQt6.QtCore import QSettings
+
+    settingsDir = os.path.expanduser(args.settings)
+    if not os.path.isdir(settingsDir):
+        os.makedirs(settingsDir)
+    QSettings.setPath(
+        QSettings.Format.IniFormat, QSettings.Scope.UserScope, settingsDir
+    )
 
 from eric7.EricWidgets.EricApplication import EricApplication
-from eric7.Globals import AppInfo
 from eric7.Toolbox import Startup
 from eric7.Tools.TRSingleApplication import TRSingleApplicationClient
 
 app = None
 
 
-def createMainWidget(argv):
+def createMainWidget(args):
     """
     Function to create the main widget.
 
-    @param argv list of commandline parameters (list of strings)
-    @return reference to the main widget (QWidget)
+    @param args namespace object containing the parsed command line parameters
+    @type argparse.Namespace
+    @return reference to the main widget
+    @rtype QWidget
     """
     from eric7.Tools.TRPreviewer import TRPreviewer
 
-    files = argv[1:] if len(argv) > 1 else []
-    previewer = TRPreviewer(files, None, "TRPreviewer")
+    previewer = TRPreviewer(args.file, None, "TRPreviewer")
 
     return previewer
 
@@ -66,35 +109,21 @@
 
     QGuiApplication.setDesktopFileName("eric7_trpreviewer")
 
-    options = [
-        (
-            "--config=configDir",
-            "use the given directory as the one containing the config files",
-        ),
-        (
-            "--settings=settingsDir",
-            "use the given directory to store the settings files",
-        ),
-    ]
-    appinfo = AppInfo.makeAppInfo(
-        sys.argv, "eric TR Previewer", "file", "TR file previewer", options
-    )
-
     # set the library paths for plugins
     Startup.setLibraryPaths()
 
-    app = EricApplication(sys.argv)
+    app = EricApplication(args)
     client = TRSingleApplicationClient()
     res = client.connect()
     if res > 0:
-        if len(sys.argv) > 1:
-            client.processArgs(sys.argv[1:])
+        if args.file:
+            client.processArgs(args)
         sys.exit(0)
     elif res < 0:
         print("eric7_trpreviewer: {0}".format(client.errstr()))
         sys.exit(res)
     else:
-        res = Startup.simpleAppStartup(sys.argv, appinfo, createMainWidget, app=app)
+        res = Startup.appStartup(args, createMainWidget, app=app)
         sys.exit(res)
 
 

eric ide

mercurial