summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2020-03-08 19:54:38 -0600
committerGitHub <noreply@github.com>2020-03-08 19:54:38 -0600
commit9da90debf54029a50502fd759c8de56f2dc47129 (patch)
tree600011e9b1a4f14f53208827d72f7c6eb49e3ae7
parent105e175a7708a05886d06a24ed2b60bdd6cf329c (diff)
parenta7af9cc2578cdc128e662d163552fcb22da3c496 (diff)
downloadnumpy-9da90debf54029a50502fd759c8de56f2dc47129.tar.gz
Merge pull request #15702 from mwtoews/optparse
MAINT: Replace optparse with argparse for 'doc' and 'tools' scripts
-rwxr-xr-xdoc/postprocess.py21
-rwxr-xr-xdoc/summarize.py19
-rwxr-xr-xtools/c_coverage/c_coverage_report.py51
3 files changed, 42 insertions, 49 deletions
diff --git a/doc/postprocess.py b/doc/postprocess.py
index 309161bc0..3e066d22e 100755
--- a/doc/postprocess.py
+++ b/doc/postprocess.py
@@ -1,27 +1,20 @@
#!/usr/bin/env python3
"""
-%prog MODE FILES...
-
Post-processes HTML and Latex files output by Sphinx.
-MODE is either 'html' or 'tex'.
-
"""
-import optparse
import io
def main():
- p = optparse.OptionParser(__doc__)
- options, args = p.parse_args()
-
- if len(args) < 1:
- p.error('no mode given')
+ import argparse
- mode = args.pop(0)
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('mode', help='file mode', choices=('html', 'tex'))
+ parser.add_argument('file', nargs='+', help='input file(s)')
+ args = parser.parse_args()
- if mode not in ('html', 'tex'):
- p.error('unknown mode %s' % mode)
+ mode = args.mode
- for fn in args:
+ for fn in args.file:
with io.open(fn, 'r', encoding="utf-8") as f:
if mode == 'html':
lines = process_html(fn, f.readlines())
diff --git a/doc/summarize.py b/doc/summarize.py
index edd260ec7..701fe2c6e 100755
--- a/doc/summarize.py
+++ b/doc/summarize.py
@@ -1,14 +1,11 @@
#!/usr/bin/env python3
"""
-summarize.py
-
Show a summary about which NumPy functions are documented and which are not.
"""
import collections.abc
import glob
import inspect
-import optparse
import os
import sys
@@ -59,13 +56,13 @@ ctypeslib ctypeslib.test
""".split()
def main():
- p = optparse.OptionParser(__doc__)
- p.add_option("-c", "--columns", action="store", type="int", dest="cols",
- default=3, help="Maximum number of columns")
- options, args = p.parse_args()
+ import argparse
- if len(args) != 0:
- p.error('Wrong number of arguments')
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument(
+ "-c", "--columns", type=int, default=3,
+ help="Maximum number of columns (default: %(default)d)")
+ args = parser.parse_args()
# prepare
fn = os.path.join(CUR_DIR, 'dump.xml')
@@ -90,13 +87,13 @@ def main():
print("--- %s\n" % filename)
last_filename = filename
print(" ** ", section)
- print(format_in_columns(sorted(names), options.cols))
+ print(format_in_columns(sorted(names), args.columns))
print("\n")
print("")
print("Undocumented")
print("============\n")
- print(format_in_columns(sorted(undocumented.keys()), options.cols))
+ print(format_in_columns(sorted(undocumented.keys()), args.columns))
def check_numpy():
documented = get_documented(glob.glob(SOURCE_DIR + '/*.rst'))
diff --git a/tools/c_coverage/c_coverage_report.py b/tools/c_coverage/c_coverage_report.py
index 6a71f1e73..bd3eeaee9 100755
--- a/tools/c_coverage/c_coverage_report.py
+++ b/tools/c_coverage/c_coverage_report.py
@@ -4,7 +4,6 @@ A script to create C code-coverage reports based on the output of
valgrind's callgrind tool.
"""
-import optparse
import os
import re
import sys
@@ -143,39 +142,43 @@ def collect_stats(files, fd, pattern):
if __name__ == '__main__':
- parser = optparse.OptionParser(
- usage="[options] callgrind_file(s)")
- parser.add_option(
- '-d', '--directory', dest='directory',
- default='coverage',
- help='Destination directory for output [default: coverage]')
- parser.add_option(
- '-p', '--pattern', dest='pattern',
- default='numpy',
- help='Regex pattern to match against source file paths [default: numpy]')
- parser.add_option(
- '-f', '--format', dest='format', default=[],
- action='append', type='choice', choices=('text', 'html'),
- help="Output format(s) to generate, may be 'text' or 'html' [default: both]")
- (options, args) = parser.parse_args()
+ import argparse
+
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument(
+ 'callgrind_file', nargs='+',
+ help='One or more callgrind files')
+ parser.add_argument(
+ '-d', '--directory', default='coverage',
+ help='Destination directory for output (default: %(default)s)')
+ parser.add_argument(
+ '-p', '--pattern', default='numpy',
+ help='Regex pattern to match against source file paths '
+ '(default: %(default)s)')
+ parser.add_argument(
+ '-f', '--format', action='append', default=[],
+ choices=['text', 'html'],
+ help="Output format(s) to generate. "
+ "If option not provided, both will be generated.")
+ args = parser.parse_args()
files = SourceFiles()
- for log_file in args:
+ for log_file in args.callgrind_file:
log_fd = open(log_file, 'r')
- collect_stats(files, log_fd, options.pattern)
+ collect_stats(files, log_fd, args.pattern)
log_fd.close()
- if not os.path.exists(options.directory):
- os.makedirs(options.directory)
+ if not os.path.exists(args.directory):
+ os.makedirs(args.directory)
- if options.format == []:
+ if args.format == []:
formats = ['text', 'html']
else:
- formats = options.format
+ formats = args.format
if 'text' in formats:
- files.write_text(options.directory)
+ files.write_text(args.directory)
if 'html' in formats:
if not has_pygments:
print("Pygments 0.11 or later is required to generate HTML")
sys.exit(1)
- files.write_html(options.directory)
+ files.write_html(args.directory)