[elbe-devel] [PATCH] elbepack: preprocess: migrate to argparse
Thomas Weißschuh
thomas.weissschuh at linutronix.de
Fri Jul 26 09:31:21 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).
Keep the optparse-based utility functions as these are still used.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
elbepack/commands/preprocess.py | 41 ++++++++++++++++++++++++++---------------
1 file changed, 26 insertions(+), 15 deletions(-)
diff --git a/elbepack/commands/preprocess.py b/elbepack/commands/preprocess.py
index 938438a34c42..c20b4e24506b 100644
--- a/elbepack/commands/preprocess.py
+++ b/elbepack/commands/preprocess.py
@@ -2,9 +2,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2017 Linutronix GmbH
+import argparse
import os
import sys
-from optparse import OptionGroup, OptionParser
+from optparse import OptionGroup
from elbepack.xmlpreprocess import XMLPreprocessError, xmlpreprocess
@@ -39,25 +40,35 @@ def add_xmlpreprocess_passthrough_options(oparser):
oparser.add_option_group(group)
+def _add_arguments(parser):
+ parser.add_argument('-v', '--variants',
+ type=lambda v: v.split(','),
+ help='enable only tags with empty or given variant')
+ parser.add_argument('-p', '--proxy', help='add proxy to mirrors')
+ parser.add_argument('-z', '--gzip', type=int, default=9,
+ help='gzip compression level 1-9 (0: no compression)')
+
+
+def add_xmlpreprocess_passthrough_arguments(parser):
+ group = parser.add_argument_group('Elbe preprocess options',
+ 'Options passed through to invocation of "elbe preprocess"')
+ _add_arguments(group)
+
+
def run_command(argv):
- oparser = OptionParser(usage='usage: %prog preprocess [options] <xmlfile>')
- oparser.add_option('-o', '--output', dest='output',
- default='preprocess.xml',
- help='preprocessed output file', metavar='<xmlfile>')
- _add_options(oparser)
- (opt, args) = oparser.parse_args(argv)
-
- if len(args) != 1:
- print('Wrong number of arguments', file=sys.stderr)
- oparser.print_help()
- sys.exit(112)
-
- if not os.path.isfile(args[0]):
+ aparser = argparse.ArgumentParser(prog='elbe preprocess')
+ aparser.add_argument('-o', '--output', default='preprocess.xml',
+ help='preprocessed output file')
+ aparser.add_argument('xmlfile')
+ _add_arguments(aparser)
+ args = aparser.parse_args(argv)
+
+ if not os.path.isfile(args.xmlfile):
print(f"{args[0]} doesn't exist", file=sys.stderr)
sys.exit(113)
try:
- xmlpreprocess(args[0], opt.output, opt.variants, opt.proxy, opt.gzip)
+ xmlpreprocess(args.xmlfile, args.output, args.variants, args.proxy, args.gzip)
except XMLPreprocessError as e:
print(e, file=sys.stderr)
sys.exit(114)
---
base-commit: e4a0e331ee6f0bec03af434ff313902e5f3cd424
change-id: 20240726-argparse-preprocess-eddc6258309a
Best regards,
--
Thomas Weißschuh <thomas.weissschuh at linutronix.de>
More information about the elbe-devel
mailing list