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.
Showing the changes to scripts (1 of 1 file). Show all
Unified · Split
| @@ -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, output | 59 | return args.log_level, directory, output |
| 66 | 60 | ||
| 67 | 61 | ||
| 68 | def process_line(root_directory, file_directory, command_prefix, relative_path): | 62 | def process_line(root_directory, file_directory, command_prefix, relative_path): |