Skip to content

Commit

Permalink
Support specifying values for combined options
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jun 3, 2017
1 parent c8065fd commit a1e6472
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
5 changes: 1 addition & 4 deletions lib/cri/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,7 @@ def handle_dash_option(e)
definition = @definitions.find { |d| d[:short] == option_key }
raise IllegalOptionError.new(option_key) if definition.nil?

if option_keys.length > 1 && definition[:argument] == :required
# This is a combined option and it requires an argument, so complain
raise OptionRequiresAnArgumentError.new(option_key)
elsif %i[required optional].include?(definition[:argument])
if %i[required optional].include?(definition[:argument])
# Get option value
option_value = find_option_value(definition, option_key)

Expand Down
53 changes: 49 additions & 4 deletions test/test_option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,19 @@ def test_parse_with_short_combined_valueless_options
end

def test_parse_with_short_combined_valueful_options_with_missing_value
input = %w[foo -abc bar]
input = %w[foo -abc bar qux]
definitions = [
{ long: 'aaa', short: 'a', argument: :required },
{ long: 'bbb', short: 'b', argument: :forbidden },
{ long: 'ccc', short: 'c', argument: :forbidden },
]

assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
Cri::OptionParser.parse(input, definitions)
end
parser = Cri::OptionParser.parse(input, definitions)

assert_equal('bar', parser.options[:aaa])
assert(parser.options[:bbb])
assert(parser.options[:ccc])
assert_equal(%w[foo qux], parser.arguments)
end

def test_parse_with_two_short_valueful_options
Expand Down Expand Up @@ -337,5 +340,47 @@ def test_parse_with_default_optional_value_and_arg
assert_equal({ animal: 'gi' }, parser.options)
assert_equal(%w[foo raffe], parser.arguments)
end

def test_parse_with_combined_required_options
input = %w[foo -abc xxx yyy zzz]
definitions = [
{ long: 'aaa', short: 'a', argument: :forbidden },
{ long: 'bbb', short: 'b', argument: :required },
{ long: 'ccc', short: 'c', argument: :required },
]

parser = Cri::OptionParser.parse(input, definitions)

assert_equal({ aaa: true, bbb: 'xxx', ccc: 'yyy' }, parser.options)
assert_equal(%w[foo zzz], parser.arguments)
end

def test_parse_with_combined_optional_options
input = %w[foo -abc xxx yyy zzz]
definitions = [
{ long: 'aaa', short: 'a', argument: :forbidden },
{ long: 'bbb', short: 'b', argument: :optional },
{ long: 'ccc', short: 'c', argument: :required },
]

parser = Cri::OptionParser.parse(input, definitions)

assert_equal({ aaa: true, bbb: 'xxx', ccc: 'yyy' }, parser.options)
assert_equal(%w[foo zzz], parser.arguments)
end

def test_parse_with_combined_optional_options_with_missing_value
input = %w[foo -abc xxx]
definitions = [
{ long: 'aaa', short: 'a', argument: :forbidden },
{ long: 'bbb', short: 'b', argument: :required },
{ long: 'ccc', short: 'c', argument: :optional, default: 'c default' },
]

parser = Cri::OptionParser.parse(input, definitions)

assert_equal({ aaa: true, bbb: 'xxx', ccc: 'c default' }, parser.options)
assert_equal(%w[foo], parser.arguments)
end
end
end

0 comments on commit a1e6472

Please sign in to comment.