Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: How Do You Get Redirected Input?

Reply
Thread Tools

Re: How Do You Get Redirected Input?

 
 
not your business
Guest
Posts: n/a
 
      07-02-2003
Irmen de Jong wrote:

> not your business wrote:
>> I have a shell tool that accepts arguments on the command line. I would
>> like
>> to check if the input is being piped in. That is,
>>
>> $ mytool.py < cmdlst.txt
>>
>> In this case sys.argv is empty. So I added
>>
>> pipein = os.read(sys.stdin.fileno(),256)
>> if (pipein):
>> input_args = pipein.split()
>> else:
>> input_args = sys.argv[1:]
>>
>> Problem is that if nothing is redirected in, the script waits for a Enter
>> pressed on the the keyboard. Anyone know a solution to this? Thanx in
>> advance for any help.

>
> Why not turn it around? First check if sys.argv is *not* empty,
> in which case the user provided command line arguments, and
> proceed to parse those. Otherwise (if sys.argv *is* empty),
> assume the input is piped in and proceed to read the standard
> input.
>
> --Irmen


Okay, but there still seems to be a problem. Lets say you typed
$ mytool.py

If sys.argv[1:] is empty, I want to display help. But if I'm checking for
redirected input by my problematic method next, the script hangs, again,
waiting for a Enter to be pressed and the user, therefore, doesn't my usage
help.

I think I need to find a way to test for redirected input without the actual
os.read() thing. One hack that came to mind was to fork() a seperate
process to do this and pipe back within a time period any results. The
parent could just go on if no response. But that seems a little extreme
(and ugly) plus it's platform dependent unless, I guess, I it do it with
threads.

 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      07-02-2003
On Wed, 02 Jul 2003 20:04:53 GMT, not your business <(E-Mail Removed)> wrote:

>Irmen de Jong wrote:
>
>> not your business wrote:
>>> I have a shell tool that accepts arguments on the command line. I would
>>> like
>>> to check if the input is being piped in. That is,
>>>
>>> $ mytool.py < cmdlst.txt
>>>
>>> In this case sys.argv is empty. So I added
>>>
>>> pipein = os.read(sys.stdin.fileno(),256)
>>> if (pipein):
>>> input_args = pipein.split()
>>> else:
>>> input_args = sys.argv[1:]
>>>
>>> Problem is that if nothing is redirected in, the script waits for a Enter
>>> pressed on the the keyboard. Anyone know a solution to this? Thanx in
>>> advance for any help.

>>
>> Why not turn it around? First check if sys.argv is *not* empty,
>> in which case the user provided command line arguments, and
>> proceed to parse those. Otherwise (if sys.argv *is* empty),
>> assume the input is piped in and proceed to read the standard
>> input.
>>
>> --Irmen

>
>Okay, but there still seems to be a problem. Lets say you typed
> $ mytool.py
>
>If sys.argv[1:] is empty, I want to display help. But if I'm checking for
>redirected input by my problematic method next, the script hangs, again,
>waiting for a Enter to be pressed and the user, therefore, doesn't my usage
>help.


If you want to use empty cmd line as help trigger, I'd say don't try to read stdin
in that case. Use an explicit command line option to indicate stdin input,
e.g., just '-' (maybe where you might otherwise have a file spec).
>
>I think I need to find a way to test for redirected input without the actual
>os.read() thing. One hack that came to mind was to fork() a seperate
>process to do this and pipe back within a time period any results. The
>parent could just go on if no response. But that seems a little extreme
>(and ugly) plus it's platform dependent unless, I guess, I it do it with
>threads.
>

Seems ugly. Any reason you couldn't use
$ cat stuff | mytool.py -
or such?

BTW, on some versions of windows, that better be
$ cat stuff | python mytool.py -
since i/o redirection with extension-associated execution has bugs for some versions (e.g. NT4).


Regards,
Bengt Richter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
You are about to redirected Meat Plow Computer Support 17 02-22-2006 02:27 AM
You are about to be redirected to a connection that is not secure Steve Harris ASP .Net Security 8 10-14-2004 08:46 PM
You are about to be redirected to a connection that is not secure Steve Harris ASP .Net 6 09-23-2004 09:36 AM
Get HTML content of a redirected URL? Kaidi Java 3 01-04-2004 07:37 AM



Advertisments