有没有什么简单的方法来实现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…
我知道模仿它并不难,但是重写:|很烦人
当前回答
我过去常做的是……
question = 'Will the apple fall?'
print(question)
answer = int(input("Pls enter the answer: "
if answer == "y",
print('Well done')
print(answer)
其他回答
我修改了fmark的答案,用python 2/3兼容更pythonic。
如果您对更多错误处理感兴趣,请参阅ipython的实用程序模块
# PY2/3 compatibility
from __future__ import print_function
# You could use the six package for this
try:
input_ = raw_input
except NameError:
input_ = input
def query_yes_no(question, default=True):
"""Ask a yes/no question via standard input and return the answer.
If invalid input is given, the user will be asked until
they acutally give valid input.
Args:
question(str):
A question that is presented to the user.
default(bool|None):
The default value when enter is pressed with no value.
When None, there is no default value and the query
will loop.
Returns:
A bool indicating whether user has entered yes or no.
Side Effects:
Blocks program execution until valid input(y/n) is given.
"""
yes_list = ["yes", "y"]
no_list = ["no", "n"]
default_dict = { # default => prompt default string
None: "[y/n]",
True: "[Y/n]",
False: "[y/N]",
}
default_str = default_dict[default]
prompt_str = "%s %s " % (question, default_str)
while True:
choice = input_(prompt_str).lower()
if not choice and default is not None:
return default
if choice in yes_list:
return True
if choice in no_list:
return False
notification_str = "Please respond with 'y' or 'n'"
print(notification_str)
您可以使用单击的确认方法。
import click
if click.confirm('Do you want to continue?', default=True):
print('Do something')
这将打印:
$ Do you want to continue? [Y/n]:
应该适用于Linux, Mac或Windows上的Python 2/3。
文档:http://click.pocoo.org/5/prompts/ # confirmation-prompts
正如你提到的,最简单的方法是使用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 x.x
res = True
while res:
res = input("Please confirm with y/yes...").lower(); res = res not in {'y','yes','Y','YES',''}
一个非常简单(但不是很复杂)的方法是:
msg = 'Shall I?'
shall = input("%s (y/N) " % msg).lower() == 'y'
你也可以写一个简单的(稍微改进的)函数:
def yn_choice(message, default='y'):
choices = 'Y/n' if default.lower() in ('y', 'yes') else 'y/N'
choice = input("%s (%s) " % (message, choices))
values = ('y', 'yes', '') if choices == 'Y/n' else ('y', 'yes')
return choice.strip().lower() in values
注意:在Python 2上,使用raw_input而不是input。