[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Lost with OptionParser

Greg Willits

7/3/2008 12:55:00 AM

I'm having a real problem understanding OptionParser -- the docs in
pickaxe are rather cryptic, Ruby Way doesn't even talk about it, and all
the Q&A I can find on the web doesn't clear it up for me.

I've gotten this far:

# test.rb ---------
require 'optparse'

commands = OptionParser.new
commands.on("-h", "--help") # more or less, I get the .on stuff
commands.on("-e=RUN_ENV")

commands.parse(ARGV)

puts commands

# ---------

That will dump a display of the commands, but for the life of me, I
can't figure out how to determine which options were actually submitted.
And if the -e param was supplied, what's its value is.

I have seen:

commands.parse(ARGV)
commands.parse!(ARGV)
options = commands.parse(ARGV)

So, confused by that to start with, then really confused by where the
parsed commands are, and what format they're in.

-- gw
--
Posted via http://www.ruby-....

7 Answers

dusty

7/3/2008 3:00:00 AM

0

Here is a quick example program. Hope this gets you started.

#!/usr/bin/env ruby
require 'optparse'

options = {}
opts = OptionParser.new
opts.on("-W", "--weekday", "Print out the weekday") do |v|
options[:weekday] = true
end
opts.on("-H", "--hour TYPE", "Print out the hour in 24 or 12 hour
clock") do |v|
options[:hour] = v
end

opts.parse!

unless options[:weekday] || options[:hour]
puts opts
exit
end

if options[:weekday]
puts Time.now.strftime("%A")
exit
end

case options[:hour]
when "12"
puts Time.now.strftime("%I")
when "24"
puts Time.now.strftime("%H")
else
puts "You must send 12 or 24 as an argument to --hour"
end

Greg Willits

7/3/2008 3:48:00 AM

0

dusty wrote:
> Here is a quick example program. Hope this gets you started.

Yes! That turned out to be the Rosetta Stone to help me interpret and
fill the gaps in the other examples I've found.

Many thanks.

-- gw


--
Posted via http://www.ruby-....

ara.t.howard

7/3/2008 4:02:00 AM

0


On Jul 2, 2008, at 6:54 PM, Greg Willits wrote:

> I'm having a real problem understanding OptionParser -- the docs in
> pickaxe are rather cryptic, Ruby Way doesn't even talk about it, and
> all
> the Q&A I can find on the web doesn't clear it up for me.
>
> I've gotten this far:
>
> # test.rb ---------
> require 'optparse'
>
> commands = OptionParser.new
> commands.on("-h", "--help") # more or less, I get the .on stuff
> commands.on("-e=RUN_ENV")
>
> commands.parse(ARGV)
>
> puts commands
>
> # ---------
>
> That will dump a display of the commands, but for the life of me, I
> can't figure out how to determine which options were actually
> submitted.
> And if the -e param was supplied, what's its value is.
>
> I have seen:
>
> commands.parse(ARGV)
> commands.parse!(ARGV)
> options = commands.parse(ARGV)
>
> So, confused by that to start with, then really confused by where the
> parsed commands are, and what format they're in.
>
> -- gw
> --
> Posted via http://www.ruby-....
>



cfp:~ > cat a.rb
require 'yaml'
require 'rubygems' ### gem install rubygems
require 'main'

Main {
option('environment', 'e'){
synopsis 'specify run time environment'
default 'development'
argument :required
validate{|arg| %w[ production development test ].include? arg}
}

def run
environment = params['environment']

y :given => environment.given?,
:value => environment.value
end
}



cfp:~ > ruby a.rb
---
:value: development
:given:


cfp:~ > ruby a.rb --environment=foobar
invalid: option(--environment)="foobar"

cfp:~ > ruby a.rb --environment=test
---
:value: test
:given: true



cfp:~ > ruby a.rb --help
NAME
a.rb

SYNOPSIS
a.rb [options]+

DESCRIPTION
main.rb takes the pain of argument parsing away

PARAMETERS
specify run time environment
--help, -h



the packages ships with a tons of examples.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Jim Morris

7/3/2008 5:23:00 PM

0

I love Main, but it does not seem to work under JRuby, any ideas?

This is the error I always get...

F, [2008-07-03T10:24:48.827600 #9761] FATAL -- : could not reopen: null (IOError)
/opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:318:in `setup_io_restoration'
/opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:318:in `call'
/opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:318:in `finalize'
/opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:29:in `run'
/opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:12:in `catch'
/opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:12:in `run'
/opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/factories.rb:11:in `run'
/opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/factories.rb:16:in `Main'


ara.t.howard wrote:

>
> cfp:~ > cat a.rb
> require 'yaml'
> require 'rubygems' ### gem install rubygems
> require 'main'
>
> Main {
> option('environment', 'e'){
> synopsis 'specify run time environment'
> default 'development'
> argument :required
> validate{|arg| %w[ production development test ].include? arg}
> }
>
> def run
> environment = params['environment']
>
> y :given => environment.given?,
> :value => environment.value
> end
> }
>
>
>
> cfp:~ > ruby a.rb
> ---
> :value: development
> :given:
>
>
> cfp:~ > ruby a.rb --environment=foobar
> invalid: option(--environment)="foobar"
>
> cfp:~ > ruby a.rb --environment=test
> ---
> :value: test
> :given: true
>
>
>
> cfp:~ > ruby a.rb --help
> NAME
> a.rb
>
> SYNOPSIS
> a.rb [options]+
>
> DESCRIPTION
> main.rb takes the pain of argument parsing away
>
> PARAMETERS
> specify run time environment
> --help, -h
>
>
>
> the packages ships with a tons of examples.
>
> a @ http://codeforp...
> --
> we can deny everything, except that we have the possibility of being
> better. simply reflect on that.
> h.h. the 14th dalai lama
>
>
>
>
>


--
Jim Morris, http://blog.w...

ara.t.howard

7/3/2008 5:30:00 PM

0


On Jul 3, 2008, at 11:22 AM, Jim Morris wrote:

> I love Main, but it does not seem to work under JRuby, any ideas?
>
> This is the error I always get...
>
> F, [2008-07-03T10:24:48.827600 #9761] FATAL -- : could not reopen:
> null (IOError)
> /opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:
> 318:in `setup_io_restoration'
> /opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:
> 318:in `call'
> /opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:
> 318:in `finalize'
> /opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:
> 29:in `run'
> /opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:
> 12:in `catch'
> /opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/base.rb:
> 12:in `run'
> /opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/
> factories.rb:11:in `run'
> /opt/jruby-1.1.2/lib/ruby/gems/1.8/gems/main-2.8.0/lib/main/
> factories.rb:16:in `Main'


it should be able to do - might take some hacking to deal with
different handling of stdout/stderr/stdin. patches welcome!

;-)

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Yossef Mendelssohn

7/3/2008 6:59:00 PM

0

On Jul 3, 12:29 pm, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
> it should be able to do - might take some hacking to deal with
> different handling of stdout/stderr/stdin. patches welcome!
>
> ;-)

I'll consider writing the patches after you write the specs.

And by "specs" I mean actual specs, not a load of numbered tests. I
was disappointed there was no test_420, btw.

--
-yossef

ara.t.howard

7/3/2008 7:22:00 PM

0


On Jul 3, 2008, at 12:59 PM, Yossef Mendelssohn wrote:

> I'll consider writing the patches after you write the specs.
>
> And by "specs" I mean actual specs, not a load of numbered tests. I
> was disappointed there was no test_420, btw.


heh - well i've got about 51 projects on rubyforge attm, nearly all of
them pre-dating bacon and co. if anyone wants to help migrate the
tests.... i've got some old perl code that needs ported to ruby too.
and a stack of svn projects which need moved to github....

;-)

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama