Class: CommandLine::Arguments
- Inherits:
-
Object
- Object
- CommandLine::Arguments
- Defined in:
- lib/cli/command_line_arguments.rb
Defined Under Namespace
Classes: Definition
Constant Summary
- OPTION_REGEXP =
/^\-\-([A-Za-z0-9-]+)$/- ALIASES_REGEXP =
/^\-([A-Aa-z0-9]+)$/
Instance Attribute Summary (collapse)
-
- (Object) command
readonly
Returns the value of attribute command.
-
- (Object) definition
readonly
Returns the value of attribute definition.
-
- (Object) options
readonly
Returns the value of attribute options.
-
- (Object) parameters
readonly
Returns the value of attribute parameters.
-
- (Object) tokens
readonly
Returns the value of attribute tokens.
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) [](option)
- - (Object) define {|@definition| ... }
-
- (Arguments) initialize
constructor
A new instance of Arguments.
- - (Object) next_parameter
- - (Object) next_token
- - (Object) parse!(tokens)
Constructor Details
- (Arguments) initialize
A new instance of Arguments
156 157 158 159 160 |
# File 'lib/cli/command_line_arguments.rb', line 156 def initialize @tokens = [] @definition = Definition.new(self) @current_definition = @definition end |
Instance Attribute Details
- (Object) command (readonly)
Returns the value of attribute command
148 149 150 |
# File 'lib/cli/command_line_arguments.rb', line 148 def command @command end |
- (Object) definition (readonly)
Returns the value of attribute definition
146 147 148 |
# File 'lib/cli/command_line_arguments.rb', line 146 def definition @definition end |
- (Object) options (readonly)
Returns the value of attribute options
148 149 150 |
# File 'lib/cli/command_line_arguments.rb', line 148 def @options end |
- (Object) parameters (readonly)
Returns the value of attribute parameters
148 149 150 |
# File 'lib/cli/command_line_arguments.rb', line 148 def parameters @parameters end |
- (Object) tokens (readonly)
Returns the value of attribute tokens
147 148 149 |
# File 'lib/cli/command_line_arguments.rb', line 147 def tokens @tokens end |
Class Method Details
+ (Object) parse(tokens = $*, &block)
150 151 152 153 154 |
# File 'lib/cli/command_line_arguments.rb', line 150 def self.parse(tokens = $*, &block) cla = Arguments.new cla.define(&block) return cla.parse!(tokens) end |
Instance Method Details
- (Object) [](option)
166 167 168 169 170 171 172 |
# File 'lib/cli/command_line_arguments.rb', line 166 def [](option) if the_option = @options.detect { |(key, _)| key =~ option } the_option[1] else @current_definition[option].default_value end end |
- (Object) define {|@definition| ... }
162 163 164 |
# File 'lib/cli/command_line_arguments.rb', line 162 def define(&block) yield(@definition) end |
- (Object) next_parameter
179 180 181 182 183 |
# File 'lib/cli/command_line_arguments.rb', line 179 def next_parameter parameter_candidate = @tokens.first parameter = (parameter_candidate.nil? || OPTION_REGEXP =~ parameter_candidate || ALIASES_REGEXP =~ parameter_candidate) ? nil : @tokens.shift return parameter end |
- (Object) next_token
174 175 176 177 |
# File 'lib/cli/command_line_arguments.rb', line 174 def next_token @current_token = @tokens.shift return @current_token end |
- (Object) parse!(tokens)
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 210 211 212 213 214 215 |
# File 'lib/cli/command_line_arguments.rb', line 185 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; handle_alias_expansion($1) when OPTION_REGEXP; handle_option($1) else; handle_other_parameter(@current_token) end @first_token = false end end validate_arguments! return self end |