Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Question About Command line arguments

11 views
Skip to first unread message

MRAB

unread,
Jun 10, 2011, 1:41:12 PM6/10/11
to pytho...@python.org
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.

Kurt Smith

unread,
Jun 10, 2011, 2:03:44 PM6/10/11
to Mark Phillips, pytho...@python.org
On Fri, Jun 10, 2011 at 12:58 PM, Mark Phillips
<ma...@phillipsmarketing.biz> wrote:
> How do I write my script so it picks up argument from the output of commands
> that pipe input into my script?

def main():
import sys
print sys.stdin.read()

if __name__ == '__main__':
main()

$ echo "fred" | python script.py
fred
$

Dennis

unread,
Jun 10, 2011, 2:05:11 PM6/10/11
to pytho...@python.org
On Fri, Jun 10, 2011 at 11:03 AM, Dennis <daod...@gmail.com> wrote:
> On Fri, Jun 10, 2011 at 10:58 AM, Mark Phillips
> <ma...@phillipsmarketing.biz> wrote:
>> On Fri, Jun 10, 2011 at 10:41 AM, MRAB <pyt...@mrabarnett.plus.com> wrote:

>>
>> On 10/06/2011 18:21, Mark Phillips wrote:
>
>>
> How do I write my script so it picks up argument from the output of commands
> that pipe input into my script?

I think if you want to replicate stdin behaviour try looking into
the xargs command, assuming that is available on your platform.

However reading in stdin into argv may be more elegant, but slightly
unexpected to the experienced user.

Dennis O.

Tim Chase

unread,
Jun 10, 2011, 2:31:19 PM6/10/11
to Mark Phillips, pytho...@python.org
On 06/10/2011 12:58 PM, Mark Phillips wrote:
> How do I write my script so it picks up argument from the
> output of commands that pipe input into my script?

You can check

if os.isatty(sys.stdin): # <-- this check
do_stuff_with_the_terminal()
else:
read_options_from_stdin()

-tkc

Robert Kern

unread,
Jun 10, 2011, 3:27:10 PM6/10/11
to pytho...@python.org
On 6/10/11 12:58 PM, Mark Phillips wrote:
> On Fri, Jun 10, 2011 at 10:41 AM, MRAB <pyt...@mrabarnett.plus.com
> <mailto:pyt...@mrabarnett.plus.com>> wrote:
>
> On 10/06/2011 18:21, Mark Phillips wrote:
>
> How do I write my script so it picks up argument from the output of commands
> that pipe input into my script?

You may want to just use the appropriate shell syntax instead:

$ python myscript `echo fred`

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Dennis

unread,
Jun 10, 2011, 4:33:36 PM6/10/11
to pytho...@python.org
On Fri, Jun 10, 2011 at 11:58 AM, Mark Phillips
<ma...@phillipsmarketing.biz> wrote:
\
>
> Kurt,
>
> How does one write a main method to handle both command line args and stdin
> args?

Here is what I came up with:

The one weird thing, the line from above didn't seem to work so I changed it
if os.isatty(sys.stdin):

to this:

if not os.isatty(sys.stdin.fileno()):

Below 3 tests, with stdin redirection, then with one argument, then
with both stdin redirection and one argument, the results are at the
very bottom.

$ cat ./argvtest.py; echo "fred" | ./argvtest.py ; ./argvtest.py
"alice"; echo "fred" | ./argvtest.py "bob"
#!/usr/bin/python


import os
import sys

def main():
#This checks to see if stdin is a not tty and > 0
if not os.isatty(sys.stdin.fileno()):
arg = sys.stdin.read()
print arg

elif len(sys.argv[1:]) > 0:

# if the length of the first argument is > 0
#[1:] strip the first string beacause it is the name of the script
arg = sys.argv[1:]
print arg

if __name__ == "__main__":
main()

fred

['alice']
fred

>
> Thanks,
>
> Mark
>

Welcome,

Dennis O.

> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

Dennis

unread,
Jun 10, 2011, 4:35:20 PM6/10/11
to pytho...@python.org
On Fri, Jun 10, 2011 at 1:33 PM, Dennis <daod...@gmail.com> wrote:
> On Fri, Jun 10, 2011 at 11:58 AM, Mark Phillips

> fred
>
> ['alice']
> fred

Just realized the if/else will have to be changed slightly if we want
to output both argv and stdin.

Benjamin Kaplan

unread,
Jun 10, 2011, 5:00:45 PM6/10/11
to pytho...@python.org
On Fri, Jun 10, 2011 at 11:31 AM, Tim Chase
<pytho...@tim.thechases.com> wrote:

> On 06/10/2011 12:58 PM, Mark Phillips wrote:
>>
>> How do I write my script so it picks up argument from the
>> output of commands that pipe input into my script?
>
> You can check
>
>  if os.isatty(sys.stdin):  # <-- this check

Any reason for that over sys.stdin.isatty()?

Tim Chase

unread,
Jun 10, 2011, 5:28:32 PM6/10/11
to Benjamin Kaplan, pytho...@python.org
On 06/10/2011 04:00 PM, Benjamin Kaplan wrote:
> On Fri, Jun 10, 2011 at 11:31 AM, Tim Chase
>> if os.isatty(sys.stdin): #<-- this check
>
> Any reason for that over sys.stdin.isatty()?

my knowledge of os.isatty() existing and my previous lack of
knowledge about sys.stdin.isatty()

:)

-tkc

Hans Mulder

unread,
Jun 10, 2011, 6:03:08 PM6/10/11
to

$ cat script.py
def main():
print raw_input()

if __name__ == '__main__':
main()

$ echo "fred" | python script.py
fred
$


Hope this helps,

-- HansM

0 new messages