lib/toycol/command.rb in toycol-0.1.0 vs lib/toycol/command.rb in toycol-0.2.0

- old
+ new

@@ -6,75 +6,107 @@ module Toycol class Command class Options class << self def parse!(argv) - options = {} - option_parser = create_option_parser - sub_command_parser = create_sub_command_parser + options = {} + option_parser = create_option_parser + sub_command_option_parser = create_sub_command_option_parser begin option_parser.order!(argv) options[:command] = argv.shift - options[:request_message] = argv.shift if %w[client c].include? options[:command] - - sub_command_parser[options[:command]].parse!(argv) + options[:request_message] = argv.shift if %w[client c].include?(options[:command]) && argv.first != "-h" + sub_command_option_parser[options[:command]].parse!(argv) rescue OptionParser::MissingArgument, OptionParser::InvalidOption, ArgumentError => e abort e.message end options end def create_option_parser OptionParser.new do |opt| - opt.banner = "Usage: #{opt.program_name} [-h|--help] [-v|--version] <command> <args>" - display_adding_summary(opt) + opt.banner = "Usage: #{opt.program_name} [-h|--help] [-v|--version] COMMAND [arg...]" - opt.on_head("-h", "--help", "Show this message") do - puts opt.help - exit - end - opt.on_head("-v", "--version", "Show Toycol version") do opt.version = Toycol::VERSION puts opt.ver exit end + opt.on_head("-h", "--help", "Show this message") { help_command(opt) } + + opt.separator "" + opt.separator "Sub commands:" + sub_command_summaries.each do |command| + opt.separator [opt.summary_indent, command[:name].ljust(31), command[:summary]].join(" ") + end end end - def create_sub_command_parser + def create_sub_command_option_parser sub_command_parser = Hash.new { |_k, v| raise ArgumentError, "'#{v}' is not sub command" } sub_command_parser["client"] = client_option_parser sub_command_parser["c"] = client_option_parser + sub_command_parser["server"] = server_option_parser + sub_command_parser["s"] = server_option_parser sub_command_parser end private + def sub_command_summaries + [ + { name: "client REQUEST_MESSAGE -p PORT", summary: "Send request message to server" }, + { name: "server -u SERVER_NAME", summary: "Start proxy and background server" } + ] + end + def client_option_parser OptionParser.new do |opt| - opt.on("-p=PORT_NUMBER", "--port=PORT_NUMBER", "Set port number") do |n| - ::Toycol::Client.port = n + opt.on("-p PORT_NUMBER", "--port PORT_NUMBER", "listen on PORT (default: 9292)") do |port| + ::Toycol::Client.port = port end + + opt.on_head("-h", "--help", "Show this message") { help_command(opt) } end end - def display_adding_summary(opt) - opt.separator "" - opt.separator "Client command options:" - client_command_help_messages.each do |command| - opt.separator [opt.summary_indent, command[:name].ljust(31), command[:summary]].join(" ") + def server_option_parser + OptionParser.new do |opt| + opt.on("-o HOST", "--host HOST", "bind to HOST (default: localhost)") do |host| + ::Rack::Handler::Toycol.host = host + end + + opt.on("-p PORT_NUMBER", "--port PORT_NUMBER", "listen on PORT (default: 9292)") do |port| + ::Rack::Handler::Toycol.port = port + end + + opt.on("-u SERVER_NAME", "--use SERVER_NAME", "switch using SERVER(puma/build_in)") do |server_name| + ::Rack::Handler::Toycol.preferred_background_server = server_name + end + + opt.on_head("-h", "--help", "Show this message") { help_command(opt) } end end def client_command_help_messages [ { name: "client -p=PORT_NUMBER", summary: "Send request to server" } ] end + + def server_command_help_messages + [ + { name: "server -u=SERVER_NAME", summary: "Start proxy & background server" } + ] + end + + def help_command(parser) + puts parser.help + exit + end end end def self.run(argv) new(argv).execute @@ -87,10 +119,14 @@ def execute options = Options.parse!(@argv) command = options.delete(:command) case command - when "client", "c" then ::Toycol::Client.execute!(options[:request_message]) + when "client", "c" + ::Toycol::Client.execute!(options[:request_message]) + when "server", "s" + ARGV.push("-q", "-s", "toycol") + Rack::Server.start end end end end