[elbe-devel] [PATCH 08/19] elbepack: daemon: migrate to argparse

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue Jul 9 09:23:41 CEST 2024


argparse has various advantages over optparse:

* Autogenerated command synopsis.
* Required arguments.
* Flexible argument types.
* Subparsers.

Furthermore optparse is deprecated since Python 3.2 (2011).

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/commands/daemon.py | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/elbepack/commands/daemon.py b/elbepack/commands/daemon.py
index 24de196c9b70..de2910eeee71 100644
--- a/elbepack/commands/daemon.py
+++ b/elbepack/commands/daemon.py
@@ -2,13 +2,13 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 # SPDX-FileCopyrightText: 2014, 2017 Linutronix GmbH
 
+import argparse
 import contextlib
 import errno
 import importlib
 import os
 import socket
 import wsgiref.simple_server
-from optparse import OptionParser
 from pkgutil import iter_modules
 
 import elbepack.daemons
@@ -70,18 +70,18 @@ def run_command(argv):
     if not daemons:
         print('no elbe daemons installed')
 
-    oparser = OptionParser(usage='usage: %prog')
-    oparser.add_option('--host', dest='host', default='0.0.0.0',
-                       help='interface to host daemon')
-    oparser.add_option('--port', dest='port', type=int, default=7587,
-                       help='port to host daemon')
+    aparser = argparse.ArgumentParser(prog='elbe daemon')
+    aparser.add_argument('--host', dest='host', default='0.0.0.0',
+                         help='interface to host daemon')
+    aparser.add_argument('--port', dest='port', type=int, default=7587,
+                         help='port to host daemon')
 
     for d in daemons:
         # unused, compatibility with old initscripts
-        oparser.add_option('--' + str(d), dest='_ignored', default=False,
-                           action='store_true', help='enable ' + str(d))
+        aparser.add_argument('--' + str(d), dest='_ignored', default=False,
+                             action='store_true', help='enable ' + str(d))
 
-    (opt, _) = oparser.parse_args(argv)
+    args = aparser.parse_args(argv)
 
     with contextlib.ExitStack() as stack:
         mapping = {}
@@ -98,7 +98,7 @@ def run_command(argv):
         dispatcher = _WsgiDispatcher(mapping)
 
         with wsgiref.simple_server.make_server(
-                opt.host, opt.port, dispatcher,
+                args.host, args.port, dispatcher,
                 handler_class=_ElbeWSGIRequestHandler,
         ) as httpd:
             _sd_notify(b'READY=1\n'

-- 
2.45.2



More information about the elbe-devel mailing list