Module Clip
In: lib/clip.rb

Methods

hash  

Classes and Modules

Class Clip::IllegalConfiguration
Class Clip::Parser

Constants

VERSION = "1.0.2"
HASHER_REGEX = /^--?(\w+)/

Public Class methods

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.

[Source]

# 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

[Validate]