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



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

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

#long_usageObject



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

def long_usage
  type == :flag ? "--#{long}" : "--#{long}=<#{type}>"
end

#optional?Boolean

Returns:

  • (Boolean)


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

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

#parserObject



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

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

#required?Boolean

Returns:

  • (Boolean)


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

def required? ; ! optional?             ; end

#shortObject



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

def short   ; spec[:short]   ; end

#short_usageObject



32
33
34
# File 'lib/climate/option.rb', line 32

def short_usage
  short && (type == :flag ? "-#{short}" : "-#{short}<#{type}>")
end

#specObject



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

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

#typeObject



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

def type    ; spec[:type]    ; end

#usage(options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/climate/option.rb', line 36

def usage(options={})
  help = short_usage || long_usage

  if options[:with_long] && (long_usage != help)
    help = [help, long_usage].compact.join(options.fetch(:separator, '|'))
  end

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