Closed GoogleCodeExporter closed 9 years ago
One use case to keep in mind would be a script that runs another program with
--options. In fact, I'd be happy to just have a way to say that "everything
after
this argument is an argument and not an --option even if it has hyphens". I'm
not
sure how to implement this, though.
Original comment by robert.kern@gmail.com
on 6 Jun 2009 at 10:15
The standard mechanism for this is the special non-option '--'. It triggers the
option recognition without having a name to let the parser know that everything
after
the '--' is positional...
Original comment by caux...@gmail.com
on 8 Jun 2009 at 9:56
Yes, argparse already supports the special '--' non-option. That's the current
recommended workaround. Something like::
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('my_arg')
>>> parser.add_argument('their_arg', nargs='*')
>>> parser.parse_args('MINE -- --theirs theirs theirs'.split())
Namespace(my_arg='MINE', their_arg=['--theirs', 'theirs', 'theirs'])
I'm still not sure what the best way to implement this is. Some of the places
that
would need to be changed::
The place where errors are raised for invalid options:
self.error(_('no such option: %s') % option_string)
The places where errors are raised for extra positionals:
msg = _('extra arguments found: %s')
self.error(_('extra arguments found: %s') % ' '.join(extras))
There may be other places, but those are the ones that jumped out at me first.
I'll
try to make some time to look into this, but if anyone is feeling generous and
wants
to take a stab at a patch, I'd be happy for the help.
Original comment by steven.b...@gmail.com
on 10 Jun 2009 at 2:27
Ok, this actually turned out to be much easier than I thought it would be. You
can
now call parser.parse_known_args() as of r23. It works just like the example
code in
the first post, returning a tuple of the usual namespace object, followed by the
remaining arg strings.
I'd be grateful if a few people could try this out and let me know how it goes
before
I make the next argparse release.
Original comment by steven.b...@gmail.com
on 14 Jun 2009 at 10:36
Original issue reported on code.google.com by
steven.b...@gmail.com
on 28 Mar 2009 at 2:03