Class: Bundler::Thor::Options

Inherits:
Arguments show all
Defined in:
lib/bundler/vendor/thor/lib/thor/parser/options.rb

Overview

:nodoc: # rubocop:disable ClassLength

Constant Summary collapse

LONG_RE =
/^(--\w+(?:-\w+)*)$/
SHORT_RE =
/^(-[a-z])$/i
EQ_RE =
/^(--\w+(?:-\w+)*|-[a-z])=(.*)$/i
SHORT_SQ_RE =

Allow either -x -v or -xv style for single char args

/^-([a-z]{2,})$/i
SHORT_NUM =
/^(-[a-z])#{NUMERIC}$/i
OPTS_END =
"--".freeze

Constants inherited from Arguments

Arguments::NUMERIC

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Arguments

parse, split

Constructor Details

#initialize(hash_options = {}, defaults = {}, stop_on_unknown = false, disable_required_check = false) ⇒ Options

Takes a hash of Bundler::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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bundler/vendor/thor/lib/thor/parser/options.rb', line 32

def initialize(hash_options = {}, defaults = {}, stop_on_unknown = false, disable_required_check = false)
  @stop_on_unknown = stop_on_unknown
  @disable_required_check = disable_required_check
  options = hash_options.values
  super(options)

  # Add defaults
  defaults.each do |key, value|
    @assigns[key.to_s] = value
    @non_assigned_required.delete(hash_options[key])
  end

  @shorts = {}
  @switches = {}
  @extra = []
  @stopped_parsing_after_extra_index = nil
  @is_treated_as_value = false

  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

Class Method Details

.to_switches(options) ⇒ Object

Receives a hash and makes it switches.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bundler/vendor/thor/lib/thor/parser/options.rb', line 11

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

Instance Method Details

#check_unknown!Object



141
142
143
144
145
146
147
# File 'lib/bundler/vendor/thor/lib/thor/parser/options.rb', line 141

def check_unknown!
  to_check = @stopped_parsing_after_extra_index ? @extra[0...@stopped_parsing_after_extra_index] : @extra

  # an unknown option starts with - or -- and has no more --'s afterward.
  unknown = to_check.select { |str| str =~ /^--?(?:(?!--).)*$/ }
  raise UnknownArgumentError.new(@switches.keys, unknown) unless unknown.empty?
end

#parse(args) ⇒ Object

rubocop:disable MethodLength



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/bundler/vendor/thor/lib/thor/parser/options.rb', line 88

def parse(args) # rubocop:disable MethodLength
  @pile = args.dup
  @is_treated_as_value = false
  @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
          unshift($2, is_value: true)
          switch = $1
        when SHORT_NUM
          unshift($2)
          switch = $1
        when LONG_RE, SHORT_RE
          switch = $1
        end

        switch = normalize_switch(switch)
        option = switch_option(switch)
        result = parse_peek(switch, option)
        assign_result!(option, result)
      elsif @stop_on_unknown
        @parsing_options = false
        @extra << shifted
        @stopped_parsing_after_extra_index ||= @extra.size
        @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 = Bundler::Thor::CoreExt::HashWithIndifferentAccess.new(@assigns)
  assigns.freeze
  assigns
end

#peekObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bundler/vendor/thor/lib/thor/parser/options.rb', line 64

def peek
  return super unless @parsing_options

  result = super
  if result == OPTS_END
    shift
    @parsing_options = false
    @stopped_parsing_after_extra_index ||= @extra.size
    super
  else
    result
  end
end

#remainingObject



60
61
62
# File 'lib/bundler/vendor/thor/lib/thor/parser/options.rb', line 60

def remaining
  @extra
end

#shiftObject



78
79
80
81
# File 'lib/bundler/vendor/thor/lib/thor/parser/options.rb', line 78

def shift
  @is_treated_as_value = false
  super
end

#unshift(arg, is_value: false) ⇒ Object



83
84
85
86
# File 'lib/bundler/vendor/thor/lib/thor/parser/options.rb', line 83

def unshift(arg, is_value: false)
  @is_treated_as_value = is_value
  super(arg)
end