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:
@@ -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
|
||||
|
Reference in New Issue
Block a user