aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/vendor/thor-0.11.3/lib/thor/parser
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/vendor/thor-0.11.3/lib/thor/parser')
-rw-r--r--railties/lib/vendor/thor-0.11.3/lib/thor/parser/argument.rb67
-rw-r--r--railties/lib/vendor/thor-0.11.3/lib/thor/parser/arguments.rb145
-rw-r--r--railties/lib/vendor/thor-0.11.3/lib/thor/parser/option.rb132
-rw-r--r--railties/lib/vendor/thor-0.11.3/lib/thor/parser/options.rb142
4 files changed, 486 insertions, 0 deletions
diff --git a/railties/lib/vendor/thor-0.11.3/lib/thor/parser/argument.rb b/railties/lib/vendor/thor-0.11.3/lib/thor/parser/argument.rb
new file mode 100644
index 0000000000..2d7f4dbafb
--- /dev/null
+++ b/railties/lib/vendor/thor-0.11.3/lib/thor/parser/argument.rb
@@ -0,0 +1,67 @@
+class Thor
+ class Argument
+ VALID_TYPES = [ :numeric, :hash, :array, :string ]
+
+ attr_reader :name, :description, :required, :type, :default, :banner
+ alias :human_name :name
+
+ def initialize(name, description=nil, required=true, type=:string, default=nil, banner=nil)
+ class_name = self.class.name.split("::").last
+
+ raise ArgumentError, "#{class_name} name can't be nil." if name.nil?
+ raise ArgumentError, "Type :#{type} is not valid for #{class_name.downcase}s." if type && !valid_type?(type)
+
+ @name = name.to_s
+ @description = description
+ @required = required || false
+ @type = (type || :string).to_sym
+ @default = default
+ @banner = banner || default_banner
+
+ validate! # Trigger specific validations
+ end
+
+ def usage
+ required? ? banner : "[#{banner}]"
+ end
+
+ def required?
+ required
+ end
+
+ def show_default?
+ case default
+ when Array, String, Hash
+ !default.empty?
+ else
+ default
+ end
+ end
+
+ protected
+
+ def validate!
+ raise ArgumentError, "An argument cannot be required and have default value." if required? && !default.nil?
+ end
+
+ def valid_type?(type)
+ VALID_TYPES.include?(type.to_sym)
+ end
+
+ def default_banner
+ case type
+ when :boolean
+ nil
+ when :string, :default
+ human_name.upcase
+ when :numeric
+ "N"
+ when :hash
+ "key:value"
+ when :array
+ "one two three"
+ end
+ end
+
+ end
+end
diff --git a/railties/lib/vendor/thor-0.11.3/lib/thor/parser/arguments.rb b/railties/lib/vendor/thor-0.11.3/lib/thor/parser/arguments.rb
new file mode 100644
index 0000000000..9a2262d6f7
--- /dev/null
+++ b/railties/lib/vendor/thor-0.11.3/lib/thor/parser/arguments.rb
@@ -0,0 +1,145 @@
+class Thor
+ class Arguments
+ NUMERIC = /(\d*\.\d+|\d+)/
+
+ # Receives an array of args and returns two arrays, one with arguments
+ # and one with switches.
+ #
+ def self.split(args)
+ arguments = []
+
+ args.each do |item|
+ break if item =~ /^-/
+ arguments << item
+ end
+
+ return arguments, args[Range.new(arguments.size, -1)]
+ end
+
+ def self.parse(base, args)
+ new(base).parse(args)
+ end
+
+ # Takes an array of Thor::Argument objects.
+ #
+ def initialize(arguments=[])
+ @assigns, @non_assigned_required = {}, []
+ @switches = arguments
+
+ arguments.each do |argument|
+ if argument.default
+ @assigns[argument.human_name] = argument.default
+ elsif argument.required?
+ @non_assigned_required << argument
+ end
+ end
+ end
+
+ def parse(args)
+ @pile = args.dup
+
+ @switches.each do |argument|
+ break unless peek
+ @non_assigned_required.delete(argument)
+ @assigns[argument.human_name] = send(:"parse_#{argument.type}", argument.human_name)
+ end
+
+ check_requirement!
+ @assigns
+ end
+
+ private
+
+ def peek
+ @pile.first
+ end
+
+ def shift
+ @pile.shift
+ end
+
+ def unshift(arg)
+ unless arg.kind_of?(Array)
+ @pile.unshift(arg)
+ else
+ @pile = arg + @pile
+ end
+ end
+
+ def current_is_value?
+ peek && peek.to_s !~ /^-/
+ end
+
+ # Runs through the argument array getting strings that contains ":" and
+ # mark it as a hash:
+ #
+ # [ "name:string", "age:integer" ]
+ #
+ # Becomes:
+ #
+ # { "name" => "string", "age" => "integer" }
+ #
+ def parse_hash(name)
+ return shift if peek.is_a?(Hash)
+ hash = {}
+
+ while current_is_value? && peek.include?(?:)
+ key, value = shift.split(':')
+ hash[key] = value
+ end
+ hash
+ end
+
+ # Runs through the argument array getting all strings until no string is
+ # found or a switch is found.
+ #
+ # ["a", "b", "c"]
+ #
+ # And returns it as an array:
+ #
+ # ["a", "b", "c"]
+ #
+ def parse_array(name)
+ return shift if peek.is_a?(Array)
+ array = []
+
+ while current_is_value?
+ array << shift
+ end
+ array
+ end
+
+ # Check if the peel is numeric ofrmat and return a Float or Integer.
+ # Otherwise raises an error.
+ #
+ def parse_numeric(name)
+ return shift if peek.is_a?(Numeric)
+
+ unless peek =~ NUMERIC && $& == peek
+ raise MalformattedArgumentError, "expected numeric value for '#{name}'; got #{peek.inspect}"
+ end
+
+ $&.index('.') ? shift.to_f : shift.to_i
+ end
+
+ # Parse string, i.e., just return the current value in the pile.
+ #
+ def parse_string(name)
+ shift
+ end
+
+ # Raises an error if @non_assigned_required array is not empty.
+ #
+ def check_requirement!
+ unless @non_assigned_required.empty?
+ names = @non_assigned_required.map do |o|
+ o.respond_to?(:switch_name) ? o.switch_name : o.human_name
+ end.join("', '")
+
+ class_name = self.class.name.split('::').last.downcase
+ raise RequiredArgumentMissingError, "no value provided for required #{class_name} '#{names}'"
+ end
+ end
+
+ end
+end
diff --git a/railties/lib/vendor/thor-0.11.3/lib/thor/parser/option.rb b/railties/lib/vendor/thor-0.11.3/lib/thor/parser/option.rb
new file mode 100644
index 0000000000..5c43f6b18f
--- /dev/null
+++ b/railties/lib/vendor/thor-0.11.3/lib/thor/parser/option.rb
@@ -0,0 +1,132 @@
+class Thor
+ class Option < Argument
+ attr_reader :aliases, :group
+
+ VALID_TYPES = [:boolean, :numeric, :hash, :array, :string]
+
+ def initialize(name, description=nil, required=nil, type=nil, default=nil, banner=nil, group=nil, aliases=nil)
+ super(name, description, required, type, default, banner)
+ @aliases = [*aliases].compact
+ @group = group.to_s.capitalize if group
+ end
+
+ # This parse quick options given as method_options. It makes several
+ # assumptions, but you can be more specific using the option method.
+ #
+ # parse :foo => "bar"
+ # #=> Option foo with default value bar
+ #
+ # parse [:foo, :baz] => "bar"
+ # #=> Option foo with default value bar and alias :baz
+ #
+ # parse :foo => :required
+ # #=> Required option foo without default value
+ #
+ # parse :foo => 2
+ # #=> Option foo with default value 2 and type numeric
+ #
+ # parse :foo => :numeric
+ # #=> Option foo without default value and type numeric
+ #
+ # parse :foo => true
+ # #=> Option foo with default value true and type boolean
+ #
+ # The valid types are :boolean, :numeric, :hash, :array and :string. If none
+ # 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)
+ 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
+ elsif value == :optional
+ # TODO Remove this warning in the future.
+ warn "Optional type is deprecated. Choose :boolean or :string instead. Assumed to be :boolean."
+ :boolean
+ end
+ when TrueClass, FalseClass
+ :boolean
+ when Numeric
+ :numeric
+ when Hash, Array, String
+ value.class.name.downcase.to_sym
+ end
+
+ self.new(name.to_s, nil, required, type, default, nil, nil, 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)
+ sample = if banner && !banner.to_s.empty?
+ "#{switch_name}=#{banner}"
+ else
+ switch_name
+ end
+
+ sample = "[#{sample}]" unless required?
+
+ if aliases.empty?
+ (" " * padding) << sample
+ else
+ "#{aliases.join(', ')}, #{sample}"
+ end
+ end
+
+ # Allow some type predicates as: boolean?, string? and etc.
+ #
+ def method_missing(method, *args, &block)
+ given = method.to_s.sub(/\?$/, '').to_sym
+ if valid_type?(given)
+ self.type == given
+ else
+ super
+ end
+ end
+
+ protected
+
+ def validate!
+ raise ArgumentError, "An option cannot be boolean and required." if boolean? && required?
+ end
+
+ def valid_type?(type)
+ VALID_TYPES.include?(type.to_sym)
+ end
+
+ def dasherized?
+ name.index('-') == 0
+ end
+
+ def undasherize(str)
+ str.sub(/^-{1,2}/, '')
+ end
+
+ def dasherize(str)
+ (str.length > 1 ? "--" : "-") + str.gsub('_', '-')
+ end
+
+ end
+end
diff --git a/railties/lib/vendor/thor-0.11.3/lib/thor/parser/options.rb b/railties/lib/vendor/thor-0.11.3/lib/thor/parser/options.rb
new file mode 100644
index 0000000000..01c86b7b27
--- /dev/null
+++ b/railties/lib/vendor/thor-0.11.3/lib/thor/parser/options.rb
@@ -0,0 +1,142 @@
+class Thor
+ # This is a modified version of Daniel Berger's Getopt::Long class, licensed
+ # under Ruby's license.
+ #
+ class Options < Arguments
+ LONG_RE = /^(--\w+[-\w+]*)$/
+ SHORT_RE = /^(-[a-z])$/i
+ EQ_RE = /^(--\w+[-\w+]*|-[a-z])=(.*)$/i
+ SHORT_SQ_RE = /^-([a-z]{2,})$/i # Allow either -x -v or -xv style for single char args
+ SHORT_NUM = /^(-[a-z])#{NUMERIC}$/i
+
+ # Receives a hash and makes it switches.
+ #
+ def self.to_switches(options)
+ options.map do |key, value|
+ case value
+ when true
+ "--#{key}"
+ when Array
+ "--#{key} #{value.map{ |v| v.inspect }.join(' ')}"
+ when Hash
+ "--#{key} #{value.map{ |k,v| "#{k}:#{v}" }.join(' ')}"
+ when nil, false
+ ""
+ else
+ "--#{key} #{value.inspect}"
+ end
+ end.join(" ")
+ end
+
+ # Takes a hash of Thor::Option objects.
+ #
+ def initialize(options={})
+ options = options.values
+ super(options)
+ @shorts, @switches = {}, {}
+
+ options.each do |option|
+ @switches[option.switch_name] = option
+
+ option.aliases.each do |short|
+ @shorts[short.to_s] ||= option.switch_name
+ end
+ end
+ end
+
+ def parse(args)
+ @pile = args.dup
+
+ while peek
+ if current_is_switch?
+ case shift
+ when SHORT_SQ_RE
+ unshift($1.split('').map { |f| "-#{f}" })
+ next
+ when EQ_RE, SHORT_NUM
+ unshift($2)
+ switch = $1
+ when LONG_RE, SHORT_RE
+ switch = $1
+ end
+
+ switch = normalize_switch(switch)
+ next unless option = switch_option(switch)
+
+ @assigns[option.human_name] = parse_peek(switch, option)
+ else
+ shift
+ end
+ end
+
+ check_requirement!
+ @assigns
+ end
+
+ protected
+
+ # Returns true if the current value in peek is a registered switch.
+ #
+ def current_is_switch?
+ case peek
+ when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM
+ switch?($1)
+ when SHORT_SQ_RE
+ $1.split('').any? { |f| switch?("-#{f}") }
+ end
+ end
+
+ def switch?(arg)
+ switch_option(arg) || @shorts.key?(arg)
+ end
+
+ def switch_option(arg)
+ if match = no_or_skip?(arg)
+ @switches[arg] || @switches["--#{match}"]
+ else
+ @switches[arg]
+ end
+ end
+
+ def no_or_skip?(arg)
+ arg =~ /^--(no|skip)-([-\w]+)$/
+ $2
+ end
+
+ # Check if the given argument is actually a shortcut.
+ #
+ def normalize_switch(arg)
+ @shorts.key?(arg) ? @shorts[arg] : arg
+ end
+
+ # Parse boolean values which can be given as --foo=true, --foo or --no-foo.
+ #
+ def parse_boolean(switch)
+ if current_is_value?
+ ["true", "TRUE", "t", "T", true].include?(shift)
+ else
+ @switches.key?(switch) || !no_or_skip?(switch)
+ end
+ end
+
+ # Parse the value at the peek analyzing if it requires an input or not.
+ #
+ def parse_peek(switch, option)
+ unless current_is_value?
+ if option.boolean?
+ # No problem for boolean types
+ elsif no_or_skip?(switch)
+ return nil # User set value to nil
+ elsif option.string? && !option.required?
+ return option.human_name # Return the option name
+ else
+ raise MalformattedArgumentError, "no value provided for option '#{switch}'"
+ end
+ end
+
+ @non_assigned_required.delete(option)
+ send(:"parse_#{option.type}", switch)
+ end
+
+ end
+end