Ruby ARGF For Command Line Applications

April 11, 2017

Ruby ARGF For Command Line Applications

About a year ago, I discovered the Ruby ARGF class as a way to help you read files in ruby scripts.

Ruby logo on railroad tracks

From the Ruby documentation:

ARGF is a stream designed for use in scripts that process files given as command-line arguments or passed in via STDIN.

Concatenate file contents

One nifty trick I picked up from this thoughtbot post is that besides concatenating of file contents, you have to shift flags off the ARGV array if you want a Ruby CLI application that accepts options.

To get a better handle on this, let’s look at an example program.

Example CLI – Summing Numbers

#!/usr/bin/env ruby

##argf_demo.rb script
require 'pry'
require 'optparse'

OptionParser.new do |opts|
  opts.banner = "\nARGF Demo app\n"

  opts.on("-h", "--help", "Help:") do |h|
    puts opts
    exit
  end

  opts.on("-v", "--version") do |v|
    puts opts.banner
    puts "Version 0.0.1"
  end

end.parse!

ARGF.each do |line|
  break if line=="quit\n"
  sum = line.strip.split(" ").map(&:to_i).inject { |s, n| n + s }
  puts "Sum: #{sum}"
end

For now, let’s focus on ARGF. Here you run the program with:

$ ruby argf_demo.rb numbers1.txt numbers2.txt

To give you an idea of the numbers in each file, here they are:

numbers1.txt

2 2
4 5

numbers2.txt

3 2
quit
3 3

If you run the program, you should see output like:

Sum: 4
Sum: 9
Sum: 5

You would see 6 if I didn’t have the quit word in the numbers2.txt file.

Shifting ARGS off ARGV with OptionParser

The cool thing about OptionParser is that it takes care of shifting arguments off ARGV for you. So you can enter the following at the terminal:

$ ruby argf_demo.rb -v numbers1.txt numbers2.txt

And you will see the following output:

ARGF Demo app
Version 0.0.1
Sum: 4
Sum: 9
Sum: 5

Summary

If you’re ever in need of an easy way to read in files from the command line, ARGF could be a handy tool for you. Here is the demo repo I used for this blog post.


Profile picture

Written by Bruce Park who lives and works in the USA building useful things. He is sometimes around on Twitter.