src/eric7/eric7_ide.py

branch
eric7
changeset 10303
ee1aadab1215
parent 10238
9ea4634a697e
child 10334
24300d16a154
--- a/src/eric7/eric7_ide.py	Sat Nov 11 10:13:29 2023 +0100
+++ b/src/eric7/eric7_ide.py	Sat Nov 11 12:44:51 2023 +0100
@@ -11,6 +11,7 @@
 of the IDE and starts the Qt event loop.
 """
 
+import argparse
 import contextlib
 import io
 import logging
@@ -58,75 +59,168 @@
 except ImportError:
     WEBENGINE_AVAILABLE = False
 
+
+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="The full featured eric Python IDE.",
+        epilog="Use '--' to indicate that there are options for the program to be"
+        " debugged (everything after that is considered arguments for this program)",
+    )
+
+    # 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(
+        "--debug",
+        action="store_true",
+        help="activate debugging output to the console",
+    )
+    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(
+        "--small-screen",
+        action="store_true",
+        help="adjust the interface for screens smaller than FHD",
+    )
+    parser.add_argument(
+        "--no-multimedia",
+        action="store_true",
+        help="disable the support of multimedia functions",
+    )
+    parser.add_argument(
+        "--no-open",
+        action="store_true",
+        help="don't open anything at startup except that given in command",
+    )
+    parser.add_argument(
+        "--no-splash",
+        action="store_true",
+        help="don't show the splash screen",
+    )
+    parser.add_argument(
+        "--no-crash",
+        action="store_true",
+        help="don't check for a crash session file on startup",
+    )
+    parser.add_argument(
+        "--disable-crash",
+        action="store_true",
+        help="disable the support for crash sessions",
+    )
+    parser.add_argument(
+        "--disable-plugin",
+        metavar="plugin-name",
+        default=[],
+        action="append",
+        help="disable the given plugin (may be repeated)",
+    )
+    parser.add_argument(
+        "--plugin",
+        metavar="plugin-file",
+        help="load the given plugin file (plugin development)",
+    )
+    parser.add_argument(
+        "--start-file",
+        action="store_true",
+        help="load the most recently opened file",
+    )
+    parser.add_argument(
+        "--start-multi",
+        action="store_true",
+        help="load the most recently opened multi-project",
+    )
+    parser.add_argument(
+        "--start-project",
+        action="store_true",
+        help="load the most recently opened project",
+    )
+    parser.add_argument(
+        "--start-session",
+        action="store_true",
+        help="load the global session file",
+    )
+    parser.add_argument(
+        "file_or_project",
+        nargs="*",
+        metavar="multi-project | project | file",
+        help="open a project, multi-project or a list of files",
+    )
+
+    # 3. preprocess the command line ('--' detection and split)
+    if "--" in sys.argv:
+        ddindex = sys.argv.index("--")
+        argv = sys.argv[1:ddindex]
+        dd_argv = sys.argv[ddindex + 1 :]
+    else:
+        argv = sys.argv[1:]
+        dd_argv = []
+
+    # 4. create the Namespace object by parsing the command line
+    args = parser.parse_args(argv)
+    args.dd_args = dd_argv
+
+    return args
+
+
 # some global variables needed to start the application
-args = None
+args = createArgparseNamespace()
 mainWindow = None
 splash = None
 inMainLoop = False
 app = None
 
-if "--debug" in sys.argv:
-    del sys.argv[sys.argv.index("--debug")]
+if args.debug:
     logging.basicConfig(level=logging.DEBUG)
 
-for arg in sys.argv[:]:
-    if arg.startswith("--config="):
-        from eric7 import Globals
+if args.config:
+    from eric7 import Globals
+
+    Globals.setConfigDir(args.config)
+if args.settings:
+    from PyQt6.QtCore import QSettings
 
-        configDir = arg.replace("--config=", "")
-        Globals.setConfigDir(configDir)
-        sys.argv.remove(arg)
-    elif arg.startswith("--settings="):
-        from PyQt6.QtCore import QSettings
-
-        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)
+    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
 
 
-def handleSingleApplication(ddindex):
+def handleSingleApplication():
     """
     Global function to handle the single application mode.
