lib/autoproj/build_option.rb in autoproj-2.14.0 vs lib/autoproj/build_option.rb in autoproj-2.15.0

- old
+ new

@@ -6,28 +6,32 @@ attr_reader :type attr_reader :options attr_reader :validator - TRUE_STRINGS = %w{on yes y true} - FALSE_STRINGS = %w{off no n false} + TRUE_STRINGS = %w[on yes y true] + FALSE_STRINGS = %w[off no n false] def initialize(name, type, options, validator) - @name, @type, @options = name.to_str, type.to_str, options.to_hash + @name = name.to_str + @type = type.to_str + @options = options.to_hash @validator = validator.to_proc if validator - if !BuildOption.respond_to?("validate_#{type}") + unless BuildOption.respond_to?("validate_#{type}") raise ConfigError.new, "invalid option type #{type}" end end def short_doc - if short_doc = options[:short_doc] + if (short_doc = options[:short_doc]) short_doc - elsif doc = options[:doc] + elsif (doc = options[:doc]) if doc.respond_to?(:to_ary) then doc.first - else doc + else + doc end - else "#{name} (no documentation for this option)" + else + "#{name} (no documentation for this option)" end end def doc doc = (options[:doc] || "#{name} (no documentation for this option)") @@ -36,11 +40,11 @@ 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 + "#{Autoproj.color(first_line, :bold)}\n#{remaining}" end else doc end end @@ -50,70 +54,64 @@ # # @return [value, Boolean] Current value, and flag whether this is a # default value def ensure_value(current_value) if !current_value.nil? - return current_value.to_s, false + [current_value.to_s, false] elsif options[:default] - return options[:default].to_str, true + [options[:default].to_str, true] else - return '', true + ["", true] end end # Ask the user for the setting of this option # by providing the current value as input and falling # back to default values if needed # # @param [String] current_value the option's current value # @param [String] doc a string to override the default option banner def ask(current_value, doc = nil) - value,_ = ensure_value(current_value) + value, = ensure_value(current_value) STDOUT.print " #{doc || self.doc} [#{value}] " STDOUT.flush answer = STDIN.readline.chomp - if answer == '' - answer = value - end + answer = value if answer == "" validate(answer) - rescue InputError => e Autoproj.message("invalid value: #{e.message}", :red) retry end def validate(value) value = BuildOption.send("validate_#{type}", value, options) - if validator - value = validator[value] - end + value = validator[value] if validator value end 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" + raise InputError, "invalid boolean value '#{value}', accepted values are '#{TRUE_STRINGS.join(', ')}' for true, and '#{FALSE_STRINGS.join(', ')} for false" end end def self.validate_string(value, options) - if possible_values = options[:possible_values] + if (possible_values = options[:possible_values]) if options[:lowercase] value = value.downcase elsif options[:uppercase] value = value.upcase end - if !possible_values.include?(value) + unless possible_values.include?(value) raise InputError, "invalid value '#{value}', accepted values are '#{possible_values.join("', '")}' (without the quotes)" end end value end end end -