Question About Command line arguments

Mark Phillips mark at phillipsmarketing.biz
Fri Jun 10 13:58:43 EDT 2011


On Fri, Jun 10, 2011 at 10:41 AM, MRAB <python at mrabarnett.plus.com> wrote:

> On 10/06/2011 18:21, Mark Phillips wrote:
>
>> I have a script that processes command line arguments
>>
>> def main(argv=None):
>>     syslog.syslog("Sparkler stared processing")
>>     if argv is None:
>>         argv = sys.argv
>>     if len(argv) != 2:
>>         syslog.syslog(usage())
>>     else:
>>         r = parseMsg(sys.argv[1])
>>         syslog.syslog(r)
>>     return 0
>>
>> if __name__ == "__main__":
>>     sys.exit(main())
>>
>> When I run "python myscript fred" it works as expected - the argument
>> fred is processed in parseMsg as sys.arv[1]
>>
>> When I run "echo fred | python myscript" the script thinks there are no
>> arguments, so it prints out the usage statement.
>>
>> Is the problem with the echo command, or how I wrote my script?
>>
>>  In the second case, there aren't any arguments. The echo command is
> writing "fred" to its standard output, which is attached to your
> script's standard input.
>
> How do I write my script so it picks up argument from the output of
commands that pipe input into my script?

Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110610/92e95e3c/attachment.html>


More information about the Python-list mailing list