[Python-Dev] potential argparse problem: bad mix of parse_known_args and prefix matching (original) (raw)
R. David Murray rdmurray at bitdance.com
Tue Nov 26 18:46:09 CET 2013
- Previous message: [Python-Dev] potential argparse problem: bad mix of parse_known_args and prefix matching
- Next message: [Python-Dev] potential argparse problem: bad mix of parse_known_args and prefix matching
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Tue, 26 Nov 2013 09:30:10 -0800, Eli Bendersky <eliben at gmail.com> wrote:
Hello,
argparse does prefix matching as long as there are no conflicts. For example: argparser = argparse.ArgumentParser() argparser.addargument('--sync-foo', action='storetrue') args = argparser.parseargs() If I pass "--sync" to this script, it recognizes it as "--sync-foo". This behavior is quite surprising although I can see the motivation for it. At the very least it should be much more explicitly documented (AFAICS it's barely mentioned in the docs). If there's another argument registered, say "--sync-bar" the above will fail due to a conflict. Now comes the nasty part. When using "parseknownargs" instead of "parseargs", the above happens too - --sync is recognized for --sync-foo and captured by the parser. But this is wrong! The whole idea of parseknownargs is to parse the known args, leaving unknowns alone. This prefix matching harms more than it helps here because maybe the program we're actually acting as a front-end for (and hence using parseknownargs) knows about --sync and wants to get it. Unless I'm missing something, this is a bug. But I'm also not sure whether we can do anything about it at this point, as existing code may be relying on it. The right thing to do would be to disable this prefix matching when parseknownargs is called. Again, at the very least this should be documented (for parseknownargs not less than a warning box, IMHO).
I'm not sure whether or not it is a bug, but there are a number of bugs involving argument prefix matching in the tracker. Including one to be able to disable it, that has a patch but didn't get into 3.4 (issue 14910).
This topic seems more suited to the bug tracker than python-dev.
Stephen hasn't been very active lately. There is at least one non-committer who has been, but no committers have made time to review the patches and take action (they are on my list, but there is a lot of stuff ahead of them). So if you'd like to help out in that regard, that would be great :)
--David
- Previous message: [Python-Dev] potential argparse problem: bad mix of parse_known_args and prefix matching
- Next message: [Python-Dev] potential argparse problem: bad mix of parse_known_args and prefix matching
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]