Class: Arrghs

Inherits:
Object
  • Object
show all
Defined in:
lib/arrghs.rb

Overview

Parses command line arguments

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/arrghs.rb', line 3

def args
  @args
end

Class Method Details

.die_noisily_about(needed) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/arrghs.rb', line 32

def self.die_noisily_about needed
  $stderr.puts "Missing options\n"
  $stderr.puts "Usage: #{$0} "
  $stderr.puts "with options:"
  needed.each {|opt,desc| $stderr.puts "#{opt} <#{desc}> "}
  $stderr.puts "\n__________\n"
  exit
end

.parse(arg_arr = ARGV) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/arrghs.rb', line 15

def self.parse(arg_arr=ARGV)
  args = {}
  arg_arr.each_with_index do |opt, i|
    if opt =~ /^-{1,1}[^-]/
      opt = opt[1..-1]
      next if opt.length > 1
      val = true
      val = arg_arr[i + 1] if arg_arr[i + 1] !~  /^-/
      opt.split(//).each {|f| args[f.to_sym] = val}
    elsif opt =~ /^-{2,2}/
      opt = opt[2..-1]
      args[opt.to_sym] = arg_arr[i + 1]
    end
  end
  args
end

.parse_and_check(arg_arr = ARGV, needed) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/arrghs.rb', line 5

def self.parse_and_check(arg_arr=ARGV, needed)
  args = Arrghs.parse(arg_arr)
  missing = []
  needed.each do |opt,desc|
    missing << [opt,desc] unless args.include?(opt.gsub(/^-{1,2}/, "").to_sym)
  end
  return args if missing.empty?
  Arrghs.die_noisily_about needed
end