Class: Choosy::Option

Inherits:
Argument show all
Defined in:
lib/choosy/option.rb

Constant Summary

Constants inherited from Argument

Argument::MANY_ARITY, Argument::ONE_ARITY, Argument::ZERO_ARITY

Instance Attribute Summary collapse

Attributes inherited from Argument

#allowable_values, #arity, #cast_to, #metaname, #validation_step

Instance Method Summary collapse

Methods inherited from Argument

#boolean!, #boolean?, #multiple!, #multiple?, #required=, #required?, #restricted?, #single!, #single?

Constructor Details

#initialize(name) ⇒ Option

Returns a new instance of Option.



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

def initialize(name)
  super()
  @name = name
end

Instance Attribute Details

#default_valueObject

Returns the value of attribute default_value.



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

def default_value
  @default_value
end

#dependent_optionsObject

Returns the value of attribute dependent_options.



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

def dependent_options
  @dependent_options
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/choosy/option.rb', line 3

def description
  @description
end

#long_flagObject

Returns the value of attribute long_flag.



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

def long_flag
  @long_flag
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/choosy/option.rb', line 3

def name
  @name
end

#negationObject

Returns the value of attribute negation.



7
8
9
# File 'lib/choosy/option.rb', line 7

def negation
  @negation
end

#short_flagObject

Returns the value of attribute short_flag.



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

def short_flag
  @short_flag
end

Instance Method Details

#finalize!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/choosy/option.rb', line 22

def finalize!
  @arity ||= ZERO_ARITY
  @cast_to ||= boolean? ? :boolean : :string

  if boolean?
    if restricted?
      raise Choosy::ConfigurationError.new("Options cannot be both boolean and restricted to certain arguments: #{@name}")
    elsif negated? && @long_flag.nil?
      raise Choosy::ConfigurationError.new("The long flag is required for negation: #{@name}")
    end
    @default_value ||= false
  else
    if negated?
      raise Choosy::ConfigurationError.new("Unable to negate a non-boolean option: #{@name}")
    end
  end
end

#negatedObject



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

def negated
  @negated ||= long_flag.gsub(/^--/, "--#{negation}-")
end

#negated?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/choosy/option.rb', line 14

def negated?
  !negation.nil?
end