Class: OptionHandler

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/log_parser/option_handler.rb

Overview

Parses command line options

Constant Summary

Constants included from Constants

Constants::DEFAULT_LOG, Constants::DEFAULT_OPTIONS, Constants::DESCRIPTORS, Constants::INFO_TITLES, Constants::LOG_WARNINGS, Constants::OPTION_DESCRIPTIONS, Constants::OUTPUT_COLORS, Constants::VALIDATION_NAMES, Constants::VALID_ADDRESS, Constants::VALID_IP4, Constants::VALID_IP6, Constants::VALID_LOG, Constants::VALID_PATH, Constants::WARNINGS_JSON, Constants::WARNING_COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptionHandler

Returns a new instance of OptionHandler.



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/log_parser/option_handler.rb', line 11

def initialize
  @options = DEFAULT_OPTIONS.clone
  @options[:file_list] = []

  begin
    OptionParser.new do |opts|
      opts.on('-v', '--verbose', 'Show extra information') do
        options[:verbose] = true unless options[:quiet]
      end

      opts.on('-q', '--quiet',
              'No display except important warnings.  Disables verbose') do
        options[:quiet] = true
        options[:verbose] = false
      end

      opts.on('-c', '--color', 'Enables colored display text') do
        options[:highlighting] = true
      end

      opts.on('-C', '--no_color',
              'Disables colored display text (default)') do
        options[:highlighting] = false
      end

      opts.on('-f', '--file [FILE]',
              'Log file to read. Default is webserver.log') do |file|
        if file
          options[:file_list].push(file)
        else
          unless ENV['APP_ENV'] == 'test'
            puts 'Missing input file name.  Exiting.'
          end
          exit 50
        end
      end

      opts.on('-m', "--multiple_files ['FILE_LIST']",
              'Read a list of log files in quotes') do |file_list|
        if file_list
          options[:file_list] += file_list.split(' ')
        else
          unless ENV['APP_ENV'] == 'test'
            puts 'Missing input file list.  Exiting.'
          end
          exit 50
        end
      end

      opts.on('-o', '--output_file [FILE]', 'Write output to file') do |file|
        options[:output_file] = file || 'log_info.txt'
      end

      opts.on('-t', '--timestamp', 'Add timestamp to output file') do
        options[:timestamp] = true
      end

      opts.on('-x', '--text', 'Sets file output format to text (default)') do
        options[:output_format] = :text
      end

      opts.on('-j', '--json', 'Sets file output format to json') do
        options[:output_format] = :json
      end

      opts.on('-h', '--help', 'Shows help') do
        puts opts if ENV['APP_ENV'] != 'test'
        exit 51
      end

      opts.on('-4', '--ip4_validation',
              'Validate ip addresses using ip4 format (default)') do
        options[:ip_validation] = :ip4
      end

      opts.on('-6', '--ip6_validation',
              'Validate ip addresses using ip6 format') do
        options[:ip_validation] = :ip6
      end

      opts.on('-i', '--ip4ip6_validation',
              'Validate ip addresses using either ip4 or ip6 format') do
        options[:ip_validation] = :ip4_ip6
      end

      opts.on('-I', '--no_ip_validation',
              'No validatation of ip addresses') do
        options[:ip_validation] = :none
      end

      opts.on('-r', '--remove_invalid',
              'Ignore log if invalid ip addresss or path') do
        options[:log_remove] = true
      end

      opts.on('-R', '--warn_invalid',
              'Warn but not ignore log if invalid ip address or path
              (default)') do
        options[:log_remove] = false
      end

      opts.on('-p', '--path_validation', 'Validate webpage path (default)') do
        options[:path_validation] = true
      end

      opts.on('-P', '--no_path_validation',
              'Does not validate webpage path') do
        options[:path_validation] = false
      end

      opts.on('-g', '--page_visits', 'Show page visits (default)') do
        options[:page_visits] = true
      end

      opts.on('-G', '--no_page_visits', 'Do not show page visits') do
        options[:page_visits] = false
      end

      opts.on('-u', '--unique_page_views',
              'Show unique page views (default)') do
        options[:unique_page_views] = true
      end

      opts.on('-U', '--no_unique_page_views',
              'Do not show unique page views') do
        options[:unique_page_views] = false
      end
    end.parse!
  rescue OptionParser::InvalidOption => e
    puts e if ENV['APP_ENV'] != 'test'
    exit
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/log_parser/option_handler.rb', line 9

def options
  @options
end