简体   繁体   English

如何使用GLI 2.0 ruby​​ gem接受ARGF数据?

[英]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. 我使用GLI 2.0 gem为所有漂亮的命令行结构创建了我自己的gem。 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)

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. GLI目前会改变ARGV ,因此ARGF应该在命令的action块内正常工作,即使你正在输入。但是,GLI确实需要某种命令来运行,即使它覆盖了命令的默认help 。题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM