class CommandLine::Arguments

Constants

ALIASES_REGEXP
OPTION_REGEXP

Attributes

command[R]
definition[R]
options[R]
parameters[R]
tokens[R]

Public Class Methods

new() click to toggle source
    # File lib/cli/command_line_arguments.rb
150 def initialize
151   @tokens = []
152   @definition = Definition.new(self)
153   @current_definition = @definition
154 end
parse(tokens = $*, &block) click to toggle source
    # File lib/cli/command_line_arguments.rb
144 def self.parse(tokens = $*, &block)
145   cla = Arguments.new
146   cla.define(&block)
147   cla.parse!(tokens)
148 end

Public Instance Methods

[](option) click to toggle source
    # File lib/cli/command_line_arguments.rb
160 def [](option)
161   if the_option = @options.find { |(key, _)| key =~ option }
162     the_option[1]
163   else
164     @current_definition[option].default_value
165   end
166 end
define() { |definition| ... } click to toggle source
    # File lib/cli/command_line_arguments.rb
156 def define(&_block)
157   yield(@definition)
158 end
next_parameter() click to toggle source
    # File lib/cli/command_line_arguments.rb
173 def next_parameter
174   parameter_candidate = @tokens.first
175   parameter = (parameter_candidate.nil? || OPTION_REGEXP =~ parameter_candidate || ALIASES_REGEXP =~ parameter_candidate) ? nil : @tokens.shift
176   parameter
177 end
next_token() click to toggle source
    # File lib/cli/command_line_arguments.rb
168 def next_token
169   @current_token = @tokens.shift
170   @current_token
171 end
parse!(tokens) click to toggle source
    # File lib/cli/command_line_arguments.rb
179 def parse!(tokens)
180   @current_definition = @definition
181   @first_token = true
182   @tokens      = tokens.clone
183 
184   @options = {}
185   @parameters   = []
186   @command = nil
187 
188   prepare_result!
189 
190   while next_token
191 
192     if @first_token && command_definition = @definition.has_command?(@current_token)
193       @current_definition = command_definition
194       @command = CommandLine::Option.rewrite(@current_token)
195     else
196       case @current_token
197         when ALIASES_REGEXP then handle_alias_expansion(Regexp.last_match[1])
198         when OPTION_REGEXP then  handle_option(Regexp.last_match[1])
199         else;                handle_other_parameter(@current_token)
200       end
201       @first_token = false
202     end
203 
204   end
205 
206   validate_arguments!
207 
208   self
209 end

Protected Instance Methods

handle_alias_expansion(aliases) click to toggle source
    # File lib/cli/command_line_arguments.rb
229 def handle_alias_expansion(aliases)
230   aliases.reverse.scan(/./) do |alias_char|
231     if option_definition = @current_definition[alias_char]
232       @tokens.unshift(option_definition.to_option)
233     else
234       fail CommandLine::UnknownOption, alias_char
235     end
236   end
237 end
handle_option(option_name) click to toggle source
    # File lib/cli/command_line_arguments.rb
243 def handle_option(option_name)
244   option_definition = @current_definition[option_name]
245   fail CommandLine::UnknownOption, option_name if option_definition.nil?
246 
247   if option_definition.multiple?
248     @options[option_definition] << option_definition.parse(self)
249   else
250     @options[option_definition] = option_definition.parse(self)
251   end
252 end
handle_other_parameter(parameter) click to toggle source
    # File lib/cli/command_line_arguments.rb
239 def handle_other_parameter(parameter)
240   @parameters << parameter
241 end
prepare_result!() click to toggle source
    # File lib/cli/command_line_arguments.rb
213 def prepare_result!
214   multiple_options = Hash[*@current_definition.options.select { |_name, o| o.multiple? }.flatten]
215   multiple_options.each { |_name, definition| @options[definition] = [] }
216 end
validate_arguments!() click to toggle source
    # File lib/cli/command_line_arguments.rb
218 def validate_arguments!
219   if @current_definition.parameters && !(@current_definition.parameters === @parameters.length)
220     fail CommandLine::ParametersOutOfRange.new(@current_definition.parameters, @parameters.length)
221   end
222 
223   required_options = Hash[*@current_definition.options.select { |_name, o| o.required? }.flatten]
224   required_options.each do |name, definition|
225     fail CommandLine::RequiredOptionMissing, definition unless self[name]
226   end
227 end