Implement CLI usage feature

This commit is contained in:
Thibault Jouan 2013-07-26 20:12:55 +00:00
parent 46f992fdb2
commit a6cfd4d7cb
5 changed files with 76 additions and 0 deletions

5
bin/producer Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env ruby
require 'producer/core'
Producer::Core::CLI.new(ARGV).run!

View File

@ -0,0 +1,9 @@
Feature: CLI usage
Scenario: prints the usage when an argument is missing
When I run `producer`
Then the exit status must be 64
And the output must contain exactly:
"""
Usage: producer host recipe_file
"""

View File

@ -2,5 +2,6 @@ require 'producer/core/version'
module Producer module Producer
module Core module Core
autoload :CLI, 'producer/core/cli'
end end
end end

27
lib/producer/core/cli.rb Normal file
View File

@ -0,0 +1,27 @@
module Producer
module Core
class CLI
attr_reader :arguments
USAGE = "Usage: #{File.basename $0} host recipe_file"
def initialize(arguments, stdout = $stdout)
@stdout = stdout
@arguments = arguments
end
def run!
print_usage_and_exit(64) unless @arguments.length == 2
end
private
def print_usage_and_exit(status)
@stdout.puts USAGE
exit status
end
end
end
end

View File

@ -0,0 +1,34 @@
require 'spec_helper'
module Producer::Core
describe CLI do
let(:arguments) { %w{host recipe.rb} }
subject(:cli) { CLI.new(arguments) }
describe '#initialize' do
it 'assigns the arguments' do
expect(cli.arguments).to eq arguments
end
end
describe '#run!' do
context 'missing argument' do
let(:arguments) { %w{host} }
let(:stdout) { StringIO.new }
subject(:cli) { CLI.new(arguments, stdout) }
it 'exits' do
expect { cli.run! }.to raise_error SystemExit
end
it 'prints the usage' do
begin
cli.run!
rescue SystemExit
end
expect(stdout.string).to match /\AUsage: .+/
end
end
end
end
end