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

@@ -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