From: Sean Smitz Date: 2010-02-10T04:22:37+09:00 Subject: OptionParser does not enforce required argument I'm not seeing the difference between specifying a required argument and an option argument in OptionParser other than they used '<<' in the assignment of the former. When I don't give an output_file it runs fine until it needs to use the filename and then gives a nil error. When I do specify the filename I get an error for using the '<<' operator. Command line output for both cases given below the code: def self.parse(args) options = OpenStruct.new String usage = "Usage: itos_logs_dms -o [Options]\n\nThe following script is used to filter a set of ITOS log messages from the log\ndatabase and format them for parsing by Attention!. The fields will be tab\ndelineated and any tabs in the ITOS event message will be replaced by 8 spaces.\n" opts = OptionParser.new do |opts| opts.banner = usage opts.separator "" opts.separator "Specific options:" //This is supposed to be the required argument opts.on("-o", "--output OUTPUT_FILE", String, "Name of OUTPUT_FILE is a required argument.") do |o| options.output_file << o end opts.on("-i", "--tag_id [TAG_ID]", String, "Retrieves only the messages that are tagged with the given tag_id.") do |i| options.tag_id = i end opts.on("-s", "--severity [SEVERITY]", Integer, "Retrieves only the messages that are >= the given severity (0-5).") do |s| options.severity = s end opts.on("-t", "--timestamp [TIMESTAMP]", Time, "Retrieves only the messages that are >= the given timestamp (yyyy-mm-dd HH:mm:ss.SS)") do |t| options.timestamp = t end opts.separator "Common options:" opts.on_tail("-h", "--help", "Show this message.") do puts opts exit end end opts.parse!(args) options end # No Option Given: ./itos_logs_dms.rb ./itos_logs_dms.rb:45:in `+': can't convert nil into String (TypeError) from ./itos_logs_dms.rb:45:in `createFile' from ./itos_logs_dms.rb:75:in `
' # Option provided (two ways): ./itos_logs_dms.rb --output log_test1 ./itos_logs_dms.rb:17:in `block (2 levels) in parse': undefined method `<<' for nil:NilClass (NoMethodError) from /usr/local/lib/ruby/1.9.1/optparse.rb:1271:in `call' from /usr/local/lib/ruby/1.9.1/optparse.rb:1271:in `block in parse_in_order' from /usr/local/lib/ruby/1.9.1/optparse.rb:1258:in `catch' from /usr/local/lib/ruby/1.9.1/optparse.rb:1258:in `parse_in_order' from /usr/local/lib/ruby/1.9.1/optparse.rb:1252:in `order!' from /usr/local/lib/ruby/1.9.1/optparse.rb:1343:in `permute!' from /usr/local/lib/ruby/1.9.1/optparse.rb:1364:in `parse!' from ./itos_logs_dms.rb:25:in `parse' from ./itos_logs_dms.rb:75:in `
' ./itos_logs_dms.rb -o log_test1 ./itos_logs_dms.rb:17:in `block (2 levels) in parse': undefined method `<<' for nil:NilClass (NoMethodError) from /usr/local/lib/ruby/1.9.1/optparse.rb:1302:in `call' from /usr/local/lib/ruby/1.9.1/optparse.rb:1302:in `block in parse_in_order' from /usr/local/lib/ruby/1.9.1/optparse.rb:1258:in `catch' from /usr/local/lib/ruby/1.9.1/optparse.rb:1258:in `parse_in_order' from /usr/local/lib/ruby/1.9.1/optparse.rb:1252:in `order!' from /usr/local/lib/ruby/1.9.1/optparse.rb:1343:in `permute!' from /usr/local/lib/ruby/1.9.1/optparse.rb:1364:in `parse!' from ./itos_logs_dms.rb:25:in `parse' from ./itos_logs_dms.rb:75:in `
' ruby -v ruby 1.9.1p376 (2009-12-07 revision 26041) [i686-linux] Thank you for your help in advance. -- Posted via http://www.ruby-forum.com/.