Greedy parsing of argparse/positional arguments

Joshua Landau joshua.landau.ws at gmail.com
Tue Nov 20 19:09:45 EST 2012


On 20 November 2012 10:02, Johannes Bauer <dfnsonfsduifb at gmx.de> wrote:

> Hi list,
>
> I have a problem with Python3.2's argparse module. The following sample:
>
> parser = argparse.ArgumentParser(prog = sys.argv[0])
> parser.add_argument("-enc", metavar = "enc", nargs = "+", type = str,
> default = [ "utf-8" ])
> parser.add_argument("pattern", metavar = "pattern", type = str, nargs = 1)
> parser.add_argument("filename", metavar = "filename", type = str, nargs =
> 1)
> args = parser.parse_args(sys.argv[1:])
>
> illustrates the problem: I want to be able to specify an encoding one or
> more times (multiple encodings possible), have a pattern and a filename
> as the last two arguments.
>
> This works as long as I don't specify '-enc' on the command line. If I
> do, for example
>
> ./foo -enc myencoding mypattern myfile
>
> The "-enc" greedy parser seems to capture ["myencoding", "mypattern",
> "myfile"], leaving nothing for "pattern" and "filename", yielding an error:
>
> ./foo: error: too few arguments
>
> How can I force positional arguments to take precedence over optional
> arguments? I could exclude them from the parsing altogether, but that
> would make them not appear in the help page (which I'd like to avoid).


My first suggestion would be to change "-enc" to "--enc",

my second to make the input "--enc FIRST --enc SECOND --enc THIRD...
pattern filename" (action="append", remove nargs="+"),

and my third to use docopt (docopt.org) where the example you have posted
is just:
-------------

"""My Program.

Usage:
    my_prog.py [--enc=<encoding>...] <pattern> <filename>

Options:
    --enc <encoding>  [default: UTF-8]

"""

from docopt import docopt
arguments = docopt(__doc__)

--------------*
Note that this will not work if you don't take my first two suggestions.


An alternative is like mplayer's, where it accepts comma-delimited lists:

my_prog.py --enc UTF-8,ASCII,FOO,BAR "pattern%" filename.txt

Where you will parse the comma-delimited list afterwards. This is only
worth it if you expect a lot of encodings.


Q: How about actually answering the question?
A: I don't know how, short of parsing it manually.


* Small differences exist
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121121/80e955e7/attachment.html>


More information about the Python-list mailing list