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

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

diff --git a/elbepack/commands/show.py b/elbepack/commands/show.py
index 3256bb8ccb2d..8cc218fff9d1 100644
--- a/elbepack/commands/show.py
+++ b/elbepack/commands/show.py
@@ -2,8 +2,8 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 # SPDX-FileCopyrightText: 2013-2015, 2017 Linutronix GmbH
 
+import argparse
 import sys
-from optparse import OptionParser
 
 from elbepack.treeutils import etree
 from elbepack.validate import validate_xml
@@ -11,38 +11,30 @@ from elbepack.validate import validate_xml
 
 def run_command(argv):
 
-    oparser = OptionParser(usage='usage: %prog show [options] <filename>')
+    aparser = argparse.ArgumentParser(prog='elbe show')
 
-    oparser.add_option('--verbose', action='store_true', dest='verbose',
-                       default=False,
-                       help='show detailed project informations')
+    aparser.add_argument('--verbose', action='store_true', dest='verbose',
+                         default=False,
+                         help='show detailed project informations')
 
-    oparser.add_option('--skip-validation', action='store_true',
-                       dest='skip_validation', default=False,
-                       help='Skip xml schema validation')
+    aparser.add_argument('--skip-validation', action='store_true',
+                         dest='skip_validation', default=False,
+                         help='Skip xml schema validation')
 
-    (opt, args) = oparser.parse_args(argv)
+    aparser.add_argument('xmlfile')
 
-    if not args:
-        print('No Filename specified')
-        oparser.print_help()
-        sys.exit(107)
-
-    if len(args) > 1:
-        print('too many filenames specified')
-        oparser.print_help()
-        sys.exit(108)
+    args = aparser.parse_args(argv)
 
     try:
-        if not opt.skip_validation:
-            validation = validate_xml(args[0])
+        if not args.skip_validation:
+            validation = validate_xml(args.xmlfile)
             if validation:
                 print('xml validation failed. Bailing out')
                 for i in validation:
                     print(i)
                 sys.exit(109)
 
-        xml = etree(args[0])
+        xml = etree(args.xmlfile)
     except BaseException:
         print('Unable to open xml File. Bailing out')
         sys.exit(110)
@@ -51,11 +43,11 @@ def run_command(argv):
         print('no project description available')
         sys.exit(111)
 
-    print(f'== {args[0]} ==')
+    print(f'== {args.xmlfile} ==')
     print(f"Debian suite: {xml.text('./project/suite')}")
     for s in xml.text('./project/description').splitlines():
         print(f'{s.strip()}')
-    if opt.verbose:
+    if args.verbose:
         if xml.has('./target/passwd'):
             print(f"root password: {xml.text('./target/passwd')}")
         print(

-- 
2.45.2



More information about the elbe-devel mailing list