-
-    @param ddindex index of a '--' option in the options list
     """
     from eric7.EricWidgets.EricSingleApplication import EricSingleApplicationClient
 
     client = EricSingleApplicationClient()
     res = client.connect()
     if res > 0:
-        for switch in (
-            "--debug",
-            "--disable-crash",
-            "--no-crash",
-            "--no-multimedia",
-            "--no-open",
-            "--no-splash",
-            "--small-screen",
-        ):
-            if switch in sys.argv and sys.argv.index(switch) < ddindex:
-                sys.argv.remove(switch)
-                ddindex -= 1
-        for arg in sys.argv[:]:
-            for switch in (
-                "--config=",
-                "--plugin=",
-                "--disable-plugin=",
-                "--settings=",
-            ):
-                if arg.startswith(switch) and sys.argv.index(arg) < ddindex:
-                    sys.argv.remove(arg)
-                    ddindex -= 1
-                    break
-
-        if len(sys.argv) > 1:
-            client.processArgs(sys.argv[1:])
+        client.processArgs(args)
         sys.exit(0)
 
     elif res < 0:
@@ -221,7 +315,6 @@
     """
     Main entry point into the application.
     """
-    from eric7.Globals import AppInfo
     from eric7.SystemUtilities import OSUtilities, QtUtilities
     from eric7.Toolbox import Startup
 
@@ -233,42 +326,6 @@
 
     QGuiApplication.setDesktopFileName("eric7")
 
-    options = [
-        (
-            "--config=configDir",
-            "use the given directory as the one containing the config files",
-        ),
-        ("--debug", "activate debugging output to the console"),
-        ("--no-multimedia", "disable the support of multimedia functions"),
-        ("--no-open", "don't open anything at startup except that given in command"),
-        ("--no-splash", "don't show the splash screen"),
-        ("--no-crash", "don't check for a crash session file on startup"),
-        ("--disable-crash", "disable the support for crash sessions"),
-        (
-            "--disable-plugin=<plug-in name>",
-            "disable the given plug-in (may be repeated)",
-        ),
-        ("--plugin=plugin-file", "load the given plugin file (plugin development)"),
-        (
-            "--settings=settingsDir",
-            "use the given directory to store the settings files",
-        ),
-        ("--small-screen", "adjust the interface for screens smaller than FHD"),
-        ("--start-file", "load the most recently opened file"),
-        ("--start-multi", "load the most recently opened multi-project"),
-        ("--start-project", "load the most recently opened project"),
-        ("--start-session", "load the global session file"),
-        ("--", "indicate that there are options for the program to be debugged"),
-        ("", "(everything after that is considered arguments for this program)"),
-    ]
-    appinfo = AppInfo.makeAppInfo(
-        sys.argv,
-        "Eric7",
-        "[project | files... [--] [debug-options]]",
-        "A Python IDE",
-        options,
-    )
-
     if "__PYVENV_LAUNCHER__" in os.environ:
         del os.environ["__PYVENV_LAUNCHER__"]
 
@@ -289,14 +346,13 @@
         scheme.setFlags(QWebEngineUrlScheme.Flag.SecureScheme)
         QWebEngineUrlScheme.registerScheme(scheme)
 
-    app = EricApplication(sys.argv)
-    ddindex = Startup.handleArgs(sys.argv, appinfo)
+    app = EricApplication(args)
 
     logging.debug("Importing Preferences")
     from eric7 import Preferences  # __IGNORE_WARNING_I101__
 
     if Preferences.getUI("SingleApplicationMode"):
-        handleSingleApplication(ddindex)
+        handleSingleApplication()
 
     # set the application style sheet
     app.setStyleSheetFile(Preferences.getUI("StyleSheet"))
@@ -310,9 +366,7 @@
         SplashScreen,
     )
 
-    if "--no-splash" in sys.argv and sys.argv.index("--no-splash") < ddindex:
-        sys.argv.remove("--no-splash")
-        ddindex -= 1
+    if args.no_splash:
         splash = NoneSplashScreen()
     elif not Preferences.getUI("ShowSplash"):
         splash = NoneSplashScreen()
@@ -329,45 +383,6 @@
             path = pyqtDataDir
         os.environ["PATH"] = path + os.pathsep + os.environ["PATH"]
 
-    pluginFile = None
-    noopen = False
-    nocrash = False
-    disablecrash = False
-    disabledPlugins = []
-    if "--no-open" in sys.argv and sys.argv.index("--no-open") < ddindex:
-        sys.argv.remove("--no-open")
-        ddindex -= 1
-        noopen = True
-    if "--no-crash" in sys.argv and sys.argv.index("--no-crash") < ddindex:
-        sys.argv.remove("--no-crash")
-        ddindex -= 1
-        nocrash = True
-    if "--disable-crash" in sys.argv and sys.argv.index("--disable-crash") < ddindex:
-        sys.argv.remove("--disable-crash")
-        ddindex -= 1
-        disablecrash = True
-    for arg in sys.argv[:]:
-        if arg.startswith("--disable-plugin=") and sys.argv.index(arg) < ddindex:
-            # extract the plug-in name
-            pluginName = arg.replace("--disable-plugin=", "")
-            sys.argv.remove(arg)
-            ddindex -= 1
-            disabledPlugins.append(pluginName)
-    for arg in sys.argv:
-        if arg.startswith("--plugin=") and sys.argv.index(arg) < ddindex:
-            # extract the plugin development option
-            pluginFile = arg.replace("--plugin=", "").replace('"', "")
-            sys.argv.remove(arg)
-            ddindex -= 1
-            pluginFile = os.path.expanduser(pluginFile)
-            pluginFile = os.path.abspath(pluginFile)
-            break
-
-    # is there a set of filenames or options on the command line,
-    # if so, pass them to the UI
-    if len(sys.argv) > 1:
-        args = sys.argv[1:]
-
     # get the Qt translations directory
     qtTransDir = Preferences.getQtTranslationsDir()
     if not qtTransDir:
@@ -393,11 +408,13 @@
         app,
         loc,
         splash,
-        pluginFile,
-        disabledPlugins,
-        noopen,
-        nocrash,
-        disablecrash,
+        None
+        if args.plugin is None
+        else os.path.abspath(os.path.expanduser(args.plugin)),
+        args.disable_plugin,
+        args.no_open,
+        args.no_crash,
+        args.disable_crash,
         restartArgs,
         originalPathString,
     )

eric ide

mercurial