Forum: Ruby please help me with OptionParser

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7ac9652c07e2ce83b8288663460f5fa8?d=identicon&s=25 Christopher J. Bottaro (Guest)
on 2007-05-27 20:17
(Received via mailing list)
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>
D0338c0de4cb3c5c17300396159933d1?d=identicon&s=25 Axel Etzold (Guest)
on 2007-05-27 20:30
(Received via mailing list)
Dear all,

I am trying to get Jeff Mitchell's linalg package
to work on my machine (OpenSuse 10.2 on i586).
I installed the linalg-0.3.2-i686-linux-ruby18.tgz package,
apparently successfully (that's what my computer told me),

but when I try to actually use it, I get:

>require "linalg"

/usr/local/lib/ruby/site_ruby/1.8/x86_64-linux/lapack.so:
/usr/local/lib/ruby/site_ruby/1.8/x86_64-linux/lapack.so: wrong ELF
class: ELFCLASS32 -
/usr/local/lib/ruby/site_ruby/1.8/x86_64-linux/lapack.so (LoadError)
  from /usr/local/lib/ruby/site_ruby/1.8/linalg.rb:7
  from test.rb:1:in `require'
  from test.rb:1

I also tried to compile the other linalg.tgz package from source,
but there, the system misses f2c, and can't be told to find it in
/usr/bin, or to compile with the flag --without-libf2c .

Thank you for any help.

Best regards,

Axel
7ac9652c07e2ce83b8288663460f5fa8?d=identicon&s=25 Christopher J. Bottaro (Guest)
on 2007-05-27 20:34
(Received via mailing list)
Ugh, you replied on my message and screwed up my thread on Google
Groups... and anyone else who uses a thread aware mail/news program.
D0338c0de4cb3c5c17300396159933d1?d=identicon&s=25 Axel Etzold (Guest)
on 2007-05-27 20:51
(Received via mailing list)
Dear Christopher,

I am sorry about this . It won't happen again.

Best regards,

Axel
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2007-05-27 20:59
(Received via mailing list)
Alle domenica 27 maggio 2007, Christopher J. Bottaro ha scritto:
> 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.

From a previous thread on the list
(http://www.ruby-forum.com/topic/49989#16302), I gather what you want to
do
is impossible with OptionParser (but it may be possible with other
libraries:
I've never used them so I can't tell). To achieve what you mean, you
should
check whether the option was given or not yourself, looking at the
variable
where you stored the options:

  if options.client_ids.empty?
    puts "You must specify an ID"
    exit
  end

> Third, I cannot get a switch to accept multiple arguments.
> --my_arg 1 2 3
> I want an array [1, 2, 3].

I don't think you can write a list of that kind, because OptionParser
would
treat every argument as a single option. You can specify more than one
argument to an option, but you need to separe them with commas, not with
spaces. If you do that, you can convert the result to an Array by
specifying
it:

  opts.on("--list", Array, "a test list){|a| options.list = a}

I hope this helps

Stefano
7ac9652c07e2ce83b8288663460f5fa8?d=identicon&s=25 Christopher J. Bottaro (Guest)
on 2007-05-28 02:38
(Received via mailing list)
On May 27, 1:59 pm, Stefano Crocco <stefano.cro...@alice.it> wrote:
> check whether the option was given or not yourself, looking at the variable
>
> Stefano
Thank you, Stefano, yes it did help.
Ae36591847393e58ff189704f5eb18f2?d=identicon&s=25 Jeremy Hinegardner (Guest)
on 2007-05-28 05:16
(Received via mailing list)
On Mon, May 28, 2007 at 03:17:32AM +0900, Christopher J. Bottaro wrote:
> 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.

Which online documentation are you using?

http://www.ruby-doc.org/stdlib/libdoc/optparse/rdo...

That's the documentation that I use.

> 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.

That's not actually how you do it.  The style to code up a required
argument is:

    opts.on("-c", "--client ID", "client id to migrate for")

Putting something after the option tells option parser that you
require an argument on that option.  In this cases putting ID after
--client tells the parser that this option has a required argument.
use [ID] to designate option parameters.

To force ID to be an integer, you can change the option to be:

    opts.on("-c", "--client ID", Integer, "client id to migrate for")
    ...

This will make sure that ID is an Integer before passing to the block.
If it isn't it will throw and exception.

> Third, I cannot get a switch to accept multiple arguments.
> --my_arg 1 2 3
> I want an array [1, 2, 3].

Array style arguments are done as comma separated items that are passed
to the block as an Array.  So ch

    opts.on("-c", "--client ID,ID", Array, "client id(s) to migrate
for")
    ...

In your case, it seems you want to make sure that the option parameters
to ID is an Array of Integers.  Which means you'll have to tweak it a
bit for yourself.  I've written up one solution in the attached .rb
file.

> Thanks for the help, the rest of the message contains my pastebin
> post:
>

Play with the attached test cases and make it do what you want.  I made
tests to describe what you wanted, and the Application class that is
tested meets those test.

enjoy,

-jeremy
2e18231ab86ae81324b23c279b3b338e?d=identicon&s=25 Allison Newman (demallien)
on 2007-05-28 10:42
I'm no expert, I've only used OptionParser for simple command parsing,
but looking at the documentation, to read an array, you probably need to
do something like this:

opts.on("--clients x,y,z", Array, "Example 'list' of arguments") do
|list|
          options.client_ids = list
        end
This topic is locked and can not be replied to.