用于用户输入的python库 [英] python library for user input

查看:19
本文介绍了用于用户输入的python库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 python 实现一个小的命令行工具,它需要向用户提出几个问题.我用

I am implementing a small command line tool in python that needs to ask the user a couple of questions. I use

raw_input('Are you male or female?')

一直以来.现在我希望能够处理愚蠢的用户(或者那些懒得阅读/记住文档的用户),所以我需要检查答案是否有意义.

all the time. Now I would like to be able to deal with dumb users (or those too lazy to read/remember the documentation), so I need to check whether the answer makes sense.

gender = ''
while gender not in ['male', 'female']:
    gender = raw_input('Are you male or female?')

我想知道是否存在像 argparse 这样的东西可以自动解决这个问题,比如

I am wondering whether there exists something like argparse that would automate this problem, something like

import inputparse 
gender = inputparse.get_input(prompt='Are you male or female?', type=str, possible_input=['male', 'female'])

会负责自动检查等吗?

推荐答案

Necromanting again...

Necromanting again...

如果您需要一个简单的问题帮助库,请查看 click.它的主要重点是命令行选项,但我认为它非常适合您的用例.

Please have a look at click if you need a simple helper library for questions. Its main focus are command line options, but it’s a good fit for your use case, I think.

这篇关于用于用户输入的python库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