gpt4 book ai didi

python shell add_option问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:47:42 25 4
gpt4 key购买 nike

我对 python add_option 感到有些困惑

我是这样写代码的

parser.add_option("-u", "--uniqueness",
action="store_true", dest="uniqueness", default=False)

稍后在 shell 中,我想写这样的东西:

if(the user input -u option):
do something

我的问题是,如何在 python 中编写“用户输入 -u 选项”?我尝试使用“if(parser.uniqueness)”,但是这个命令不起作用。顺便说一句,我正在使用 python 2.7.3(只是想知道这个命令是否与 python 3 中的命令不同)thx

最佳答案

首先,optparse 已弃用,因此您应该使用 argparse - 但逻辑是相似的。

您需要先在某处执行 args = parser.parse_args(),然后使用 :

checkin 此对象
if args.uniqueness:
# potato

关于python shell add_option问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14493945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com