Class: CP::Option

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) {|_self| ... } ⇒ Option

see: OptionsParser#make_switch

Yields:

  • (_self)

Yield Parameters:

  • _self (CP::Option)

    the object that the method was called on



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cp/option.rb', line 9

def initialize( *args )
  @required = false
  parsed = parse_args( args )

  yield self if block_given?

  # pick up any args we dropped the first time
  parse_args( self.to_option_parser_args + args ) unless parsed

  unless self.short || self.long
    raise ArgumentError.new( "no switch given" )
  end
end

Instance Attribute Details

#allowedObject

Returns the value of attribute allowed.



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

def allowed
  @allowed
end

#argObject (readonly)

Returns the value of attribute arg.



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

def arg
  @arg
end

#defaultObject

Returns the value of attribute default.



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

def default
  @default
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#longObject

Returns the value of attribute long.



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

def long
  @long
end

#shortObject

Returns the value of attribute short.



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

def short
  @short
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#action(&block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cp/option.rb', line 23

def action( &block )
  save_value = lambda { |value| @value = value }

  if !block_given?
    @action || save_value
  else
    @action = lambda { |value|
      save_value.call( value )
      block.call( *( block.arity === 0 ? [] : [value] ) ) if block
    }
  end
end

#action=(block) ⇒ Object



36
37
38
# File 'lib/cp/option.rb', line 36

def action=( block )
  action( &block )
end

#merge(opt) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/cp/option.rb', line 58

def merge( opt )
  new_opt = self.dup
  new_opt.action = @action
  [:allowed, :default, :description, :type, :short, :long].each { |attr|
    val = opt.send( :"#{attr}" )
    new_opt.send( :"#{attr}=", val.dup ) unless val.nil?
  }

  new_opt
end

#nameObject



49
50
51
52
53
54
55
56
# File 'lib/cp/option.rb', line 49

def name
  if self.long
    name = self.long.sub( /^-+(\[no-\])?/, '' )
  else
    name = self.short.sub( /^-/, '' ).sub( /\[(.-.)\]/, '\\1' )
  end
  name.gsub( "-", "_" ).to_sym
end

#required=(val) ⇒ Object



69
70
71
# File 'lib/cp/option.rb', line 69

def required=( val )
  @required = !!val
end

#required?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/cp/option.rb', line 73

def required?
  @required
end

#switchesObject



81
82
83
84
85
86
87
88
89
# File 'lib/cp/option.rb', line 81

def switches
  [self.short, self.long].inject( [] ) { |acc, s|
    unless s.nil?
      s += self.arg unless self.arg.nil?
      acc << s
    end
    acc
  }
end

#to_option_parser_argsObject



91
92
93
94
# File 'lib/cp/option.rb', line 91

def to_option_parser_args
  args = self.switches + [self.type, self.allowed, self.description, self.action]
  args.find_all { |a| !a.nil? }
end

#valueObject



96
97
98
# File 'lib/cp/option.rb', line 96

def value
  @value.nil? ? self.default : @value
end