Add Task#[]

This commit is contained in:
Josh Holtrop 2022-01-30 13:11:47 -05:00
parent f2a56f1c61
commit ac0f6087fc
3 changed files with 44 additions and 0 deletions

View File

@ -18,3 +18,11 @@ task "four", desc: "Task four", params: [
puts "myparam:" + params["myparam"].inspect puts "myparam:" + params["myparam"].inspect
puts "myp2:" + params["myp2"].inspect puts "myp2:" + params["myp2"].inspect
end end
task "five" do
puts "four myparam value is #{Task["four"]["myparam"]}"
end
task "six" do |task|
puts task["nope"]
end

View File

@ -131,6 +131,21 @@ module Rscons
modify(options, &block) modify(options, &block)
end end
# Get a parameter's value.
#
# @param param_name [String]
# Parameter name.
#
# @return [String]
# Parameter value.
def [](param_name)
param = @params[param_name]
unless param
raise RsconsError.new("Could not find parameter '#{param_name}'")
end
param.value
end
# Execute a task's actions. # Execute a task's actions.
# #
# @return [void] # @return [void]

View File

@ -2885,6 +2885,27 @@ EOF
expect(result.stdout).to eq %[four\nmyparam:"cli-value"\nmyp2:"--myp2"\none\n] expect(result.stdout).to eq %[four\nmyparam:"cli-value"\nmyp2:"--myp2"\none\n]
end end
it "allows accessing task arguments via Task#[]" do
test_dir "tasks"
result = run_rscons(args: %w[-f tasks.rb configure])
result = run_rscons(args: %w[-f tasks.rb five])
expect(result.stderr).to eq ""
expect(result.status).to eq 0
expect(result.stdout).to match /four myparam value is defaultvalue/
result = run_rscons(args: %w[-f tasks.rb four --myparam=v42 five])
expect(result.stderr).to eq ""
expect(result.status).to eq 0
expect(result.stdout).to match /four myparam value is v42/
end
it "exits with an error when attempting to get a nonexistent parameter value" do
test_dir "tasks"
result = run_rscons(args: %w[-f tasks.rb configure])
result = run_rscons(args: %w[-f tasks.rb six])
expect(result.stderr).to match /Could not find parameter 'nope'/
expect(result.status).to_not eq 0
end
it "displays tasks and their arguments in the help info" do it "displays tasks and their arguments in the help info" do
test_dir "tasks" test_dir "tasks"
result = run_rscons(args: %w[-f tasks.rb -h]) result = run_rscons(args: %w[-f tasks.rb -h])