[elbe-devel] [PATCH 05/19] elbepack: pkgdiff: migrate to argparse

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue Jul 9 09:23:38 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/pkgdiff.py | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/elbepack/commands/pkgdiff.py b/elbepack/commands/pkgdiff.py
index 9493415da7a2..f024ca436c6e 100644
--- a/elbepack/commands/pkgdiff.py
+++ b/elbepack/commands/pkgdiff.py
@@ -2,9 +2,8 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 # SPDX-FileCopyrightText: 2013-2017 Linutronix GmbH
 
+import argparse
 import os
-import sys
-from optparse import OptionParser
 
 import apt
 
@@ -15,23 +14,19 @@ from elbepack.elbexml import ElbeXML, ValidationMode
 
 def run_command(argv):
 
-    oparser = OptionParser(
-        usage='usage: %prog pkgdiff [options] <rfs1> <rfs2>')
-    oparser.add_option(
+    aparser = argparse.ArgumentParser(prog='elbe pkgdiff')
+    aparser.add_argument(
         '--noauto',
         action='store_true',
         dest='noauto',
         default=False,
         help='Dont compare automatically installed Packages')
-    (opt, args) = oparser.parse_args(argv)
+    aparser.add_argument('rfs1')
+    aparser.add_argument('rfs2')
+    args = aparser.parse_args(argv)
 
-    if len(args) != 2:
-        print('Wrong number of arguments')
-        oparser.print_help()
-        sys.exit(41)
-
-    gen_rfs = args[0]
-    fix_rfs = args[1]
+    gen_rfs = args.rfs1
+    fix_rfs = args.rfs2
 
     x = os.path.join(gen_rfs, 'etc/elbe_base.xml')
     xml = ElbeXML(
@@ -49,7 +44,7 @@ def run_command(argv):
 
     gen_pkgs = {}
     for p in gen_cache.packages:
-        if opt.noauto:
+        if args.noauto:
             if p.current_ver and not \
                gc[p.name].is_auto_installed and not \
                p.essential:
@@ -67,7 +62,7 @@ def run_command(argv):
 
     fix_pkgs = {}
     for p in fix_cache.packages:
-        if opt.noauto:
+        if args.noauto:
             if p.current_ver and not \
                fc[p.name].is_auto_installed and not \
                p.essential:

-- 
2.45.2



More information about the elbe-devel mailing list