Class: Samovar::Command

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil, name: File.basename($0), parent: nil, output: nil) ⇒ Command

Returns a new instance of Command.



101
102
103
104
105
106
107
# File 'lib/samovar/command.rb', line 101

def initialize(input = nil, name: File.basename($0), parent: nil, output: nil)
	@name = name
	@parent = parent
	@output = output
	
	parse(input) if input
end

Class Attribute Details

.descriptionObject

Returns the value of attribute description.



41
42
43
# File 'lib/samovar/command.rb', line 41

def description
  @description
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



119
120
121
# File 'lib/samovar/command.rb', line 119

def name
  @name
end

#outputObject (readonly)

Returns the value of attribute output.



109
110
111
# File 'lib/samovar/command.rb', line 109

def output
  @output
end

#parentObject (readonly)

Returns the value of attribute parent.



120
121
122
# File 'lib/samovar/command.rb', line 120

def parent
  @parent
end

Class Method Details

.[](*input, **options) ⇒ Object



36
37
38
# File 'lib/samovar/command.rb', line 36

def self.[](*input, **options)
	self.new(input, **options)
end

.append(row) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/samovar/command.rb', line 48

def self.append(row)
	if method_defined?(row.key, false)
		warning "Method for key #{row.key} is already defined!", caller
		# raise ArgumentError, "Method for key #{row.key} is already defined!"
	end
	
	attr_accessor(row.key) if row.respond_to?(:key)
	
	self.table << row
end

.call(input = ARGV) ⇒ Object



19
20
21
22
23
# File 'lib/samovar/command.rb', line 19

def self.call(input = ARGV)
	if command = self.parse(input)
		command.call
	end
end

.command_line(name) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/samovar/command.rb', line 93

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

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



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

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

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



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

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

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



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

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

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



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

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

.parse(input) ⇒ Object

The top level entry point for parsing ARGV.



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

def self.parse(input)
	self.new(input)
rescue Error => error
	error.command.print_usage(output: $stderr) do |formatter|
		formatter.map(error)
	end
	
	return nil
end

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



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

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

.tableObject



44
45
46
# File 'lib/samovar/command.rb', line 44

def self.table
	@table ||= Table.nested(self)
end

.usage(rows, name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/samovar/command.rb', line 79

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

Instance Method Details

#[](*input) ⇒ Object



122
123
124
# File 'lib/samovar/command.rb', line 122

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

#parse(input) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/samovar/command.rb', line 126

def parse(input)
	self.class.table.merged.parse(input, self)
	
	if input.empty?
		return self
	else
		raise InvalidInputError.new(self, input)
	end
end


136
137
138
139
140
141
142
# File 'lib/samovar/command.rb', line 136

def print_usage(output: self.output, formatter: Output::UsageFormatter, &block)
	rows = Output::Rows.new
	
	self.class.usage(rows, @name)
	
	formatter.print(rows, output, &block)
end

#to_sObject



115
116
117
# File 'lib/samovar/command.rb', line 115

def to_s
	self.class.name
end