allow nargs starting with dash

2,125 views
Skip to first unread message

tarsius

unread,
Sep 29, 2009, 3:03:55 PM9/29/09
to argparse-users
How can I allow nargs that start with a dash like in this example?

$ cat test.py
#!/usr/bin/python
import argparse
from subprocess import Popen
from pipes import quote
parser = argparse.ArgumentParser()
parser.add_argument('shellcmd')
parser.add_argument('cmdargs', nargs='+')
args = parser.parse_args()
Popen([args.shellcmd] + [quote(x) for x in args.cmdargs])

$ ./test.py ls .
test.py

$ ./test.py ls -al
usage: test.py [-h] shellcmd cmdargs [cmdargs ...]
test.py: error: too few arguments

$ ./test.py ls -al .
usage: test.py [-h] shellcmd cmdargs [cmdargs ...]
test.py: error: unrecognized arguments: -al

Steven Bethard

unread,
Sep 29, 2009, 4:25:50 PM9/29/09
to argpars...@googlegroups.com

If you have command line arguments that start with a dash but are not
options, you typically need to insert the pseudo-argument '--' to
indicate that everything after that should be considered an argument,
regardless of whether or not it contains a '-' prefix. See the
documentation on "Arguments containing '-'" for more details:

http://argparse.googlecode.com/svn/trunk/doc/parse_args.html#arguments-containing

Steve
--
Where did you get that preposterous hypothesis?
Did Steve tell you that?
--- The Hiphopopotamus

Steven Bethard

unread,
Sep 29, 2009, 4:27:07 PM9/29/09
to argpars...@googlegroups.com

Another option would be to use the .parse_known_args method, which
just collects the unknown arguments into a list:

http://argparse.googlecode.com/svn/trunk/doc/other-methods.html#partial-parsing

tarsius

unread,
Sep 29, 2009, 6:28:05 PM9/29/09
to argparse-users
On Sep 29, 10:27 pm, Steven Bethard <steven.beth...@gmail.com> wrote:
> On Tue, Sep 29, 2009 at 1:25 PM, Steven Bethard
> > On Tue, Sep 29, 2009 at 12:03 PM, tarsius <jonasbernou...@gmail.com> wrote:
> >> How can I allow nargs that start with a dash
>
> Another option would be to use the .parse_known_args method, which
> just collects the unknown arguments into a list:

Seams like I didn't read the documentation good enough...

The simple example I provided can be fixed like this:

$ cat argparse-test2.py
#!/usr/bin/python

import sys
import argparse
from gettext import gettext as _

parser = argparse.ArgumentParser()
parser.add_argument('shellcmd')
args, argv = parser.parse_known_args()

print "args: (1): %s" % args

if argv:
before_cmd=[]
for arg in argv:
if sys.argv.index(args.shellcmd) > sys.argv.index(arg):
before_cmd.append(arg)
if len(before_cmd) > 0:
msg = _('unrecognized arguments: %s')
parser.error(msg % ' '.join(before_cmd))
args.cmdargs = argv

print "args: (2): %s" % args

$ ./argparse-test2.py ls -al
args: (1): Namespace(shellcmd='ls')
args: (2): Namespace(cmdargs=['-al'], shellcmd='ls')

$ ./argparse-test2.py --to-early ls -al # should fail
args: (1): Namespace(shellcmd='ls')
usage: argparse-test2.py [-h] shellcmd
argparse-test2.py: error: unrecognized arguments: --to-early

However when I use subparsers this doesn't seam to work. Before I ask
a stupid question again I will get some sleep and read the
documentation again. If that doesn't help I will post an example using
subcommands.

Thanks for your help
Jonas

Steven Bethard

unread,
Sep 29, 2009, 8:03:12 PM9/29/09
to argpars...@googlegroups.com
On Tue, Sep 29, 2009 at 3:28 PM, tarsius <jonasbe...@gmail.com> wrote:
> On Sep 29, 10:27 pm, Steven Bethard <steven.beth...@gmail.com> wrote:
>> On Tue, Sep 29, 2009 at 1:25 PM, Steven Bethard
>> > On Tue, Sep 29, 2009 at 12:03 PM, tarsius <jonasbernou...@gmail.com> wrote:
>> >> How can I allow nargs that start with a dash
>>
>> Another option would be to use the .parse_known_args method, which
>> just collects the unknown arguments into a list:
>
> However when I use subparsers this doesn't seam to work.  Before I ask
> a stupid question again I will get some sleep and read the
> documentation again. If that doesn't help I will post an example using
> subcommands.

I think you're right that there are troubles getting what you want
with subcommands::

>>> parser = argparse.ArgumentParser()
>>> parser.add_subparsers().add_parser('foo')
>>> parser.parse_known_args(['foo', 'bar'])
usage: foo [-h]
foo: error: unrecognized arguments: bar

That's because _SubParsersAction.__call__ always calls .parse_args()
because it can't tell whether it's being called within parse_args() or
parse_known_args(). Feel free to file a feature request to make
parse_known_args work with subparsers as well. A patch doing so would
be even better since I'm not currently sure how to implement this. ;-)

tarsius

unread,
Oct 1, 2009, 9:41:08 AM10/1/09
to argparse-users
On Sep 30, 2:03 am, Steven Bethard <steven.beth...@gmail.com> wrote:
> On Tue, Sep 29, 2009 at 3:28 PM, tarsius <jonasbernou...@gmail.com> wrote:
> > On Sep 29, 10:27 pm, Steven Bethard <steven.beth...@gmail.com> wrote:
> >> On Tue, Sep 29, 2009 at 1:25 PM, Steven Bethard
> >> > On Tue, Sep 29, 2009 at 12:03 PM, tarsius <jonasbernou...@gmail.com> wrote:
> >> >> How can I allow nargs that start with a dash
>
> >> Another option would be to use the .parse_known_args method, which
> >> just collects the unknown arguments into a list:
>
> > However when I use subparsers this doesn't seam to work.
>
> I think you're right that there are troubles getting what you want
> with subcommands::
> That's because _SubParsersAction.__call__ always calls .parse_args()
> because it can't tell whether it's being called within parse_args() or
> parse_known_args(). Feel free to file a feature request to make
> parse_known_args work with subparsers as well. A patch doing so would
> be even better since I'm not currently sure how to implement this. ;-)

Again, thanks for the information.

As it turned out some other changes I had to make to my application
(unrelated to argparse) allow me to easily work around this problem.

So for now I won't file a feature request or even provide a patch. The
time might come thought where I need this feature for some other
project though - but currently I just do not have the time (and now
not even the need anymore) to look into this any further.

And I would like to add that apart from this feature being missing
argparse hasn't disappointed me so far. Keep up the good work.
Reply all
Reply to author
Forward
0 new messages