class Olib::ScriptVars

Attributes

opts[RW]

Public Class Methods

new() click to toggle source
# File lib/Olib/core/utils.rb, line 7
def initialize
  opts          = {}
  opts[:flags]  = {}
  return opts if Script.current.vars.empty?
  list          = Script.current.vars.map(&:downcase).last(Script.current.vars.length-1)
  unless list.first.start_with?('--')
    opts[:cmd]   = list.shift
  end
  # iterate over list for flag values
  
  list.each.with_index {|ele, i|
    if ele.start_with?('--')
      opts[:flags][ symbolize(ele) ] = ''
    else
      # cast to Number if is number
      ele = ele.to_i if ele =~ /^([\d\.]+$)/
      # look back to previous flag and set it to it's value
      opts[:flags][ symbolize(list[i-1]) ] = ele
    end
  }
  
  @opts = opts
  self
end

Public Instance Methods

cmd() click to toggle source
# File lib/Olib/core/utils.rb, line 32
def cmd
  @opts[:cmd]
end
cmd?(action) click to toggle source
# File lib/Olib/core/utils.rb, line 40
def cmd?(action)
  cmd == action
end
empty?(flag) click to toggle source
# File lib/Olib/core/utils.rb, line 36
def empty?(flag)
  opts[:flags][flag].class == TrueClass || opts[:flags][flag].class == NilClass
end
flag() click to toggle source
# File lib/Olib/core/utils.rb, line 60
def flag
  self
end
flag?(f) click to toggle source
# File lib/Olib/core/utils.rb, line 64
def flag?(f)
  opts[:flags][ symbolize(f) ]
end
flags() click to toggle source
# File lib/Olib/core/utils.rb, line 52
def flags
  opts[:flags].keys
end
help?() click to toggle source
# File lib/Olib/core/utils.rb, line 48
def help?
  cmd =~ /help/
end
method_missing(arg1, arg2=nil) click to toggle source
# File lib/Olib/core/utils.rb, line 68
def method_missing(arg1, arg2=nil)
  @opts[:flags][arg1]
end
symbolize(flag) click to toggle source
# File lib/Olib/core/utils.rb, line 44
def symbolize(flag)
  flag.gsub('--', '').gsub('-', '_').to_sym
end
to_s() click to toggle source
# File lib/Olib/core/utils.rb, line 56
def to_s
  @opts.to_s
end