Module: Recorder::Command::Options

Defined in:
lib/recorder/command/options.rb

Class Method Summary collapse

Class Method Details

.create_command_parserObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/recorder/command/options.rb', line 64

def self.create_command_parser
  OptionParser.new do |opt|
    sub_command_help = [
                        {name: 'create -w weight -b bodyfat',           summary: 'Create Data'},
                        {name: 'update id -w weiht -b bodyfat -d date', summary: 'Update Data'},
                        {name: 'delete id',                             summary: 'Delete Data'}
                      ]
    opt.banner = "Usage: #{opt.program_name} [-h|--help] [-v|--version] <command> [<args>]"
    opt.separator ''
    opt.separator "#{opt.program_name} Available Commands:"
    sub_command_help.each do | command|
      opt.separator [opt.summary_indent, command[:name].ljust(40), command[:summary]].join(' ')
    end

    opt.on_head('-h','--help','Show this message') do|v|
      puts opt.help
      exit
    end

    opt.on_head('-v','--version','show program version') do |v|
      opt.version = Recorder::VERSION
      puts opt.ver
      exit
    end
  end
end

.create_sub_command_parsers(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/recorder/command/options.rb', line 28

def self.create_sub_command_parsers(options)
  sub_command_parsers = Hash.new do |k,v|
    raise ArgumentError, "'#{v} is not recorder sub command."
  end

  sub_command_parsers['create'] = OptionParser.new do |opt|
    opt.banner = 'Usage: create <args>'
    opt.on('-w VAL','--weight=VAL',  'data weight'){|v| options[:weight] = v}
    opt.on('-b VAL','--bodyfat=VAL','data bodyfat'){|v| options[:bodyfat] = v}
    opt.on_tail('-h','--help', 'Show this message')   {|v| help_sub_command(opt) }
  end

  sub_command_parsers['update'] = OptionParser.new do |opt|
    opt.banner = 'Usage: update id <args>'
    opt.on('-d VAL','--date=VAL','update date'){|v| options[:date] = v}
    opt.on_tail('-h','--help', 'Show this message')   {|v| help_sub_command(opt) }
  end

  sub_command_parsers['list'] = OptionParser.new do |opt|
    opt.banner = 'Usage: list id <args>'
    opt.on_tail('-h','--help', 'Show this message')   {|v| help_sub_command(opt) }
  end

  sub_command_parsers['delete'] = OptionParser.new do |opt|
    opt.banner = 'Usage: delete id'
    opt.on_tail('-h','--help', 'Show this message')   {|v| help_sub_command(opt) }
  end

  return sub_command_parsers
end

.help_sub_command(parser) ⇒ Object



59
60
61
62
# File 'lib/recorder/command/options.rb', line 59

def self.help_sub_command(parser)
  puts parser.help
  exit
end

.parse!(argv) ⇒ Object



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

def self.parse!(argv)
  options = {}

  sub_command_parsers = create_sub_command_parsers(options)
  command_parser      = create_command_parser

  begin
    command_parser.order!(argv)
    options[:command] = argv.shift
    sub_command_parsers[options[:command]].parse!(argv)
    if %(update delete).include?(options[:command])
      raise ArgumentError, "#{options[:command]} id not found." if argv.empty?
      options[:id] = Integer(argv.first)
    end
  rescue OptionParser::MissingArgument, OptionParser::InvalidOption, ArgumentError => e
    abort e.message
  end
  options
end