有没有什么简单的方法来实现APT(高级包工具)命令行界面在Python中的作用?
我的意思是,当包管理器提示一个yes/no问题,后面跟着[yes/no]时,脚本接受yes/ Y/yes/ Y或Enter(默认为yes,由大写字母提示)。
我在官方文档中唯一找到的是input和raw_input…
我知道模仿它并不难,但是重写:|很烦人
有没有什么简单的方法来实现APT(高级包工具)命令行界面在Python中的作用?
我的意思是,当包管理器提示一个yes/no问题,后面跟着[yes/no]时,脚本接受yes/ Y/yes/ Y或Enter(默认为yes,由大写字母提示)。
我在官方文档中唯一找到的是input和raw_input…
我知道模仿它并不难,但是重写:|很烦人
当前回答
我会这样做:
# raw_input returns the empty string for "enter"
yes = {'yes','y', 'ye', ''}
no = {'no','n'}
choice = raw_input().lower()
if choice in yes:
return True
elif choice in no:
return False
else:
sys.stdout.write("Please respond with 'yes' or 'no'")
其他回答
def question(question, answers):
acceptable = False
while not acceptable:
print(question + "specify '%s' or '%s'") % answers
answer = raw_input()
if answer.lower() == answers[0].lower() or answers[0].lower():
print('Answer == %s') % answer
acceptable = True
return answer
raining = question("Is it raining today?", ("Y", "N"))
换做是我就会这么做。
输出
Is it raining today? Specify 'Y' or 'N'
> Y
answer = 'Y'
你也可以使用提词器。
无耻地从README中截取:
#pip install prompter
from prompter import yesno
>>> yesno('Really?')
Really? [Y/n]
True
>>> yesno('Really?')
Really? [Y/n] no
False
>>> yesno('Really?', default='no')
Really? [y/N]
True
Python 3.8及以上版本的一行代码:
while res:= input("When correct, press enter to continue...").lower() not in {'y','yes','Y','YES',''}: pass
正如你提到的,最简单的方法是使用raw_input()(或简单的input()对于Python 3)。没有内置的方法可以做到这一点。配方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")
(对于Python 2,使用raw_input而不是input。) 使用的例子:
>>> 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
对python 3执行同样的操作。X, raw_input()不存在:
def ask(question, default = None):
hasDefault = default is not None
prompt = (question
+ " [" + ["y", "Y"][hasDefault and default] + "/"
+ ["n", "N"][hasDefault and not default] + "] ")
while True:
sys.stdout.write(prompt)
choice = input().strip().lower()
if choice == '':
if default is not None:
return default
else:
if "yes".startswith(choice):
return True
if "no".startswith(choice):
return False
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")