Class: Flagabowski::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/flagabowski.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Option

Returns a new instance of Option.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/flagabowski.rb', line 6

def initialize(opts)
  @name      = opts[:name]
  @default   = opts[:default] || nil
  @desc      = opts[:desc] || "Lazy option authors have failed you."
  @long      = opts[:long]
  @opts      = opts #for #cast
  @required  = opts[:required] || false
  @satisfied = false
  @short     = opts[:short]
  @valid     = opts[:valid]
end

Instance Method Details

#castObject



18
19
20
# File 'lib/flagabowski.rb', line 18

def cast
  @cast ||= @opts[:cast] || @default.class
end

#defaultObject



22
23
24
# File 'lib/flagabowski.rb', line 22

def default
  @default
end

#descObject



26
27
28
# File 'lib/flagabowski.rb', line 26

def desc
  @desc
end

#longObject



30
31
32
# File 'lib/flagabowski.rb', line 30

def long
  @long || name
end

#long_argObject



34
35
36
# File 'lib/flagabowski.rb', line 34

def long_arg
  %Q{--#{long}#{cast_arg}}
end

#nameObject



38
39
40
# File 'lib/flagabowski.rb', line 38

def name
  @name.to_s
end

#satisfied!Object



42
43
44
# File 'lib/flagabowski.rb', line 42

def satisfied!
  @satisfied = true
end

#satisfied?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/flagabowski.rb', line 46

def satisfied?
  @satisfied
end

#shortObject



54
55
56
# File 'lib/flagabowski.rb', line 54

def short
  @short ||= namechars.first
end

#short_argObject



58
59
60
# File 'lib/flagabowski.rb', line 58

def short_arg
  %Q{-#{short}#{cast_arg}}
end

#short_try_againObject



62
63
64
65
# File 'lib/flagabowski.rb', line 62

def short_try_again
  # perfect is the enemy of good
  @short = namechars[namechars.index(short)+1]
end

#unsatisfied?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/flagabowski.rb', line 50

def unsatisfied?
  @required ? ! @satisfied : false
end

#validate!(thing) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/flagabowski.rb', line 67

def validate!(thing)
  return true if @valid.nil?

  case @valid
  when Array
    @valid.include?(thing)
  when Regexp
    @valid.match?(thing)
  else
    false
  end
end