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
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/file_templater/options_handler.rb', line 3

def initialize(argv)
  @nomodify = false

  # Commands that can only be run once.
  @template = nil
  @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|
      can_only_be_used_once @template, "-t"

      @actions << [:template]
      @template = t
    end

    o.on("-b", "--binding BINDING",
       "Load BINDING as the binding",
       "for the loaded template") do |b|
      can_only_be_used_once @binding, "-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 THING", Array,
       "Copies THING, a template or 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
      puts
      puts "Copyright (C) 2015 Sam Craig"
      puts "Licensed under the GNU General Public License version 3."

      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

#can_only_be_used_once(variable, switch) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/file_templater/options_handler.rb', line 119

def can_only_be_used_once(variable, switch)
  if variable
    puts "The #{switch} switch can only be used once!"

    exit
  end
end

#process_actionsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/file_templater/options_handler.rb', line 87

def process_actions
  # All actions should be unique.
  @actions.uniq!

  @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(@template, @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