APT 命令行界面之类的是/否输入? [英] APT command line interface-like yes/no input?

查看:51
本文介绍了APT 命令行界面之类的是/否输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么捷径可以实现 Python 中的 APT(Advanced Package Tool)命令行界面的功能?

我的意思是,当包管理器提示是/否问题后跟 [Yes/no] 时,脚本接受 YES/Y/yes/y回车(默认为Yes,如大写字母所示).

我在官方文档中找到的唯一内容是 inputraw_input...

我知道模仿并不难,但重写很烦人:|

解决方案

正如你所提到的,最简单的方法是使用 raw_input()(或简单的 input()对于 Python 3).没有内置的方法可以做到这一点.来自 食谱 577058:

导入系统def query_yes_no(问题,默认=是"):"通过 raw_input() 提出是/否问题并返回他们的答案.问题"是呈现给用户的字符串.默认"如果用户只是点击 ,则是假定的答案.必须是是"(默认),否"或无(意思是需要用户回答).答案"是"的返回值为真或否"为假."有效 = {是":真,y":真,是":真,否":假,n":假}如果默认为无:提示 = "[是/否] "elif 默认 == 是":提示 = "[是/否] "elif 默认==否":提示 = "[是/否] "别的:raise ValueError("无效的默认答案:'%s'" % default)为真:sys.stdout.write(问题+提示)选择 = 输入().下()如果默认值不是 None 并且选择 == ":返回有效[默认]elif 选择有效:返回有效[选择]别的:sys.stdout.write("请回复'是'或'否'""(或'y'或'n').\n")

(对于 Python 2,使用 raw_input 而不是 input.)用法示例:

<预><代码>>>>query_yes_no(卷心菜比花椰菜好吃吗?")卷心菜比花椰菜好吃吗?[是/否] 哎呀请回答是"或否"(或y"或n").卷心菜比花椰菜好吃吗?[是/否] [输入]>>>真的>>>query_yes_no(卷心菜比花椰菜好吃吗?", None)卷心菜比花椰菜好吃吗?[是/否] [输入]请回答是"或否"(或y"或n").卷心菜比花椰菜好吃吗?[是/否] 是>>>真的

Is there any short way to achieve what the APT (Advanced Package Tool) command line interface does in Python?

I mean, when the package manager prompts a yes/no question followed by [Yes/no], the script accepts YES/Y/yes/y or Enter (defaults to Yes as hinted by the capital letter).

The only thing I find in the official docs is input and raw_input...

I know it's not that hard to emulate, but it's annoying to rewrite :|

解决方案

As you mentioned, the easiest way is to use raw_input() (or simply input() for Python 3). There is no built-in way to do this. From Recipe 577058:

import sys


def query_yes_no(question, default="yes"):
    """Ask a yes/no question via raw_input() and return their answer.

    "question" is a string that is presented to the user.
    "default" is the presumed answer if the user just hits <Enter>.
            It must be "yes" (the default), "no" or None (meaning
            an answer is required of the user).

    The "answer" return value is True for "yes" or False for "no".
    """
    valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
    if default is None:
        prompt = " [y/n] "
    elif default == "yes":
        prompt = " [Y/n] "
    elif default == "no":
        prompt = " [y/N] "
    else:
        raise ValueError("invalid default answer: '%s'" % default)

    while True:
        sys.stdout.write(question + prompt)
        choice = input().lower()
        if default is not None and choice == "":
            return valid[default]
        elif choice in valid:
            return valid[choice]
        else:
            sys.stdout.write("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n")

(For Python 2, use raw_input instead of input.) Usage example:

>>> query_yes_no("Is cabbage yummier than cauliflower?")
Is cabbage yummier than cauliflower? [Y/n] oops
Please respond with 'yes' or 'no' (or 'y' or 'n').
Is cabbage yummier than cauliflower? [Y/n] [ENTER]
>>> True

>>> query_yes_no("Is cabbage yummier than cauliflower?", None)
Is cabbage yummier than cauliflower? [y/n] [ENTER]
Please respond with 'yes' or 'no' (or 'y' or 'n').
Is cabbage yummier than cauliflower? [y/n] y
>>> True

这篇关于APT 命令行界面之类的是/否输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