I have a script with several functions and is too long to write here, but I am implementing optparse
in a main function, which is:
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()
The script has some other functions, which depends on the input of the user (like a filename
and the options defined in the main function) so how can i use the options I defined inside this main function, for example if everything is outside the main function I would just have to write options.filename
or options.modules
, anytime I need this, but inside the function I do not know what to do.
If your functions are called inside de main function you can pass options.what_you_need_in_function
as an parameter.
def your_funcion(parameter_you_need):
...
def main():
...
your_function(options.what_you_need_in_function)
...
BTW optparse
module was deprecated, you can change to argparse
module.