[elbe-devel] [PATCH 04/19] elbepack: cyclonedx-sbom: migrate to argparse

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

diff --git a/elbepack/commands/cyclonedx-sbom.py b/elbepack/commands/cyclonedx-sbom.py
index 63007f33fa6c..b48bb48a5460 100644
--- a/elbepack/commands/cyclonedx-sbom.py
+++ b/elbepack/commands/cyclonedx-sbom.py
@@ -1,9 +1,9 @@
 # ELBE - Debian Based Embedded Rootfilesystem Builder
 
+import argparse
 import datetime
 import itertools
 import json
-import optparse
 import os
 import sys
 import urllib
@@ -82,17 +82,12 @@ def _component_from_apt_pkg(pkg):
 
 
 def run_command(argv):
-    oparser = optparse.OptionParser()
-    oparser.add_option('-d', dest='elbe_build')
-    options, args = oparser.parse_args(argv)
-
-    if args != [] or options.elbe_build is None:
-        print('invalid options')
-        oparser.print_help()
-        sys.exit(1)
+    aparser = argparse.ArgumentParser(prog='elbe cyclonedx-sbom')
+    aparser.add_argument('-d', dest='elbe_build', required=True)
+    args = aparser.parse_args(argv)
 
     ts = datetime.datetime.now(tz=datetime.timezone.utc)
-    project_dir = options.elbe_build
+    project_dir = args.elbe_build
     source_file = ElbeXML(os.path.join(project_dir, 'source.xml'))
 
     project_name = source_file.text('/name').strip()

-- 
2.45.2



More information about the elbe-devel mailing list