src/eric7/Plugins/VcsPlugins/vcsMercurial/HisteditExtension/HgHisteditEditor.py

branch
eric7
changeset 10303
ee1aadab1215
parent 9653
e67609152c5e
child 10439
21c28b0f9e41
--- a/src/eric7/Plugins/VcsPlugins/vcsMercurial/HisteditExtension/HgHisteditEditor.py	Sat Nov 11 10:13:29 2023 +0100
+++ b/src/eric7/Plugins/VcsPlugins/vcsMercurial/HisteditExtension/HgHisteditEditor.py	Sat Nov 11 12:44:51 2023 +0100
@@ -11,6 +11,7 @@
 of two possible dialogs will be shown.
 """
 
+import argparse
 import os
 import sys
 
@@ -20,51 +21,90 @@
 )
 # five times up is our installation directory
 
-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 editor for the Mercurial 'histedit' command.",
+        epilog="Copyright (c) 2016 - 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="'histedit' file to be edited",
+    )
 
-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
 
 
-def createMainWidget(argv):
+def createMainWidget(args):
     """
     Function to create the main widget.
 
-    @param argv list of commandline parameters
-    @type list of str
-    @return reference to the main widget or None in case of an error
-    @rtype QWidget or None
+    @param args namespace object containing the parsed command line parameters
+    @type argparse.Namespace
+    @return reference to the main widget
+    @rtype QWidget
     """
-    if len(argv) > 1:
-        fileName = os.path.basename(argv[1])
+    if args.file:
+        fileName = os.path.basename(args.file)
         if fileName.startswith("hg-histedit-"):
-            from HgHisteditPlanEditor import (  # __IGNORE_WARNING_I10__
-                HgHisteditPlanEditor,
-            )
+            from HgHisteditPlanEditor import HgHisteditPlanEditor  # noqa: I101, I102
 
-            return HgHisteditPlanEditor(argv[1])
+            return HgHisteditPlanEditor(args.file)
         elif fileName.startswith("hg-editor-"):
-            from HgHisteditCommitEditor import (  # __IGNORE_WARNING_I10__
+            from HgHisteditCommitEditor import (  # noqa: I101, I102
                 HgHisteditCommitEditor,
             )
 
-            return HgHisteditCommitEditor(argv[1])
+            return HgHisteditCommitEditor(args.file)
 
     return None
 
@@ -73,25 +113,7 @@
     """
     Main entry point into the application.
     """
-    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",
-        ),
-        ("", "name of file to edit"),
-    ]
-    appinfo = AppInfo.makeAppInfo(
-        sys.argv,
-        "Mercurial Histedit Editor",
-        "",
-        "Editor for the Mercurial histedit command",
-        options,
-    )
-    res = Startup.simpleAppStartup(sys.argv, appinfo, createMainWidget)
+    res = Startup.appStartup(args, createMainWidget)
     sys.exit(res)
 
 

eric ide

mercurial