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

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue Jul 9 09:23:47 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/chroot.py | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/elbepack/commands/chroot.py b/elbepack/commands/chroot.py
index c7b7002ab211..4c05f0ed7057 100644
--- a/elbepack/commands/chroot.py
+++ b/elbepack/commands/chroot.py
@@ -2,11 +2,11 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 # SPDX-FileCopyrightText: 2014-2017 Linutronix GmbH
 
+import argparse
 import logging
 import os
 import subprocess
 import sys
-from optparse import OptionParser
 
 from elbepack.elbeproject import ElbeProject
 from elbepack.elbexml import ValidationError, ValidationMode
@@ -14,29 +14,25 @@ from elbepack.log import elbe_logging
 
 
 def run_command(argv):
-    oparser = OptionParser(
-        usage='usage: %prog chroot [options] <builddir> [cmd]')
-    oparser.add_option('--skip-validation', action='store_true',
-                       dest='skip_validation', default=False,
-                       help='Skip xml schema validation')
-    oparser.add_option('--target', action='store_true', dest='target',
-                       help='chroot into target instead of buildenv',
-                       default=False)
-    oparser.add_option('--buildtype', dest='buildtype',
-                       help='Override the buildtype')
-
-    (opt, args) = oparser.parse_args(argv)
-
-    if not args:
-        print('wrong number of arguments')
-        oparser.print_help()
-        sys.exit(72)
+    aparser = argparse.ArgumentParser(prog='elbe chroot')
+    aparser.add_argument('--skip-validation', action='store_true',
+                         dest='skip_validation', default=False,
+                         help='Skip xml schema validation')
+    aparser.add_argument('--target', action='store_true', dest='target',
+                         help='chroot into target instead of buildenv',
+                         default=False)
+    aparser.add_argument('--buildtype', dest='buildtype',
+                         help='Override the buildtype')
+    aparser.add_argument('builddir')
+    aparser.add_argument('cmd', nargs='*')
+
+    args = aparser.parse_args(argv)
 
     with elbe_logging({'streams': sys.stdout}):
         try:
-            project = ElbeProject(args[0],
-                                  override_buildtype=opt.buildtype,
-                                  skip_validate=opt.skip_validation,
+            project = ElbeProject(args.builddir,
+                                  override_buildtype=args.buildtype,
+                                  skip_validate=args.skip_validation,
                                   url_validation=ValidationMode.NO_CHECK)
         except ValidationError:
             logging.exception('XML validation failed.  Bailing out')
@@ -48,12 +44,9 @@ def run_command(argv):
         # TODO: howto set env in chroot?
         os.environ['PS1'] = project.xml.text('project/name') + r': \w\$'
 
-        chroot_args = ['/bin/bash']
-
-        if len(args) > 1:
-            chroot_args = args[1:]
+        chroot_args = args.cmd or ['/bin/bash']
 
-        chroot, path = (project.targetfs, project.targetpath) if opt.target else \
+        chroot, path = (project.targetfs, project.targetpath) if args.target else \
                        (project.buildenv, project.chrootpath)
 
         try:

-- 
2.45.2



More information about the elbe-devel mailing list