Simmo Simmo - 1 year ago 65
Ruby Question

How do I use the GLI 2.0 ruby gem to accept ARGF data?

I've created my own gem using the GLI 2.0 gem for all the nice command line structure. It's working fine, but I'd also like to support data piped in.

my_prog new some_file # this is ok already
some_process | my_prog # how do I add this?

Somewhere I need to check how it was called (somehow) and then act appropriately. I've simplified a little, but my current code is below.

require 'rubygems'
require 'gli'

include GLI::App

desc 'My example'
command :new do |c|
desc 'Specify input file'
arg_name 'filename'
c.flag [:i,:input]

c.action do |global_options,options,args|
exit_now!("Input file must be specified") if options[:o].nil?
exit_now!("New failed") unless File.exists?(options[:o])

exit run(ARGV)

Answer Source

Just to sum up the discussion above:

GLI currently mutates ARGV, so ARGF should work fine inside the action block of a command, even if you're piping in. BUT, GLI does require some sort of command to run, even if it's overriding the default of help to the command in question.