Class: CLI::Kit::Args::Evaluation
- Inherits:
-
Object
- Object
- CLI::Kit::Args::Evaluation
- 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
-
#defn ⇒ Object
readonly
: Definition.
-
#parse ⇒ Object
readonly
: Array.
Instance Method Summary collapse
-
#check_required_options! ⇒ Object
: -> void.
-
#flag ⇒ Object
: -> FlagProxy.
-
#initialize(defn, parse) ⇒ Evaluation
constructor
: (Definition defn, Array parse) -> void.
-
#lookup_flag(flag) ⇒ Object
: (Definition::Flag flag) -> bool.
-
#lookup_option(opt) ⇒ Object
: (Definition::Option opt) -> (String | Array)?.
-
#lookup_position(position) ⇒ Object
: (Definition::Position position) -> (String | Array)?.
-
#opt ⇒ Object
: -> OptionProxy.
-
#position ⇒ Object
: -> PositionProxy.
-
#resolve_positions! ⇒ Object
: -> void.
-
#unparsed ⇒ Object
: -> Array.
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 end |
Instance Attribute Details
#defn ⇒ Object (readonly)
: Definition
114 115 116 |
# File 'lib/cli/kit/args/evaluation.rb', line 114 def defn @defn end |
#parse ⇒ Object (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 @defn..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 |
#flag ⇒ Object
: -> 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 |
#opt ⇒ Object
: -> OptionProxy
104 105 106 |
# File 'lib/cli/kit/args/evaluation.rb', line 104 def opt @option_proxy ||= OptionProxy.new(self) end |
#position ⇒ Object
: -> 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
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 |
#unparsed ⇒ Object
: -> 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 |