lib/toycol/command.rb in toycol-0.2.2 vs lib/toycol/command.rb in toycol-0.3.0

- old
+ new

@@ -1,29 +1,32 @@ # frozen_string_literal: true require "optparse" require_relative "./client" +require_relative "./template_generator" module Toycol class Command class Options + @options = {} + class << self def parse!(argv) - 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]) && argv.first != "-h" - sub_command_option_parser[options[:command]].parse!(argv) + @options[:command] = argv.shift + @options[:request_message] = argv.shift if request_message?(argv.first) + @options[:protocol_name] = argv.shift if protocol_name?(argv.first) + sub_command_option_parser[@options[:command]].parse!(argv) rescue OptionParser::MissingArgument, OptionParser::InvalidOption, ArgumentError => e abort e.message end - options + @options end def create_option_parser OptionParser.new do |opt| opt.banner = "Usage: #{opt.program_name} [-h|--help] [-v|--version] COMMAND [arg...]" @@ -43,23 +46,38 @@ end end 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["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["generate"] = generator_option_parser + sub_command_parser["g"] = generator_option_parser sub_command_parser end private + def request_message?(arg) + %w[client c].include?(@options[:command]) \ + && arg != "-p" \ + && arg != "-h" + end + + def protocol_name?(arg) + %w[geberate g].include?(@options[:command]) \ + && arg != "-t" \ + && arg != "-h" + end + 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" } + { name: "server -u SERVER_NAME", summary: "Start proxy and background server" }, + { name: "generate NAME -t TYPE", summary: "Generate new protocol or Rack app" } ] end def client_option_parser OptionParser.new do |opt| @@ -87,20 +105,18 @@ 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 generator_option_parser + OptionParser.new do |opt| + opt.on("-t TYPE", "--type TYPE", "generate TYPE of template (default: :all)") do |type| + @options[:template_type] = type + end - def server_command_help_messages - [ - { name: "server -u=SERVER_NAME", summary: "Start proxy & background server" } - ] + opt.on_head("-h", "--help", "Show this message") { help_command(opt) } + end end def help_command(parser) puts parser.help exit @@ -124,9 +140,12 @@ when "client", "c" ::Toycol::Client.execute!(options[:request_message]) when "server", "s" ARGV.push("-q", "-s", "toycol") Rack::Server.start + when "generate", "g" + type = options[:template_type] || "all" + ::Toycol::TemplateGenerator.generate!(type: type, name: options[:protocol_name]) end end end end