[fix_includes] Use argparse instead of deprecated optparse

Roughly following
https://docs.python.org/3/library/argparse.html#upgrading-optparse-code.

The --help output comes out slightly different, but no other functional
change intended.
This commit is contained in:
Kim Gräsman 2022-12-15 20:48:49 +01:00
parent 6c3b7bcfb8
commit 523c16e8da
1 changed files with 81 additions and 78 deletions

View File

@ -60,17 +60,14 @@ up. Then we're done!
__author__ = 'csilvers@google.com (Craig Silverstein)'
import difflib
import optparse
import argparse
import os
import re
import sys
from collections import OrderedDict
_USAGE = """\
%prog [options] [filename] ... < <output from include-what-you-use script>
OR %prog -s [other options] <filename> ...
%prog reads the output from the include-what-you-use
_EPILOG = """\
Reads the output from the include-what-you-use
script on stdin -- run with --v=1 (default) verbose or above -- and,
unless --sort_only or --dry_run is specified,
modifies the files mentioned in the output, removing their old
@ -395,7 +392,7 @@ class IWYUOutputParser(object):
Arguments:
iwyu_output: a File object returning lines from an iwyu run
flags: commandline flags, as parsed by optparse. We use
flags: commandline flags, as parsed by argparse. We use
flags.comments, which controls whether we output comments
generated by iwyu.
Returns:
@ -1249,7 +1246,7 @@ def _ShouldInsertBlankLine(decorated_move_span, next_decorated_move_span,
next_decorated_move_span: the next decorated_move_span, which may
be a sentinel decorated_move_span at end-of-file.
file_lines: an array of LineInfo objects with .deleted filled in.
flags: commandline flags, as parsed by optparse. We use
flags: commandline flags, as parsed by argparse. We use
flags.blank_lines, which controls whether we put blank
lines between different 'kinds' of #includes.
@ -1714,7 +1711,7 @@ def _FirstReorderSpanWith(file_lines, good_reorder_spans, kind, filename,
kind: one of *_KIND values.
filename: the name of the file that file_lines comes from.
This is passed to _GetLineKind (are we a main-CU #include?)
flags: commandline flags, as parsed by optparse. We use
flags: commandline flags, as parsed by argparse. We use
flags.separate_project_includes to sort the #includes for the
current project separately from other #includes.
@ -1877,7 +1874,7 @@ def _DecoratedMoveSpanLines(iwyu_record, file_lines, move_span_lines, flags):
forward-declares already in the file, this will be a sub-list
of file_lines. For #includes and forward-declares we're adding
in, it will be a newly created list.
flags: commandline flags, as parsed by optparse. We use
flags: commandline flags, as parsed by argparse. We use
flags.separate_project_includes to sort the #includes for the
current project separately from other #includes.
@ -2088,7 +2085,7 @@ def FixFileLines(iwyu_record, file_lines, flags, fileinfo):
higher) pertaining to a single source file.
file_lines: a list of LineInfo objects holding the parsed output of
the file in iwyu_record.filename
flags: commandline flags, as parsed by optparse. We use
flags: commandline flags, as parsed by argparse. We use
flags.safe_headers to turn off deleting lines, and use the
other flags indirectly (via calls to other routines).
fileinfo: FileInfo for the current file.
@ -2215,7 +2212,7 @@ def FixManyFiles(iwyu_records, flags):
the parsed output of the include-what-you-use script (run at
verbose level 1 or higher) pertaining to a single source file.
iwyu_record.filename indicates what file to edit.
flags: commandline flags, as parsed by optparse..
flags: commandline flags, as parsed by argparse..
Returns:
The number of files fixed (as opposed to ones that needed no fixing).
@ -2258,7 +2255,7 @@ def ProcessIWYUOutput(f, files_to_process, flags, cwd):
f: an iterable object that is the output of include_what_you_use.
files_to_process: A set of filenames, or None. If not None, we
ignore files mentioned in f that are not in files_to_process.
flags: commandline flags, as parsed by optparse. The only flag
flags: commandline flags, as parsed by argparse. The only flag
we use directly is flags.ignore_re, to indicate files not to
process; we also pass the flags to other routines.
cwd: the current working directory, externalized for testing.
@ -2339,7 +2336,7 @@ def SortIncludesInFiles(files_to_process, flags):
Arguments:
files_to_process: a list (or set) of filenames.
flags: commandline flags, as parsed by optparse. We do not use
flags: commandline flags, as parsed by argparse. We do not use
any flags directly, but pass them to other routines.
Returns:
@ -2360,82 +2357,88 @@ def SortIncludesInFiles(files_to_process, flags):
def main(argv):
# Parse the command line.
parser = optparse.OptionParser(usage=_USAGE)
parser.add_option('-b', '--blank_lines', action='store_true', default=True,
help=('Put a blank line between primary header file and'
' C/C++ system #includes, and another blank line'
' between system #includes and google #includes'
' [default]'))
parser.add_option('--noblank_lines', action='store_false', dest='blank_lines')
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description='Update files based on include-what-you-use output',
epilog=_EPILOG)
parser.add_argument('-b', '--blank_lines', action='store_true', default=True,
help=('Put a blank line between primary header file and'
' C/C++ system #includes, and another blank line'
' between system #includes and google #includes'
' [default]'))
parser.add_argument('--noblank_lines', action='store_false',
dest='blank_lines')
parser.add_option('--comments', action='store_true', default=False,
help='Put comments after the #include lines')
parser.add_option('--nocomments', action='store_false', dest='comments')
parser.add_argument('--comments', action='store_true', default=False,
help='Put comments after the #include lines')
parser.add_argument('--nocomments', action='store_false', dest='comments')
parser.add_option('--update_comments', action='store_true', default=False,
help=('Replace \'why\' comments with the ones provided by'
' IWYU'))
parser.add_option('--noupdate_comments', action='store_false',
dest='update_comments')
parser.add_argument('--update_comments', action='store_true', default=False,
help=('Replace \'why\' comments with the ones provided by'
' IWYU'))
parser.add_argument('--noupdate_comments', action='store_false',
dest='update_comments')
parser.add_option('--safe_headers', action='store_true', default=True,
help=('Do not remove unused #includes/fwd-declares from'
' header files; just add new ones [default]'))
parser.add_option('--nosafe_headers', action='store_false',
dest='safe_headers')
parser.add_argument('--safe_headers', action='store_true', default=True,
help=('Do not remove unused #includes/fwd-declares from'
' header files; just add new ones [default]'))
parser.add_argument('--nosafe_headers', action='store_false',
dest='safe_headers')
parser.add_option('--reorder', action='store_true', default=False,
help=('Re-order lines relative to other similar lines '
'(e.g. headers relative to other headers)'))
parser.add_option('--noreorder', action='store_false', dest='reorder',
help=('Do not re-order lines relative to other similar '
'lines.'))
parser.add_argument('--reorder', action='store_true', default=False,
help=('Re-order lines relative to other similar lines '
'(e.g. headers relative to other headers)'))
parser.add_argument('--noreorder', action='store_false', dest='reorder',
help=('Do not re-order lines relative to other similar '
'lines.'))
parser.add_option('-s', '--sort_only', action='store_true',
help=('Just sort #includes of files listed on cmdline;'
' do not add or remove any #includes'))
parser.add_argument('-s', '--sort_only', action='store_true',
help=('Just sort #includes of files listed on cmdline;'
' do not add or remove any #includes'))
parser.add_option('-n', '--dry_run', action='store_true', default=False,
help=('Do not actually edit any files; just print diffs.'
' Return code is 0 if no changes are needed,'
' else min(the number of files that would be'
' modified, 100)'))
parser.add_argument('-n', '--dry_run', action='store_true', default=False,
help=('Do not actually edit any files; just print diffs.'
' Return code is 0 if no changes are needed,'
' else min(the number of files that would be'
' modified, 100)'))
parser.add_option('--ignore_re', default=None,
help=('fix_includes.py will skip editing any file whose'
' name matches this regular expression.'))
parser.add_argument('--ignore_re', default=None,
help=('%(prog)s will skip editing any file whose name'
' matches this regular expression.'))
parser.add_option('--only_re', default=None,
help='fix_includes.py will skip editing any file whose'
' name does not match this regular expression.')
parser.add_argument('--only_re', default=None,
help=('%(prog)s will skip editing any file whose name'
' does not match this regular expression.'))
parser.add_option('--separate_project_includes', default=None,
help=('Sort #includes for current project separately'
' from all other #includes. This flag specifies'
' the root directory of the current project.'
' If the value is "<tld>", #includes that share the'
' same top-level directory are assumed to be in the'
' same project. If not specified, project #includes'
' will be sorted with other non-system #includes.'))
parser.add_argument('--separate_project_includes', default=None,
help=('Sort #includes for current project separately'
' from all other #includes. This flag specifies'
' the root directory of the current project.'
' If the value is "<tld>", #includes that share the'
' same top-level directory are assumed to be in the'
' same project. If not specified, project #includes'
' will be sorted with other non-system #includes.'))
parser.add_option('-m', '--keep_iwyu_namespace_format', action='store_true',
default=False,
help=('Keep forward-declaration namespaces in IWYU format, '
'eg. namespace n1 { namespace n2 { class c1; } }.'
' Do not convert to "normalized" Google format: '
'namespace n1 {\\nnamespace n2 {\\n class c1;'
'\\n}\\n}.'))
parser.add_option('--nokeep_iwyu_namespace_format', action='store_false',
dest='keep_iwyu_namespace_format')
parser.add_argument('-m', '--keep_iwyu_namespace_format', action='store_true',
default=False,
help=('Keep forward-declaration namespaces in IWYU format'
', eg. namespace n1 { namespace n2 { class c1; } }.'
' Do not convert to "normalized" Google format: '
'namespace n1 {\\nnamespace n2 {\\n class c1;'
'\\n}\\n}.'))
parser.add_argument('--nokeep_iwyu_namespace_format', action='store_false',
dest='keep_iwyu_namespace_format')
parser.add_option('--basedir', '-p', default=None,
help=('Specify the base directory. fix_includes will '
'interpret non-absolute filenames relative to this '
'path.'))
parser.add_argument('--basedir', '-p', default=None,
help=('Specify the base directory. fix_includes will '
'interpret non-absolute filenames relative to this '
'path.'))
(flags, files_to_modify) = parser.parse_args(argv[1:])
if files_to_modify:
files_to_modify = set(files_to_modify)
parser.add_argument('files', nargs='*', metavar='FILES')
flags = parser.parse_args(argv[1:])
if flags.files:
files_to_modify = set(flags.files)
else:
files_to_modify = None