Class: Samovar::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/samovar/command.rb,
lib/samovar/command/system.rb,
lib/samovar/command/track_time.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil) ⇒ Command

Returns a new instance of Command.



58
59
60
# File 'lib/samovar/command.rb', line 58

def initialize(input = nil)
	parse(input) if input
end

Class Attribute Details

.descriptionObject

Returns the value of attribute description.



63
64
65
# File 'lib/samovar/command.rb', line 63

def description
  @description
end

Class Method Details

.[](*input) ⇒ Object



46
47
48
# File 'lib/samovar/command.rb', line 46

def self.[](*input)
	self.parse(input)
end

.append(row) ⇒ Object



70
71
72
73
74
# File 'lib/samovar/command.rb', line 70

def self.append(row)
	attr_accessor(row.key) if row.respond_to?(:key)
	
	self.table << row
end

.command_line(name) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/samovar/command.rb', line 110

def self.command_line(name)
	if @table
		"#{name} #{@table.usage}"
	else
		name
	end
end

.many(*args, **options) ⇒ Object



88
89
90
# File 'lib/samovar/command.rb', line 88

def self.many(*args, **options)
	append Many.new(*args, **options)
end

.nested(*args, **options) ⇒ Object



80
81
82
# File 'lib/samovar/command.rb', line 80

def self.nested(*args, **options)
	append Nested.new(*args, **options)
end

.one(*args, **options) ⇒ Object



84
85
86
# File 'lib/samovar/command.rb', line 84

def self.one(*args, **options)
	append One.new(*args, **options)
end

.options(*args, **options, &block) ⇒ Object



76
77
78
# File 'lib/samovar/command.rb', line 76

def self.options(*args, **options, &block)
	append Options.parse(*args, **options, &block)
end

.parse(input) ⇒ Object

Raises:



38
39
40
41
42
43
44
# File 'lib/samovar/command.rb', line 38

def self.parse(input)
	command = self.new(input)
	
	raise IncompleteParse.new("Could not parse #{input}") unless input.empty?
	
	return command
end

.split(*args, **options) ⇒ Object



92
93
94
# File 'lib/samovar/command.rb', line 92

def self.split(*args, **options)
	append Split.new(*args, **options)
end

.tableObject



66
67
68
# File 'lib/samovar/command.rb', line 66

def self.table
	@table ||= Table.new
end

.usage(rows, name) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/samovar/command.rb', line 96

def self.usage(rows, name)
	rows.nested(name, self) do |rows|
		return unless @table
		
		@table.rows.each do |row|
			if row.respond_to?(:usage)
				row.usage(rows)
			else
				rows << row
			end
		end
	end
end

Instance Method Details

#[](*input) ⇒ Object



50
51
52
# File 'lib/samovar/command.rb', line 50

def [](*input)
	self.dup.tap{|command| command.parse(input)}
end

#parse(input) ⇒ Object



54
55
56
# File 'lib/samovar/command.rb', line 54

def parse(input)
	self.class.table.parse(input, self)
end


118
119
120
121
122
123
124
# File 'lib/samovar/command.rb', line 118

def print_usage(*args, output: $stderr, formatter: Output::DetailedFormatter)
	rows = Output::Rows.new
	
	self.class.usage(rows, *args)
	
	formatter.print(rows, output)
end

#system(*args, **options) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/samovar/command/system.rb', line 31

def system(*args, **options)
	log_system(args, options)
	
	Kernel::system(*args, **options)
rescue Errno::ENOENT
	return false
end

#system!(*args, **options) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/samovar/command/system.rb', line 39

def system!(*args, **options)
	if system(*args, **options)
		return true
	else
		raise SystemError.new("Command #{args.first.inspect} failed: #{$?.to_s}")
	end
end

#track_timeObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/samovar/command/track_time.rb', line 25

def track_time
	start_time = Time.now
	
	yield
ensure
	end_time = Time.now
	elapsed_time = end_time - start_time
	
	$stdout.flush
	$stderr.puts Rainbow("Elapsed Time: %0.3fs" % elapsed_time).magenta
end