经过一番搜索,我找到了两个满足我需求的库!
第一个是python 询问者 https://github.com/magmax/python-inquirer,Python 端口询问者.js https://github.com/SBoudrias/Inquirer.js/,Yeoman 等项目使用的 CLI 库。我发现这个库有一个非常好的 API(构建在祝福 https://pypi.python.org/pypi/blessings/)但在设计/功能方面缺乏完善。
第二个(我将使用的)是whaaaaat https://github.com/finklabs/whaaaaat,Inquirer 的另一个 Python 端口。该库提供的功能更接近原始 Inquirer.js,并且正是我所需要的。然而,该 API 不如 python-inquirer 干净。
例子:
python-inquirer
例子:
from pprint import pprint
import inquirer
questions = [
inquirer.List(
"size",
message="What size do you need?",
choices=["Jumbo", "Large", "Standard", "Medium", "Small", "Micro"],
),
]
answers = inquirer.prompt(questions)
pprint(answers)
whaaaaat
例子:
from whaaaaat import prompt, print_json, Separator
questions = [
{
"type": "list",
"name": "theme",
"message": "What do you want to do?",
"choices": [
"Order a pizza",
"Make a reservation",
Separator(),
"Ask for opening hours",
{"name": "Contact support", "disabled": "Unavailable at this time"},
"Talk to the receptionist",
],
},
{
"type": "list",
"name": "size",
"message": "What size do you need?",
"choices": ["Jumbo", "Large", "Standard", "Medium", "Small", "Micro"],
"filter": lambda val: val.lower(),
},
]
answers = prompt(questions)
print_json(answers)