<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>Le Jul 27, 2012 à 4:43 PM, Oscar Benjamin a écrit :</div><br class="Apple-interchange-newline"><blockquote type="cite"><br><br><div class="gmail_quote">On 27 July 2012 15:26, Benoist Laurent <span dir="ltr"><<a href="mailto:benoist@ibpc.fr" target="_blank">benoist@ibpc.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hi,<br>
<br>
I'm impletting a tool in Python.<br>
I'd like this tool to behave like a standard unix tool, as grep for exemple.<br>
I chose to use the argparse module to parse the command line and I think I'm getting into several limitations of this module.<br>
<br>
> First Question.<br>
How can I configure the the ArgumentParser to allow the user to give either an input file or to pipe the output from another program?<br>
<br>
$ mytool.py file.txt </blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">$ cat file.txt | mytool.py<br></blockquote><div> </div><div>A better way to do that last line is:</div>

<div>$ mytool.py < file.txt </div><div><br></div><div>To answer the question, just make the first argument optional defaulting to None. Then you can do:</div><div>if file1 is None:</div><div>    file1 = sys.stdin</div></div></blockquote><div><br></div><div>That's the solution I came to.</div><div>But I'm not very happy with this since I can definitively not make my program act as a standard unix tool.</div><div>Any other solution?</div><div><br></div><br><blockquote type="cite"><div class="gmail_quote">

<div> </div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; position: static; z-index: auto; ">
<br>
<br>
> Second Question.<br>
How can I get the nargs options working with subparser?<br>
Cause basically if I've got a positionnal argument with nargs > 1, then the subparsers are recognized as values for the positionnal argument.<br>
<br>
$ mytool.py file1.txt file2.txt foo<br>
<br>
Here foo is a command I'd like to pass to mytool but argparse considers it's another input file (as are file1.txt and file2.txt).<br></blockquote><div><br></div><div>I haven't used subparsers in argparse but I imagine that you would call it like:</div>
<div>$ mytool.py foo file1.txt file2.txt</div></div></blockquote><div><br></div><div>As far as I know it is not the case.</div><div>Let's get into the code.</div><div><br></div><div>parser = argparse.ArgumentParser()</div><div>parser.add_argument("-i", help="input files", nargs="+")</div><div><br></div><div>subparsers = parser.add_subparsers() </div><div>foo_parser = subparser.add_parser("foo")</div><div># ... here come some foo parser options</div><div><div>bar_parser = subparser.add_parser("bar")</div><div># ... here come some bar parser options</div><div><br></div></div><div>What argparse expects is the "-i" arguments coming before the subparsers.</div><div><br></div><div>To summarize, if I adopt your solution to my first question, the I should add the "-i" argument to each subparser.</div><div>I don't want to since it's ugly and I'd have to write the help myself (which I don't want to).</div><div><br></div><div><br></div><div>Cheers</div><div><br></div><br><blockquote type="cite"><div class="gmail_quote"><div> </div><div><br></div><div>Cheers,</div><div>Oscar.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<br>
<br>
Any help would be appreciated.<br>
Ben.<br>
<span><font color="#888888"><br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br>
</blockquote></div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; ">-- <br>Benoist Laurent<br>Laboratoire de Biochimie Theorique / CNRS UPR 9080<br>Institut de Biologie Physico-Chimique<br>13, rue Pierre et Marie Curie<br>F-75005 Paris<br>Tel. +33 [0]1 58 41 51 67 or +33 [0]6 21 64 50 56</span>
</div>
<br></body></html>