Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Using optparse

Reply
Thread Tools

Using optparse

 
 
loial
Guest
Posts: n/a
 
      04-23-2009
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



 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      04-23-2009
loial wrote:

> 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
 
Reply With Quote
 
 
 
 
Piet van Oostrum
Guest
Posts: n/a
 
      04-23-2009
>>>>> loial <(E-Mail Removed)> (L) wrote:

>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.

--
Piet van Oostrum <(E-Mail Removed)>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      04-23-2009
In article <(E-Mail Removed)>,
loial <(E-Mail Removed)> wrote:
>
>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.
--
Aahz ((E-Mail Removed)) <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur." --Red Adair
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
seg fault using optparse Robert McGovern Ruby 1 07-12-2004 02:31 PM
request for a constructor feature in module optparse Daniel Ortmann Python 0 10-13-2003 03:38 PM
Required options in optparse Gregory (Grisha) Trubetskoy Python 0 10-04-2003 01:31 AM
blanks embedded in python 2.3 optparse pascal barbedor Python 3 07-29-2003 10:19 PM
Re: Iterating over optparse options David Goodger Python 0 06-25-2003 01:46 PM



Advertisments