Class: GlobaLog::Hijack

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

Overview

The Hijack class manages the acquisition of command line arguments, playing nicely with other command line aware libs.

Author

lp ([email protected])

Copyright

2009 Louis-Philippe Perron - Released under the terms of the MIT license

:title:GlobaLog/Hijack

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHijack

Returns a new instance of Hijack.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/globalog_hijack.rb', line 11

def initialize
	super()
	unless $keep_argv.empty?
		self[:opts] = nil
		self[:log_level] = nil
		self[:log_output] = nil
		self[:opts] = true 
		opts = OptionParser.new do |opts|
			opts.on('-L','--log-level [STRING]','sets the Test Log Level') do |string|
				self[:log_level] = string.to_sym
			end
			opts.on('-O','--log-output [I/O]','sets the Test Log output') do |io|
				self[:log_output] = io
			end
		end
		opts.parse!($keep_argv)
	end
end

Class Method Details

.keep_wanted_argvObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/globalog_hijack.rb', line 30

def self.keep_wanted_argv
	args = Array.new(ARGV); ARGV.clear; keep = []
	wanted = ['-L', '--log-level', '-O', '--log-output']
	catch :finished do
		loop do
			throw :finished if args.empty?
			catch :found do
				wanted.each do |arg|
					if args[0] == arg
						2.times { keep << args.shift }
						throw :found
					end
				end
				ARGV << args.shift
			end
		end
	end
	$keep_argv ||= keep
end