class Samovar::Command

Attributes

description[RW]
name[R]
output[R]
parent[R]

Public Class Methods

[](*input, **options) click to toggle source
# File lib/samovar/command.rb, line 36
def self.[](*input, **options)
        self.new(input, **options)
end
append(row) click to toggle source
# 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) click to toggle source
# File lib/samovar/command.rb, line 19
def self.call(input = ARGV)
        if command = self.parse(input)
                command.call
        end
end
command_line(name) click to toggle source
# 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) click to toggle source
# File lib/samovar/command.rb, line 71
def self.many(*arguments, **options)
        append Many.new(*arguments, **options)
end
nested(*arguments, **options) click to toggle source
# File lib/samovar/command.rb, line 63
def self.nested(*arguments, **options)
        append Nested.new(*arguments, **options)
end
new(input = nil, name: File.basename($0), parent: nil, output: nil) click to toggle source
# 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
one(*arguments, **options) click to toggle source
# File lib/samovar/command.rb, line 67
def self.one(*arguments, **options)
        append One.new(*arguments, **options)
end
options(*arguments, **options, &block) click to toggle source
# File lib/samovar/command.rb, line 59
def self.options(*arguments, **options, &block)
        append Options.parse(*arguments, **options, &block)
end
parse(input) click to toggle source

The top level entry point for parsing ARGV.

# 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) click to toggle source
# File lib/samovar/command.rb, line 75
def self.split(*arguments, **options)
        append Split.new(*arguments, **options)
end
table() click to toggle source
# File lib/samovar/command.rb, line 44
def self.table
        @table ||= Table.nested(self)
end
usage(rows, name) click to toggle source
# 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

Public Instance Methods

[](*input) click to toggle source
# File lib/samovar/command.rb, line 122
def [](*input)
        self.dup.tap{|command| command.parse(input)}
end
parse(input) click to toggle source
# 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
print_usage(output: self.output, formatter: Output::UsageFormatter, &block) click to toggle source
to_s() click to toggle source
# File lib/samovar/command.rb, line 115
def to_s
        self.class.name
end