用于输入简化的命令行用户工具

cutie的Python项目详细描述


可爱

用于输入简化的命令行用户工具

PRs WelcomePyPI versionPyPI licensePyPI pyversionsPEP8GitHub contributors

以优雅的方式处理普通用户输入函数的工具。 它支持询问“是”或“否”问题,使用箭头键从列表中选择元素,强制用户输入数字并确保文本输入安全,同时具有许多自定义选项。

例如,“是”或“否”输入支持使用箭头键强制用户匹配大小写、制表符自动完成和切换选项。 数字输入允许设置最小值和最大值,输入浮点或强制用户使用整数。 只有当用户以该格式输入一个数字时,它才会返回,如果不符合,则向用户显示警告。

它应该适用于所有主要的操作系统(Mac、Linux、Windows)

example

用法

这些是皮肤的主要功能。 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)

参数

argumenttypedefaultdescription
^{}strThe prompt asking the user to input.
^{}float, optional- infinityThe [inclusive] minimum value.
^{}float, optionalinfinityThe [inclusive] maximum value.
^{}bool, optionalTrueAllow floats or force integers.

返回

用户输入的数字

安全输入

获取安全输入,而不在命令行中显示它。

这可以用于密码:

password=cutie.secure_input('Please enter your password:')

参数

argumenttypedescription
^{}strThe 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)

参数

argumenttypedefaultdescription
^{}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, optional0The 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 ]]]] ]')

参数

argumenttypedefaultdescription
^{}List[str]The options to select from.
^{}List[int], optionalNon-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, optional0The index the cursor starts at.
^{}int, optional0The minimal amount of lines that have to be ticked.
^{}int, optionalinfinityThe maximal amount of lines that have to be ticked.
^{}bool, optional^{}Hide the confirm button. This causes ^{} to confirm the entire selection and not just tick the line.
^{}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)

参数

argumenttypedefaultdescription
^{}strThe 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, optionalTrueDoes enter on empty string work.
^{}bool, optionalFalseIs yes selected by default
^{}str, optionalPrefix 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[偏差]

0.2.2

  • 修复了示例中的python

0.2.1

  • 扩展自述文件说明

0.2.0

  • select_multiple
  • 调整自述文件

0.1.1

  • 修复了pypi下载不起作用的问题

0.1.0

0.0.7

0.0.x

  • 初次上传,一切正常

贡献

如果你想贡献,请随时建议功能或实现他们自己。

还有请报告您可能发现的任何问题和错误!

如果你有一个使用可爱的项目,请让我知道,我会链接到这里!

作者

许可证

该项目是根据MIT-License授权的。

致谢

  • 此项目使用模块Readchar进行直接输入处理。

GNU特里·普拉切特

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java附加两个数组   java如何在Extjs的不同页面中使用相同的存储   java Jung,大顶点的布局重叠,图形出现在奇怪的位置   java如何在Android中通过画布绘制圆?   java验证库设计模式选择   java研磨机未知标记“timedTests”   java Android领域子类实例方法   java使用resteasy/jaxrs从请求负载接收两种类型的数据   缓冲策略Java缓冲策略有时不能正确绘制   java跟踪棋子   密度无关像素如何在java中锐化图像缩略图?   java如何在MongoDB更新查询中编写(或)更新   java A*寻路游戏系统退出问题   java在安卓中读取xml的最佳方式是什么?   通过反射调用的方法的java抑制警告   安卓 java。语言错误:信号11(SIGSEGV),代码10(?),故障地址006e006f   java lombok 1.18.2使用DexBuilderForDebug抛出TransformClasses   java JOptionPane CD对话框按钮?   java只返回json中的一些值,而不是完整实体对象   java包不存在错误,请稍后重试