Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions lib/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,11 @@ def pretty_print(q) # :nodoc:
q.object_group(self) {pretty_print_contents(q)}
end

def omitted_argument(val) # :nodoc:
val.pop if val.size == 3 and val.last.nil?
val
end

#
# Switch that takes no arguments.
#
Expand Down Expand Up @@ -755,7 +760,7 @@ def parse(arg, argv, &error)
if arg
conv_arg(*parse_arg(arg, &error))
else
conv_arg(arg)
omitted_argument conv_arg(arg)
end
end

Expand All @@ -774,13 +779,14 @@ class PlacedArgument < self
#
def parse(arg, argv, &error)
if !(val = arg) and (argv.empty? or /\A-./ =~ (val = argv[0]))
return nil, block, nil
return nil, block
end
opt = (val = parse_arg(val, &error))[1]
val = conv_arg(*val)
if opt and !arg
argv.shift
else
omitted_argument val
val[0] = nil
end
val
Expand Down Expand Up @@ -1658,9 +1664,9 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
raise $!.set_option(arg, true)
end
begin
opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
val = cb.call(val) if cb
setter.call(sw.switch_name, val) if setter
opt, cb, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}
val = callback!(cb, 1, *val) if cb
callback!(setter, 2, sw.switch_name, *val) if setter
rescue ParseError
raise $!.set_option(arg, rest)
end
Expand Down Expand Up @@ -1690,16 +1696,16 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
raise $!.set_option(arg, true)
end
begin
opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
opt, cb, *val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
rescue ParseError
raise $!.set_option(arg, arg.length > 2)
else
raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
end
begin
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
val = cb.call(val) if cb
setter.call(sw.switch_name, val) if setter
val = callback!(cb, 1, *val) if cb
callback!(setter, 2, sw.switch_name, *val) if setter
rescue ParseError
raise $!.set_option(arg, arg.length > 2)
end
Expand All @@ -1725,6 +1731,17 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
end
private :parse_in_order

# Calls callback with _val_.
def callback!(cb, max_arity, *args) # :nodoc:
if (size = args.size) < max_arity and cb.to_proc.lambda?
(arity = cb.arity) < 0 and arity = (1-arity)
arity = max_arity if arity > max_arity
args[arity - 1] = nil if arity > size
end
cb.call(*args)
end
private :callback!

#
# Parses command line arguments +argv+ in permutation mode and returns
# list of non-option arguments. When optional +into+ keyword
Expand Down
5 changes: 5 additions & 0 deletions test/optparse/test_acceptable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def setup
@opt.def_option("--integer VAL", Integer) { |v| @integer = v }
@opt.def_option("--float VAL", Float) { |v| @float = v }
@opt.def_option("--numeric VAL", Numeric) { |v| @numeric = v }
@opt.def_option("--array VAL", Array) { |v| @array = v }

@opt.def_option("--decimal-integer VAL",
OptionParser::DecimalInteger) { |i| @decimal_integer = i }
Expand Down Expand Up @@ -195,4 +196,8 @@ def test_decimal_numeric
end
end

def test_array
assert_equal(%w"", no_error {@opt.parse!(%w"--array a,b,c")})
assert_equal(%w"a b c", @array)
end
end
16 changes: 16 additions & 0 deletions test/optparse/test_optarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def setup
@opt.def_option("--regexp[=REGEXP]", Regexp) {|x| @reopt = x}
@opt.def_option "--with_underscore[=VAL]" do |x| @flag = x end
@opt.def_option "--with-hyphen[=VAL]" do |x| @flag = x end
@opt.def_option("--fallback[=VAL]") do |x = "fallback"| @flag = x end
@opt.def_option("--lambda[=VAL]", &->(x) {@flag = x})
@reopt = nil
end

Expand Down Expand Up @@ -57,4 +59,18 @@ def test_hyphenize
assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen=foo4")})
assert_equal("foo4", @flag)
end

def test_default_argument
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback=val1")})
assert_equal("val1", @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
assert_equal("fallback", @flag)
end

def test_lambda
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")})
assert_equal("lambda1", @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda")})
assert_equal(nil, @flag)
end
end
7 changes: 7 additions & 0 deletions test/optparse/test_optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ def test_into
@opt.def_option "-p", "--port=PORT", "port", Integer
@opt.def_option "-v", "--verbose" do @verbose = true end
@opt.def_option "-q", "--quiet" do @quiet = true end
@opt.def_option "-o", "--option [OPT]" do |opt| @option = opt end
result = {}
@opt.parse %w(--host localhost --port 8000 -v), into: result
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
assert_equal(true, @verbose)
result = {}
@opt.parse %w(--option -q), into: result
assert_equal({quiet: true, option: nil}, result)
result = {}
@opt.parse %w(--option OPTION -v), into: result
assert_equal({verbose: true, option: "OPTION"}, result)
end

def test_require_exact
Expand Down
20 changes: 20 additions & 0 deletions test/optparse/test_placearg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def setup
@reopt = nil
@opt.def_option "--with_underscore=VAL" do |x| @flag = x end
@opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
@opt.def_option("--fallback [VAL]") do |x = "fallback"| @flag = x end
@opt.def_option("--lambda [VAL]", &->(x) {@flag = x})
end

def test_short
Expand Down Expand Up @@ -73,4 +75,22 @@ def test_conv
assert_equal(%w"te.rb", no_error('[ruby-dev:38333]') {@opt.parse!(%w"-T1 te.rb")})
assert_equal(1, @topt)
end

def test_default_argument
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback=val1")})
assert_equal("val1", @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback val2")})
assert_equal("val2", @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
assert_equal("fallback", @flag)
end

def test_lambda
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")})
assert_equal("lambda1", @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda lambda2")})
assert_equal("lambda2", @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda")})
assert_equal(nil, @flag)
end
end
6 changes: 6 additions & 0 deletions test/optparse/test_reqarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def setup
super
@opt.def_option "--with_underscore=VAL" do |x| @flag = x end
@opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
@opt.def_option("--lambda=VAL", &->(x) {@flag = x})
end

class Def1 < TestOptionParser
Expand Down Expand Up @@ -81,6 +82,11 @@ def test_hyphenize
assert_equal("foo4", @flag)
end

def test_lambda
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")})
assert_equal("lambda1", @flag)
end

class TestOptionParser::WithPattern < TestOptionParser
def test_pattern
pat = num = nil
Expand Down