Python:如何使选项在optparse需要? [英] Python: How to make an option to be required in optparse?
本文介绍了Python:如何使选项在optparse需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已阅读了这篇 http://docs.python.org/release/2.6。 2 / library / optparse.html
但是我不太清楚如何在optparse中设置一个选项?
But I'm not so clear how to make an option to be required in optparse?
我尝试设置required = 1,但出现错误:
I've tried to set "required=1" but I got an error:
arguments:required
invalid keyword arguments: required
我想让我的脚本require - file
由用户输入。我知道 action
关键字给你错误,当你不提供值到 - 文件
其 action =store_true
。
I want to make my script require --file
option to be input by users. I know that the action
keyword gives you error when you don't supply value to --file
whose action="store_true"
.
推荐答案
parser = OptionParser(usage='usage: %prog [options] arguments')
parser.add_option('-f', '--file',
dest='filename',
help='foo help')
(options, args) = parser.parse_args()
if not options.filename: # if filename is not given
parser.error('Filename not given')
这篇关于Python:如何使选项在optparse需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文