lib/thor/parser/option.rb in thor-0.18.1 vs lib/thor/parser/option.rb in thor-0.19.0

- old
+ new

@@ -2,11 +2,11 @@ class Option < Argument #:nodoc: attr_reader :aliases, :group, :lazy_default, :hide VALID_TYPES = [:boolean, :numeric, :hash, :array, :string] - def initialize(name, options={}) + def initialize(name, options = {}) options[:required] = false unless options.key?(:required) super @lazy_default = options[:lazy_default] @group = options[:group].to_s.capitalize if options[:group] @aliases = Array(options[:aliases]) @@ -38,55 +38,59 @@ # is given a default type is assumed. This default type accepts arguments as # string (--foo=value) or booleans (just --foo). # # By default all options are optional, unless :required is given. # - def self.parse(key, value) + def self.parse(key, value) # rubocop:disable MethodLength if key.is_a?(Array) name, *aliases = key else name, aliases = key, [] end name = name.to_s default = value type = case value - when Symbol - default = nil - if VALID_TYPES.include?(value) - value - elsif required = (value == :required) - :string - end - when TrueClass, FalseClass - :boolean - when Numeric - :numeric - when Hash, Array, String - value.class.name.downcase.to_sym - end - self.new(name.to_s, :required => required, :type => type, :default => default, :aliases => aliases) + when Symbol + default = nil + if VALID_TYPES.include?(value) + value + elsif required = (value == :required) # rubocop:disable AssignmentInCondition + :string + end + when TrueClass, FalseClass + :boolean + when Numeric + :numeric + when Hash, Array, String + value.class.name.downcase.to_sym + end + new(name.to_s, :required => required, :type => type, :default => default, :aliases => aliases) end def switch_name @switch_name ||= dasherized? ? name : dasherize(name) end def human_name @human_name ||= dasherized? ? undasherize(name) : name end - def usage(padding=0) + def usage(padding = 0) sample = if banner && !banner.to_s.empty? "#{switch_name}=#{banner}" else switch_name end sample = "[#{sample}]" unless required? + if boolean? + sample << ", [#{dasherize("no-" + human_name)}]" unless name == "force" + end + if aliases.empty? (" " * padding) << sample else "#{aliases.join(', ')}, #{sample}" end @@ -101,21 +105,21 @@ end protected def validate! - raise ArgumentError, "An option cannot be boolean and required." if boolean? && required? + fail ArgumentError, "An option cannot be boolean and required." if boolean? && required? end def dasherized? - name.index('-') == 0 + name.index("-") == 0 end def undasherize(str) - str.sub(/^-{1,2}/, '') + str.sub(/^-{1,2}/, "") end def dasherize(str) - (str.length > 1 ? "--" : "-") + str.gsub('_', '-') + (str.length > 1 ? "--" : "-") + str.gsub("_", "-") end end end