class Thor::Options

Constants

EQ_RE
LONG_RE

Constants

OPTS_END

The “bare double-dash” used to indicate that following arguments should not be parsed for options.

@return [String]

SHORT_NUM

Matches things like `'-x123'`.

@return [Regexp]

SHORT_RE
SHORT_SQ_RE

Matches “multiple short switches”, like `-xv`.

@return [Regexp]

Public Class Methods

new(options_to_parse_by_name = {}) click to toggle source

Takes a hash of Thor::Option and a hash with defaults.

If stop_on_unknown is true, parse will stop as soon as it encounters an unknown option or a regular argument.

@param [Hash<Symbol, Thor::Option>] hash_options

Calls superclass method Thor::Arguments::new
# File lib/thor/parser/options.rb, line 68
def initialize  options_to_parse_by_name = {},
                option_default_values = {},
                stop_on_unknown = false,
                disable_required_check = false
  @stop_on_unknown = stop_on_unknown
  @disable_required_check = disable_required_check

  options = options_to_parse_by_name.values
  super( options )

  # Add defaults
  option_default_values.each do |option_name, value|
    @assigns[ option_name.to_s ] = value

    @non_assigned_required.delete \
      options_to_parse_by_name[ option_name ]
  end

  @shorts = {}
  @switches = {}
  @extra = []

  options.each do |option|
    @switches[option.switch_name] = option

    option.aliases.each do |short|
      name = short.to_s.sub(/^(?!\-)/, "-")
      @shorts[name] ||= option.switch_name
    end
  end
end
to_switches(options) click to toggle source

Receives a hash and makes it switches.

# File lib/thor/parser/options.rb, line 39
def self.to_switches(options)
  options.map do |key, value|
    case value
    when true
      "--#{key}"
    when Array
      "--#{key} #{value.map(&:inspect).join(' ')}"
    when Hash
      "--#{key} #{value.map { |k, v| "#{k}:#{v}" }.join(' ')}"
    when nil, false
      nil
    else
      "--#{key} #{value.inspect}"
    end
  end.compact.join(" ")
end

Public Instance Methods

check_unknown!() click to toggle source
# File lib/thor/parser/options.rb, line 201
def check_unknown!
  # an unknown option starts with - or -- and has no more --'s afterward.
  unknown = @extra.select { |str| str =~ /^--?(?:(?!--).)*$/ }
  unless unknown.empty?
    raise UnknownArgumentError, "Unknown switches '#{unknown.join(', ')}'"
  end
end
parse(args) click to toggle source
# File lib/thor/parser/options.rb, line 145
def parse args # rubocop:disable MethodLength
  logger.debug __method__.to_s,
    args: args
  
  @pile = args.dup
  @parsing_options = true

  while peek
    if parsing_options?
      match, is_switch = current_is_switch?
      shifted = shift

      if is_switch
        case shifted
        when SHORT_SQ_RE
          unshift($1.split("").map { |f| "-#{f}" })
          next
        when EQ_RE, SHORT_NUM
          unshift $2
          raw_switch_arg = $1
        when LONG_RE, SHORT_RE
          raw_switch_arg = $1
        end

        switch = normalize_switch raw_switch_arg
        option = switch_option switch
        @assigns[option.human_name] = parse_peek switch, option
      elsif @stop_on_unknown
        @parsing_options = false
        @extra << shifted
        @extra << shift while peek
        break
      elsif match
        @extra << shifted
        @extra << shift while peek && peek !~ /^-/
      else
        @extra << shifted
      end
    else
      @extra << shift
    end
  end

  check_requirement! unless @disable_required_check

  assigns = Thor::CoreExt::HashWithIndifferentAccess.new(@assigns)
  assigns.freeze
  
  logger.debug "#{ __method__ } done",
    assigns: assigns,
    remaining: remaining
  
  assigns
end
peek() click to toggle source

What's next?! I think.

@note

This *used to* remove `--` separators (what {OPTS_END} is),
but that was problematic with multiple nested subcommands
'cause Thor classes further down the chain wouldn't know that
it was there and would parse options that had been after it.

Maybe that's how Thor was supposed to work (???), but it didn't really
jive with me... I've always felt like stuff after `--` meant
**_stop parsing options - these are always args_** since I usually
see it used when passing shell commands to other shell commands -
which is how I was using it when I came across the issues.

And it ain't like Thor has any documentation to straiten it out. Hell,
this method had no doc when I showed up. The line that dropped the `--`
has no comment. The {Thor::Options} class itself had no doc.

So, now it *does* mean that... `--` means "no option parsing after
here". For real.
Calls superclass method
# File lib/thor/parser/options.rb, line 130
def peek
  return super unless @parsing_options

  result = super
  if result == OPTS_END
    # Removed, see note above:
    # shift
    @parsing_options = false
    super
  else
    result
  end
end
remaining() click to toggle source

Instance Methods

# File lib/thor/parser/options.rb, line 104
def remaining
  @extra
end