Module: Clip
- Defined in:
- lib/clip.rb
Defined Under Namespace
Classes: Flag, IllegalConfiguration, Option, Parser
Constant Summary collapse
- VERSION =
"1.0.2"
- HASHER_REGEX =
/^--?(\w+)/
Class Method Summary collapse
-
.hash(argv = ARGV.dup, keys = []) ⇒ Object
Turns ARGV into a hash.
-
.reset_hash! ⇒ Object
Clear the cached hash value.
Class Method Details
.hash(argv = ARGV.dup, keys = []) ⇒ Object
Turns ARGV into a hash.
my_clip_script -c config.yml # Clip.hash == { 'c' => 'config.yml' }
my_clip_script command -c config.yml # Clip.hash == { 'c' => 'config.yml' }
my_clip_script com -c config.yml -d # Clip.hash == { 'c' => 'config.yml' }
my_clip_script -c config.yml --mode optimistic
# Clip.hash == { 'c' => 'config.yml', 'mode' => 'optimistic' }
The returned hash also has a remainder
method that contains unparsed values.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/clip.rb', line 424 def self.hash(argv = ARGV.dup, keys = []) return @hash if @hash # used the cached value if available opts = Clip(argv) do |clip| keys = argv.select{ |a| a =~ HASHER_REGEX }.map do |a| a = a.sub(HASHER_REGEX, '\\1') clip.optional(a[0,1], a); a end end # The "|| true" on the end is for when no value is found for a # key; it's assumed that a flag was meant instead of an optional # argument, so it's set to true. A bit weird-looking, but more useful. @hash = keys.inject({}) { |h, key| h.merge(key => opts.send(key) || true) } # module_eval is necessary to define a singleton method using a closure =\ (class << @hash; self; end).module_eval do define_method(:remainder) { opts.remainder } end return @hash end |
.reset_hash! ⇒ Object
Clear the cached hash value. Probably only useful for tests, but whatever.
449 |
# File 'lib/clip.rb', line 449 def self.reset_hash!; @hash = nil end |