module LaceArgvExtension

Copyright 2009-2014 Max Howell and other contributors.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR “AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Public Instance Methods

debug?() click to toggle source
# File lib/extend/ARGV.rb, line 51
def debug?
  flag? '--debug'
end
flag?(flag) click to toggle source
# File lib/extend/ARGV.rb, line 67
def flag? flag
  options_only.any? do |arg|
    arg == flag || arg[1..1] != '-' && arg.include?(flag[2..2])
  end
end
force?() click to toggle source
# File lib/extend/ARGV.rb, line 59
def force?
  flag? '--force'
end
include?(arg) click to toggle source

self documenting perhaps?

# File lib/extend/ARGV.rb, line 34
def include? arg
  @n=index arg
end
interactive?() click to toggle source
# File lib/extend/ARGV.rb, line 63
def interactive?
  flag? '--interactive'
end
named() click to toggle source
# File lib/extend/ARGV.rb, line 25
def named
  @named ||= reject{|arg| arg[0..0] == '-'}
end
next() click to toggle source
# File lib/extend/ARGV.rb, line 38
def next
  at @n+1 or raise UsageError
end
nohooks?() click to toggle source
# File lib/extend/ARGV.rb, line 55
def nohooks?
  flag? '--no-hooks'
end
options_only() click to toggle source
# File lib/extend/ARGV.rb, line 29
def options_only
  select {|arg| arg[0..0] == '-'}
end
switch?(switch_character) click to toggle source

eg. `foo -ns -i –bar` has three switches, n, s and i

# File lib/extend/ARGV.rb, line 74
def switch? switch_character
  return false if switch_character.length > 1
  options_only.any? do |arg|
    arg[1..1] != '-' && arg.include?(switch_character)
  end
end
value(arg) click to toggle source
# File lib/extend/ARGV.rb, line 42
def value arg
  arg = find {|o| o =~ /--#{arg}=(.+)/}
  $1 if arg
end
verbose?() click to toggle source
# File lib/extend/ARGV.rb, line 47
def verbose?
  flag? '--verbose' or !ENV['VERBOSE'].nil?
end

Private Instance Methods

downcased_unique_named() click to toggle source
# File lib/extend/ARGV.rb, line 83
def downcased_unique_named
  # Only lowercase names, not paths or URLs
  @downcased_unique_named ||= named.map do |arg|
    arg.include?("/") ? arg : arg.downcase
  end.uniq
end