Skip to content

Commit 4e3334a

Browse files
committed
Migrate from optparse to argparse
1 parent 18fe331 commit 4e3334a

File tree

1 file changed

+54
-57
lines changed

1 file changed

+54
-57
lines changed

gprof2dot.py

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import os.path
2727
import re
2828
import textwrap
29-
import optparse
29+
import argparse
3030
import xml.parsers.expat
3131
import collections
3232
import locale
@@ -3635,146 +3635,143 @@ def main(argv=sys.argv[1:]):
36353635
labelNames = list(labels.keys())
36363636
labelNames.sort()
36373637

3638-
optparser = optparse.OptionParser(
3639-
usage="\n\t%prog [options] [file] ...")
3640-
optparser.add_option(
3638+
argparser = argparse.ArgumentParser(
3639+
usage="\n %(prog)s [options] [file] ...")
3640+
argparser.add_argument(
36413641
'-o', '--output', metavar='FILE',
3642-
type="string", dest="output",
3642+
dest="output",
36433643
help="output filename [stdout]")
3644-
optparser.add_option(
3644+
argparser.add_argument(
36453645
'-n', '--node-thres', metavar='PERCENTAGE',
3646-
type="float", dest="node_thres", default=0.5,
3647-
help="eliminate nodes below this threshold [default: %default]")
3648-
optparser.add_option(
3646+
type=float, dest="node_thres", default=0.5,
3647+
help="eliminate nodes below this threshold [default: %(default)s]")
3648+
argparser.add_argument(
36493649
'-e', '--edge-thres', metavar='PERCENTAGE',
3650-
type="float", dest="edge_thres", default=0.1,
3651-
help="eliminate edges below this threshold [default: %default]")
3652-
optparser.add_option(
3650+
type=float, dest="edge_thres", default=0.1,
3651+
help="eliminate edges below this threshold [default: %(default)s]")
3652+
argparser.add_argument(
36533653
'-f', '--format',
3654-
type="choice", choices=formatNames,
3654+
choices=formatNames,
36553655
dest="format", default="prof",
3656-
help="profile format: %s [default: %%default]" % naturalJoin(formatNames))
3657-
optparser.add_option(
3656+
help="profile format: %s [default: %%(default)s]" % naturalJoin(formatNames))
3657+
argparser.add_argument(
36583658
'--total',
3659-
type="choice", choices=('callratios', 'callstacks'),
3659+
choices=('callratios', 'callstacks'),
36603660
dest="totalMethod", default=totalMethod,
3661-
help="preferred method of calculating total time: callratios or callstacks (currently affects only perf format) [default: %default]")
3662-
optparser.add_option(
3661+
help="preferred method of calculating total time: callratios or callstacks (currently affects only perf format) [default: %(default)s]")
3662+
argparser.add_argument(
36633663
'-c', '--colormap',
3664-
type="choice", choices=themeNames,
3664+
choices=themeNames,
36653665
dest="theme", default="color",
3666-
help="color map: %s [default: %%default]" % naturalJoin(themeNames))
3667-
optparser.add_option(
3666+
help="color map: %s [default: %%(default)s]" % naturalJoin(themeNames))
3667+
argparser.add_argument(
36683668
'-s', '--strip',
36693669
action="store_true",
36703670
dest="strip", default=False,
36713671
help="strip function parameters, template parameters, and const modifiers from demangled C++ function names")
3672-
optparser.add_option(
3672+
argparser.add_argument(
36733673
'--color-nodes-by-selftime',
36743674
action="store_true",
36753675
dest="color_nodes_by_selftime", default=False,
36763676
help="color nodes by self time, rather than by total time (sum of self and descendants)")
3677-
optparser.add_option(
3677+
argparser.add_argument(
36783678
'--colour-nodes-by-selftime',
36793679
action="store_true",
36803680
dest="color_nodes_by_selftime",
3681-
help=optparse.SUPPRESS_HELP)
3682-
optparser.add_option(
3681+
help=argparse.SUPPRESS)
3682+
argparser.add_argument(
36833683
'-w', '--wrap',
36843684
action="store_true",
36853685
dest="wrap", default=False,
36863686
help="wrap function names")
3687-
optparser.add_option(
3687+
argparser.add_argument(
36883688
'--show-samples',
36893689
action="store_true",
36903690
dest="show_samples", default=False,
36913691
help="show function samples")
3692-
optparser.add_option(
3692+
argparser.add_argument(
36933693
'--time-format',
36943694
default=timeFormat,
3695-
help="format to use for showing time values [default: %default]")
3696-
optparser.add_option(
3695+
help="format to use for showing time values [default: %(default)s]")
3696+
argparser.add_argument(
36973697
'--node-label', metavar='MEASURE',
3698-
type='choice', choices=labelNames,
3698+
choices=labelNames,
36993699
action='append',
37003700
dest='node_labels',
37013701
help="measurements to on show the node (can be specified multiple times): %s [default: %s]" % (
37023702
naturalJoin(labelNames), ', '.join(defaultLabelNames)))
37033703
# add option to show information on available entries ()
3704-
optparser.add_option(
3704+
argparser.add_argument(
37053705
'--list-functions',
3706-
type="string",
37073706
dest="list_functions", default=None,
37083707
help="""\
37093708
list functions available for selection in -z or -l, requires selector argument
37103709
( use '+' to select all).
37113710
Recall that the selector argument is used with Unix/Bash globbing/pattern matching,
37123711
and that entries are formatted '<pkg>:<linenum>:<function>'. When argument starts
3713-
with '%', a dump of all available information is performed for selected entries,
3714-
after removal of leading '%'.
3712+
with '%%', a dump of all available information is performed for selected entries,
3713+
after removal of leading '%%'.
37153714
""")
37163715
# add option to create subtree or show paths
3717-
optparser.add_option(
3716+
argparser.add_argument(
37183717
'-z', '--root',
3719-
type="string",
37203718
dest="root", default="",
37213719
help="prune call graph to show only descendants of specified root function")
3722-
optparser.add_option(
3720+
argparser.add_argument(
37233721
'-l', '--leaf',
3724-
type="string",
37253722
dest="leaf", default="",
37263723
help="prune call graph to show only ancestors of specified leaf function")
3727-
optparser.add_option(
3724+
argparser.add_argument(
37283725
'--depth',
3729-
type="int",
3726+
type=int,
37303727
dest="depth", default=-1,
37313728
help="prune call graph to show only descendants or ancestors until specified depth")
37323729
# add a new option to control skew of the colorization curve
3733-
optparser.add_option(
3730+
argparser.add_argument(
37343731
'--skew',
3735-
type="float", dest="theme_skew", default=1.0,
3732+
type=float, dest="theme_skew", default=1.0,
37363733
help="skew the colorization curve. Values < 1.0 give more variety to lower percentages. Values > 1.0 give less variety to lower percentages")
37373734
# add option for filtering by file path
3738-
optparser.add_option(
3735+
argparser.add_argument(
37393736
'-p', '--path', action="append",
3740-
type="string", dest="filter_paths",
3737+
dest="filter_paths",
37413738
help="Filter all modules not in a specified path")
3742-
optparser.add_option(
3739+
argparser.add_argument(
37433740
'--compare',
37443741
action="store_true",
37453742
dest="compare", default=False,
37463743
help="Compare two graphs with almost identical structure. With this option two files should be provided."
37473744
"gprof2dot.py [options] --compare [file1] [file2] ...")
3748-
optparser.add_option(
3745+
argparser.add_argument(
37493746
'--compare-tolerance',
3750-
type="float", dest="tolerance", default=0.001,
3751-
help="Tolerance threshold for node difference (default=0.001%)."
3747+
type=float, dest="tolerance", default=0.001,
3748+
help="Tolerance threshold for node difference (default=0.001%%)."
37523749
"If the difference is below this value the nodes are considered identical.")
3753-
optparser.add_option(
3750+
argparser.add_argument(
37543751
'--compare-only-slower',
37553752
action="store_true",
37563753
dest="only_slower", default=False,
37573754
help="Display comparison only for function which are slower in second graph.")
3758-
optparser.add_option(
3755+
argparser.add_argument(
37593756
'--compare-only-faster',
37603757
action="store_true",
37613758
dest="only_faster", default=False,
37623759
help="Display comparison only for function which are faster in second graph.")
3763-
optparser.add_option(
3760+
argparser.add_argument(
37643761
'--compare-color-by-difference',
37653762
action="store_true",
37663763
dest="color_by_difference", default=False,
37673764
help="Color nodes based on the value of the difference. "
37683765
"Nodes with the largest differences represent the hot spots.")
3769-
(options, args) = optparser.parse_args(argv)
3766+
(options, args) = argparser.parse_args(argv)
37703767

37713768
if len(args) > 1 and options.format != 'pstats' and not options.compare:
3772-
optparser.error('incorrect number of arguments')
3769+
argparser.error('incorrect number of arguments')
37733770

37743771
try:
37753772
theme = themes[options.theme]
37763773
except KeyError:
3777-
optparser.error('invalid colormap \'%s\'' % options.theme)
3774+
argparser.error('invalid colormap \'%s\'' % options.theme)
37783775

37793776
# set skew on the theme now that it has been picked.
37803777
if options.theme_skew:
@@ -3786,7 +3783,7 @@ def main(argv=sys.argv[1:]):
37863783
try:
37873784
Format = formats[options.format]
37883785
except KeyError:
3789-
optparser.error('invalid format \'%s\'' % options.format)
3786+
argparser.error('invalid format \'%s\'' % options.format)
37903787

37913788
if Format.stdinInput:
37923789
if not args:
@@ -3810,15 +3807,15 @@ def main(argv=sys.argv[1:]):
38103807
parser = Format(fp)
38113808
elif Format.multipleInput:
38123809
if not args:
3813-
optparser.error('at least a file must be specified for %s input' % options.format)
3810+
argparser.error('at least a file must be specified for %s input' % options.format)
38143811
if options.compare:
38153812
parser1 = Format(args[-2])
38163813
parser2 = Format(args[-1])
38173814
else:
38183815
parser = Format(*args)
38193816
else:
38203817
if len(args) != 1:
3821-
optparser.error('exactly one file must be specified for %s input' % options.format)
3818+
argparser.error('exactly one file must be specified for %s input' % options.format)
38223819
parser = Format(args[0])
38233820

38243821
if options.compare:

0 commit comments

Comments
 (0)