Merge pull request #4274 from stephenfin/use-argumentparser-error

sphinx-build: Use 'ArgumentParser.error'
This commit is contained in:
Takeshi KOMIYA 2017-12-10 21:07:07 +09:00 committed by GitHub
commit c30579b38e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -182,11 +182,7 @@ def main(argv=sys.argv[1:]): # type: ignore
# type: (List[unicode]) -> int # type: (List[unicode]) -> int
parser = get_parser() parser = get_parser()
# parse options args = parser.parse_args(argv)
try:
args = parser.parse_args(argv)
except SystemExit as err:
return err.code
# get paths (first and second positional argument) # get paths (first and second positional argument)
try: try:
@ -194,34 +190,28 @@ def main(argv=sys.argv[1:]): # type: ignore
confdir = abspath(args.confdir or srcdir) confdir = abspath(args.confdir or srcdir)
if args.noconfig: if args.noconfig:
confdir = None confdir = None
if not path.isdir(srcdir): if not path.isdir(srcdir):
print('Error: Cannot find source directory `%s\'.' % srcdir, parser.error('cannot find source directory (%s)' % srcdir)
file=sys.stderr)
return 1
if not args.noconfig and not path.isfile(path.join(confdir, 'conf.py')): if not args.noconfig and not path.isfile(path.join(confdir, 'conf.py')):
print('Error: Config directory doesn\'t contain a conf.py file.', parser.error("config directory doesn't contain a conf.py file "
file=sys.stderr) "(%s)" % confdir)
return 1
outdir = abspath(args.outputdir) outdir = abspath(args.outputdir)
if srcdir == outdir: if srcdir == outdir:
print('Error: source directory and destination directory are same.', parser.error('source directory and destination directory are same')
file=sys.stderr)
return 1
except UnicodeError: except UnicodeError:
print( parser.error('multibyte filename not supported on this filesystem '
'Error: Multibyte filename not supported on this filesystem ' 'encoding (%r)' % fs_encoding)
'encoding (%r).' % fs_encoding, file=sys.stderr)
return 1
# handle remaining filename arguments # handle remaining filename arguments
filenames = args.filenames filenames = args.filenames
errored = False missing_files = []
for filename in filenames: for filename in filenames:
if not path.isfile(filename): if not path.isfile(filename):
print('Error: Cannot find file %r.' % filename, file=sys.stderr) missing_files.append(filename)
errored = True if missing_files:
if errored: parser.error('cannot find files %r' % missing_files)
return 1
# likely encoding used for command-line arguments # likely encoding used for command-line arguments
try: try:
@ -231,8 +221,7 @@ def main(argv=sys.argv[1:]): # type: ignore
likely_encoding = None likely_encoding = None
if args.force_all and filenames: if args.force_all and filenames:
print('Error: Cannot combine -a option and filenames.', file=sys.stderr) parser.error('cannot combine -a option and filenames')
return 1
if args.color == 'no' or (args.color == 'auto' and not color_terminal()): if args.color == 'no' or (args.color == 'auto' and not color_terminal()):
nocolor() nocolor()
@ -245,15 +234,16 @@ def main(argv=sys.argv[1:]): # type: ignore
if args.quiet: if args.quiet:
status = None status = None
if args.really_quiet: if args.really_quiet:
status = warning = None status = warning = None
if warning and args.warnfile: if warning and args.warnfile:
try: try:
warnfp = open(args.warnfile, 'w') warnfp = open(args.warnfile, 'w')
except Exception as exc: except Exception as exc:
print('Error: Cannot open warning file %r: %s' % parser.error('cannot open warning file %r: %s' % (
(args.warnfile, exc), file=sys.stderr) args.warnfile, exc))
sys.exit(1)
warning = Tee(warning, warnfp) # type: ignore warning = Tee(warning, warnfp) # type: ignore
error = warning error = warning
@ -262,9 +252,7 @@ def main(argv=sys.argv[1:]): # type: ignore
try: try:
key, val = val.split('=', 1) key, val = val.split('=', 1)
except ValueError: except ValueError:
print('Error: -D option argument must be in the form name=value.', parser.error('-D option argument must be in the form name=value')
file=sys.stderr)
return 1
if likely_encoding and isinstance(val, binary_type): if likely_encoding and isinstance(val, binary_type):
try: try:
val = val.decode(likely_encoding) val = val.decode(likely_encoding)
@ -276,9 +264,7 @@ def main(argv=sys.argv[1:]): # type: ignore
try: try:
key, val = val.split('=') key, val = val.split('=')
except ValueError: except ValueError:
print('Error: -A option argument must be in the form name=value.', parser.error('-A option argument must be in the form name=value')
file=sys.stderr)
return 1
try: try:
val = int(val) val = int(val)
except ValueError: except ValueError:
@ -302,4 +288,4 @@ def main(argv=sys.argv[1:]): # type: ignore
return app.statuscode return app.statuscode
except (Exception, KeyboardInterrupt) as exc: except (Exception, KeyboardInterrupt) as exc:
handle_exception(app, args, exc, error) handle_exception(app, args, exc, error)
return 1 return 2