Refactor command line arguments parsing

This commit is contained in:
grossmj committed 2025-01-17 07:57:42 +07:00
1 parent 035a104957
commit 8e96c4c2e8
3 files changed
+99 -68

No files matched your search

+40 -2
View File
@@ -30,6 +30,7 @@ import gns3server.utils.get_resource
import os
import sys
import asyncio
import argparse
def daemonize():
@@ -59,6 +60,42 @@ def daemonize():
print("Second fork failed: %d (%s)\n" % (e.errno, e.strerror), file=sys.stderr)
sys.exit(1)
def parse_arguments(argv):
"""
Parse command line arguments
:param argv: Array of command line arguments
"""
from gns3server.version import __version__
parser = argparse.ArgumentParser(description=f"GNS3 server version {__version__}")
parser.add_argument("-v", "--version", help="show the version", action="version", version=__version__)
parser.add_argument("--host", help="run on the given host/IP address")
parser.add_argument("--port", help="run on the given port", type=int)
parser.add_argument("--ssl", action="store_true", help="run in SSL mode")
parser.add_argument("--config", help="Configuration file")
parser.add_argument("--certfile", help="SSL cert file")
parser.add_argument("--certkey", help="SSL key file")
parser.add_argument("-L", "--local", action="store_true", help="local mode (allows some insecure operations)")
parser.add_argument(
"-A", "--allow", action="store_true", help="allow remote connections to local console ports"
)
parser.add_argument("-q", "--quiet", default=False, action="store_true", help="do not show logs on stdout")
parser.add_argument("-d", "--debug", default=False, action="store_true", help="show debug logs")
parser.add_argument("--logfile", "--log", help="send output to logfile instead of console")
parser.add_argument("--logmaxsize", default=10000000, help="maximum logfile size in bytes (default is 10MB)")
parser.add_argument(
"--logbackupcount", default=10, help="number of historical log files to keep (default is 10)"
)
parser.add_argument(
"--logcompression", default=False, action="store_true", help="compress inactive (historical) logs"
)
parser.add_argument("--daemon", action="store_true", help="start as a daemon")
parser.add_argument("--pid", help="store process pid")
parser.add_argument("--profile", help="Settings profile (blank will use default settings files)")
args = parser.parse_args(argv)
return parser, args
def main():
"""
@@ -69,10 +106,11 @@ def main():
raise SystemExit("Windows is not a supported platform to run the GNS3 server")
if "--daemon" in sys.argv:
daemonize()
from gns3server.server import Server
try:
asyncio.run(Server().run())
parser, args = parse_arguments(sys.argv[1:])
from gns3server.server import Server
asyncio.run(Server().run(parser, args))
except KeyboardInterrupt:
pass