Assert stdin exists for all interactive functions.

This commit is contained in:
voussoir 2021-10-10 00:42:18 -07:00
parent a4196f2b13
commit 1a375e4964
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -1,8 +1,14 @@
'''
This module provides functions for interactive command line UIs.
'''
import sys
from voussoirkit import pipeable
def assert_stdin():
if sys.stdin is None:
raise RuntimeError('Interactive functions don\'t work when stdin is None.')
####################################################################################################
# ABC_CHOOSER ######################################################################################
####################################################################################################
@ -26,6 +32,8 @@ def abc_chooser(options, prompt='', must_pick=False):
The return value is the item from the options list, or None if must_pick is
False and the user entered nothing.
'''
assert_stdin()
option_letters = _abc_make_option_letters(options)
while True:
@ -55,6 +63,8 @@ def abc_chooser_many(options, prompt='', label='X'):
they like, and submit their selection by entering one more blank line.
The return value is a list of items from the options list.
'''
assert_stdin()
selected = set()
option_letters = _abc_make_option_letters(options)
@ -110,6 +120,8 @@ def getpermission(
until they choose an acceptable answer. Either way, the intended usage of
`if getpermission():` will always only accept in case of explicit yes.
'''
assert_stdin()
if prompt is not None:
pipeable.stderr(prompt)
while True: