Skip to content

Commit

Permalink
Merge pull request #102 from ddfreyne/fix-stray-option-definitions
Browse files Browse the repository at this point in the history
Fix bug which causes option definitions to be merged
  • Loading branch information
denisdefreyne committed Jun 14, 2019
2 parents eb698d7 + e36dd77 commit 705cc08
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/cri/command.rb
Expand Up @@ -361,7 +361,11 @@ def run_this(opts_and_args, parent_opts = {})
end

def all_opt_defns
supercommand ? supercommand.all_opt_defns.merge(option_definitions) : option_definitions
if supercommand
supercommand.all_opt_defns | option_definitions
else
option_definitions
end
end

# @return [String] The help text for this command
Expand Down
37 changes: 37 additions & 0 deletions test/test_command.rb
Expand Up @@ -963,5 +963,42 @@ def test_required_option_defaults_to_given_value_with_transform
assert_equal ['Animal = cow'], lines(out)
assert_equal [], lines(err)
end

def test_option_definitions_are_not_shared_across_commands
root_cmd = Cri::Command.define do
name 'root'
option :r, :rrr, 'Rrr!', argument: :required
end

subcmd_a = root_cmd.define_command do
name 'a'
option :a, :aaa, 'Aaa!', argument: :required

run do |_opts, _args, cmd|
puts cmd.all_opt_defns.map(&:long).sort.join(',')
end
end

subcmd_b = root_cmd.define_command do
name 'b'
option :b, :bbb, 'Bbb!', argument: :required

run do |_opts, _args, cmd|
puts cmd.all_opt_defns.map(&:long).sort.join(',')
end
end

out, err = capture_io_while do
subcmd_a.run(%w[])
end
assert_equal ['aaa,rrr'], lines(out)
assert_equal [], lines(err)

out, err = capture_io_while do
subcmd_b.run(%w[])
end
assert_equal ['bbb,rrr'], lines(out)
assert_equal [], lines(err)
end
end
end

0 comments on commit 705cc08

Please sign in to comment.