Class: Olib::ScriptVars

Inherits:
Object
  • Object
show all
Defined in:
lib/Olib/utils/cli.rb,
lib/Olib/core/utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScriptVars


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/Olib/utils/cli.rb', line 4

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
  flags = list.compact.join(' ').split('--')
  flags.shift
  flags.each { |flag|
    segments  = flag.split(' ')
    name      =  symbolize(segments.shift)
    opts[:flags][name] = true 
    if !segments.empty?
      value = segments.join(' ').strip
      if value =~ /[\d]+/
        value = value.to_i
      end
      opts[:flags][name] = value
    end
  }

  @opts = opts
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(arg1, arg2 = nil) ⇒ Object


70
71
72
# File 'lib/Olib/utils/cli.rb', line 70

def method_missing(arg1, arg2=nil)
  @opts[:flags][arg1]
end

Instance Attribute Details

#optsObject

Returns the value of attribute opts


3
4
5
# File 'lib/Olib/utils/cli.rb', line 3

def opts
  @opts
end

Instance Method Details

#cmdObject


34
35
36
# File 'lib/Olib/utils/cli.rb', line 34

def cmd
  @opts[:cmd]
end

#cmd?(action) ⇒ Boolean


42
43
44
# File 'lib/Olib/utils/cli.rb', line 42

def cmd?(action)
  cmd == action
end

#empty?(flag) ⇒ Boolean


38
39
40
# File 'lib/Olib/utils/cli.rb', line 38

def empty?(flag)
  opts[:flags][flag].class == TrueClass || opts[:flags][flag].class == NilClass
end

#flagObject


62
63
64
# File 'lib/Olib/utils/cli.rb', line 62

def flag
  self
end

#flag?(f) ⇒ Boolean


66
67
68
# File 'lib/Olib/utils/cli.rb', line 66

def flag?(f)
  opts[:flags][ symbolize(f) ]
end

#flagsObject


54
55
56
# File 'lib/Olib/utils/cli.rb', line 54

def flags
  opts[:flags].keys
end

#help?Boolean


50
51
52
# File 'lib/Olib/utils/cli.rb', line 50

def help?
  cmd =~ /help/
end

#symbolize(flag) ⇒ Object


46
47
48
# File 'lib/Olib/utils/cli.rb', line 46

def symbolize(flag)
  flag.gsub('--', '').gsub('-', '_').to_sym
end

#to_sObject


58
59
60
# File 'lib/Olib/utils/cli.rb', line 58

def to_s
  @opts.to_s
end