Module: Sickle::ClassMethods

Defined in:
lib/sickle.rb

Instance Method Summary collapse

Instance Method Details

#__commandsObject



221
222
223
# File 'lib/sickle.rb', line 221

def __commands
  @__commands ||= {}
end

#__global_optionsObject



225
226
227
# File 'lib/sickle.rb', line 225

def __global_options
  @__global_options ||= {}
end

#desc(label) ⇒ Object



193
194
195
# File 'lib/sickle.rb', line 193

def desc(label)
  Sickle.push_desc(label)
end

#flag(name) ⇒ Object



209
210
211
# File 'lib/sickle.rb', line 209

def flag(name)
  option(name, :default => false)
end

#global_flag(name) ⇒ Object



201
202
203
# File 'lib/sickle.rb', line 201

def global_flag(name)
  global_option(name, :default => false)
end

#global_option(name, opts = {}) ⇒ Object



197
198
199
# File 'lib/sickle.rb', line 197

def global_option(name, opts = {})
  __global_options[name.to_s] = Option.new(name, opts)
end

#include_modules(hash) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/sickle.rb', line 213

def include_modules(hash)
  hash.each do |key, value|
    Sickle.push_namespace(key)
    send(:include, value)
    Sickle.pop_namespace
  end
end

#included(base) ⇒ Object



186
187
188
189
190
191
# File 'lib/sickle.rb', line 186

def included(base)
  __commands.each do |name, command|
    name = (Sickle.namespace + [name]).join(":")
    base.__commands[name] = command
  end
end

#method_added(a) ⇒ Object



269
270
271
272
273
274
# File 'lib/sickle.rb', line 269

def method_added(a)
  meth = instance_method(a)
  if desc = Sickle.pop_desc
    __commands[a.to_s] = Command.new(meth, a, desc, Sickle.pop_options)
  end
end

#option(name, opts = {}) ⇒ Object



205
206
207
# File 'lib/sickle.rb', line 205

def option(name, opts = {})
  Sickle.push_option(name, opts)
end

#run(argv) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/sickle.rb', line 229

def run(argv)
  # puts "ARGV: #{argv.inspect}"

  if command_name = argv.shift
    command_name = command_name.gsub("-", "_")

    if command = __commands[command_name]
      all = __global_options.values + command.options.values

      results = {}
      args = OptionParser.new do |parser|
        all.each do |option|
          option.register(parser, results)
        end
      end.parse!(argv)

      all.each do |o|
        results[o.name] ||= o.default
      end

      # puts "args: #{args.inspect}"
      # puts "results: #{results.inspect}"


      obj = self.new
      obj.instance_variable_set(:@__options, results)
      command.meth.bind(obj).call(*args)
    else
      puts "\e[31mCommand '#{command_name}' not found\e[0m"
      puts
      run(["help"])
      exit 127
    end
  else
    run(["help"])
  end
end