Implement recipe arguments

CLI will stop arguments processing after the special `--' argument,
the rest will be saved in the env and accessible through a new task
keyword: `recipe_argv'.
This commit is contained in:
Thibault Jouan 2015-04-04 04:41:25 +00:00
parent f17f8c354f
commit 433f830c54
7 changed files with 54 additions and 1 deletions

View File

@ -51,6 +51,11 @@ When /^I successfully execute the recipe with option (-.+)$/ do |option|
assert_exit_status 0
end
When /^I successfully execute the recipe with arguments "([^"]+)"$/ do |arguments|
run_simple "producer recipe.rb -- #{arguments}", false
assert_exit_status 0
end
When /^I execute the recipe interactively$/ do
run_interactive 'producer recipe.rb'
end

View File

@ -0,0 +1,13 @@
Feature: `recipe_argv' task keyword
Background:
Given a recipe with:
"""
task :echo_arguments do
echo recipe_argv
end
"""
Scenario: returns recipe arguments
When I successfully execute the recipe with arguments "foo bar"
Then the output must contain exactly "foo\nbar\n"

View File

@ -8,6 +8,8 @@ module Producer
EX_USAGE = 64
EX_SOFTWARE = 70
ARGUMENTS_SEPARATOR = '--'.freeze
class << self
def run!(arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr)
cli = new(arguments, stdin: stdin, stdout: stdout, stderr: stderr)
@ -39,6 +41,9 @@ module Producer
end
def parse_arguments!
if @arguments.include? ARGUMENTS_SEPARATOR
@arguments, @env.recipe_argv = split_arguments_lists @arguments
end
option_parser.parse!(@arguments)
fail ArgumentError, option_parser if @arguments.empty?
end
@ -60,6 +65,13 @@ module Producer
Env.new(input: @stdin, output: @stdout, error_output: @stderr)
end
def split_arguments_lists(arguments)
arguments
.chunk { |e| e == ARGUMENTS_SEPARATOR }
.reject { |b, a| b }
.map &:last
end
def option_parser
OptionParser.new do |opts|
opts.banner = USAGE

View File

@ -5,7 +5,7 @@ module Producer
def_delegators :@registry, :[]=, :key?
attr_reader :input, :output, :error_output, :registry, :logger
attr_accessor :target, :verbose, :debug, :dry_run
attr_accessor :target, :verbose, :debug, :dry_run, :recipe_argv
def initialize(input: $stdin, output: $stdout, error_output: $stderr, remote: nil, registry: {})
@verbose = @debug = @dry_run = false

View File

@ -18,6 +18,7 @@ module Producer
def_delegators :@env, :target
def_delegator :@env, :[], :get
def_delegator :@env, :key?, :set?
def_delegator :@env, :recipe_argv
define_action :echo, Actions::Echo
define_action :sh, Actions::ShellCommand

View File

@ -130,6 +130,20 @@ module Producer::Core
end
end
context 'with recipe arguments' do
let(:arguments) { %w[recipe.rb -- foo] }
it 'removes recipe arguments' do
cli.parse_arguments!
expect(cli.arguments).to eq %w[recipe.rb]
end
it 'assigns env recipe arguments' do
cli.parse_arguments!
expect(cli.env.recipe_argv).to eq %w[foo]
end
end
context 'when no arguments remains after parsing' do
let(:arguments) { [] }

View File

@ -179,5 +179,13 @@ module Producer::Core
expect(task.target).to eq :some_target
end
end
describe '#recipe_argv' do
before { env.recipe_argv = %w[foo bar] }
it 'returns recipe arguments' do
expect(task.recipe_argv).to eq %w[foo bar]
end
end
end
end