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.
Unified · Split
| @@ -45,24 +45,18 @@ def parse_arguments(): · 44 unmodified lines | ||
| 45 | 45 | 'compile_commands.json in the search directory)') |
| 46 | 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 ' + | |
| 49 | − ', '.join(_VALID_LOG_LEVELS) + '; defaults to ' + | |
| 48 | + log_level_help = ('the level of log messages to produce (defaults to ' + | |
| 50 | 49 | _DEFAULT_LOG_LEVEL + ')') |
| 51 | − parser.add_argument( | |
| 52 | − '--log_level', type=str, default=_DEFAULT_LOG_LEVEL, | |
| 53 | − help=log_level_help) | |
| 50 | + parser.add_argument('--log_level', choices=_VALID_LOG_LEVELS, | |
| 51 | + default=_DEFAULT_LOG_LEVEL, help=log_level_help) | |
| 54 | 52 | |
| 55 | 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 | 55 | directory = args.directory or os.getcwd() |
| 62 | 56 | output = args.output or os.path.join(directory, _DEFAULT_OUTPUT) |
| 63 | 57 | directory = os.path.abspath(directory) |
| 64 | 58 | |
| 65 | − return log_level, directory, output | |
| 59 | + return args.log_level, directory, output | |
| 66 | 60 | |
| 67 | 61 | |
| 68 | 62 | def process_line(root_directory, file_directory, command_prefix, relative_path): |