Class: Toycol::Command::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/toycol/command.rb

Class Method Summary collapse

Class Method Details

.create_option_parserObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/toycol/command.rb', line 26

def create_option_parser
  OptionParser.new do |opt|
    opt.banner = "Usage: #{opt.program_name} [-h|--help] [-v|--version] COMMAND [arg...]"

    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

.create_sub_command_option_parserObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/toycol/command.rb', line 45

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["generate"] = generator_option_parser
  sub_command_parser["g"]        = generator_option_parser
  sub_command_parser
end

.parse!(argv) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/toycol/command.rb', line 9

def parse!(argv)
  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 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
end