prompt: handle KeyboardInterrupt more gracefully
This commit is contained in:
@@ -7,6 +7,8 @@ from dataclasses import dataclass
|
|||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from clan_cli.errors import ClanError
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
# This is for simulating user input in tests.
|
# This is for simulating user input in tests.
|
||||||
@@ -107,17 +109,23 @@ def ask(
|
|||||||
log.info(f"Prompting value for {ident}")
|
log.info(f"Prompting value for {ident}")
|
||||||
if MOCK_PROMPT_RESPONSE:
|
if MOCK_PROMPT_RESPONSE:
|
||||||
return next(MOCK_PROMPT_RESPONSE)
|
return next(MOCK_PROMPT_RESPONSE)
|
||||||
match input_type:
|
try:
|
||||||
case PromptType.LINE:
|
match input_type:
|
||||||
result = input(f"{text}: ")
|
case PromptType.LINE:
|
||||||
case PromptType.MULTILINE:
|
result = input(f"{text}: ")
|
||||||
print(f"{text} (Finish with Ctrl-D): ")
|
case PromptType.MULTILINE:
|
||||||
result = sys.stdin.read()
|
print(f"{text} (Finish with Ctrl-D): ")
|
||||||
case PromptType.MULTILINE_HIDDEN:
|
result = sys.stdin.read()
|
||||||
print("Enter multiple lines (press Ctrl-D to finish or Ctrl-C to cancel):")
|
case PromptType.MULTILINE_HIDDEN:
|
||||||
result = get_multiline_hidden_input()
|
print(
|
||||||
case PromptType.HIDDEN:
|
"Enter multiple lines (press Ctrl-D to finish or Ctrl-C to cancel):"
|
||||||
result = getpass(f"{text} (hidden): ")
|
)
|
||||||
|
result = get_multiline_hidden_input()
|
||||||
|
case PromptType.HIDDEN:
|
||||||
|
result = getpass(f"{text} (hidden): ")
|
||||||
|
except KeyboardInterrupt as e:
|
||||||
|
msg = "User cancelled the input."
|
||||||
|
raise ClanError(msg) from e
|
||||||
|
|
||||||
log.info("Input received. Processing...")
|
log.info("Input received. Processing...")
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user