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