--- a/src/eric7/eric7_browser.py Sat Nov 11 10:13:29 2023 +0100 +++ b/src/eric7/eric7_browser.py Sat Nov 11 12:44:51 2023 +0100 @@ -12,31 +12,119 @@ of the integrated web browser. It is based on QtWebEngine. """ +import argparse import os import sys from PyQt6.QtGui import QGuiApplication -app = None -SettingsDir = None - from eric7 import Globals -for arg in sys.argv[:]: - if arg.startswith("--config="): - configDir = arg.replace("--config=", "") - Globals.setConfigDir(configDir) - sys.argv.remove(arg) - elif arg.startswith("--settings="): - from PyQt6.QtCore import QSettings + +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="Web Browser application of the eric tool suite.", + 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( + "--name", + metavar="browser name", + default="", + help="name to be used for the browser instance", + ) + parser.add_argument( + "--new-tab", + metavar="URL", + action="append", + help="open a new tab for the given URL", + ) + parser.add_argument( + "--private", + action="store_true", + help="start the browser in private browsing mode", + ) + parser.add_argument( + "--qthelp", + action="store_true", + help="start the browser with support for QtHelp", + ) + parser.add_argument( + "--quiet", + action="store_true", + help="don't show any startup error messages", + ) + parser.add_argument( + "--search", + metavar="searchword", + help="search for the given word", + ) + parser.add_argument( + "--shutdown", + action="store_true", + help="shut down the browser instance", + ) + parser.add_argument( + "--single", + action="store_true", + help="start the browser as a single application", + ) + parser.add_argument( + "home", + nargs="?", + default="", + metavar="file | URL", + help="open a file or URL", + ) + + # 3. create the Namespace object by parsing the command line + args = parser.parse_args() + return args + + +args = createArgparseNamespace() +if args.config: + 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 + ) +else: + SettingsDir = None + +app = None try: from PyQt6 import QtWebEngineWidgets # __IGNORE_WARNING__ @@ -62,66 +150,34 @@ sys.exit(100) from eric7.EricWidgets.EricApplication import EricApplication -from eric7.Globals import AppInfo from eric7.Toolbox import Startup from eric7.WebBrowser.WebBrowserSingleApplication import ( WebBrowserSingleApplicationClient, ) -def createMainWidget(argv): +def createMainWidget(args): """ Function to create the main widget. - @param argv list of command line 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.WebBrowser.WebBrowserWindow import WebBrowserWindow - searchWord = None - private = False - qthelp = False - single = False - name = "" - - for arg in reversed(argv): - if arg.startswith("--search="): - searchWord = argv[1].split("=", 1)[1] - argv.remove(arg) - elif arg.startswith("--name="): - name = arg.replace("--name=", "") - argv.remove(arg) - elif arg == "--private": - private = True - argv.remove(arg) - elif arg == "--qthelp": - qthelp = True - argv.remove(arg) - elif arg == "--single": - single = True - argv.remove(arg) - elif arg.startswith(("--newtab=", "--")) or arg == "--quiet": - # only needed until we reach this point - argv.remove(arg) - - try: - home = argv[1] - except IndexError: - home = "" - browser = WebBrowserWindow( - home, + args.home, ".", None, "web_browser", - searchWord=searchWord, - private=private, + searchWord=args.search, + private=args.private, settingsDir=SettingsDir, - qthelp=qthelp, - single=single, - saname=name, + qthelp=args.qthelp, + single=args.single, + saname=args.name, ) return browser @@ -134,25 +190,6 @@ QGuiApplication.setDesktopFileName("eric7_browser") - options = [ - ( - "--config=configDir", - "use the given directory as the one containing the config files", - ), - ("--private", "start the browser in private browsing mode"), - ("--qthelp", "start the browser with support for QtHelp"), - ("--quiet", "don't show any startup error messages"), - ("--search=word", "search for the given word"), - ( - "--settings=settingsDir", - "use the given directory to store the settings files", - ), - ("--single", "start the browser as a single application"), - ] - appinfo = AppInfo.makeAppInfo( - sys.argv, "eric Web Browser", "file", "web browser", options - ) - # set the library paths for plugins Startup.setLibraryPaths() @@ -169,22 +206,19 @@ scheme.setFlags(QWebEngineUrlScheme.Flag.SecureScheme) QWebEngineUrlScheme.registerScheme(scheme) - app = EricApplication(sys.argv) - if "--private" not in sys.argv: + app = EricApplication(args) + if not args.private: client = WebBrowserSingleApplicationClient() res = client.connect() if res > 0: - if len(sys.argv) > 1: - client.processArgs(sys.argv[1:]) + client.processArgs(args) sys.exit(0) elif res < 0: print("eric7_browser: {0}".format(client.errstr())) # __IGNORE_WARNING_M801__ sys.exit(res) - res = Startup.simpleAppStartup( - sys.argv, appinfo, createMainWidget, installErrorHandler=True, app=app - ) + res = Startup.appStartup(args, createMainWidget, installErrorHandler=True, app=app) sys.exit(res)