Class: Turnstile::Parser
- Inherits:
-
Object
- Object
- Turnstile::Parser
- Extended by:
- Forwardable
- Defined in:
- lib/turnstile/parser.rb
Instance Attribute Summary collapse
-
#argv ⇒ Object
Returns the value of attribute argv.
-
#options ⇒ Object
Returns the value of attribute options.
-
#system ⇒ Object
Returns the value of attribute system.
Instance Method Summary collapse
-
#initialize(argv, system) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
Constructor Details
#initialize(argv, system) ⇒ Parser
Returns a new instance of Parser.
14 15 16 17 18 19 |
# File 'lib/turnstile/parser.rb', line 14 def initialize(argv, system) self.system = system self.argv = argv.dup self. = Hashie::Mash.new self.argv << '-h' if argv.empty? end |
Instance Attribute Details
#argv ⇒ Object
Returns the value of attribute argv.
12 13 14 |
# File 'lib/turnstile/parser.rb', line 12 def argv @argv end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/turnstile/parser.rb', line 12 def @options end |
#system ⇒ Object
Returns the value of attribute system.
12 13 14 |
# File 'lib/turnstile/parser.rb', line 12 def system @system end |
Instance Method Details
#parse ⇒ Object
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 |
# File 'lib/turnstile/parser.rb', line 21 def parse OptionParser.new do |opts| opts. = "Usage:\n".bold.magenta + " turnstile -f <file> [ --daemon ] [ options ]\n".yellow + " turnstile -s [ json | csv | nad ] [ options ]\n".yellow + " turnstile -a 'platform:ip:user' [ options ]\n".yellow opts.separator 'Description:'.bold.magenta opts.separator ' ' + ::Turnstile::DESCRIPTION.gsub(/\n/, "\n ") opts.separator 'Log File Specification:'.bold.magenta opts.on('-f', '--file FILE', 'File to monitor') do |file| [:file] = file end opts.on('-t', '--file-type TYPE', 'Either: json_formatted, pipe_delimited,', 'or comma_delimited (default).') do |type| [:filetype] = type end opts.on('-D', '--delimiter CHAR', 'Forces "delimited" file type, and uses ', 'the character in the argument as the delimiter') do |v| [:delimiter] = v end opts.separator "\nRedis Server:".bold.magenta opts.on('-r', '--redis-url URL', 'Redis server URL') do |host| Turnstile.config.redis_url = host end opts.on('--redis-host HOST', 'Redis server host') do |host| Turnstile.config.redis_host = host end opts.on('--redis-port PORT', 'Redis server port') do |port| Turnstile.config.redis_port = port end opts.on('--redis-db DB', 'Redis server db') do |db| Turnstile.config.redis_db = db end opts.separator "\nMode of Operation:".bold.magenta opts.on('-d', '--daemonize', 'Daemonize to watch the logs') do |v| [:daemonize] = true end opts.on('-s', '--summary [FORMAT]', 'Print current stats and exit. Optional format can be', 'json (default), nad, yaml, or csv') do |v| [:summary] = true [:summary_format] = (v || 'json').to_sym end opts.on('-a', '--add TOKEN', 'Registers an event from the token, such as ', '"ios:123.4.4.4:32442". Use -d to customize delimiter.') do |v| [:add] = v end opts.separator "\nTiming Adjustments:".bold.magenta opts.on('-b', '--buffer-interval INTERVAL', 'Buffer for this many seconds') do |v| [:buffer_interval] = v.to_i end opts.on('-i', '--flush-interval INTERVAL', 'Flush then sleep for this many seconds') do |v| [:flush_interval] = v.to_i end opts.separator "\nMiscellaneous:".bold.magenta opts.on('-v', '--verbose', 'Print status to stdout') do |v| [:debug] = true end opts.on_tail('-h', '--help', 'Show this message') do puts opts return end end.parse!(argv) if [:summary] Turnstile::Summary.print([:summary_format] || :json, [:delimiter]) elsif [:add] Turnstile::Tracker.new.add_token([:add], [:delimiter] || ':') Turnstile::Summary.print([:summary_format] || :json) else Turnstile::Collector::Runner.new().run end rescue OptionParser::MissingArgument => e STDERR.puts e..bold.red rescue Exception => e STDERR.puts e..bold.red end |