Module: NginxTail::Options

Included in:
Application
Defined in:
lib/ntail/options.rb

Instance Method Summary collapse

Instance Method Details

#parse_options(argv, defaults = {}) ⇒ Object



4
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ntail/options.rb', line 4

def parse_options(argv, defaults = {})

  options = OpenStruct.new(defaults)

  OptionParser.new do |opts|

    opts.banner = "Usage: ntail {options} {file(s)}"
    opts.separator ""
    opts.separator "Options are ..."

    opts.on '--verbose', '-v', "Run verbosely (log messages to STDERR)." do |value|
      options.verbose = true
    end

    opts.on '--format FORMAT_STRING',  "Set the format string to use when outputting parsed log lines" do |value|
      NginxTail::LogLine.format = value
    end

    opts.on '--filter',  '-f CODE', "Ruby code block for filtering (parsed) lines - needs to return true or false." do |value|
      options.filter = value
    end

    opts.on '--execute',  '-e CODE', "Ruby code block for processing each (parsed) line." do |value|
      options.code = value
    end

    opts.on '--line-number', '-l LINE_NUMBER', "Only process the line with the given line number" do |value|
      options.line_number = value.to_i
    end

    opts.on '--dry-run', '-n', "Dry-run: process files, but don't actually parse the lines" do |value|
      options.dry_run = true
    end

    opts.on '--parse-only', '-p', "Parse only: parse all lines, but don't actually process them" do |value|
      options.parse_only = true
    end

    opts.on '--raw', '-r', "Parse lines, and - for parseable ones - print out the raw input" do |value|
      options.raw = true
    end

    opts.on '--browser', '--html', "generate HTML output (the default is ANSI output)" do |value|
      options.output = :html
    end

    opts.on '--upstream', "Try to match lines using the extended nginx 'upstream' log format" do |value|
      options.pattern = :upstream
    end

    opts.on '--apache', "Try to match lines using the Apache log format instead of nginx (the default)" do |value|
      options.pattern = :apache
    end

    opts.on '--sleep [SECONDS]', '-s', Float, "Sleeps for the given number of seconds before processing the next line (--raw only)" do |value|
      options.sleep = value
    end

    opts.on '--progress', '-p', String, "In-flight progress animation during parsing" do |value|
      unless $stdout.tty?
        Sickill::Rainbow.enabled = true
        options.progress = true
      end
    end

    opts.on '--static-repo [REPO]', String, "Add [REPO] to the list of static repos" do |value|
      NginxTail::LogLine.add_static_repo(value)
    end

    opts.on '--persist', '-P', String, "Persist the parsed lines for future use" do |value|
      options.persist = true
    end

    opts.on '--version', '-V', "Display the program version." do |value|
      puts "#{NTAIL_NAME}, version #{NTAIL_VERSION}"
      options.running = false
    end

    opts.on_tail("-h", "--help", "-H", "Display this help message.") do
      puts opts
      options.running = false
    end

  end.parse!(argv)

  return options

end