Module: Nr::Runner

Defined in:
lib/nr/runner.rb

Class Method Summary collapse

Class Method Details

.helpObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nr/runner.rb', line 53

def help
  maxn = options.transpose.first.map(&:size).max
  maxd = options.transpose.last .map(&:size).max
  "Usage: nr HOST PORT\n" +
  options.map{ |(name, desc)|
    if desc.empty?
      name
    else
      sprintf("  %-*s  %-*s", maxn, name, maxd, desc)
    end
  }.join("\n")
end

.optionsObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/nr/runner.rb', line 6

def options
  @options ||=
  [['-o, --host HOST'                            ,
    'HOST it is listening to (default: 0.0.0.0)'],

   ['-p, --port PORT'                            ,
    'PORT it is bound     to (default: 12344)'  ],

   ['-h, --help'       , 'Print this message'   ],
   ['-v, --version'    , 'Print the version'    ]]
end

.parse(argv) ⇒ Object



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
# File 'lib/nr/runner.rb', line 24

def parse argv
  unused, host, port = [], '0.0.0.0', 12344
  until argv.empty?
    case arg = argv.shift
    when /^-p=?(.+)?/, /^--port=?(.+)?/
      port = $1 || argv.shift

    when /^-o=?(.+)?/, /^--host=?(.+)?/
      host = $1 || argv.shift

    when /^-h/, '--help'
      puts(help)
      exit

    when /^-v/, '--version'
      require 'nr/version'
      puts(Nr::VERSION)
      exit

    when /^(\d+)/
      port = arg

    else
      host = arg
    end
  end
  [host, port]
end

.run(argv = ARGV) ⇒ Object



18
19
20
21
22
# File 'lib/nr/runner.rb', line 18

def run argv=ARGV
  host, port = parse(argv)
  warn("Listening on #{host}:#{port}")
  Nr.new(host, port).start{ |bytes| print bytes }
end