Option parser question - reading options from file as well as command line

Andrew Robert andrew.arobert at gmail.com
Tue May 16 19:30:33 CEST 2006


Hi Everyone.


I tried the following to get input into optionparser from either a file
or command line.


The code below detects the passed file argument and prints the file
contents but the individual swithces do not get passed to option parser.

Doing a test print of options.qmanager shows it unassigned.

Any ideas?

    #
    # Parse command line options and automatically build help/usage
    # display
    #
    if len(sys.argv) == 2:
	    infile= open(sys.argv[1],"rb").read()
	    print infile
	    parser=OptionParser( infile  )
    else:
	    parser = OptionParser()

    parser.add_option("-m","--qmanager", dest="qmanager",
                help="\t\tQueue Manager to inquire against"),
    parser.add_option("-q","--queue", dest="queue",
                help="\t\tQueue the message will be sent to"),
    parser.add_option("-t","--to", dest="mto",
                help="\t\taddress any mail messages will be sent to")
    (options, args) = parser.parse_args()

    print options.qmanager



More information about the Python-list mailing list