Class: GitStyleBinary::Parser

Inherits:
Trollop::Parser
  • Object
show all
Defined in:
lib/git-style-binary/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a, &b) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
# File 'lib/git-style-binary/parser.rb', line 7

def initialize *a, &b
  super
  @runs = []
  setup_callbacks    
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



3
4
5
# File 'lib/git-style-binary/parser.rb', line 3

def callbacks
  @callbacks
end

#commandObject

Returns the value of attribute command.



5
6
7
# File 'lib/git-style-binary/parser.rb', line 5

def command
  @command
end

#runsObject (readonly)

Returns the value of attribute runs.



3
4
5
# File 'lib/git-style-binary/parser.rb', line 3

def runs
  @runs
end

#short_desc(s = nil) ⇒ Object (readonly)

Returns the value of attribute short_desc.



4
5
6
# File 'lib/git-style-binary/parser.rb', line 4

def short_desc
  @short_desc
end

#themeObject



34
35
36
# File 'lib/git-style-binary/parser.rb', line 34

def theme
  @theme ||= :long
end

Instance Method Details

#action(name = :action, &block) ⇒ Object



217
218
219
# File 'lib/git-style-binary/parser.rb', line 217

def action(name = :action, &block)
  block.call(self) if block
end

#all_options_stringObject



209
210
211
# File 'lib/git-style-binary/parser.rb', line 209

def all_options_string
  '#{spec_names.collect(&:to_s).collect{|name| "[" + "--" + name + "]"}.join(" ")}'
end


27
# File 'lib/git-style-binary/parser.rb', line 27

def banner      s=nil; @banner     = s if s; @banner     end

#bin_nameObject



205
206
207
# File 'lib/git-style-binary/parser.rb', line 205

def bin_name
  GitStyleBinary.full_current_command_name
end

#colorize_known_words(txt) ⇒ Object



191
192
193
194
195
# File 'lib/git-style-binary/parser.rb', line 191

