[elbe-devel] [PATCH 17/19] elbepack: repodir: migrate to argparse
Thomas Weißschuh
thomas.weissschuh at linutronix.de
Tue Jul 9 09:23:50 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/repodir.py | 28 ++++++++++++----------------
1 file changed, 12 insertions(+), 16 deletions(-)
diff --git a/elbepack/commands/repodir.py b/elbepack/commands/repodir.py
index 62d8a077b608..bf5c72efee9a 100644
--- a/elbepack/commands/repodir.py
+++ b/elbepack/commands/repodir.py
@@ -1,37 +1,33 @@
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2022 Linutronix GmbH
+import argparse
import os
import sys
-from optparse import OptionParser
from threading import Event
from elbepack.repodir import Repodir, RepodirError
def run_command(argv):
- oparser = OptionParser(usage='usage: %prog repodir [options] <xmlfile>')
- oparser.add_option('-o', '--output', dest='output',
- default='repodir.xml',
- help='preprocessed output file', metavar='<xmlfile>')
- (opt, args) = oparser.parse_args(argv)
-
- if len(args) != 1:
- print('Wrong number of arguments', file=sys.stderr)
- oparser.print_help()
- sys.exit(55)
-
- xml_input = args[0]
+ aparser = argparse.ArgumentParser(prog='elbe repodir')
+ aparser.add_argument('-o', '--output', dest='output',
+ default='repodir.xml',
+ help='preprocessed output file', metavar='<xmlfile>')
+ aparser.add_argument('xmlfile')
+ args = aparser.parse_args(argv)
+
+ xml_input = args.xmlfile
if not os.path.isfile(xml_input):
print(f'{xml_input} does not exist', file=sys.stderr)
sys.exit(56)
- if os.path.exists(opt.output):
+ if os.path.exists(args.output):
# This will be overridden. Try to delete first to make sure it is a regular file.
- os.remove(opt.output)
+ os.remove(args.output)
try:
- with Repodir(xml_input, opt.output):
+ with Repodir(xml_input, args.output):
Event().wait()
except KeyboardInterrupt:
print()
--
2.45.2
More information about the elbe-devel
mailing list