Commit ca448b36 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

option parsing

parent 03bf1031
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -3,3 +3,5 @@ $: << ::File.expand_path('../../lib', __FILE__)

require "fnordquery"
require "fnordquery/runner"

FnordQuery::Runner.new
 No newline at end of file
+29 −0
Original line number Diff line number Diff line
class FnordQuery::Query
  
  X_VALIDATE = /^(([a-z]+\([^\)]*\)) *)+$/
  X_EXTRACT  = /(([a-z]+)\(([^\)]*)\))/

  def initialize(str)
  	raise "Invalid Query: #{str}" unless str.match(X_VALIDATE)
  end

end  

#   filter(KEY = VALUE)
#   filter(KEY < MAX)
#   filter(KEY > MIN)
#   filter(KEY ~ MIN-MAX)
#   filter(KEY & ONE,TWO,THREE...)
#   filter(KEY)
#   since(TIMESTAMP)
#   since(now)
#   until(TIMESTAMP)
#   until(now)
#   stream()
#
#   filter(channel = 'dawanda-firehose') since(0) until(now)
#   filter(channel = 'dawanda-firehose') since(now) stream()
#   filter(channel & 'dawanda-firehose','dawanda-searchfeed') since(now) stream()


+82 −35
Original line number Diff line number Diff line
require 'rake'
class FnordQuery::Runner

task :help do
  require "optparse"

  def initialize
    @opts = {}

    tasks    = %w(query web udp tcp exec)
    backends = %w(redis fyrehose)
    shorts   = { redis: :r, fyrehose: :x }

    OptionParser.new do |opts|
      opts.on("-h", "--help") do
        print_usage
        exit!
      end
      opts.on("-v", "--version") do
        print_version
  print_help
  print_examples
  print_copy
        exit!
      end
      { :task => tasks, :backend => backends }.each do |lkey, list|
        list.each do |key|
          okeys = ["--#{key} [ARG]"]
          okeys << "-#{shorts[key.to_sym]} [ARG]" if shorts[key.to_sym]
          opts.on(*(okeys << String)) do |arg|
            unless arg
              puts "error: missing argument: --#{key}"
              exit!(1)
            end
            if @opts[lkey]
              puts "error: only one of #{list.join(", ")} can be given"
              exit!(1)
            end
            @opts[lkey] = [key, arg]
          end
        end
      end
    end.parse!

    if [@opts[:task], @opts[:backend]].compact.size == 0
      print_usage
    elsif @opts[:task].nil?
      puts "error: no task given"
    elsif @opts[:backend].nil?
      puts "error: no backend specified"
    end

    #backend = @opts...


  end

task :default => :help
private

  def print_usage
    print_version
    print_help
  end

  def print_version
    puts "fnordquery 0.0.1\n\n"
@@ -15,28 +63,27 @@ end

  def print_help
    help = <<-EOH
  Usage: fnordquery [OPTIONS...] [TASK]
    Usage: fnordquery [OPTIONS...]
    -r <address>      use redis backend
    -x <address>      use fyrehose backend
  -T               print all tasks
  -h               print this message

    --web <address>   start web interface
    --tcp <address>   listen on tcp for events
    --udp <address>   listen on udp for events
    --query <query>   print all matching events 
    --exec <file>     run this file
    EOH
  puts [help.lstrip, nil]
end

def print_examples
    examples = <<-EOH
    Examples:
  fnordquery -r localhost:6379 web[0.0.0.0:8080]
  fnordquery -r localhost:6379 listen:tcp[0.0.0.0:2325]
  fnordquery -r localhost:6379 listen:udp[0.0.0.0:2323]
    fnordquery -f --emit "FILTER(_channel = 'fnord')"  
    fnordquery -r localhost:6379 --udp 0.0.0.0:2323
    fnordquery -r --web 0.0.0.0:8080
    EOH
  puts [examples.lstrip, nil]
end

def print_copy
    puts [help.lstrip, nil]
    puts [examples.lstrip, nil]
    puts "http://github.com/paulasmuth/fnordquery"
  end

Rake.application.init('fnordquery')
Rake.application.top_level
 No newline at end of file
end