diff options
Diffstat (limited to 'lib/python/qmk/questions.py')
| -rw-r--r-- | lib/python/qmk/questions.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/lib/python/qmk/questions.py b/lib/python/qmk/questions.py new file mode 100644 index 000000000..34b0b43bc --- /dev/null +++ b/lib/python/qmk/questions.py | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | """Functions to collect user input. | ||
| 2 | """ | ||
| 3 | |||
| 4 | from milc import cli, format_ansi | ||
| 5 | |||
| 6 | |||
| 7 | def yesno(prompt, *args, default=None, **kwargs): | ||
| 8 | """Displays prompt to the user and gets a yes or no response. | ||
| 9 | |||
| 10 | Returns True for a yes and False for a no. | ||
| 11 | |||
| 12 | If you add `--yes` and `--no` arguments to your program the user can answer questions by passing command line flags. | ||
| 13 | |||
| 14 | @add_argument('-y', '--yes', action='store_true', arg_only=True, help='Answer yes to all questions.') | ||
| 15 | @add_argument('-n', '--no', action='store_true', arg_only=True, help='Answer no to all questions.') | ||
| 16 | |||
| 17 | Arguments: | ||
| 18 | prompt | ||
| 19 | The prompt to present to the user. Can include ANSI and format strings like milc's `cli.print()`. | ||
| 20 | |||
| 21 | default | ||
| 22 | Whether to default to a Yes or No when the user presses enter. | ||
| 23 | |||
| 24 | None- force the user to enter Y or N | ||
| 25 | |||
| 26 | True- Default to yes | ||
| 27 | |||
| 28 | False- Default to no | ||
| 29 | """ | ||
| 30 | if not args and kwargs: | ||
| 31 | args = kwargs | ||
| 32 | |||
| 33 | if 'no' in cli.args and cli.args.no: | ||
| 34 | return False | ||
| 35 | |||
| 36 | if 'yes' in cli.args and cli.args.yes: | ||
| 37 | return True | ||
| 38 | |||
| 39 | if default is not None: | ||
| 40 | if default: | ||
| 41 | prompt = prompt + ' [Y/n] ' | ||
| 42 | else: | ||
| 43 | prompt = prompt + ' [y/N] ' | ||
| 44 | |||
| 45 | while True: | ||
| 46 | print() | ||
| 47 | answer = input(format_ansi(prompt % args)) | ||
| 48 | print() | ||
| 49 | |||
| 50 | if not answer and prompt is not None: | ||
| 51 | return default | ||
| 52 | |||
| 53 | elif answer.lower() in ['y', 'yes']: | ||
| 54 | return True | ||
| 55 | |||
| 56 | elif answer.lower() in ['n', 'no']: | ||
| 57 | return False | ||
| 58 | |||
| 59 | |||
| 60 | def question(prompt, *args, default=None, confirm=False, answer_type=str, **kwargs): | ||
| 61 | """Prompt the user to answer a question with a free-form input. | ||
| 62 | |||
| 63 | prompt | ||
| 64 | The prompt to present to the user. Can include ANSI and format strings like milc's `cli.print()`. | ||
| 65 | |||
| 66 | default | ||
| 67 | The value to return when the user doesn't enter any value. Use None to prompt until they enter a value. | ||
| 68 | |||
| 69 | answer_type | ||
| 70 | Specify a type function for the answer. Will re-prompt the user if the function raises any errors. Common choices here include int, float, and decimal.Decimal. | ||
| 71 | """ | ||
| 72 | if not args and kwargs: | ||
| 73 | args = kwargs | ||
| 74 | |||
| 75 | if default is not None: | ||
| 76 | prompt = '%s [%s] ' % (prompt, default) | ||
| 77 | |||
| 78 | while True: | ||
| 79 | print() | ||
| 80 | answer = input(format_ansi(prompt % args)) | ||
| 81 | print() | ||
| 82 | |||
| 83 | if answer: | ||
| 84 | if confirm: | ||
| 85 | if yesno('Is the answer "%s" correct?', answer, default=True): | ||
| 86 | try: | ||
| 87 | return answer_type(answer) | ||
| 88 | except Exception as e: | ||
| 89 | cli.log.error('Could not convert answer (%s) to type %s: %s', answer, answer_type.__name__, str(e)) | ||
| 90 | else: | ||
| 91 | try: | ||
| 92 | return answer_type(answer) | ||
| 93 | except Exception as e: | ||
| 94 | cli.log.error('Could not convert answer (%s) to type %s: %s', answer, answer_type.__name__, str(e)) | ||
| 95 | |||
| 96 | elif default is not None: | ||
| 97 | return default | ||
