class Command

Public Class Methods

command_name() click to toggle source
# File lib/commands-lite/base.rb, line 61
def self.command_name
  ## note: cut-off leading Yorobot:: for now in class name!!!
  ## note: always remove _ for now too!!!
  ## note: do NOT use @@name!!! - one instance variable per class needed!!
  @name ||= self.name.downcase
                     .sub( /^yorobot::/, '' )   ## todo/fix: make "exclude" list configure-able  why? why not?
                     .gsub( /[_-]/, '' )
  @name
end
inherited( klass ) click to toggle source
# File lib/commands-lite/base.rb, line 77
def self.inherited( klass )
  # puts  klass.class.name  #=> Class
  ## auto-register commands for now - why? why not?
  Commands.register( klass )
end
option( key, *args ) click to toggle source
# File lib/commands-lite/base.rb, line 7
def self.option( key, *args )
  option_defs[ key ] = args
end
option_defs() click to toggle source
# File lib/commands-lite/base.rb, line 3
def self.option_defs
  @option_defs ||= {}
end
run( args=[] ) click to toggle source
# File lib/commands-lite/base.rb, line 37
def self.run( args=[] )
  command      = new

  puts "--> (#{command.name})  #{args.join('ยท')}"

  ## check for options
  command.parse!( args )

  puts "     #{command.options.size} opt(s): #{command.options.pretty_inspect}"
  puts "     #{args.size} arg(s):"
  args.each_with_index do |arg,i|
    puts "            #{[i]} >#{arg}<"
  end


  if args.size > 0
    ## todo/check: check/verify arity of run - why? why not?
    command.call( *args )   ## use run - why? why not?
  else
    command.call
  end
end

Public Instance Methods

name() click to toggle source
# File lib/commands-lite/base.rb, line 71
def name() self.class.command_name; end
options() click to toggle source
# File lib/commands-lite/base.rb, line 13
def options
  @options ||= {}
end
parse!( args ) click to toggle source
# File lib/commands-lite/base.rb, line 17
def parse!( args )
  ### todo/check - cache option parser!!!! - why? why not?
  OptionParser.new do |parser|
    ## add default banner - overwrite if needed/to customize
    parser.banner = <<TXT

Usage: #{name} [OPTIONS] ARGUMENTS

TXT

    self.class.option_defs.each do | key, on_args|
      parser.on( *on_args ) do |value|
        options[ key ] = value
      end
    end
  end.parse!( args )
end