Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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])
  end
end

exit run(ARGV)
like image 429
Simmo Avatar asked Dec 05 '25 23:12

Simmo


1 Answers

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.

like image 172
davetron5000 Avatar answered Dec 07 '25 15:12

davetron5000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!