Class: FileTemplater::OptionsHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/file_templater/options_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ OptionsHandler

Returns a new instance of OptionsHandler.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/file_templater/options_handler.rb', line 3

def initialize(argv)
	@nomodify = false
	@binding = nil

	@actions = []

	parser = OptionParser.new do |o|
		o.banner = "Usage: template [options] [binding arguments]"

		o.separator ""
		o.separator "Specific options:"

		o.on("-t", "--template TEMPLATE",
			 "Load TEMPLATE to insert",
			 "into the current directory") do |t|
			@actions << [:template, t]
		end

		o.on("-b", "--binding BINDING",
			 "Load BINDING as the binding",
			 "for the loaded template") do |b|
			@binding = b
		end

		o.on("-a", "--add THING", Array,
			 "Add THING, a template or a binding,",
			 "to the template or binding directory") do |t|
			@actions << [:add, t]
		end

		o.on("-r", "--remove THING", Array,
			 "Removes template or binding THING") do |tb|
			@actions << [:remove, tb]
		end

		o.on("-l", "--list",
			 "Lists the templates and bindings",
			 "that are loaded") do
			@actions << [:list]
		end

		o.on("-m", "--no-modify",
			 "Prevents modifying the template source",
			 "when loading") do
			@nomodify = true
		end

		o.on("-c", "--copy TEMPLATE", Array,
			 "Copies TEMPLATE and corresponding binding",
			 "into current directory") do |tb|
			@actions << [:copy, tb]
		end

		o.separator ""
		o.separator "Common options:"

		o.on_tail("-v", "--version", "Display the version") do
			puts "File Templater (template) version " + VERSION
			exit
		end

		o.on_tail("-h", "--help", "Show this message") do
			puts o
			exit
		end
	end

	parser.parse!(argv)
	@arguments = argv
end

Instance Method Details

#process_actionsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/file_templater/options_handler.rb', line 74

def process_actions
	@actions.each do |a|
		command = a.first
		arguments = a[1]

		case command
		when :template
			# arguments is the template name,
			# @arguments are the extraneous arguments
			template = Template.new(arguments, @arguments, nomodify: @nomodify, bind: @binding)
			template.load
		when :add
			arguments.each do |ar|
				FileActions.add(ar)
			end
		when :remove
			arguments.each do |ar|
				FileActions.remove(ar)
			end
		when :list
			puts FileActions.list
		when :copy
			arguments.each do |ar|
				FileActions.copy(ar)
			end
		end
	end
end