_cli.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:SDK 作者: Keypirinha 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号