# Piping data in on the command line

**URL:** <https://www.ruby-forum.com/t/piping-data-in-on-the-command-line/142566>\
**Category:** Ruby\
**Created:** [July 9, 2008, 9:27am UTC](https://www.ruby-forum.com/t/piping-data-in-on-the-command-line/142566 "2008-07-09T09:27:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [July 9, 2008, 9:27am UTC](https://www.ruby-forum.com/t/piping-data-in-on-the-command-line/142566/1 "2008-07-09T09:27:34Z")

</div>

Hello all,

I know you can get command line args using ARGV, but is there a way to  
use  
the ‘\<’ operator to pipe data into your program, as in

ruby myprog.rb \< ~/myfile.txt

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [July 9, 2008, 11:57am UTC](https://www.ruby-forum.com/t/piping-data-in-on-the-command-line/142566/2 "2008-07-09T11:57:40Z")

</div>

On 09.07.2008, at 09:22, James C. wrote:

> I know you can get command line args using ARGV, but is there a way  
> to use  
> the ‘\<’ operator to pipe data into your program, as in

AFAIK that’s called I/O redirection.

> ruby myprog.rb \< ~/myfile.txt

$ ruby cat.rb \< cat.rb  
puts \*ARGF

regards, Sandor  
Szücs

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [July 9, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/piping-data-in-on-the-command-line/142566/3 "2008-07-09T14:38:50Z")

</div>

Just read the IO $stdin, like:

$stdin.each { |line| puts “Found: #{line}” }

The \< “operator” is a shell feature that will pass the content of a file  
to  
the programs standard input (stdin).

martin

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [July 11, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/piping-data-in-on-the-command-line/142566/4 "2008-07-11T18:51:39Z")

</div>

On 09.07.2008, at 14:34, Martin B. wrote:

> Just read the IO $stdin, like:
> 
> $stdin.each { |line| puts “Found: #{line}” }
> 
> The \< “operator” is a shell feature that will pass the content of a  
> file to  
> the programs standard input (stdin).

Maybe that’s more readable, but my point wasn’t the shell feature \< .  
cat.rb has one line:  
puts \*ARGF

$ ruby cat.rb \< cat.rb  
puts \*ARGF

regards, Sandor  
Szücs

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [July 11, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/piping-data-in-on-the-command-line/142566/5 "2008-07-11T19:04:32Z")

</div>

2008/7/11 Sandor SzÃ¼cs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 
> 
> Maybe that’s more readable, but my point wasn’t the shell feature \< .  
> cat.rb has one line:  
> puts \*ARGF

Thank you both. If anyone is the least bit interested I’m writing myself  
yet  
another commandline option parser:

> **[GitHub - jcoglan/oyster: Command-line input parser that doesn't hate you](https://github.com/jcoglan/oyster)**
>
> Command-line input parser that doesn't hate you. Contribute to jcoglan/oyster development by creating an account on GitHub.

It’s inspired by trollop, but it has some more option types and is more  
easily extensible, automates a few extra things (like recognizing  
–no-verbose if you’ve got a --verbose option for example) and it  
generates  
slightly nicer (more man-page-like) help text.

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [July 11, 2008, 8:13pm UTC](https://www.ruby-forum.com/t/piping-data-in-on-the-command-line/142566/6 "2008-07-11T20:13:37Z")

</div>

On Jul 11, 2008, at 10:59 AM, James C. wrote:

> –no-verbose if you’ve got a --verbose option for example) and it  
> generates  
> slightly nicer (more man-page-like) help text.

you may be interested in

[http://codeforpeople.com/lib/ruby/main/main-2.8.1/README](http://codeforpeople.com/lib/ruby/main/main-2.8.1/README)

[http://codeforpeople.com/lib/ruby/main/main-2.8.1/samples/](http://codeforpeople.com/lib/ruby/main/main-2.8.1/samples/)

gem install main

the whole concept is to obviate the notion of ever parsing options

cheers.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)
