diff options
Diffstat (limited to 'packages/meshbay-cli/src/meshbay_cli/output.py')
| -rw-r--r-- | packages/meshbay-cli/src/meshbay_cli/output.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/packages/meshbay-cli/src/meshbay_cli/output.py b/packages/meshbay-cli/src/meshbay_cli/output.py index 75a54ff..d4c0758 100644 --- a/packages/meshbay-cli/src/meshbay_cli/output.py +++ b/packages/meshbay-cli/src/meshbay_cli/output.py @@ -138,3 +138,23 @@ class Progress: def sentence(text: str) -> str: """The first letter up, the rest untouched: `str.capitalize` turns -R into -r.""" return text[:1].upper() + text[1:] + + +def confirm(question: str, yes: bool) -> None: + """ + Go on only on a yes: `--yes`, or `y` typed at a terminal. With neither — a + script that did not say so — refuse before anything is sent, rather than + take silence for consent. + """ + from .errors import USAGE, CliError + if yes: + return + if not sys.stdin.isatty(): + raise CliError(USAGE, f"{question} Not done: there is no terminal to ask on; " + "pass --yes to confirm.") + # On stderr, like every other word to the person: stdout is the result. + sys.stderr.write(f"{question} [y/N] ") + sys.stderr.flush() + answer = sys.stdin.readline() + if answer.strip().lower() not in ("y", "yes"): + raise CliError(USAGE, "Not done.") |