def ask(message, ofile=sys.stderr, ifile=sys.stdin, style=Fore.MAGENTA,
noecho=False, accept_empty=True):
"""
Print a question on *ofile* and wait for an answer on *ifile* using
:py:meth:`io.TextIOBase.readline`.
*style* may be ``None`` for non-colored output (this does not override the
behavior setup by :py:func:`enable_colors`).
"""
if noecho and ifile != sys.stdin:
raise ValueError("noecho option implies input from stdin")
while True:
with ScopedColoredStream(ofile, style, flush_on_exit=True) as stream:
stream.write(message)
if noecho:
ans = getpass.getpass(prompt="", stream=ofile)
else:
ans = ifile.readline().rstrip("\n\r")
if not accept_empty and not ans.strip():
continue
return ans
评论列表
文章目录