Re: argparse '--' not working?

Discussion in 'Python' started by Peter Otten, Nov 12, 2010.

  1. Peter Otten

    Peter Otten Guest

    Neal Becker wrote:

    > It is a 'standard' behaviour that a lone '--' terminates options.
    > argparse says:
    >
    > If you have positional arguments that must begin with '-' and don’t look
    > like negative numbers, you can insert the pseudo-argument '--' which tells
    > parse_args that everything after that is a positional argument:


    /If/ you have positonal arguments...

    > But it doesn't seem to work:
    >
    > import argparse
    >
    > parser = argparse.ArgumentParser()
    > parser.add_argument ('--submit', '-s', action='store_true')
    > parser.add_argument ('--list', '-l', action='store_true')
    > opt = parser.parse_args()
    >
    > ./queue --submit -- test1.py -n
    > usage: queue [-h] [--submit] [--list]
    > queue: error: unrecognized arguments: -- test1.py -n


    Unlike optparse in argparse you must declare positional arguments, too:

    >>> import argparse
    >>> parser = argparse.ArgumentParser()
    >>> parser.add_argument("-s", action="store_true")

    [snip]
    >>> parser.add_argument("whatever", nargs="*")

    [snip]
    >>> parser.parse_args(["-s", "yadda"])

    Namespace(s=True, whatever=['yadda'])
    >>> parser.parse_args(["--", "-s", "yadda"])

    Namespace(s=False, whatever=['-s', 'yadda'])

    Peter
    Peter Otten, Nov 12, 2010
    #1
    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. Steven Bethard

    [ANN] The argparse module

    Steven Bethard, Aug 3, 2006, in forum: Python
    Replies:
    0
    Views:
    789
    Steven Bethard
    Aug 3, 2006
  2. Steven Bethard
    Replies:
    1
    Views:
    273
    Steven Bethard
    Oct 11, 2006
  3. Steven Bethard
    Replies:
    0
    Views:
    266
    Steven Bethard
    Oct 24, 2006
  4. Steven Bethard
    Replies:
    0
    Views:
    274
    Steven Bethard
    Dec 5, 2006
  5. Neal Becker

    argparse '--' not working?

    Neal Becker, Nov 12, 2010, in forum: Python
    Replies:
    0
    Views:
    192
    Neal Becker
    Nov 12, 2010
Loading...

Share This Page