[elbe-devel] [PATCH 07/19] elbepack: updated: migrate to argparse

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue Jul 9 09:23:40 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/updated.py | 60 ++++++++++++++++++++++----------------------
 1 file changed, 30 insertions(+), 30 deletions(-)

diff --git a/elbepack/commands/updated.py b/elbepack/commands/updated.py
index dacd41b898a3..0ae7f244ebd0 100644
--- a/elbepack/commands/updated.py
+++ b/elbepack/commands/updated.py
@@ -3,10 +3,10 @@
 # SPDX-FileCopyrightText: 2015 Ferdinand Schwenk <ferdinand at ping.lan>
 # SPDX-FileCopyrightText: 2017 Linutronix GmbH
 
+import argparse
 import os
 import signal
 import sys
-from optparse import OptionParser
 from wsgiref.simple_server import make_server
 
 from spyne.protocol.soap import Soap11
@@ -32,48 +32,48 @@ def run_command(argv):
 
     status = UpdateStatus()
 
-    oparser = OptionParser(usage='usage: %prog updated [options] <filename>')
+    aparser = argparse.ArgumentParser(prog='elbe updated')
 
-    oparser.add_option('--directory', dest='update_dir',
-                       help='monitor dir (default is /var/cache/elbe/updates)',
-                       metavar='FILE')
+    aparser.add_argument('--directory', dest='update_dir',
+                         help='monitor dir (default is /var/cache/elbe/updates)',
+                         metavar='FILE')
 
-    oparser.add_option('--repocache', dest='repo_dir',
-                       help='monitor dir (default is /var/cache/elbe/repos)',
-                       metavar='FILE')
+    aparser.add_argument('--repocache', dest='repo_dir',
+                         help='monitor dir (default is /var/cache/elbe/repos)',
+                         metavar='FILE')
 
-    oparser.add_option('--host', dest='host', default='',
-                       help='listen host')
+    aparser.add_argument('--host', dest='host', default='',
+                         help='listen host')
 
-    oparser.add_option('--port', dest='port', default=8088,
-                       help='listen port')
+    aparser.add_argument('--port', dest='port', default=8088,
+                         help='listen port')
 
-    oparser.add_option('--nosign', action='store_true', dest='nosign',
-                       default=False,
-                       help='accept none signed files')
+    aparser.add_argument('--nosign', action='store_true', dest='nosign',
+                         default=False,
+                         help='accept none signed files')
 
-    oparser.add_option('--verbose', action='store_true', dest='verbose',
-                       default=False,
-                       help='force output to stdout instead of syslog')
+    aparser.add_argument('--verbose', action='store_true', dest='verbose',
+                         default=False,
+                         help='force output to stdout instead of syslog')
 
-    oparser.add_option('--usb', action='store_true', dest='use_usb',
-                       default=False,
-                       help='monitor USB devices')
+    aparser.add_argument('--usb', action='store_true', dest='use_usb',
+                         default=False,
+                         help='monitor USB devices')
 
-    (opt, _) = oparser.parse_args(argv)
+    args = aparser.parse_args(argv)
 
-    status.nosign = opt.nosign
-    status.verbose = opt.verbose
+    status.nosign = args.nosign
+    status.verbose = args.verbose
 
-    if not opt.update_dir:
+    if not args.update_dir:
         update_dir = '/var/cache/elbe/updates'
     else:
-        update_dir = opt.update_dir
+        update_dir = args.update_dir
 
-    if not opt.repo_dir:
+    if not args.repo_dir:
         status.repo_dir = '/var/cache/elbe/repos'
     else:
-        status.repo_dir = opt.repo_dir
+        status.repo_dir = args.repo_dir
 
     if not os.path.isdir(update_dir):
         os.makedirs(update_dir)
@@ -82,7 +82,7 @@ def run_command(argv):
 
     fm = FileMonitor(status, update_dir)
     status.monitors.append(fm)
-    if opt.use_usb:
+    if args.use_usb:
         if usbmonitor_available:
             um = USBMonitor(status, recursive=False)
             status.monitors.append(um)
@@ -104,7 +104,7 @@ def run_command(argv):
 
     wsgi_application = WsgiApplication(application)
 
-    status.soapserver = make_server(opt.host, int(opt.port),
+    status.soapserver = make_server(args.host, int(args.port),
                                     wsgi_application)
 
     try:

-- 
2.45.2



More information about the elbe-devel mailing list