Class: CLI::Kit::Args::Evaluation

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/kit/args/evaluation.rb

Defined Under Namespace

Classes: FlagProxy, MissingRequiredOption, MissingRequiredPosition, OptionProxy, PositionProxy, TooManyPositions

Constant Summary collapse

Error =
Class.new(Args::Error)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defn, parse) ⇒ Evaluation

: (Definition defn, Array parse) -> void



128
129
130
131
132
# File 'lib/cli/kit/args/evaluation.rb', line 128

def initialize(defn, parse)
  @defn = defn
  @parse = parse
  check_required_options!
end

Instance Attribute Details

#defnObject (readonly)

: Definition



114
115
116
# File 'lib/cli/kit/args/evaluation.rb', line 114

def defn
  @defn
end

#parseObject (readonly)

: Array



117
118
119
# File 'lib/cli/kit/args/evaluation.rb', line 117

def parse
  @parse
end

Instance Method Details

#check_required_options!Object

: -> void



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cli/kit/args/evaluation.rb', line 135

def check_required_options!
  @defn.options.each do |opt|
    next unless opt.required?

    node = @parse.detect do |node|
      node.is_a?(Parser::Node::Option) && node.name.to_sym == opt.name
    end
    unless node
      raise(MissingRequiredOption, opt.as_written_by_user)
    end

    node = node #: as Parser::Node::Option
    if node.value.nil?
      raise(MissingRequiredOption, opt.as_written_by_user)
    end
  end
end

#flagObject

: -> FlagProxy



99
100
101
# File 'lib/cli/kit/args/evaluation.rb', line 99

def flag
  @flag_proxy ||= FlagProxy.new(self)
end

#lookup_flag(flag) ⇒ Object

: (Definition::Flag flag) -> bool



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/cli/kit/args/evaluation.rb', line 175

def lookup_flag(flag)
  if flag.short
    flags = parse.select { |node| node.is_a?(Parser::Node::ShortFlag) } #: as Array[Parser::Node::ShortFlag]
    return true if flags.any? { |node| node.value == flag.short }
  end
  if flag.long
    flags = parse.select { |node| node.is_a?(Parser::Node::LongFlag) } #: as Array[Parser::Node::LongFlag]
    return true if flags.any? { |node| node.value == flag.long }
  end
  false
end

#lookup_option(opt) ⇒ Object

: (Definition::Option opt) -> (String | Array)?



188
189
190
191
192
193
194
195
196
# File 'lib/cli/kit/args/evaluation.rb', line 188

def lookup_option(opt)
  opts = parse.select { |node| node.is_a?(Parser::Node::ShortOption) || node.is_a?(Parser::Node::LongOption) } #: as Array[Parser::Node::ShortOption | Parser::Node::LongOption]
  matches = opts.select { |node| (opt.short && node.name == opt.short) || (opt.long && node.name == opt.long) }
  if (last = matches.last)
    return (opt.multi? ? matches.map(&:value) : last.value)
  end

  opt.default
end

#lookup_position(position) ⇒ Object

: (Definition::Position position) -> (String | Array)?



199
200
201
# File 'lib/cli/kit/args/evaluation.rb', line 199

def lookup_position(position)
  @position_values.fetch(position.name) { position.multi? ? [] : position.default }
end

#optObject

: -> OptionProxy



104
105
106
# File 'lib/cli/kit/args/evaluation.rb', line 104

def opt
  @option_proxy ||= OptionProxy.new(self)
end

#positionObject

: -> PositionProxy



109
110
111
# File 'lib/cli/kit/args/evaluation.rb', line 109

def position
  @position_proxy ||= PositionProxy.new(self)
end

#resolve_positions!Object

: -> void

Raises:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/cli/kit/args/evaluation.rb', line 154

def resolve_positions!
  args_i = 0
  @position_values = Hash.new
  @defn.positions.each do |position|
    raise(MissingRequiredPosition) if position.required? && args_i >= args.size
    next if args_i >= args.size || position.skip?(
      args[args_i], #: as !nil
    )

    if position.multi?
      @position_values[position.name] = args[args_i..]
      args_i = args.size
    else
      @position_values[position.name] = args[args_i] #: as !nil
      args_i += 1
    end
  end
  raise(TooManyPositions) if args_i < args.size
end

#unparsedObject

: -> Array



120
121
122
123
124
125
# File 'lib/cli/kit/args/evaluation.rb', line 120

def unparsed
  @unparsed ||= begin
    nodes = parse.select { |node| node.is_a?(Parser::Node::Unparsed) } #: as Array[Parser::Node::Unparsed]
    nodes.flat_map(&:value)
  end
end