class BaseChip::Cli::TaskOrData

Attributes

description[RW]
long_description[RW]
name[RW]
options[RW]

Public Class Methods

new() click to toggle source
# File lib/base_chip/cli.rb, line 112
def initialize
  @options  = {}
end

Public Instance Methods

mine_options(ostruct, arguments, unknown_check = false) click to toggle source
# File lib/base_chip/cli.rb, line 148
def mine_options(ostruct, arguments, unknown_check = false)
  found = true
  while found
    found = false
    arguments.size.times do |i|
      a = arguments[i]
      case a
      when *(self.options.keys.map{|k|k.to_s})
        h = self.options[a]
        case h[:type]
        when :boolean; ostruct.send("#{h[:name]}=",true               )
        when :string ; ostruct.send("#{h[:name]}=",arguments[i+1].to_s); arguments.delete_at(i+1) # TODO error if i+1 doesn't exist
        when :integer; ostruct.send("#{h[:name]}=",arguments[i+1].to_i); arguments.delete_at(i+1) # TODO error if i+1 doesn't exist
        end
        arguments.delete_at(i)
        found = true
        break
      end
    end
  end
  if unknown_check
    arguments.each do |a|
      fault "#{self.name || 'base_chip'} didn't understand the option #{a}" if a =~ /^\-/
    end
  end
end
option(name,hash) click to toggle source
# File lib/base_chip/cli.rb, line 121
def option(name,hash)
  hash.merge! :name=>name
  @options[s2o name] = hash
  if hash[:alias]
    hash[:alias].each do |a|
      @options[s2o a] = hash
    end
  end
end
option_usage(o) click to toggle source
# File lib/base_chip/cli.rb, line 130
def option_usage(o)
  name = s2o o[:name]
  alt = o[:alias] ? " (#{ o[:alias].map{|a|s2o a}.join('|')  })" : ''
  case o[:type]
  when :boolean; return name + alt
  else           return "#{name}#{alt} <#{o[:type]}>"
  end
end
options_table() click to toggle source
# File lib/base_chip/cli.rb, line 138
def options_table
  return if self.options.size == 0
  array = []
  self.options.values.uniq.each do |t|
    array << [self.option_usage(t),t[:description]]
  end
  puts ''
  puts "    #{ self.name ? self.name.to_s.titleize : 'Global' } Options:"
  table(array,nil,"        ")
end
s2o(arg) click to toggle source
# File lib/base_chip/cli.rb, line 115
def s2o(arg)
  arg = arg.to_s
  arg = (arg.length > 1 ? '--' : '-') + arg
  arg.gsub!(/_/,'-')
  arg
end
table(array,delimiter=' : ',indent='') click to toggle source
# File lib/base_chip/cli.rb, line 174
def table(array,delimiter='  : ',indent='')
  delimiter ||= '  : '
  widths = []
  array.each do |array2|
    array2.size.times do |i|
      widths[i] ||= 0
      array2[i] ||= ''
      len = array2[i].length + 2
      widths[i] = len if widths[i] < len
    end
  end
  format = indent + widths.map{ |w| "%-#{w}s" }.join(delimiter)
  array.each do |array2|
    puts format % array2
  end
end