<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Nov 26, 2013 at 9:38 AM, Guido van Rossum <span dir="ltr"><<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div>I think matching on the shortest unique prefix is common for command line parsers in general, not just argparse. I believe optparse did this too, and even the venerable getopt does! I think all this originated in the original (non-Python) GNU standard for long option parsing. All that probably explains why the docs hardly touch upon it.<br>



<br></div>As to why parse_known_args also does this, I can see the reasoning behind this behavior: to the end user, "--sync" is a valid option, so it would be surprising if it didn't get recognized under certain conditions.<br>



<br></div>I suppose you were badly bitten by this recently? Can you tell us more about what happened?<br></div></blockquote><div><br></div><div>Sure. We have a Python script that serves as a gateway to another program. That other program has a "--sync" option. The gateway script has a "--sync-foo" option. When the gateway script is invoked with "--sync", we'd expect it to pass it to the program; instead, it matches it to its own "--sync-foo" and consumes the option.<br>

<br></div><div>Practically, this means a big caveat on exactly the use case parse_known_args was designed for: whenever I have a Python script using argparse and passing unknown arguments to other programs, I have to manually make sure there are no common prefixes between any commands to avoid this problem.<br>

<br></div><div>Frankly I don't see how the current behavior can be seen as the intended one.<br><br></div><div>Eli<br><br></div><div><br><br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div dir="ltr"></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div class="h5">On Tue, Nov 26, 2013 at 9:30 AM, Eli Bendersky <span dir="ltr"><<a href="mailto:eliben@gmail.com" target="_blank">eliben@gmail.com</a>></span> wrote:<br>



</div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div class="h5"><div dir="ltr"><div><div><div><div>Hello,<br><br></div>argparse does prefix matching as long as there are no conflicts. For example:<br>



<br><div style="margin-left:40px">argparser = argparse.ArgumentParser()<br>argparser.add_argument('--sync-foo', action='store_true')<br>

args = argparser.parse_args()<br></div><br></div>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). <br>





<br>If there's another argument registered, say "--sync-bar" the above will fail due to a conflict.<br><br>Now comes the nasty part. When using "parse_known_args" instead of "parse_args", the above happens too - --sync is recognized for --sync-foo and captured by the parser. But this is wrong! The whole idea of parse_known_args 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 parse_known_args) knows about --sync and wants to get it.<br>





<br></div>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 parse_known_args is called.<br>





<br></div>Again, at the very least this should be documented (for parse_known_args not less than a warning box, IMHO).<span><font color="#888888"><br><br>Eli<br></font></span></div>
<br></div></div>_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/guido%40python.org" target="_blank">https://mail.python.org/mailman/options/python-dev/guido%40python.org</a><br>
<br></blockquote></div><span class=""><font color="#888888"><br><br clear="all"><br>-- <br>--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)
</font></span></div>
</blockquote></div><br></div></div>