Using optparse

L

loial

A shell script is passing parameters to my python script in the
following format

-PARAM1 12345 -PARAM2 67890

Can I parse these with optparse ? If so how?

I can't seem to get it to work. It seems to expect --PARAM1 and --
PARAM2
 
P

Peter Otten

loial said:
A shell script is passing parameters to my python script in the
following format

-PARAM1 12345 -PARAM2 67890

Can I parse these with optparse ? If so how?

I can't seem to get it to work. It seems to expect --PARAM1 and --
PARAM2

You are out of luck. Quoting

http://docs.python.org/library/optparse.html

"""
There are many different syntaxes for options; the traditional Unix syntax
is a hyphen (“-“) followed by a single letter, e.g. "-x" or "-F". Also,
traditional Unix syntax allows multiple options to be merged into a single
argument, e.g. "-x -F" is equivalent to "-xF". The GNU project
introduced "--" followed by a series of hyphen-separated words,
e.g. "--file" or "--dry-run". These are the only two option syntaxes
provided by optparse.
"""

Peter
 
P

Piet van Oostrum

loial said:
L> A shell script is passing parameters to my python script in the
L> following format
L> -PARAM1 12345 -PARAM2 67890
L> Can I parse these with optparse ? If so how?
L> I can't seem to get it to work. It seems to expect --PARAM1 and --
L> PARAM2

See the doc:

Some other option syntaxes that the world has seen include:
[...]
* a hyphen followed by a whole word, e.g. "-file" [...]

These option syntaxes are not supported by optparse, and they never will be.
 
A

Aahz

A shell script is passing parameters to my python script in the
following format

-PARAM1 12345 -PARAM2 67890

Can I parse these with optparse ? If so how?

You might try using shlex instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,178
Latest member
SusanaHam4

Latest Threads

Top