如何在Ruby中使用getoptlong类?

| 我需要在Ruby中使用getoptlong类的帮助。我需要执行命令prog_name.ruby -u -i -s filename。到目前为止,我只能使用prog_name.ruby -u filename -i filename -s filename执行它。 这是我的getoptlong代码:
require \'getoptlong\'

class CommonLog
parser = GetoptLong.new
parser.set_options([\"-h\", \"--help\", GetoptLong::NO_ARGUMENT],
                   [\"-u\", \"--url\",  GetoptLong::NO_ARGUMENT],
                   [\"-i\", \"--ip\",   GetoptLong::NO_ARGUMENT],
                   [\"-s\", \"--stat\", GetoptLong::NO_ARGUMENT])

begin
  begin
      opt,arg = parser.get_option
      break if not opt

      case opt
         when \"-h\" || \"--help\"
           puts \"Usage: -u  filename\"
           puts \"Usage: -i  filename\"
           puts \"Usage: -s  filename\"
         exit
         when \"-u\" || \"--url\"
            log = CommonLog.new(ARGV[0])
            log.urlReport
         when \"-i\" || \"--ip\"
            log = CommonLog.new(ARGV[0])
            log.ipReport
         when \"-s\" || \"--stat\"
            log = CommonLog.new(ARGV[0])
            log.statReport
         end
      rescue => err
         puts \"#{err.class()}: #{err.message}\"
         puts \"Usage: -h -u -i -s filename\"
      exit
   end
end while 1

if ARGV[0] == nil || ARGV.size != 1
   puts \"invalid! option and filename required\"
   puts \"usage: -h -u -i -s filename\"
end
    
已邀请:
我将通过推荐看一下新的“倾斜”宝石来回答这个问题。它是1英镑左右的包装纸。 如果您正在使用RVM,则可以使用
gem install slop
,否则可以使用
sudo gem install slop
。 GetOptLong功能非常强大,但是,尽管我已经使用了几次,但每次仍然需要审阅文档。 如果您想要更多的功能,并且使用接口比GetOptLong更容易使用,请查看Ruby的ѭ4。您需要更好地制定逻辑,但这是转换代码的快速通道。我不得不为CommonLog gem设置一个类,因为我不使用它。重要的事情遵循从
ARGV
提取日志的行:
require \'optparse\'

class CommonLog
  def initialize(*args); end
  def urlReport();     puts \"running urlReport()\";        end
  def ipReport();      puts \"running ipReport()\";         end
  def statReport(arg); puts \"running statReport(#{arg})\"; end
end

log = CommonLog.new(ARGV[0])

OptionParser.new { |opts|
  opts.banner = \"Usage: #{File.basename($0)} -u -i -s filename\"

  opts.on( \'-u\', \'--[no-]url\', \'some short text describing URL\') do
    log.urlReport()
  end

  opts.on(\'-i\', \'--[no-]ip\', \'some short text describing IP\') do
    log.ipReport()
  end

  opts.on(\'-s\', \'--stat FILENAME\', \'some short text describing STAT\') do |arg|
    log.statReport(arg)
  end
}.parse!
另外,作为一种快速的批评,您不是在编写惯用的Ruby代码: 可以写7ѭ陈述:
when \"-h\", \"--help\"
if ARGV[0] == nil || ARGV.size != 1
盘绕了。研究ARGV和阵列的工作原理。通常,对于
ARGV[0]
为零,将不再有参数,因此
ARGV.empty?
就足够了。     
  您在示例程序中有几个错误         #each和#get仅返回选项中的第一个字符串,并将其他字符串转换为该字符串。   您应该在选项处理之前先检查参数   您可能不希望在日志记录类中加入   
require \'getoptlong\'
# don\'t pollute CommonLog with this
include CommonLog
# if this is the startup module
if __FILE__ == $0 then
  # Check to ensure there are arguments
  if ARGV.size < 1
    puts \"invalid! option and filename required\"
    puts \"usage: -h -u -i -s filename\"
  end
  # set up parser and get the options
  parser_opts=GetoptLong.new(
    [\"--help\", \"-h\", GetoptLong::NO_ARGUMENT],
    [\"--url\", \"-u\", GetoptLong::NO_ARGUMENT],
    [\"--ip\", \"-i\", \"--ip\", GetoptLong::NO_ARGUMENT],
    [\"--stat\", \"-s\", GetoptLong::NO_ARGUMENT]
  )

  parser_opts.each do |opt,arg|
    begin # this is for the exception processing
      case opt
      when \"--help\" #only the first option is returned read ruby doc on #each
        puts \"Usage: -u  filename\"
        puts \"Usage: -i  filename\"
        puts \"Usage: -s  filename\"
        exit
      when \"--url\" #only the first option is returned
        log = CommonLog.new(ARGV[0])
        log.urlReport
      when \"--ip\" #only the first option is returned
        log = CommonLog.new(ARGV[0])
        log.ipReport
      when \"--stat\" #only the first option is returned
        log = CommonLog.new(ARGV[0])
        log.statReport
      else # this should not be used
        puts \"unexpected option %s\"%opt 
        puts \"Usage: -h -u -i -s filename\"
      end
    rescue Exception => err #rescuing an unexpected Exception
      puts \"#{err.class()}: #{err.message}\"
      puts \"Usage: -h -u -i -s filename\"
      Kernel.exit
    end
  end
end
    

要回复问题请先登录注册