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.



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

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.



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

def description
  @description
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

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



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

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

.append(row) ⇒ Object



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

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

.command_line(name) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/samovar/command.rb', line 97

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

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



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

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

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



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

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

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



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

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

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



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

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

.parse(input = ARGV) ⇒ Object

The top level entry point for parsing ARGV.



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

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

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



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

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

.tableObject



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

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

.usage(rows, name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/samovar/command.rb', line 83

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



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

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

#parse(input) ⇒ Object



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

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


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

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