C
Christopher J. Bottaro
Here's the pastebin of this post if you prefer looking at formatted
source:
http://pastebin.ca/514353
Ok, I'm having a multitude of problems with OptionParser. First, I
can't get it to work following the online documentation.
Second, I got it to work by adding :REQUIRED to the on() method call,
but that doesn't work as expected: the argument isn't really
"required"; OptionParser does not bomb if the argument is omitted.
Third, I cannot get a switch to accept multiple arguments.
--my_arg 1 2 3
I want an array [1, 2, 3].
Thanks for the help, the rest of the message contains my pastebin
post:
!/usr/bin/env ruby
require 'optparse'
require 'ostruct'
options = OpenStruct.new
options.client_ids = []
options.save = false
op = OptionParser.new do |opts|
opts.on("-c", "--client", "client id to migrate for") { |client_id|
options.client_ids << client_id }
opts.on_tail("-h", "--help", "Show this message") { puts opts;
exit }
end
begin
op.parse!
rescue Exception => e
puts op
puts e
exit
end
puts options
# ruby script.rb --client 123
# #<OpenStruct client_ids=[true], save=false>
# if I change on() to be:
opts.on("-c", "--client", Integer "client id to migrate for") { |
client_id| options.client_ids << client_id }
# then, ruby script.rb --client 123
# #<OpenStruct client_ids=[nil], save=false>
# furthermore it works if I change on() to be:
opts.on("-c", "--client", :REQUIRED, Integer "client id to migrate
for") { |client_id| options.client_ids << client_id }
# then, ruby script.rb --client 123
# #<OpenStruct client_ids=[123], save=false>
# which is correct, but if I run, ruby script.rb, it does not bomb out
saying it's missing a required argument.
# and still furthermore, I can't do something like this: ruby
script.rb --client 123 456
# and get #<OpenStruct client_ids=[123, 456], save=false>
source:
http://pastebin.ca/514353
Ok, I'm having a multitude of problems with OptionParser. First, I
can't get it to work following the online documentation.
Second, I got it to work by adding :REQUIRED to the on() method call,
but that doesn't work as expected: the argument isn't really
"required"; OptionParser does not bomb if the argument is omitted.
Third, I cannot get a switch to accept multiple arguments.
--my_arg 1 2 3
I want an array [1, 2, 3].
Thanks for the help, the rest of the message contains my pastebin
post:
!/usr/bin/env ruby
require 'optparse'
require 'ostruct'
options = OpenStruct.new
options.client_ids = []
options.save = false
op = OptionParser.new do |opts|
opts.on("-c", "--client", "client id to migrate for") { |client_id|
options.client_ids << client_id }
opts.on_tail("-h", "--help", "Show this message") { puts opts;
exit }
end
begin
op.parse!
rescue Exception => e
puts op
puts e
exit
end
puts options
# ruby script.rb --client 123
# #<OpenStruct client_ids=[true], save=false>
# if I change on() to be:
opts.on("-c", "--client", Integer "client id to migrate for") { |
client_id| options.client_ids << client_id }
# then, ruby script.rb --client 123
# #<OpenStruct client_ids=[nil], save=false>
# furthermore it works if I change on() to be:
opts.on("-c", "--client", :REQUIRED, Integer "client id to migrate
for") { |client_id| options.client_ids << client_id }
# then, ruby script.rb --client 123
# #<OpenStruct client_ids=[123], save=false>
# which is correct, but if I run, ruby script.rb, it does not bomb out
saying it's missing a required argument.
# and still furthermore, I can't do something like this: ruby
script.rb --client 123 456
# and get #<OpenStruct client_ids=[123, 456], save=false>