diff --git a/lib/cri/command.rb b/lib/cri/command.rb index be0135c..ba424eb 100644 --- a/lib/cri/command.rb +++ b/lib/cri/command.rb @@ -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 diff --git a/test/test_command.rb b/test/test_command.rb index b2108cb..9f0b1af 100644 --- a/test/test_command.rb +++ b/test/test_command.rb @@ -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