Skip to content

Commit 3c1acc0

Browse files
committed
Start copying over CLI parsing
1 parent 5ac56e4 commit 3c1acc0

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

lib/parser.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
require 'optparse'
2+
3+
# Class for handling command line arguments
4+
class Parser
5+
def self.parse(args)
6+
new(args).parse
7+
end
8+
9+
attr_reader :args
10+
11+
ANNOTATION_POSITIONS = %w[before top after bottom].freeze
12+
FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze
13+
14+
def initialize(args)
15+
@args = args
16+
end
17+
18+
def parse
19+
options = default_options
20+
21+
parser(options).parse!(args)
22+
23+
options
24+
end
25+
26+
private
27+
28+
def parser(options)
29+
has_set_position = {}
30+
positions = ANNOTATION_POSITIONS
31+
32+
OptionParser.new do |opts|
33+
opts.banner = 'Usage: annotate [options] [model_file]*'
34+
35+
opts.on('-d', '--delete', 'Remove annotations from all model files or the routes.rb file') do
36+
options[:target_action] = :remove_annotations
37+
end
38+
39+
opts.on('-p', '--position [before|top|after|bottom]', positions,
40+
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |p|
41+
ENV['position'] = p
42+
43+
FILE_TYPE_POSITIONS.each do |key|
44+
ENV[key] = p unless has_set_position[key]
45+
end
46+
end
47+
end
48+
end
49+
50+
def default_options
51+
{
52+
target_action: :do_annotations
53+
}
54+
end
55+
end

spec/parser_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require File.dirname(__FILE__) + '/spec_helper.rb'
2+
3+
require 'pry'
4+
5+
describe Parser do
6+
context 'when given empty args' do
7+
it 'returns an options hash with defaults' do
8+
result = Parser.parse([])
9+
expect(result).to be_a(Hash)
10+
expect(result).to include(target_action: :do_annotations)
11+
end
12+
end
13+
14+
%w[-d --delete].each do |option|
15+
describe option do
16+
it 'sets target_action to :remove_annotations' do
17+
result = Parser.parse([option])
18+
expect(result).to include(target_action: :remove_annotations)
19+
end
20+
end
21+
end
22+
23+
['-p', '--position'].each do |option|
24+
describe option do
25+
Parser::ANNOTATION_POSITIONS.each do |position|
26+
context "when specifying #{position}" do
27+
it "#{position} position is an option" do
28+
Parser.parse([option, position])
29+
expect(Parser::ANNOTATION_POSITIONS).to include(position)
30+
end
31+
32+
it "sets ENV['position'] to be position" do
33+
Parser.parse([option, position])
34+
expect(ENV['position']).to eq(position)
35+
end
36+
37+
it 'sets the value in ENV for the different file types' do
38+
Parser.parse([option, position])
39+
Parser::FILE_TYPE_POSITIONS.each do |file_type|
40+
expect(ENV[file_type]).to eq(position)
41+
end
42+
end
43+
end
44+
end
45+
end
46+
end
47+
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
require 'active_support/core_ext/class/subclasses'
2929
require 'active_support/core_ext/string/inflections'
3030
require 'annotate'
31+
require 'parser'
3132
require 'byebug'
3233

3334
module Annotate

0 commit comments

Comments
 (0)