在Ruby中未输入GetoptLong :: REQUIRED_ARGUMENT时报告错误

时间:2014-01-07 10:48:07

标签: ruby

我有这个非常简单的Ruby代码:

#!/res/software/pkg/ruby-2.0.0/bin/ruby -w
require( 'getoptlong' )
opts = GetoptLong.new( [ '--netlist_file', GetoptLong::REQUIRED_ARGUMENT ] )
puts 'error in getting netlist' << opts.error_message.to_s()

输出结果为:

cs059:Florida_domains$ ~/test.rb
error in getting netlist

如果没有输入任何GetoptLong :: REQUIRED_ARGUMENT参数,我怎么能输出错误? 有通用的方法吗? 感谢。

1 个答案:

答案 0 :(得分:1)

require( 'getoptlong' )
begin
  opts = GetoptLong.new( [ '--netlist_file', GetoptLong::REQUIRED_ARGUMENT ] )

  opts.each do |option, arg|
    case option
      when "--netlist_file"
        #assign the argument to my_var or do whatever you need to do with it
        my_var = arg
    end
rescue => e
  puts "Error in arguments"
  puts e.to_s
end