Module: JSE::CLI

Defined in:
lib/jse/cli.rb

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jse/cli.rb', line 5

def self.execute(stdout, arguments = [])
  print = []
  ignore_case = false
  parser = OptionParser.new do |opts|
    opts.banner = "    Json Stream Editor.\n\n    Usage: \#{File.basename($0)} [options]\n\n    Options are:\n  BANNER\n    opts.separator \"\"\n    opts.on(\"-f\", \"--fields a,b,c\", Array,\n            \"List of fields to return\") do |fields|\n      print = fields\n    end\n    opts.on(\"-i\", \"--ignore-case\",\n            \"Make all matches case insensitive\") do\n      ignore_case = true\n    end\n    opts.on(\"-h\", \"--help\",\n            \"Show this help message.\") { stdout.puts opts; exit }\n    opts.parse!(arguments)\n  end\n\n  if File.exist?(arguments.last)\n    stream = JSE::Stream.new(File.open(arguments.pop, 'r'))\n  else\n    stream = JSE::Stream.new(STDIN)\n  end\n\n  arguments.each do |arg|\n    field, text = arg.split(':')\n    stream.filter!(field, text, ignore_case)\n  end\n\n  unless print.empty?\n    stream.print!(*print)\n  end\n\n  begin\n    stream.each_line do |line|\n      stdout.puts line\n    end\n  rescue Errno::EPIPE, Interrupt\n    # Catch broken pipes so we can use head etc.\n  end\nend\n".gsub(/^[ \t]*/, '')