diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2020-08-14 16:58:23 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-02-07 10:11:48 +0000 |
commit | 4edd467b28c895483cd5468d51d1c6824a21715a (patch) | |
tree | 04a4f32d617905acfc23653025b6e8d3899f51c6 /litecli/packages/prompt_utils.py | |
parent | Initial commit. (diff) | |
download | litecli-4edd467b28c895483cd5468d51d1c6824a21715a.tar.xz litecli-4edd467b28c895483cd5468d51d1c6824a21715a.zip |
Adding upstream version 1.5.0.upstream/1.5.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'litecli/packages/prompt_utils.py')
-rw-r--r-- | litecli/packages/prompt_utils.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/litecli/packages/prompt_utils.py b/litecli/packages/prompt_utils.py new file mode 100644 index 0000000..d9ad2b6 --- /dev/null +++ b/litecli/packages/prompt_utils.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + + +import sys +import click +from .parseutils import is_destructive + + +def confirm_destructive_query(queries): + """Check if the query is destructive and prompts the user to confirm. + + Returns: + * None if the query is non-destructive or we can't prompt the user. + * True if the query is destructive and the user wants to proceed. + * False if the query is destructive and the user doesn't want to proceed. + + """ + prompt_text = ( + "You're about to run a destructive command.\n" "Do you want to proceed? (y/n)" + ) + if is_destructive(queries) and sys.stdin.isatty(): + return prompt(prompt_text, type=bool) + + +def confirm(*args, **kwargs): + """Prompt for confirmation (yes/no) and handle any abort exceptions.""" + try: + return click.confirm(*args, **kwargs) + except click.Abort: + return False + + +def prompt(*args, **kwargs): + """Prompt the user for input and handle any abort exceptions.""" + try: + return click.prompt(*args, **kwargs) + except click.Abort: + return False |