Class: Autoproj::BuildOption

Inherits:
Object
  • Object
show all
Defined in:
lib/autoproj/build_option.rb

Overview

Definition of an autoproj option as defined by Configuration#declare

Constant Summary collapse

TRUE_STRINGS =
%w[on yes y true]
FALSE_STRINGS =
%w[off no n false]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options, validator) ⇒ BuildOption

Returns a new instance of BuildOption.



13
14
15
16
17
18
19
20
21
# File 'lib/autoproj/build_option.rb', line 13

def initialize(name, type, options, validator)
    @name = name.to_str
    @type = type.to_str
    @options = options.to_hash
    @validator = validator.to_proc if validator
    unless BuildOption.respond_to?("validate_#{type}")
        raise ConfigError.new, "invalid option type #{type}"
    end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/autoproj/build_option.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/autoproj/build_option.rb', line 7

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/autoproj/build_option.rb', line 6

def type
  @type
end

#validatorObject (readonly)

Returns the value of attribute validator.



9
10
11
# File 'lib/autoproj/build_option.rb', line 9

def validator
  @validator
end

Class Method Details

.validate_boolean(value, options) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/autoproj/build_option.rb', line 92

def self.validate_boolean(value, options)
    if TRUE_STRINGS.include?(value.downcase)
        true
    elsif FALSE_STRINGS.include?(value.downcase)
        false
    else
        raise InputError, "invalid boolean value '#{value}', accepted values are '#{TRUE_STRINGS.join(', ')}' for true, and '#{FALSE_STRINGS.join(', ')} for false"
    end
end

.validate_string(value, options) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/autoproj/build_option.rb', line 102

def self.validate_string(value, options)
    if (possible_values = options[:possible_values])
        if options[:lowercase]
            value = value.downcase
        elsif options[:uppercase]
            value = value.upcase
        end

        unless possible_values.include?(value)
            raise InputError, "invalid value '#{value}', accepted values are '#{possible_values.join("', '")}' (without the quotes)"
        end
    end
    value
end

Instance Method Details

#ask(current_value, doc = nil) ⇒ Object

Ask the user for the setting of this option by providing the current value as input and falling back to default values if needed

Parameters:

  • current_value (String)

    the option’s current value

  • doc (String) (defaults to: nil)

    a string to override the default option banner



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/autoproj/build_option.rb', line 73

def ask(current_value, doc = nil)
    value, = ensure_value(current_value)

    STDOUT.print "  #{doc || self.doc} [#{value}] "
    STDOUT.flush
    answer = STDIN.readline.chomp
    answer = value if answer == ""
    validate(answer)
rescue InputError => e
    Autoproj.message("invalid value: #{e.message}", :red)
    retry
end

#docObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/autoproj/build_option.rb', line 36

def doc
    doc = (options[:doc] || "#{name} (no documentation for this option)")
    if doc.respond_to?(:to_ary) # multi-line
        first_line = doc[0]
        remaining = doc[1..-1]
        if remaining.empty?
            first_line
        else
            remaining = remaining.join("\n").split("\n").join("\n    ")
            "#{Autoproj.color(first_line, :bold)}\n#{remaining}"
        end
    else
        doc
    end
end

#ensure_value(current_value) ⇒ value, Boolean

Return either the current value if it is not nil, or use a default value

default value

Returns:

  • (value, Boolean)

    Current value, and flag whether this is a



57
58
59
60
61
62
63
64
65
# File 'lib/autoproj/build_option.rb', line 57

def ensure_value(current_value)
    if !current_value.nil?
        [current_value.to_s, false]
    elsif options[:default]
        [options[:default].to_str, true]
    else
        ["", true]
    end
end

#short_docObject



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

def short_doc
    if (short_doc = options[:short_doc])
        short_doc
    elsif (doc = options[:doc])
        if doc.respond_to?(:to_ary) then doc.first
        else
            doc
        end
    else
        "#{name} (no documentation for this option)"
    end
end

#validate(value) ⇒ Object



86
87
88
89
90
# File 'lib/autoproj/build_option.rb', line 86

def validate(value)
    value = BuildOption.send("validate_#{type}", value, options)
    value = validator[value] if validator
    value
end