Class: CLASP::Arguments
- Inherits:
-
Object
- Object
- CLASP::Arguments
- Defined in:
- lib/clasp/arguments.rb
Overview
The main class for processing command-line arguments
Defined Under Namespace
Classes: FlagArgument, OptionArgument
Constant Summary collapse
- Flag =
#################################################################### # backwards-compatible
FlagArgument- Option =
OptionArgument
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
an immutable array of aliases.
-
#argv ⇒ Object
readonly
the (possibly mutated) array of arguments instance passed to new.
-
#argv_original_copy ⇒ Object
readonly
unchanged copy of the original array of arguments passed to new.
-
#flags ⇒ Object
readonly
an immutable array of flags.
-
#options ⇒ Object
readonly
an immutable array of options.
-
#program_name ⇒ Object
readonly
Returns the value of attribute program_name.
-
#values ⇒ Object
readonly
an immutable array of values.
Class Method Summary collapse
-
.load(argv, source, options = {}) ⇒ Object
Loads an instance of the class, as specified by
source, according to the given parameters.
Instance Method Summary collapse
-
#find_first_unknown(options = {}) ⇒ Object
finds the first unknown flag or option;
nilif all used. -
#find_flag(id) ⇒ Object
Searches for a flag that matches the given id, returning the flag if found;
nilotherwise. -
#find_option(id) ⇒ Object
Searches for a option that matches the given id, returning the option if found;
nilotherwise. -
#initialize(argv = ARGV, aliases = nil, options = {}) ⇒ Arguments
constructor
Constructs an instance of the class, according to the given parameters.
Constructor Details
#initialize(argv = ARGV, aliases = nil, options = {}) ⇒ Arguments
Constructs an instance of the class, according to the given parameters
See the documentation for the ::CLASP module for examples
Signature
-
Parameters:
argv-
(
Array) The arguments array. May not benil. Defaults toARGV.
aliases-
(
Array) The aliases array. Defaults tonil. If none supplied, no aliasing will be performed.
options-
An options hash, containing any of the following options.
-
Options:
mutate_argv:-
(
Boolean) Determines if the library should mutateargv. Defaults totrue. This is essential when using CLASP in conjunction with$<.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/clasp/arguments.rb', line 329 def initialize(argv = ARGV, aliases = nil, = {}) # have to do this name-swap, as 'options' has CLASP-specific # meaning init_opts, = .dup, nil init_opts[:mutate_argv] = true unless init_opts.has_key? :mutate_argv @program_name = init_opts[:program_name] || Arguments.derive_program_name_ @argv = argv argv = argv.dup @argv_original_copy = argv.dup.freeze @aliases = aliases aliases = nil if aliases and aliases.empty? flags, , values = Arguments.parse(argv, aliases) [ flags, , values ].each do |ar| class << ar undef :inspect undef :to_s def to_s s = '' s += '[' s += self.map { |v| %Q<"#{v}"> }.join(', ') s += ']' s end def inspect s = '' s += "#<#{self.class}:0x#{(object_id << 1).to_s(16)} [" s += self.map { |v| v.inspect }.join(', ') s += "]>" s end end end @flags = flags.freeze = .freeze @values = values.freeze # do argv-mutation, if required if init_opts[:mutate_argv] while not argv.empty? argv.shift end @values.each do |v| argv << v end end end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
an immutable array of aliases
548 549 550 |
# File 'lib/clasp/arguments.rb', line 548 def aliases @aliases end |
#argv ⇒ Object (readonly)
the (possibly mutated) array of arguments instance passed to new
560 561 562 |
# File 'lib/clasp/arguments.rb', line 560 def argv @argv end |
#argv_original_copy ⇒ Object (readonly)
unchanged copy of the original array of arguments passed to new
563 564 565 |
# File 'lib/clasp/arguments.rb', line 563 def argv_original_copy @argv_original_copy end |
#flags ⇒ Object (readonly)
an immutable array of flags
551 552 553 |
# File 'lib/clasp/arguments.rb', line 551 def flags @flags end |
#options ⇒ Object (readonly)
an immutable array of options
554 555 556 |
# File 'lib/clasp/arguments.rb', line 554 def end |
#program_name ⇒ Object (readonly)
Returns the value of attribute program_name.
565 566 567 |
# File 'lib/clasp/arguments.rb', line 565 def program_name @program_name end |
#values ⇒ Object (readonly)
an immutable array of values
557 558 559 |
# File 'lib/clasp/arguments.rb', line 557 def values @values end |
Class Method Details
.load(argv, source, options = {}) ⇒ Object
Loads an instance of the class, as specified by source, according to the given parameters
See the documentation for the ::CLASP module for examples
Signature
-
Parameters:
argv-
(
Array) The arguments array. May not benil. Defaults toARGV.
source-
(
Hash,IO) The arguments specification, either as a Hash or an instance of an IO-implementing type containing a YAML specification.
options-
An options hash, containing any of the following options.
-
Options:
mutate_argv:-
(
Boolean) Determines if the library should mutateargv. Defaults totrue. This is essential when using CLASP in conjunction with$<.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/clasp/arguments.rb', line 205 def self.load(argv, source, = {}) ||= {} h = nil case source when ::IO h = YAML.load(source.read) when ::Hash h = source else if source.respond_to?(:to_hash) h = source.to_hash else raise TypeError, "#{self}.#{__method__}() 'source' argument must be a #{::Hash}, or an object implementing #{::IO}, or a type implementing to_hash'" end end aliases = [] _clasp = h['clasp'] or raise ArgumentError, "missing top-level 'clasp' element in load configuration" ::Hash === _clasp or raise ArgumentError, "top-level 'clasp' element must be a #{::Hash}" _specs = (_clasp['arg-specs'] || _clasp['aliases']) or raise ArgumentError, "missing element 'clasp/arg-specs'" ::Array === _specs or raise ArgumentError, "top-level 'arg-specs' element must be a #{::Hash}" _specs.each do |_spec| case _spec when ::Hash # TODO: make a utility function and shrink all the following _spec.each do |_arg_type, _details| case _arg_type when 'flag', :flag _name = _details['name'] unless _name warn "flag arg-spec missing required 'name' field" else _alias = _details['alias'] _aliases = _details['aliases'] _help = _details['help'] || _details['description'] aliases << CLASP.Flag(_name, alias: _alias, aliases: _aliases, help: _help) end when 'option', :option _name = _details['name'] unless _name warn "option arg-spec missing required 'name' field" else _alias = _details['alias'] _aliases = _details['aliases'] _default_value = _details['default_value'] || _details['default'] _help = _details['help'] || _details['description'] _required = _details['required'] = _details['required_message'] _values_range = _details['values_range'] || _details['values'] aliases << CLASP.Option(_name, alias: _alias, aliases: _aliases, default_value: _default_value, help: _help, required: _required, required_message: , values_range: _values_range) end when 'alias', :alias _resolved = _details['resolved'] unless _resolved warn "alias arg-spec missing required 'resolved' field" else _alias = _details['alias'] _aliases = _details['aliases'] unless _alias || _aliases warn "alias arg-spec missing required 'alias' or 'aliases' field" else aliases << CLASP.Flag(_resolved, alias: _alias, aliases: _aliases) end end else warn "unknown arg-type '#{_arg_type}' specified" end end else warn "non-#{::Hash} element in 'clasp/arg-specs': #{_spec} (of type #{_spec.class})" end end self.new argv, aliases, end |
Instance Method Details
#find_first_unknown(options = {}) ⇒ Object
finds the first unknown flag or option; nil if all used
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/clasp/arguments.rb', line 569 def find_first_unknown = {} option = {} if .nil? raise ArgumentError, "options must be nil or Hash - #{option.class} given" unless .is_a? ::Hash aliases = [:aliases] || @aliases raise ArgumentError, "aliases may not be nil" if aliases.nil? flags.each do |f| return f unless aliases.any? { |al| al.is_a?(::CLASP::FlagAlias) && al.name == f.name } end self..each do |o| return o unless aliases.any? { |al| al.is_a?(::CLASP::OptionAlias) && al.name == o.name } end nil end |
#find_flag(id) ⇒ Object
Searches for a flag that matches the given id, returning the flag if found; nil otherwise
Signature
-
Parameters:
id-
(String, CLASP::Flag) The name of a flag, or the flag
itself
600 601 602 603 604 605 606 607 608 |
# File 'lib/clasp/arguments.rb', line 600 def find_flag(id) flags.each do |flag| return flag if flag == id end nil end |
#find_option(id) ⇒ Object
Searches for a option that matches the given id, returning the option if found; nil otherwise
Signature
-
Parameters:
id-
(String, CLASP::Flag) The name of a option, or the option
itself
618 619 620 621 622 623 624 625 626 |
# File 'lib/clasp/arguments.rb', line 618 def find_option(id) .each do |option| return option if option == id end nil end |