Module: Wolf

Extended by:
Wolf
Included in:
Wolf
Defined in:
lib/wolf.rb,
lib/wolf/mouth.rb,
lib/wolf/stomach.rb,
lib/wolf/version.rb

Defined Under Namespace

Modules: Mouth, Stomach

Constant Summary collapse

ALIASES =
{}
OPTIONS =
{:o => :open, :m => :menu, :x => :xml, :v => :verbose, :h => :help,
:a => :all, :l => :load, :t => :title, :V => :version}
HELP_OPTIONS =
[
  ['-m, --menu', 'Choose from links in a menu and requery with one'],
  ['-a, --all', 'Print all tables and rows (uninteresting ones are hidden by default)'],
  ['-t, --title=TITLE', 'Only display tables whose title match TITLE'],
  ['-x, --xml', 'Print raw xml response instead of printing tables'],
  ['-l, --load', "Load one or more xml files to print"],
  ['-o, --open', 'Open query in the browser (mac only)'],
  ['-v, --verbose', 'Print additional information'],
  ['-V, --version', 'Print version'],
  ['-h, --help', 'Print help']
]
VERSION =
'0.1.0'

Instance Method Summary collapse

Instance Method Details

#devour(argv = ARGV) ⇒ Object



25
26
27
28
29
30
# File 'lib/wolf.rb', line 25

def devour(argv=ARGV)
  options, fetch_options = parse_options(argv)
  return puts(VERSION) if options[:version]
  return puts(help) if argv.empty? || options[:help]
  Mouth.eat(argv, options, fetch_options)
end

#helpObject



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

def help
  name_max = HELP_OPTIONS.map {|e| e[0].length }.max
  desc_max = HELP_OPTIONS.map {|e| e[1].length }.max
  ["Usage: wolf [OPTIONS] [ARGS]", "\nOptions:",
    HELP_OPTIONS.map {|k,v| "  %-*s  %-*s" % [name_max, k, desc_max, v] },
    "", "Append parameters to a query with options in the format --PARAM=VALUE i.e.",
  "    --reinterpret=true --format=html"]
end

#option?(opt) ⇒ Boolean

Returns:

  • (Boolean)


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

def option?(opt)
  OPTIONS.key?(opt) || OPTIONS.value?(opt)
end

#parse_options(argv) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wolf.rb', line 41

def parse_options(argv)
  options, fetch_options = {}, {}
  arg = argv.find {|e| e[/^-/] }
  index = argv.index(arg)
  while arg =~ /^-/
    if arg[/^--?(\w+)=(\S+)/]
      opt = $1.to_sym
      option?(opt) ? options[OPTIONS[opt] || opt] = $2 :
        fetch_options[$1.to_sym] = $2
    elsif (opt = arg[/^--?(\w+)/, 1]) && option?(opt.to_sym)
      options[OPTIONS[opt.to_sym] || opt.to_sym] = true
    end
    argv.delete_at(index)
    arg = argv[index]
  end
  [options, fetch_options]
end