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) ⇒ Command

Returns a new instance of Command.



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

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

Class Attribute Details

.descriptionObject

Returns the value of attribute description.



56
57
58
# File 'lib/samovar/command.rb', line 56

def description
  @description
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

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



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

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

.append(row) ⇒ Object



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

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

.call(input = ARGV) ⇒ Object



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

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

.command_line(name) ⇒ Object



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

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

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



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

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

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



73
74
75
# File 'lib/samovar/command.rb', line 73

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

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



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

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

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



69
70
71
# File 'lib/samovar/command.rb', line 69

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

.parse(input) ⇒ Object

The top level entry point for parsing ARGV.



41
42
43
44
45
46
47
48
49
# File 'lib/samovar/command.rb', line 41

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



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

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

.tableObject



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

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

.usage(rows, name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/samovar/command.rb', line 89

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



125
126
127
# File 'lib/samovar/command.rb', line 125

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

#parse(input) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/samovar/command.rb', line 129

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


139
140
141
142
143
144
145
# File 'lib/samovar/command.rb', line 139

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

#to_sObject



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

def to_s
	self.class.name
end