Class: CommandLine::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/command_line_arguments.rb

Defined Under Namespace

Classes: Definition

Constant Summary collapse

OPTION_REGEXP =
/^\-\-([A-Za-z0-9-]+)$/
ALIASES_REGEXP =
/^\-([A-Aa-z0-9]+)$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArguments



150
151
152
153
154
# File 'lib/cli/command_line_arguments.rb', line 150

def initialize
  @tokens = []
  @definition = Definition.new(self)
  @current_definition = @definition
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



142
143
144
# File 'lib/cli/command_line_arguments.rb', line 142

def command
  @command
end

#definitionObject (readonly)

Returns the value of attribute definition.



140
141
142
# File 'lib/cli/command_line_arguments.rb', line 140

def definition
  @definition
end

#optionsObject (readonly)

Returns the value of attribute options.



142
143
144
# File 'lib/cli/command_line_arguments.rb', line 142

def options
  @options
end

#parametersObject (readonly)

Returns the value of attribute parameters.



142
143
144
# File 'lib/cli/command_line_arguments.rb', line 142

def parameters
  @parameters
end

#tokensObject (readonly)

Returns the value of attribute tokens.



141
142
143
# File 'lib/cli/command_line_arguments.rb', line 141

def tokens
  @tokens
end

Class Method Details

.parse(tokens = $*, &block) ⇒ Object



144
145
146
147
148
# File 'lib/cli/command_line_arguments.rb', line 144

def self.parse(tokens = $*, &block)
  cla = Arguments.new
  cla.define(&block)
  cla.parse!(tokens)
end

Instance Method Details

#[](option) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/cli/command_line_arguments.rb', line 160

def [](option)
  if the_option = @options.find { |(key, _)| key =~ option }
    the_option[1]
  else
    @current_definition[option].default_value
  end
end

#define {|@definition| ... } ⇒ Object

Yields:



156
157
158
# File 'lib/cli/command_line_arguments.rb', line 156

def define(&_block)
  yield(@definition)
end

#next_parameterObject



173
174
175
176
177
# File 'lib/cli/command_line_arguments.rb', line 173

def next_parameter
  parameter_candidate = @tokens.first
  parameter = (parameter_candidate.nil? || OPTION_REGEXP =~ parameter_candidate || ALIASES_REGEXP =~ parameter_candidate) ? nil : @tokens.shift
  parameter
end

#next_tokenObject



168
169
170
171
# File 'lib/cli/command_line_arguments.rb', line 168

def next_token
  @current_token = @tokens.shift
  @current_token
end

#parse!(tokens) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cli/command_line_arguments.rb', line 179

def parse!(tokens)
  @current_definition = @definition
  @first_token = true
  @tokens      = tokens.clone

  @options = {}
  @parameters   = []
  @command = nil

  prepare_result!

  while next_token

    if @first_token && command_definition = @definition.has_command?(@current_token)
      @current_definition = command_definition
      @command = CommandLine::Option.rewrite(@current_token)
    else
      case @current_token
        when ALIASES_REGEXP then handle_alias_expansion(Regexp.last_match[1])
        when OPTION_REGEXP then  handle_option(Regexp.last_match[1])
        else;                handle_other_parameter(@current_token)
      end
      @first_token = false
    end

  end

  validate_arguments!

  self
end