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

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


argparse has various advantages of 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/setsel.py | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/elbepack/commands/setsel.py b/elbepack/commands/setsel.py
index 9c572a0ac849..4c757c9346eb 100644
--- a/elbepack/commands/setsel.py
+++ b/elbepack/commands/setsel.py
@@ -2,8 +2,7 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 # SPDX-FileCopyrightText: 2013-2014, 2017 Linutronix GmbH
 
-import sys
-from optparse import OptionParser
+import argparse
 
 from elbepack.treeutils import etree
 
@@ -32,24 +31,21 @@ def parse_selections(fname):
 
 def run_command(argv):
 
-    oparser = OptionParser(usage='usage: %prog setsel <xmlfile> <pkglist.txt>')
-    (_, args) = oparser.parse_args(argv)
+    aparser = argparse.ArgumentParser(prog='elbe setsel')
+    aparser.add_argument('xmlfile')
+    aparser.add_argument('pkglist')
+    args = aparser.parse_args(argv)
 
-    if len(args) != 2:
-        print('Wrong number of arguments')
-        oparser.print_help()
-        sys.exit(23)
-
-    xml = etree(args[0])
+    xml = etree(args.xmlfile)
 
     pkg_list = xml.node('/pkg-list')
 
     pkg_list.clear()
 
-    sels = parse_selections(args[1])
+    sels = parse_selections(args.pkglist)
 
     for s in sels:
         new = pkg_list.append('pkg')
         new.set_text(s)
 
-    xml.write(args[0])
+    xml.write(args.xmlfile)

-- 
2.45.2



More information about the elbe-devel mailing list