summaryrefslogtreecommitdiffstats
path: root/util/locale_database/qlocalexml2cpp.py
diff options
context:
space:
mode:
authorIevgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>2021-07-07 12:41:10 +0200
committerIevgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>2021-07-16 19:04:20 +0200
commit5ef5dce53bb9f25b79275d1a2d7087b3eabfa792 (patch)
tree370257eeb50ac9e0acf55715cf1dea2aad660a4c /util/locale_database/qlocalexml2cpp.py
parent41458fafa0996660890099d8527756c3582282cf (diff)
locale_database: Use argparse module to parse command line arguments
arparse is the standard way to parse command line arguments in Python. It provides help and usage information for free and is easier to extend than a custom argument parser. Task-number: QTBUG-83488 Pick-to: 6.2 Change-Id: I1e4c9cd914449e083d01932bc871ef10d26f0bc2 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'util/locale_database/qlocalexml2cpp.py')
-rwxr-xr-xutil/locale_database/qlocalexml2cpp.py49
1 files changed, 28 insertions, 21 deletions
diff --git a/util/locale_database/qlocalexml2cpp.py b/util/locale_database/qlocalexml2cpp.py
index e21577db9e..50a03dc9f3 100755
--- a/util/locale_database/qlocalexml2cpp.py
+++ b/util/locale_database/qlocalexml2cpp.py
@@ -26,15 +26,16 @@
## $QT_END_LICENSE$
##
#############################################################################
-"""Script to generate C++ code from CLDR data in qLocaleXML form
+"""Script to generate C++ code from CLDR data in QLocaleXML form
-See ``cldr2qlocalexml.py`` for how to generate the qLocaleXML data itself.
+See ``cldr2qlocalexml.py`` for how to generate the QLocaleXML data itself.
Pass the output file from that as first parameter to this script; pass
the root of the qtbase check-out as second parameter.
"""
import os
import datetime
+import argparse
from qlocalexml import QLocaleXmlReader
from localetools import unicode2hex, wrap_list, Error, Transcriber, SourceFileEditor
@@ -498,30 +499,36 @@ class LocaleHeaderWriter (SourceFileEditor):
out('\n };\n')
-def usage(name, err, message = ''):
- err.write(f"""Usage: {name} path/to/qlocale.xml root/of/qtbase
-""") # TODO: elaborate
- if message:
- err.write('\n' + message + '\n')
-def main(args, out, err):
- # TODO: Make calendars a command-line parameter
+def main(out, err):
# map { CLDR name: Qt file name }
- calendars = {'gregorian': 'roman', 'persian': 'jalali', 'islamic': 'hijri',} # 'hebrew': 'hebrew',
-
- name = args.pop(0)
- if len(args) != 2:
- usage(name, err, 'I expect two arguments')
- return 1
-
- qlocalexml = args.pop(0)
- qtsrcdir = args.pop(0)
+ calendars_map = {
+ 'gregorian': 'roman',
+ 'persian': 'jalali',
+ 'islamic': 'hijri',
+ # 'hebrew': 'hebrew'
+ }
+ all_calendars = list(calendars_map.keys())
+
+ parser = argparse.ArgumentParser(
+ description='Generate C++ code from CLDR data in QLocaleXML form.',
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter)
+ parser.add_argument('input_file', help='input XML file name',
+ metavar='input-file.xml')
+ parser.add_argument('qtbase_path', help='path to the root of the qtbase source tree')
+ parser.add_argument('--calendars', help='select calendars to emit data for',
+ nargs='+', metavar='CALENDAR',
+ choices=all_calendars, default=all_calendars)
+ args = parser.parse_args()
+
+ qlocalexml = args.input_file
+ qtsrcdir = args.qtbase_path
+ calendars = {cal: calendars_map[cal] for cal in args.calendars}
if not (os.path.isdir(qtsrcdir)
and all(os.path.isfile(os.path.join(qtsrcdir, 'src', 'corelib', 'text', leaf))
for leaf in ('qlocale_data_p.h', 'qlocale.h', 'qlocale.qdoc'))):
- usage(name, err, f'Missing expected files under qtbase source root {qtsrcdir}')
- return 1
+ parser.error(f'Missing expected files under qtbase source root {qtsrcdir}')
reader = QLocaleXmlReader(qlocalexml)
locale_map = dict(reader.loadLocaleMap(calendars, err.write))
@@ -617,4 +624,4 @@ def main(args, out, err):
if __name__ == "__main__":
import sys
- sys.exit(main(sys.argv, sys.stdout, sys.stderr))
+ sys.exit(main(sys.stdout, sys.stderr))