Class: Climate::Option

Inherits:
Object
  • Object
show all
Includes:
Described
Defined in:
lib/climate/parser.rb

Overview

Wraps the properties supplied to Trollop::Parser#opt for some OO over engineered sweetness

Instance Attribute Summary collapse

Attributes included from Described

#name

Instance Method Summary collapse

Methods included from Described

#description

Constructor Details

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

Returns a new instance of Option.



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

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



21
22
23
# File 'lib/climate/parser.rb', line 21

def options
  @options
end

Instance Method Details

#add_to(parser) ⇒ Object



67
68
69
# File 'lib/climate/parser.rb', line 67

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

#defaultObject



31
# File 'lib/climate/parser.rb', line 31

def default ; spec[:default] ; end

#longObject



29
# File 'lib/climate/parser.rb', line 29

def long    ; spec[:long]    ; end

#long_usageObject



43
44
45
# File 'lib/climate/parser.rb', line 43

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

#multi?Boolean

Returns:

  • (Boolean)


35
# File 'lib/climate/parser.rb', line 35

def multi?    ; spec[:multi]            ; end

#optional?Boolean

Returns:

  • (Boolean)


33
# File 'lib/climate/parser.rb', line 33

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

#parserObject



39
40
41
# File 'lib/climate/parser.rb', line 39

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

#required?Boolean

Returns:

  • (Boolean)


34
# File 'lib/climate/parser.rb', line 34

def required? ; ! optional?             ; end

#shortObject



30
# File 'lib/climate/parser.rb', line 30

def short   ; spec[:short]   ; end

#short_usageObject



47
48
49
# File 'lib/climate/parser.rb', line 47

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

#specObject



37
# File 'lib/climate/parser.rb', line 37

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

#typeObject



28
# File 'lib/climate/parser.rb', line 28

def type    ; spec[:type]    ; end

#usage(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/climate/parser.rb', line 51

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}]"
  elsif multi? && !options.fetch(:hide_optional, false)
    "[#{help}...]"
  else
    help
  end
end