src/eric7/eric7_testing.py

branch
eric7-maintenance
changeset 10349
df7edc29cbfb
parent 10272
7ae72d1df070
parent 10303
ee1aadab1215
child 10460
3b34efa2857c
--- a/src/eric7/eric7_testing.py	Tue Oct 31 09:23:05 2023 +0100
+++ b/src/eric7/eric7_testing.py	Wed Nov 29 14:23:36 2023 +0100
@@ -12,52 +12,90 @@
 version of the integrated testing module.
 """
 
+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.
 
-        configDir = arg.replace("--config=", "")
-        Globals.setConfigDir(configDir)
-        sys.argv.remove(arg)
-    elif arg.startswith("--settings="):
-        from PyQt6.QtCore import QSettings
+    @return created argument parser object
+    @rtype argparse.ArgumentParser
+    """
+    from eric7.UI.Info import Version
+
+    # 1. create the argument parser
+    parser = argparse.ArgumentParser(
+        description="Graphical tool of the eric tool suite to execute unit tests.",
+        epilog="Copyright (c) 2002 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>.",
+    )
 
-        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)
+    # 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="test script",
+    )
 
-from eric7.Globals import AppInfo
+    # 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.Toolbox import Startup
 
 # make Python debug client available as a package repository (needed for 'coverage')
 sys.path.insert(2, os.path.join(os.path.dirname(__file__), "DebugClients", "Python"))
 
 
-def createMainWidget(argv):
+def createMainWidget(args):
     """
     Function to create the main widget.
 
-    @param argv list of commandline parameters
-    @type list of str
+    @param args namespace object containing the parsed command line parameters
+    @type argparse.Namespace
     @return reference to the main widget
     @rtype QWidget
     """
     from eric7.Testing.TestingWidget import TestingWindow
 
-    try:
-        fn = argv[1]
-    except IndexError:
-        fn = None
-    return TestingWindow(fn)
+    return TestingWindow(args.file)
 
 
 def main():
@@ -66,20 +104,7 @@
     """
     QGuiApplication.setDesktopFileName("eric7_testing")
 
-    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 Testing", "file", "Graphical test application", options
-    )
-    res = Startup.simpleAppStartup(sys.argv, appinfo, createMainWidget)
+    res = Startup.appStartup(args, createMainWidget)
     sys.exit(res)
 
 

eric ide

mercurial