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 03:45:56 EDT 2006


Andrew Robert wrote:
> 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.
>

After reading your post I decided to play around with optparse a bit,
to get acquainted with it.

Experimenting with IDLE I found that the Values object returned by
parse_args has a method 'readfile', and this 'readfile' method allows
you to add options to the Values object.

The syntax should be in the form:

<option>=<value>

<option> should not include the hyphens -- if your option is added as
'-x', then you should write:

x=3

not:

-x=3

If you have options with both long names and short names, then you
should use the long name -- if your option is added as '-f', '--file',
then you should write:

file=foo.txt

not:

f=foo.txt


Also, you need the assignment-operator in your file.

I didn't find any documentation on this, and I didn't try this with any
actions; only with options added to the parser like
op.add_option('-x', dest='x')

Using this 2-step approach allows you to use optparse itself to get to
the command-line option with your command-line settings...


Out of pure personal interest, what queuing-system are you writing to
from Python? What libraries do you have for that? And is it commercial
software, or freely downloadable?
(I'd love to be able to write messages to IBM MQ Series from Python)

Cheers,

--Tim




More information about the Python-list mailing list