From 0add05de87b827ea0582cc75aabf52eeb4ce1209 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Thu, 18 Aug 2016 02:18:37 -0700 Subject: [PATCH] do not print infer.py usage message on wrong arguments Summary: On wrong arguments (or on no arguments at all), `infer` would spew the error message of `infer.py`, which makes no sense. Make the python code swallow error messages and exit with a special code on errors coming from command line parsing so that the OCaml side is in charge of printing usage messages. Reviewed By: cristianoc Differential Revision: D3731594 fbshipit-source-id: fe49cda --- infer/lib/python/infer.py | 17 ++++++++++++++++- infer/src/backend/infer.ml | 3 +++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/infer/lib/python/infer.py b/infer/lib/python/infer.py index c65a7838ed6..27c31ba73d9 100755 --- a/infer/lib/python/infer.py +++ b/infer/lib/python/infer.py @@ -78,8 +78,23 @@ def split_args_to_parse(): return (sys_argv[1:dd_index], cmd_raw) +class FailSilentlyArgumentParser(argparse.ArgumentParser): + '''We want to leave the handling of printing usage messages to the + OCaml code. To do so, swallow error messages from ArgumentParser + and exit with a special error code (101) that infer.ml looks for. + ''' + + def error(self, message): + utils.stderr(message) + utils.stderr('') + exit(22) # in sync with infer.ml + + def print_help(self, file=None): + exit(22) # in sync with infer.ml + + def create_argparser(parents=[]): - parser = argparse.ArgumentParser( + parser = FailSilentlyArgumentParser( parents=[analyze.infer_parser] + parents, add_help=False, formatter_class=argparse.RawDescriptionHelpFormatter, diff --git a/infer/src/backend/infer.ml b/infer/src/backend/infer.ml index 95011fad63a..8d31a43952a 100644 --- a/infer/src/backend/infer.ml +++ b/infer/src/backend/infer.ml @@ -104,6 +104,9 @@ let () = ) in let pid = Unix.create_process args_py.(0) args_py Unix.stdin Unix.stdout Unix.stderr in let _, status = Unix.waitpid [] pid in + if status = Unix.WEXITED 22 then + (* swallow infer.py argument parsing error *) + Config.print_usage_exit (); (* collect crashcontext summaries *) let analysis_is_crashcontext = match Config.analyzer with | Some Crashcontext -> true