Class: Climate::Option

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, options = {}) ⇒ Option

Returns a new instance of Option.



8
9
10
11
12
# File 'lib/climate/option.rb', line 8

def initialize(name, description, options={})
  @name        = name
  @description = description
  @options     = options
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/climate/option.rb', line 5

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/climate/option.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/climate/option.rb', line 6

def options
  @options
end

Instance Method Details

#add_to(parser) ⇒ Object



57
58
59
# File 'lib/climate/option.rb', line 57

def add_to(parser)
  parser.opt(@name, @description, @options)
end

#defaultObject



17
# File 'lib/climate/option.rb', line 17

def default ; spec[:default] ; end

#longObject



15
# File 'lib/climate/option.rb', line 15

def long    ; spec[:long]    ; end

#optional?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/climate/option.rb', line 19

def optional?
  spec.has_key?(:default)
end

#parserObject



25
26
27
# File 'lib/climate/option.rb', line 25

def parser
  @parser ||= Trollop::Parser.new.tap {|p| add_to(p) }
end

#required?Boolean

Returns:

  • (Boolean)


23
# File 'lib/climate/option.rb', line 23

def required? ; ! optional? ; end

#shortObject



16
# File 'lib/climate/option.rb', line 16

def short   ; spec[:short]   ; end

#specObject



29
30
31
# File 'lib/climate/option.rb', line 29

def spec
  @specs ||= parser.specs[@name]
end

#typeObject



14
# File 'lib/climate/option.rb', line 14

def type    ; spec[:type]    ; end

#usage(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/climate/option.rb', line 33

def usage(options={})
  help =
    if type == :flag
      "-#{short}"
    else
      "-#{short}<#{type}>"
    end

  if options[:with_long]
    help = help + options.fetch(:separator, '|') +
      if type == :flag
        "--#{long}"
      else
        "--#{long}=<#{type}>"
      end
  end

  if optional? && !options.fetch(:hide_optional, false)
    "[#{help}]"
  else
    help
  end
end