[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

OptionParser: unexpected behaviour with -help

Stephan Wehner

5/24/2009 4:58:00 AM

I ran into some unexpected behaviour with the Ruby OptionParser class.
When running this script (without any command line arguments) the output
is as in the last, commented, line.

--------------------------------------

require 'optparse'
options = {}
OptionParser.new do |opt|
opt.on "--ex EXECUTABLE", String do |val|
options[:ex] = val
end
opt.on "--headers" do |val|
options[:headers] = val
end
opt.parse(%w( -help ))
end

puts options.inspect

# Output is {:ex=>"lp", :headers=>true}

--------------------------------------


I don't think that '-help' should be treated the same as '-h -ex lp' or
'-h --ex lp'. In particular, '-help' may be a misspelling of the
common '--help' option. Am I doing something wrong? Is there a way to
avoid this?

Thanks a lot,

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

6 Answers

Brian Candler

5/24/2009 9:21:00 AM

0

Look at the source - /usr/lib/ruby/1.8/optparse.rb or somewhere similar
on your system.

# if no short options match, try completion with long
# options.
sw, = complete(:long, opt)

So it looks that if you stub out 'complete' you can get the behaviour
you require: that is, I think you want -h to be treated as an error
instead of as a shortcut for --headers, and ditto -e as an error instead
of a shortcut for --ex

Alternatively, if only '-help' is a problem, you could just handle this
special case:

ARGV.each { |a| a.replace('--help') if a == '-help' }
--
Posted via http://www.ruby-....

Robert Klemme

5/24/2009 3:46:00 PM

0

On 24.05.2009 06:58, Stephan Wehner wrote:
> I ran into some unexpected behaviour with the Ruby OptionParser class.
> When running this script (without any command line arguments) the output
> is as in the last, commented, line.
>
> --------------------------------------
>
> require 'optparse'
> options = {}
> OptionParser.new do |opt|
> opt.on "--ex EXECUTABLE", String do |val|
> options[:ex] = val
> end
> opt.on "--headers" do |val|
> options[:headers] = val
> end
> opt.parse(%w( -help ))

In addition to what Brian wrote, the line above is wrongly placed IMHO.
You cannot know whether initialization of the OptionParser instance is
completed when still inside the block. You should rather invoke #parse
on the result of OptionParser.new.

> end
>
> puts options.inspect

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Stephan Wehner

5/25/2009 6:42:00 AM

0

Brian Candler wrote:
> Look at the source - /usr/lib/ruby/1.8/optparse.rb or somewhere similar
> on your system.
>
> # if no short options match, try completion with long
> # options.
> sw, = complete(:long, opt)
>
> So it looks that if you stub out 'complete' you can get the behaviour
> you require: that is, I think you want -h to be treated as an error
> instead of as a shortcut for --headers, and ditto -e as an error instead
> of a shortcut for --ex
>
> Alternatively, if only '-help' is a problem, you could just handle this
> special case:
>
> ARGV.each { |a| a.replace('--help') if a == '-help' }

Well, thanks a lot for that. I feel the guessing game should be avoided
-- there's too much potential for confusion.

Stephan

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

Stephan Wehner

5/25/2009 6:43:00 AM

0

Robert Klemme wrote:
> On 24.05.2009 06:58, Stephan Wehner wrote:
>> options[:ex] = val
>> end
>> opt.on "--headers" do |val|
>> options[:headers] = val
>> end
>> opt.parse(%w( -help ))
>
> In addition to what Brian wrote, the line above is wrongly placed IMHO.
> You cannot know whether initialization of the OptionParser instance is
> completed when still inside the block. You should rather invoke #parse
> on the result of OptionParser.new.
>
>> end
>>
>> puts options.inspect

Good point -- but the result is the same.

Stephan

> Kind regards
>
> robert

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

Eric Hodel

5/26/2009 8:51:00 PM

0

On May 23, 2009, at 21:58, Stephan Wehner wrote:

> I don't think that '-help' should be treated the same as '-h -ex lp'
> or
> '-h --ex lp'. In particular, '-help' may be a misspelling of the
> common '--help' option. Am I doing something wrong? Is there a way to
> avoid this?

When you use - instead of -- you invoke short-option-parsing like:

$ ruby -dvrubygems -e 'raise rescue nil'
ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
Exception `LoadError' at /Library/Ruby/Site/1.8/rubygems.rb:1106 - no
such file to load -- rubygems/defaults/operating_system
Exception `LoadError' at /Library/Ruby/Site/1.8/rubygems/
config_file.rb:35 - no such file to load -- Win32API
Exception `RuntimeError' at -e:1 -
$

Which is equivalent to:

ruby -d -v -r ubygems -e 'raise rescue nil'

Stephan Wehner

5/28/2009 4:39:00 PM

0

Eric Hodel wrote:
> On May 23, 2009, at 21:58, Stephan Wehner wrote:
>
>> I don't think that '-help' should be treated the same as '-h -ex lp'
>> or
>> '-h --ex lp'. In particular, '-help' may be a misspelling of the
>> common '--help' option. Am I doing something wrong? Is there a way to
>> avoid this?
>
> When you use - instead of -- you invoke short-option-parsing like:
>
> $ ruby -dvrubygems -e 'raise rescue nil'
> ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
> Exception `LoadError' at /Library/Ruby/Site/1.8/rubygems.rb:1106 - no
> such file to load -- rubygems/defaults/operating_system
> Exception `LoadError' at /Library/Ruby/Site/1.8/rubygems/
> config_file.rb:35 - no such file to load -- Win32API
> Exception `RuntimeError' at -e:1 -
> $
>
> Which is equivalent to:
>
> ruby -d -v -r ubygems -e 'raise rescue nil'

There is some precedence for disabling this kind of command line
parsing, see

http://jargs.sourceforge.net/doc/api/jargs/gnu/CmdLineParser.NotFlagExce...

http://tinyurl.com/notfla...

Quote:

public static class CmdLineParser.NotFlagException
extends CmdLineParser.UnknownOptionException

Thrown when the parsed commandline contains multiple concatenated
short options, such as -abcd, where one or more requires a value.
getMessage() returns an english human-readable error string.

End quote

This looks like a useful option to support.

Stephan

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