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

Tim N. van der Leeuw tim.leeuwvander at nl.unisys.com
Wed May 17 08:40:18 EDT 2006


Andrew Robert wrote:
> Tim N. van der Leeuw wrote:
> > Andrew Robert wrote:
[...]
> Hi Tim,
>
> I am using the pymqi module which is freely available at
> http://pymqi.sourceforge.net/ .
>
> Documentation on the module can be found at
> http://pymqi.sourceforge.net/pymqidoc.html .
>
> I have a few python examples on my web site located at
> http://home.townisp.com/~arobert/
>
> There are also a lot of good examples at
> http://www.koders.com/info.aspx?c=ProjectInfo&pid=TVM5FGBZMY4E5ZH7GC9AX54PAC
> .
>
> If you come up with anything, I would be glad to see what you have.
>
>

Thanks a lot for these examples! I have some Java tools that send MQ
messages (reading, in fact, a ton of command-line arguments from a
file) and I need better tools. If I could use some Python for rewriting
this, it might speed me up a lot.


> Back to the original issue:
>
> I'm not sure exactly what you mean about the readfile option and format.
>
> Could you send me a code snippet so I can get a better feel for it?
>
>
> Thanks,
> Andy

Here's the file I used:

===cut here===
x=4
w=6
what=7
zoo=9
===cut here===

Here's some snippets of code:

>>> from optparse import OptionParser
>>> op = OptionParser()
>>> op.add_option('-x', dest='x')
>>> op.add_option('--what', '-w', dest='what')
>>> v=op.parse_args()[0]
>>> v.read_file('options-test.txt')
>>> v
<Values at 0x12a9c88: {'x': 4, 'what': 7}>

As you can see, I'm parsing an empty command-line but I could parse a
full command-line as well before loading options from file.
After parsing the command-line, I get an instance of a 'Values' object,
and on this object I call a method 'read_file' with a filename. (I
could also call 'read_module', and it will add the __doc__ string).

I didn't test what happens with a more advanced useage of OptionParser
options.


Cheers,

--Tim




More information about the Python-list mailing list