Class: CommandLine::Arguments
- Inherits:
-
Object
- Object
- CommandLine::Arguments
- 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
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
- #[](option) ⇒ Object
- #define {|@definition| ... } ⇒ Object
-
#initialize ⇒ Arguments
constructor
A new instance of Arguments.
- #next_parameter ⇒ Object
- #next_token ⇒ Object
- #parse!(tokens) ⇒ Object
Constructor Details
#initialize ⇒ Arguments
Returns 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
#command ⇒ Object (readonly)
Returns the value of attribute command.
148 149 150 |
# File 'lib/cli/command_line_arguments.rb', line 148 def command @command end |
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
146 147 148 |
# File 'lib/cli/command_line_arguments.rb', line 146 def definition @definition end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
148 149 150 |
# File 'lib/cli/command_line_arguments.rb', line 148 def end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
148 149 150 |
# File 'lib/cli/command_line_arguments.rb', line 148 def parameters @parameters end |
#tokens ⇒ Object (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
.parse(tokens = $*, &block) ⇒ Object
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
#[](option) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/cli/command_line_arguments.rb', line 166 def [](option) if the_option = .detect { |(key, value)| key =~ option } the_option[1] else @current_definition[option].default_value end end |
#define {|@definition| ... } ⇒ Object
162 163 164 |
# File 'lib/cli/command_line_arguments.rb', line 162 def define(&block) yield(@definition) end |
#next_parameter ⇒ Object
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 |
#next_token ⇒ Object
174 175 176 177 |
# File 'lib/cli/command_line_arguments.rb', line 174 def next_token @current_token = @tokens.shift return @current_token end |
#parse!(tokens) ⇒ Object
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 = {} @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 |