Implement CLI usage feature
This commit is contained in:
parent
46f992fdb2
commit
a6cfd4d7cb
5
bin/producer
Executable file
5
bin/producer
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require 'producer/core'
|
||||||
|
|
||||||
|
Producer::Core::CLI.new(ARGV).run!
|
9
features/cli/usage.feature
Normal file
9
features/cli/usage.feature
Normal 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
|
||||||
|
"""
|
@ -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
27
lib/producer/core/cli.rb
Normal 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
|
34
spec/producer/core/cli_spec.rb
Normal file
34
spec/producer/core/cli_spec.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user