用于输入简化的命令行用户工具
cutie的Python项目详细描述
可爱
用于输入简化的命令行用户工具
以优雅的方式处理普通用户输入函数的工具。 它支持询问“是”或“否”问题,使用箭头键从列表中选择元素,强制用户输入数字并确保文本输入安全,同时具有许多自定义选项。
例如,“是”或“否”输入支持使用箭头键强制用户匹配大小写、制表符自动完成和切换选项。 数字输入允许设置最小值和最大值,输入浮点或强制用户使用整数。 只有当用户以该格式输入一个数字时,它才会返回,如果不符合,则向用户显示警告。
它应该适用于所有主要的操作系统(Mac、Linux、Windows)
用法
这些是皮肤的主要功能。
example.py包含扩展版本,还显示了select_multiple
选项
importcutieifcutie.prompt_yes_or_no('Are you brave enough to continue?'):# List of names to select from, including some captionsnames=['Kings:','Arthur, King of the Britons','Knights of the Round Table:','Sir Lancelot the Brave','Sir Robin the Not-Quite-So-Brave-as-Sir-Lancelot','Sir Bedevere the Wise','Sir Galahad the Pure','Swedish captions:','Møøse']# Names which are captions and thus not selectablecaptions=[0,2,7]# Get the namename=names[cutie.select(names,caption_indices=captions,selected_index=8)]print(f'Welcome, {name}')# Get an integer greater or equal to 0age=cutie.get_number('What is your age?',min_value=0,allow_float=False)# Get input without showing it being typedquest=cutie.secure_input('What is your quest?')print(f'{name}\'s quest (who is {age}) is {quest}.')
运行时,如上面的gif所示,它会产生以下输出:
Are you brave enough to continue? (Y/N) Yes
Kings:
[ ] Arthur, King of the Britons
Knights of the Round Table:
[ ] Sir Lancelot the Brave
[x] Sir Robin the Not-Quite-So-Brave-as-Sir-Lancelot
[ ] Sir Bedevere the Wise
[ ] Sir Galahad the Pure
Swedish captions:
[ ] Møøse
Welcome, Sir Robin the Not-Quite-So-Brave-as-Sir-Lancelot
What is your age? 31
What is your quest?
Sir Robin the Not-Quite-So-Brave-as-Sir-Lancelot's quest (who is 31) is to find the holy grail.
安装
使用来自pypi的pip:
pip3 install cutie
使用源或虚拟环境中的PIP:
pip3 install -r requirements.txt
文件
这里解释了皮肤的所有功能。 如果还不清楚,或者您对实现有疑问,请查看cutie.py。 实施是相当直接的
获取U编号
从用户输入中获取数字
如果输入的号码无效,将再次提示用户。
可以提供最小值和最大值。它们是包容的。
如果allow_float
选项(默认情况下为True
)设置为False
,则强制用户输入整数。
例如,获取任何三位数都可以这样做:
number=cutie.get_number('Please enter a three digit number:',min_value=100,max_value=999,allow_float=False)# which is equivalent tonumber=cutie.get_number('Please enter a three digit number',100,999,False)
参数
argument | type | default | description |
---|---|---|---|
^{ | str | The prompt asking the user to input. | |
^{ | float, optional | - infinity | The [inclusive] minimum value. |
^{ | float, optional | infinity | The [inclusive] maximum value. |
^{ | bool, optional | True | Allow floats or force integers. |
返回
用户输入的数字
安全输入
获取安全输入,而不在命令行中显示它。
这可以用于密码:
password=cutie.secure_input('Please enter your password:')
参数
argument | type | description |
---|---|---|
^{ | str | The prompt asking the user to input. |
返回
安全输入。
选择
从列表中选择一个选项。
通过将标题或分隔符作为选项添加并将其索引包含在caption_indices
中,可以在选项之间包含标题或分隔符。
可以提供预选索引。
在最简单的情况下,可以这样使用:
colors=['red','green','blue','yellow']print('What is your favorite color?')favorite_color=colors[cutie.select(colors)]
具有高度的可定制性,但也可以执行如下操作:
print('Select server to ping')server_id=cutie.select(servers,deselected_prefix=' ',selected_prefix='PING',selected_index=default_server_ip)
参数
argument | type | default | description | ||
---|---|---|---|---|---|
^{ | List[str] | The options to select from. | |||
^{ | List[int], optional | ^{ | Non-selectable indices. | ||
^{ | str, optional | ^{ | Prefix for deselected option. | ||
^{ | str, optional | ^{ | Prefix for selected option. | ||
^{ | str, optional | ^{ | int, optional | 0 | The index to be selected at first. |
返回
已选择的索引。
选择多个
从列表中选择多个选项。
默认显示“确认”按钮。 在这种情况下,空格键并输入select a line。 按钮可以隐藏。 在这种情况下,空格键选择行并输入确认选择
这不在本自述文件的示例中,而是在example.py中。
packages_to_update=cutie.select_multiple(outdated_packages,deselected_unticked_prefix=' KEEP ',deselected_ticked_prefix=' UPDATE ',selected_unticked_prefix='[ KEEP ]',selected_ticked_prefix='[UPDATE]',ticked_indices=list(range(len(outdated_packages))),deselected_confirm_label=' [[[[ UPDATE ]]]] ',selected_confirm_label='[ [[[[ UPDATE ]]]] ]')
参数
argument | type | default | description | ||
---|---|---|---|---|---|
^{ | List[str] | The options to select from. | |||
^{ | List[int], optional | Non-selectable indices. | |||
^{ | str, optional | ^{ | Prefix for lines that are not selected and not ticked . | ||
^{ | str, optional | ^{ | Prefix for lines that are not selected but ticked . | ||
^{ | str, optional | ^{ | Prefix for lines that are selected but not ticked . | ||
^{ | str, optional | ^{ | Prefix for lines that are selected and ticked . | ||
^{ | str, optional | ^{ | List[int], optional | ^{ | Indices that are ticked initially. |
^{ | int, optional | 0 | The index the cursor starts at. | ||
^{ | int, optional | 0 | The minimal amount of lines that have to be ticked. | ||
^{ | int, optional | infinity | The maximal amount of lines that have to be ticked. | ||
^{ | bool, optional | ^{ | Hide the confirm button. This causes ^{ | ||
^{ | str, optional | ^{ | The confirm label if not selected. | ||
^{ | str, optional | ^{ | The confirm label if selected. |
返回
已选择的索引列表。
提示“是”或“否
提示用户输入是或否。
这同样可以从非常简单到高度定制:
ifcutie.prompt_yes_or_no('Do you want to continue?'):do_continue()
ifcutie.prompt_yes_or_no('Do you want to hear ze funniest joke in ze world? Proceed at your own risk.',yes_text='JA',no_text='nein',has_to_match_case=True,# The user has to type the exact caseenter_empty_confirms=False,# An answer has to be selected)
参数
argument | type | default | description |
---|---|---|---|
^{ | str | The prompt asking the user to input. | |
^{ | str, optional | ^{ | The text corresponding to 'yes'. |
^{ | str, optional | ^{ | The text corresponding to 'no'. |
^{ | bool, optional | ^{ | Does the case have to match. |
^{ | bool, optional | True | Does enter on empty string work. |
^{ | bool, optional | False | Is yes selected by default |
^{ | str, optional |
| Prefix if something is deselected. |
^{ | str, optional | ^{ | Prefix if something is selected |
^{ | bool, optional | ^{ | The value on interrupt. |
^{ | bool, optional | ^{ | Add a [Y/N] to the prompt. |
返回
选择了什么。
更改日志
0.2.3[偏差]
- PEP8遵守Christopher Bilger
0.2.2
- 修复了示例中的python
0.2.1
- 扩展自述文件说明
0.2.0
select_multiple
- 调整自述文件
0.1.1
- 修复了pypi下载不起作用的问题
0.1.0
caption_indices
选项由dherrada
0.0.7
- 由Lhitrom 提供的Windows支持
0.0.x
- 初次上传,一切正常
贡献
如果你想贡献,请随时建议功能或实现他们自己。
还有请报告您可能发现的任何问题和错误!
如果你有一个使用可爱的项目,请让我知道,我会链接到这里!
作者
- 主要项目由me。
- 由Lhitrom提供的Windows支持。
caption_indices
和dherrada的小道消息。- PEP8遵守Christopher Bilger。
许可证
该项目是根据MIT-License授权的。
致谢
- 此项目使用模块Readchar进行直接输入处理。
GNU特里·普拉切特