History

gen_compile_commands: use choices for --log_levels option

Use 'choices' to check if the given parameter is valid. I also simplified the help message because, with 'choices', --help shows the list of valid parameters: --log_level {DEBUG,INFO,WARNING,ERROR,CRITICAL} I started the help message with a lower case, "the level of log ..." in order to be consistent with the -h option: -h, --help show this help message and exit The message "show this help ..." comes from the ArgumentParser library code, and I do not know how to change it. So, I changed our code.

Masahiro Yamada committed 6 years ago

Commit ea6cedc1 parent 8a685db1 file · +4 −10Browse files

Unified · Split

scripts/gen_compile_commands.py+4−10
@@ -45,24 +45,18 @@ def parse_arguments(): · 44 unmodified lines
45 'compile_commands.json in the search directory)')45 'compile_commands.json in the search directory)')
46 parser.add_argument('-o', '--output', type=str, help=output_help)46 parser.add_argument('-o', '--output', type=str, help=output_help)
47 47
48 log_level_help = ('The level of log messages to produce (one of ' +48 log_level_help = ('the level of log messages to produce (defaults to ' +
49 ', '.join(_VALID_LOG_LEVELS) + '; defaults to ' +
50 _DEFAULT_LOG_LEVEL + ')')49 _DEFAULT_LOG_LEVEL + ')')
51 parser.add_argument(50 parser.add_argument('--log_level', choices=_VALID_LOG_LEVELS,
52 '--log_level', type=str, default=_DEFAULT_LOG_LEVEL,51 default=_DEFAULT_LOG_LEVEL, help=log_level_help)
53 help=log_level_help)
54 52
55 args = parser.parse_args()53 args = parser.parse_args()
56 54
57 log_level = args.log_level
58 if log_level not in _VALID_LOG_LEVELS:
59 raise ValueError('%s is not a valid log level' % log_level)
60
61 directory = args.directory or os.getcwd()55 directory = args.directory or os.getcwd()
62 output = args.output or os.path.join(directory, _DEFAULT_OUTPUT)56 output = args.output or os.path.join(directory, _DEFAULT_OUTPUT)
63 directory = os.path.abspath(directory)57 directory = os.path.abspath(directory)
64 58
65 return log_level, directory, output59 return args.log_level, directory, output
66 60
67 61
68def process_line(root_directory, file_directory, command_prefix, relative_path):62def process_line(root_directory, file_directory, command_prefix, relative_path):