Greedy parsing of argparse/positional arguments

Joshua Landau at
Wed Nov 21 01:09:45 CET 2012

On 20 November 2012 10:02, Johannes Bauer <dfnsonfsduifb at> 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 ( where the example you have posted
is just:

"""My Program.

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

    --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: --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: <>

More information about the Python-list mailing list