Request user input, updating the underlying config if applicable. Args: prompt: On-screen prompt before user input default_value: The default (existing) value config: BinaryAlertConfig instance, if updating the underlying configuration If None, the valid valu
(prompt: str, default_value: str,
config: Any = None, property_name: str = None)
| 19 | |
| 20 | |
| 21 | def get_input(prompt: str, default_value: str, |
| 22 | config: Any = None, property_name: str = None) -> str: |
| 23 | """Request user input, updating the underlying config if applicable. |
| 24 | |
| 25 | Args: |
| 26 | prompt: On-screen prompt before user input |
| 27 | default_value: The default (existing) value |
| 28 | config: BinaryAlertConfig instance, if updating the underlying configuration |
| 29 | If None, the valid values are assumed to be 'yes' and 'no' |
| 30 | property_name: Name of the config property to update (applicable only if config != None) |
| 31 | |
| 32 | Returns: |
| 33 | Lowercase user input, stripped of extra spaces, or the default value if no input was given |
| 34 | """ |
| 35 | if default_value: |
| 36 | prompt = '{} ({}): '.format(prompt, default_value) |
| 37 | else: |
| 38 | prompt = '{}: '.format(prompt) |
| 39 | |
| 40 | # Keep requesting user input until it is valid |
| 41 | while True: |
| 42 | user_input = input(prompt).strip().lower() or default_value |
| 43 | if config and property_name: |
| 44 | try: |
| 45 | setattr(config, property_name, user_input) |
| 46 | break |
| 47 | except InvalidConfigError as error: |
| 48 | print('ERROR: {}'.format(error)) |
| 49 | elif user_input in {'yes', 'no'}: |
| 50 | break |
| 51 | else: |
| 52 | print('ERROR: Please enter exactly "yes" or "no"') |
| 53 | |
| 54 | return user_input |
| 55 | |
| 56 | |
| 57 | class BinaryAlertConfig: |
no outgoing calls
no test coverage detected