def colorize_known_words(txt)
  txt = txt.gsub(/^([A-Z]+\s*)$/, '\1'.colorize(:red))       # all caps words on their own line
  txt = txt.gsub(/\b(#{bin_name})\b/, '\1'.colorize(:light_blue))  # the current command name
  txt = txt.gsub(/\[([^\s]+)\]/, "[".colorize(:magenta) + '\1'.colorize(:green) + "]".colorize(:magenta)) # synopsis options
end

#colorize_known_words_array(txts) ⇒ Object



187
188
189
# File 'lib/git-style-binary/parser.rb', line 187

def colorize_known_words_array(txts)
  txts.collect{|txt| colorize_known_words(txt)}
end

#consume(&block) ⇒ Object



197
198
199
# File 'lib/git-style-binary/parser.rb', line 197

def consume(&block)
  cloaker(&block).bind(self).call
end

#consume_all(blocks) ⇒ Object



201
202
203
# File 'lib/git-style-binary/parser.rb', line 201

def consume_all(blocks)
  blocks.each {|b| consume(&b)}
end

#educate(stream = $stdout) ⇒ Object

Print the help message to ‘stream’.



54
55
56
57
58
59
60
# File 'lib/git-style-binary/parser.rb', line 54

def educate(stream=$stdout)
  load_all_commands
  width # just calculate it now; otherwise we have to be careful not to
        # call this unless the cursor's at the beginning of a line.
  GitStyleBinary::Helpers::Pager.run_pager
  self.send("educate_#{theme}", stream) 
end

#educate_long(stream = $stdout) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/git-style-binary/parser.rb', line 62

def educate_long(stream=$stdout)
  left = {}

  @specs.each do |name, spec| 
    left[name] = 
      ((spec[:short] ? "-#{spec[:short]}, " : "") +
      "--#{spec[:long]}" +
      case spec[:type]
      when :flag; ""
      when :int; "=<i>"
      when :ints; "=<i+>"
      when :string; "=<s>"
      when :strings; "=<s+>"
      when :float; "=<f>"
      when :floats; "=<f+>"
      end).colorize(:red)
  end

  leftcol_width = left.values.map { |s| s.length }.max || 0
  rightcol_start = leftcol_width + 6 # spaces
  leftcol_start = 6
  leftcol_spaces = " " * leftcol_start

  unless @order.size > 0 && @order.first.first == :text

    if @name_desc
      stream.puts "NAME".colorize(:red)
      stream.puts "#{leftcol_spaces}"+  colorize_known_words(eval(%Q["#{@name_desc}"])) + "\n"
      stream.puts
    end

    if @version
      stream.puts "VERSION".colorize(:red)
      stream.puts "#{leftcol_spaces}#@version\n"
    end
 
    stream.puts

    banner = colorize_known_words_array(wrap(eval(%Q["#{@banner}"]) + "\n", :prefix => leftcol_start)) if @banner # lazy banner
    stream.puts banner

    stream.puts
    stream.puts "OPTIONS".colorize(:red)
  else
    stream.puts "#@banner\n" if @banner
  end

  @order.each do |what, opt|
    if what == :text
      stream.puts wrap(opt)
      next
    end

    spec = @specs[opt]
    stream.printf "    %-#{leftcol_width}s\n", left[opt]
    desc = spec[:desc] + 
      if spec[:default]
        if spec[:desc] =~ /\.$/
          " (Default: #{spec[:default]})"
        else
          " (default: #{spec[:default]})"
        end
      else
        ""
      end
    stream.puts wrap("      %s" % [desc], :prefix => leftcol_start, :width => width - rightcol_start - 1 )
    stream.puts
    stream.puts
  end

end

#educate_short(stream = $stdout) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/git-style-binary/parser.rb', line 134

def educate_short(stream=$stdout)
  left = {}

  @specs.each do |name, spec| 
    left[name] = "--#{spec[:long]}" +
      (spec[:short] ? ", -#{spec[:short]}" : "") +
      case spec[:type]
      when :flag; ""
      when :int; " <i>"
      when :ints; " <i+>"
      when :string; " <s>"
      when :strings; " <s+>"
      when :float; " <f>"
      when :floats; " <f+>"
      end
  end

  leftcol_width = left.values.map { |s| s.length }.max || 0
  rightcol_start = leftcol_width + 6 # spaces
  leftcol_start = 0

  unless @order.size > 0 && @order.first.first == :text
    stream.puts "#@version\n" if @version
    stream.puts colorize_known_words_array(wrap(eval(%Q["#{@banner}"]) + "\n", :prefix => leftcol_start)) if @banner # jit banner
    stream.puts "Options:"
  else
    stream.puts "#@banner\n" if @banner
  end

  @order.each do |what, opt|
    if what == :text
      stream.puts wrap(opt)
      next
    end

    spec = @specs[opt]
    stream.printf "  %#{leftcol_width}s:   ", left[opt]
    desc = spec[:desc] + 
      if spec[:default]
        if spec[:desc] =~ /\.$/
          " (Default: #{spec[:default]})"
        else
          " (default: #{spec[:default]})"
        end
      else
        ""
      end
    stream.puts wrap(desc, :width => width - rightcol_start - 1, :prefix => rightcol_start)
  end

end

#load_all_commandsObject

should probably be somewhere else



46
47
48
49
50
51
# File 'lib/git-style-binary/parser.rb', line 46

def load_all_commands
  GitStyleBinary.subcommand_names.each do |name|
    cmd_file = GitStyleBinary.binary_filename_for(name)
    GitStyleBinary.load_command_file(name, cmd_file)
  end
end

#name_desc(s = nil) ⇒ Object



29
# File 'lib/git-style-binary/parser.rb', line 29

def name_desc   s=nil; @name_desc = s if s; @name_desc end

#run(&block) ⇒ Object



213
214
215
# File 'lib/git-style-binary/parser.rb', line 213

def run(&block)
  @runs << block
end

#run_callbacks(at, from) ⇒ Object



23
24
25
# File 'lib/git-style-binary/parser.rb', line 23

def run_callbacks(at, from)
  @callbacks[at].each {|c| c.call(from) }
end

#setup_callbacksObject



13
14
15
16
17
18
19
20
21
# File 'lib/git-style-binary/parser.rb', line 13

def setup_callbacks
  @callbacks =  {}
  %w(run).each do |event|
    %w(before after).each do |time|
      @callbacks["#{time}_#{event}".to_sym] = []
      instance_eval "def #{time}_#{event}(&block);@callbacks[:#{time}_#{event}] << block;end"
    end
  end
end

#spec_namesObject



41
42
43
# File 'lib/git-style-binary/parser.rb', line 41

def spec_names
  @specs.collect{|name, spec| spec[:long]}
end

#text(s) ⇒ Object

Adds text to the help display.



39
# File 'lib/git-style-binary/parser.rb', line 39

def text s; @order << [:text, s] end