Using optparse

Discussion in 'Python' started by loial, Apr 23, 2009.

  1. loial

    loial Guest

    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
     
    loial, Apr 23, 2009
    #1
    1. Advertising

  2. loial

    Peter Otten Guest

    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
     
    Peter Otten, Apr 23, 2009
    #2
    1. Advertising

  3. >>>>> loial <> (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 <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Apr 23, 2009
    #3
  4. loial

    Aahz Guest

    In article <>,
    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?


    You might try using shlex instead.
    --
    Aahz () <*> 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
     
    Aahz, Apr 23, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert  McGovern

    seg fault using optparse

    Robert McGovern, Jul 12, 2004, in forum: Ruby
    Replies:
    1
    Views:
    120
  2. Ganesh Pal
    Replies:
    0
    Views:
    114
    Ganesh Pal
    Feb 25, 2014
  3. Peter Otten
    Replies:
    0
    Views:
    74
    Peter Otten
    Feb 25, 2014
  4. Mark Lawrence
    Replies:
    0
    Views:
    70
    Mark Lawrence
    Feb 25, 2014
  5. Ganesh Pal
    Replies:
    1
    Views:
    92
Loading...

Share This Page