我们假设我有这样的代码:
import optparseparser = optparse.OptionParser()parser.add_option('-i','--in_dir',action = "store",default = 'n',help = 'this is an optional arg')(options,args) = parser.parse_args()
然后我做:
if options.in_dir == 'n': print 'the user dID not pass any value for the in_dir option'else: print 'the user in_dir=%s' %(options.in_dir)
基本上我想要有默认值,这意味着用户没有输入这样的选项与实际值.使用’n’是随意的,有更好的推荐吗?
解决方法 你可以使用一个空字符串“”,Python将其解释为False;你可以简单测试:if options.in_dir: # argument supplIEdelse: # still empty,no arg
或者,使用None:
if options.in_dir is None: # no argelse: # arg supplIEd
请注意,后者是,每the documentation是未提供参数的默认值.
总结以上是内存溢出为你收集整理的python optparse,可选选项的默认值全部内容,希望文章能够帮你解决python optparse,可选选项的默认值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)