<p><br>
On Jul 31, 2012 10:32 AM, "Benoist Laurent" <<a href="mailto:benoist@ibpc.fr">benoist@ibpc.fr</a>> wrote:<br>
><br>
> Well sorry about that but it seems I was wrong.<br>
> It was Friday evening and I guess I've not been careful.<br>
><br>
> Actually when you specify nargs="?",  the doc says "One argument will be consumed from the command line if possible, and produced as a single item".<br>
> So you can't pass several arguments to the program.</p>
<p>Right below that in the docs it explains about using nargs='*' and nargs='+'. One of those will do what you want.</p>
<p>Oscar.</p>
<p>><br>
> So, to rephrase the question, how can I get a argument parser that parses the command-line just as Unix grep would do?<br>
> i.e.<br>
><br>
> $ echo 42 > foo.txt<br>
> $ echo 172 >> foo.txt<br>
> $ cp foo.txt bar.txt<br>
> $<br>
> $ grep 42 foo.txt<br>
> 42<br>
> $ grep 42 foo.txt bar.txt<br>
> foo.txt:42<br>
> bar.txt:42<br>
> $ cat foo.txt | grep 42<br>
> 42<br>
> $ grep -c 42 foo.txt<br>
> 1<br>
><br>
><br>
> Cheers,<br>
> Ben<br>
><br>
><br>
><br>
><br>
> Le Jul 27, 2012 à 7:08 PM, Benoist Laurent a écrit :<br>
><br>
>><br>
>><br>
>> Yes basically looks like you get it.<br>
>> I have to further test it but my first impression is that it's correct.<br>
>><br>
>> So actually the point was to use nargs="?".<br>
>><br>
>> Thank you very much.<br>
>> Ben<br>
>><br>
>><br>
>><br>
>> Le Jul 27, 2012 à 5:44 PM, Peter Otten a écrit :<br>
>><br>
>>> Benoist Laurent wrote:<br>
>>><br>
>>>> I'm impletting a tool in Python.<br>
>>>><br>
>>>> I'd like this tool to behave like a standard unix tool, as grep for<br>
>>>><br>
>>>> exemple. I chose to use the argparse module to parse the command line and<br>
>>>><br>
>>>> I think I'm getting into several limitations of this module.<br>
>>>><br>
>>>><br>
>>>>> First Question.<br>
>>>><br>
>>>> How can I configure the the ArgumentParser to allow the user to give<br>
>>>><br>
>>>> either an input file or to pipe the output from another program?<br>
>>>><br>
>>>><br>
>>>> $ mytool.py file.txt<br>
>>>><br>
>>>> $ cat file.txt | mytool.py<br>
>>><br>
>>><br>
>>> $ echo alpha > in.txt<br>
>>> $ cat in.txt | ./mytool.py <br>
>>> ALPHA<br>
>>> $ cat in.txt | ./mytool.py - out.txt<br>
>>> $ cat out.txt <br>
>>> ALPHA<br>
>>> $ ./mytool.py in.txt <br>
>>> ALPHA<br>
>>> $ ./mytool.py in.txt out2.txt<br>
>>> $ cat out2.txt <br>
>>> ALPHA<br>
>>> $ cat ./mytool.py<br>
>>> #!/usr/bin/env python<br>
>>> assert __name__ == "__main__"<br>
>>><br>
>>> import argparse<br>
>>> import sys<br>
>>><br>
>>> parser = argparse.ArgumentParser()<br>
>>> parser.add_argument("infile", nargs="?", type=argparse.FileType("r"), <br>
>>> default=sys.stdin)<br>
>>> parser.add_argument("outfile", nargs="?", type=argparse.FileType("w"), <br>
>>> default=sys.stdout)<br>
>>> args = parser.parse_args()<br>
>>><br>
>>> args.outfile.writelines(line.upper() for line in args.infile)<br>
>>><br>
>>> Is that good enough?<br>
>>><br>
>>><br>
>>> -- <br>
>>> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br>
>>><br>
>><br>
>> -- <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<br>
>><br>
>> -- <br>
>> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br>
><br>
><br>
> -- <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<br>
><br>
><br>
> --<br>
> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br>
><br>
</p>