Class: ColorTail::Options

Inherits:
Hash
  • Object
show all
Defined in:
lib/colortail/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Options

Returns a new instance of Options.



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
# File 'lib/colortail/configuration.rb', line 53

def initialize(args)
    super()

    user_home = ENV['HOME']

    @orig_args = args.clone

    options = {}
    
    require 'optparse'
    @opts = OptionParser.new do |o|
        o.banner = "Usage: #{File.basename($0)} <file1> <file2> ..."
    
        options[:group] = 'default'
        o.on( '-g', '--group <group>', 'Specify the color grouping to use for these files' ) do |group|
            options[:group] = group
        end
        
        options[:filename_prefix] = false
        o.on( '-F', '--filename_prefix', 'Prefix each colored line with it\'s filename') do
            options[:filename_prefix] = true
        end

        options[:list] = false
        o.on( '-l', '--list', 'List all the available color groupings' ) do |group|
            options[:list] = true
        end

        o.separator ""
    
        options[:conf] = "#{user_home}/.colortailrc"
        o.on( '-c', '--conf <file>', 'Specify an alternate config file' ) do |file|
            if File.exists?(file)
                options[:conf] = file
            else
                raise FileDoesNotExist, "Config file #{file} cannot be found."
            end
        end
        
        o.on('-V', '--version', "Display version information") do
            options[:version] = true
        end

        options[:help] = false
        o.on( '-h', '--help', 'Display this help screen' ) do
            options[:help] = true
        end
        
        o.separator ""
        o.separator "Examples:"
        o.separator "  Tail messages and mail log through STDIN with syslog group:"
        o.separator "    cat /var/log/maillog | #{File.basename($0)} -g syslog /var/log/messages"

        o.separator "  Tail messages with syslog group and maillog with mail group:"
        o.separator "    #{File.basename($0)} /var/log/messages#syslog /var/log/messages#mail"

        o.separator "  Tail messages with syslog group and maillog with mail group with line prefix:"
        o.separator "    #{File.basename($0)} -F /var/log/messages#syslog /var/log/messages#mail"
    end

    begin
        @opts.parse!(args)
        self[:files] = args
        self[:options] = options
    rescue OptionParser::InvalidOption => e
        self[:invalid_argument] = e.message
        @opts.parse(args, flags={ :delete_invalid_opts => true })
        self[:files] = args
        self[:options] = options
    end
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



51
52
53
# File 'lib/colortail/configuration.rb', line 51

def opts
  @opts
end

#orig_argsObject (readonly)

Returns the value of attribute orig_args.



51
52
53
# File 'lib/colortail/configuration.rb', line 51

def orig_args
  @orig_args
end