diff options
author | Gregory Schevchenko <3405066+gvsheva@users.noreply.github.com> | 2020-07-25 19:58:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-25 19:58:45 (GMT) |
commit | daff39070e7ea71b0ba49d9150ac7a210a125682 (patch) | |
tree | 4de54c29cf333f91ca25f73e5693fb938a24c798 /Lib/py_compile.py | |
parent | af08db7bac3087aac313d052c1a6302bee7c9c89 (diff) | |
download | cpython-daff39070e7ea71b0ba49d9150ac7a210a125682.zip cpython-daff39070e7ea71b0ba49d9150ac7a210a125682.tar.gz cpython-daff39070e7ea71b0ba49d9150ac7a210a125682.tar.bz2 |
bpo-38731: Add --quiet option to py_compile CLI (GH-17134)
Diffstat (limited to 'Lib/py_compile.py')
-rw-r--r-- | Lib/py_compile.py | 74 |
1 files changed, 34 insertions, 40 deletions
diff --git a/Lib/py_compile.py b/Lib/py_compile.py index 2173689..0f9b590 100644 --- a/Lib/py_compile.py +++ b/Lib/py_compile.py @@ -173,46 +173,40 @@ def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1, return cfile -def main(args=None): - """Compile several source files. - - The files named in 'args' (or on the command line, if 'args' is - not specified) are compiled and the resulting bytecode is cached - in the normal manner. This function does not search a directory - structure to locate source files; it only compiles files named - explicitly. If '-' is the only parameter in args, the list of - files is taken from standard input. - - """ - if args is None: - args = sys.argv[1:] - rv = 0 - if args == ['-']: - while True: - filename = sys.stdin.readline() - if not filename: - break - filename = filename.rstrip('\n') - try: - compile(filename, doraise=True) - except PyCompileError as error: - rv = 1 - if quiet < 2: - sys.stderr.write("%s\n" % error.msg) - except OSError as error: - rv = 1 - if quiet < 2: - sys.stderr.write("%s\n" % error) +def main(): + import argparse + + description = 'A simple command-line interface for py_compile module.' + parser = argparse.ArgumentParser(description=description) + parser.add_argument( + '-q', '--quiet', + action='store_true', + help='Suppress error output', + ) + parser.add_argument( + 'filenames', + nargs='+', + help='Files to compile', + ) + args = parser.parse_args() + if args.filenames == ['-']: + filenames = sys.stdin.readlines() else: - for filename in args: - try: - compile(filename, doraise=True) - except PyCompileError as error: - # return value to indicate at least one failure - rv = 1 - if quiet < 2: - sys.stderr.write("%s\n" % error.msg) - return rv + filenames = args.filenames + for filename in filenames: + try: + compile(filename, doraise=True) + except PyCompileError as error: + if args.quiet: + parser.exit(1) + else: + parser.exit(1, error.msg) + except OSError as error: + if args.quiet: + parser.exit(1) + else: + parser.exit(1, str(error)) + if __name__ == "__main__": - sys.exit(main()) + main() |