Bug #14728
closedOptionParser takes other options as argument values
Description
test.rb
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: test.rb [options]"
opts.on("--verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.on("--custom MANDATORY", "custom") do |x|
options[:custom] = x
end
end.parse!
p options
If I run the above script as follows below is output:
$ ruby test.rb --custom --verbose
{:custom=>"--verbose"}
This is not the expected behaviour as --verbose
should not be set as the option value.
Updated by nobu (Nobuyoshi Nakada) about 7 years ago
- Description updated (diff)
- Status changed from Open to Rejected
If --custom
with no argument is allowed, it is not "MANDATORY".
Try:
opts.on("-q","--custom [CUSTOM]", "custom") do |x|
options[:custom] = x
end
Updated by xz0r (xz0r xz0r) about 7 years ago
nobu (Nobuyoshi Nakada) wrote:
If
--custom
with no argument is allowed, it is not "MANDATORY".Try:
opts.on("-q","--custom [CUSTOM]", "custom") do |x| options[:custom] = x end
--custom
with no argument is not allowed, hence an error would be ideal in the above scenario.
Updated by nobu (Nobuyoshi Nakada) about 7 years ago
In common, an argument is determined by the preceding option, consider grep
command's -e
option.
If you want to disallow an argument starting with -
, you'd need to specify a pattern
opts.on("-q","--custom MANDATORY", "custom", /\A(?!-)/) do |x|
options[:custom] = x
end
or allowed values
opts.on("-q","--custom MANDATORY", "custom", %w[A B C]) do |x|
options[:custom] = x
end
Updated by xz0r (xz0r xz0r) almost 7 years ago
nobu (Nobuyoshi Nakada) wrote:
In common, an argument is determined by the preceding option, consider
grep
command's-e
option.
If you want to disallow an argument starting with-
, you'd need to specify a patternopts.on("-q","--custom MANDATORY", "custom", /\A(?!-)/) do |x| options[:custom] = x end
or allowed values
opts.on("-q","--custom MANDATORY", "custom", %w[A B C]) do |x| options[:custom] = x end
Thanks!