Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple definitions of a command with a block #152

Merged
merged 2 commits into from
Jul 6, 2013
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
15 changes: 10 additions & 5 deletions lib/gli/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,25 @@ def command(*names)
:skips_post => @skips_post,
:skips_around => @skips_around,
}
@commands_declaration_order ||= []
if names.first.kind_of? Hash
command = GLI::Commands::CompoundCommand.new(self,
names.first,
command_options)
command.parent = self
commands[command.name] = command
@commands_declaration_order << command
else
command = Command.new(command_options.merge(:names => [names].flatten))
command.parent = self
commands[command.name] = command
new_command = Command.new(command_options.merge(:names => [names].flatten))
command = commands[new_command.name]
if command.nil?
command = new_command
command.parent = self
commands[command.name] = command
@commands_declaration_order << command
end
yield command
end
@commands_declaration_order ||= []
@commands_declaration_order << command
clear_nexts
end
alias :c :command
Expand Down
61 changes: 61 additions & 0 deletions test/tc_subcommands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,67 @@ def teardown
:args => ['bar'])
end

test_that "we can reopen commands to add new subcommands" do
Given {
@app.command :remote do |p|
p.command :add do |c|
c.action do |global_options,command_options,args|
@ran_command = :add
end
end
end
@app.command :remote do |p|
p.command :new do |c|
c.action do |global_options,command_options,args|
@ran_command = :new
end
end
end
}
When run_app('remote','new')
Then { assert_equal(@ran_command, :new) }
When run_app('remote', 'add')
Then { assert_equal(@ran_command, :add) }
end

test_that "reopening commands doesn't re-add them to the output" do
Given {
@app.command :remote do |p|
p.command(:add) { }
end
@app.command :remote do |p|
p.command(:new) { }
end
}
command_names = @app.instance_variable_get("@commands_declaration_order").collect { |c| c.name }
assert_equal 1, command_names.grep(:remote).size
end


test_that "we can reopen commands doesn't cause conflicts" do
Given {
@app.command :remote do |p|
p.command :add do |c|
c.action do |global_options,command_options,args|
@ran_command = :remote_add
end
end
end
@app.command :local do |p|
p.command :add do |c|
c.action do |global_options,command_options,args|
@ran_command = :local_add
end
end
end
}
When run_app('remote','add')
Then { assert_equal(@ran_command, :remote_add) }
When run_app('local', 'add')
Then { assert_equal(@ran_command, :local_add) }
end


test_that "we can nest subcommands very deep" do
Given {
@run_results = { :add => nil, :rename => nil, :base => nil }
Expand Down