首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >主要功能,如何使用里面的选项

主要功能,如何使用里面的选项
EN

Stack Overflow用户
提问于 2014-08-12 13:33:22
回答 1查看 970关注 0票数 0

我有一个带有几个函数的脚本,在这里编写的时间太长了,但是我在一个主函数中实现了optparse,即:

代码语言:javascript
运行
复制
def main():
    usage = "useage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-f", "--file", type="string", dest="filename", help="Get the name of the cabling file")
    parser.add_option("-i","--imod",dest="modules", help="write the modules in order to get some info",metavar="FILE")
    parser.add_option("-d","--data", type="string", dest="datas",default=True, help="write the options of the Info about a (set of) module(s)")
    parser.add_option("-j","--values", type="string", dest="infor",default=True, help="Modules associated to a(some) value(s)")
    parser.add_option("-k","--common", type="string", dest="common",default=True, help="Values with modules in common")

    (options, args) = parser.parse_args()

    if (options.filename is None):
        parser.error("No cabling filename was given!")


    #Info about modules    
    list1 = options.modules.split(',')
    list2 = options.datas.split(',')

    for i in list1:
        print "For module with DetId\n: %r " % (i)
    for j in ist2: 
        print  "%r is: %r" % (j,MyHVDict[i][j]) 
    if __name__=="__main__":
        main()

这个脚本还有其他一些函数,这取决于用户的输入(比如filename和主函数中定义的选项),所以我如何使用我在这个主函数中定义的选项,例如,如果所有东西都在主函数之外,我只需要随时编写options.filenameoptions.modules,但是在函数中我不知道该做什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-12 13:44:10

如果函数在de函数中被调用,则可以将options.what_you_need_in_function作为参数传递。

代码语言:javascript
运行
复制
def your_funcion(parameter_you_need):
    ...

def main():
    ...
    your_function(options.what_you_need_in_function)
    ...

顺便说一下,optparse模块是不推荐的,您可以更改为argparse模块。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25265673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档