class Samovar::Nested
def initialize(name, commands, key: :command, default: nil)
def initialize(name, commands, key: :command, default: nil) @name = name @commands = commands @key = key # This is the default name [of a command], not the default command: @default = default end
def parse(input, default)
def parse(input, default) if command = @commands[input.first] input.shift # puts "Instantiating #{command} with #{input}" command.new(input) elsif @default default || @commands[@default].new(input) end end
def to_a
def to_a usage = [@name] if @commands.size == 0 usage << "No commands available." elsif @commands.size == 1 usage << "Only #{@commands.first}." else usage << "One of: #{@commands.keys.join(', ')}." end if @default usage << "Default: #{@default}" end return usage end
def to_s
def to_s @name end
def usage(rows)
def usage(rows) rows << self @commands.each do |key, klass| klass.usage(rows, key) end end