Sha256: 94e5b303b5e5cd3f5ea81714b09bd443f607ddb240fe46c84c3abd8edc065652
Contents?: true
Size: 1.09 KB
Versions: 1
Compression:
Stored size: 1.09 KB
Contents
require "console/version" require 'readline' module Console def self.included(cls) cls.extend(ClassMethods) ### default commands ### cls.instance_eval do define_cmd(:help, "show commands") do @commands.each do |key, val| puts " #{key}: #{val[:desc]}" end end define_cmd(:exit, "quit client") do puts 'good bye!' exit end end end module ClassMethods def define_cmd(name, desc, &block) @commands ||= {} @commands[name.to_s] = {desc: desc, block: block} end def run_cmd(cmd_name, args) if @commands[cmd_name] @commands[cmd_name][:block].call(*args) else puts "Invalid command '#{cmd_name}'" end rescue => e puts e.message end end def start(prompt = '> ', hello = nil) puts (hello || "use 'help' command show all commands") loop do cmd_name, *cmd_args = Readline.readline(prompt, true).split unless cmd_name puts next else self.class.run_cmd(cmd_name, cmd_args) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
simple_console-0.0.1 | lib/console.rb |