Module: LaceArgvExtension

Defined in:
lib/extend/ARGV.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#confirm?(prompt) ⇒ Boolean



81
82
83
84
85
86
87
88
89
# File 'lib/extend/ARGV.rb', line 81

def confirm?(prompt)
  if flag?('--yes')
    true
  else
    print "#{prompt} [y/N] "
    $stdout.flush
    /y/i.match?(STDIN.gets)
  end
end

#debug?Boolean



51
52
53
# File 'lib/extend/ARGV.rb', line 51

def debug?
  flag? '--debug'
end

#flag?(flag) ⇒ Boolean



67
68
69
70
71
# 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?Boolean



59
60
61
# File 'lib/extend/ARGV.rb', line 59

def force?
  flag? '--force'
end

#include?(arg) ⇒ Boolean

self documenting perhaps?



34
35
36
# File 'lib/extend/ARGV.rb', line 34

def include? arg
  @n=index arg
end

#interactive?Boolean



63
64
65
# File 'lib/extend/ARGV.rb', line 63

def interactive?
  flag? '--interactive'
end

#namedObject



25
26
27
# File 'lib/extend/ARGV.rb', line 25

def named
  @named ||= reject{|arg| arg[0..0] == '-'}
end

#nextObject



38
39
40
# File 'lib/extend/ARGV.rb', line 38

def next
  at @n+1 or raise UsageError
end

#nohooks?Boolean



55
56
57
# File 'lib/extend/ARGV.rb', line 55

def nohooks?
  flag? '--no-hooks'
end

#options_onlyObject



29
30
31
# File 'lib/extend/ARGV.rb', line 29

def options_only
  select {|arg| arg[0..0] == '-'}
end

#switch?(switch_character) ⇒ Boolean

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



74
75
76
77
78
79
# 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) ⇒ Object



42
43
44
45
# File 'lib/extend/ARGV.rb', line 42

def value arg
  arg = find {|o| o =~ /--#{arg}=(.+)/}
  $1 if arg
end

#verbose?Boolean



47
48
49
# File 'lib/extend/ARGV.rb', line 47

def verbose?
  flag? '--verbose' or !ENV['VERBOSE'].nil?
end