Refactor CLI usage into CLI.run!:

* Add Producer::Core::CLI::ArgumentError class;
* Raise ArgumentError in CLI.new when recipe is missing;
* Remove #check_arguments! and #print_usage_and_exit, move code into
  .run! method.
This commit is contained in:
Thibault Jouan
2014-01-07 21:02:00 +00:00
parent dc9e7f1412
commit f7efd9c42f
3 changed files with 54 additions and 42 deletions

View File

@@ -1,24 +1,34 @@
module Producer
module Core
class CLI
ArgumentError = Class.new(::ArgumentError)
USAGE = "Usage: #{File.basename $0} recipe_file"
class << self
def run!(arguments, output: $stderr)
begin
cli = new(arguments)
rescue ArgumentError
output.puts USAGE
exit 64
end
cli.run!
end
end
attr_reader :arguments, :stdout
def initialize(arguments, stdout: $stdout)
raise ArgumentError unless arguments.any?
@arguments = arguments
@stdout = stdout
end
def run!
check_arguments!
interpreter.process recipe.tasks
end
def check_arguments!
print_usage_and_exit(64) unless @arguments.length == 1
end
def env
@env ||= Env.new
end
@@ -30,14 +40,6 @@ module Producer
def interpreter
@interpreter ||= Interpreter.new
end
private
def print_usage_and_exit(status)
@stdout.puts USAGE
exit status
end
end
end
end