Class: Bmi::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/bmi/cli.rb

Class Method Summary collapse

Class Method Details

.calc(arguments = []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bmi/cli.rb', line 5

def self.calc(arguments=[])



  options = {
    :weight_k     => 0,
    :height_m     => 0,
    :weight_p     => 0,
    :height_i     => 0
  }
  mandatory_options = %w(  )

  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      Body Mass Index [BMI] help guide

      Usage: #{File.basename($0)} [options]

      Options are:
    BANNER
    opts.separator ""

    opts.on("-w", "--weight KILOGRAMS", String,
            "Your current weight.",
            "Pleace introduce your weight in kilograms.",
            "Default: ~") { |arg| options[:weight_k] = arg }

    opts.on("-b", "--height METERS", String,
            "How hight you are.",
            "Pleace introduce your height in meters.",
            "Default: ~") { |arg| options[:height_m] = arg }

    opts.on("-W", "--Weight POUNDS", String,
            "Your current weight.",
            "Pleace introduce your weight in pounds.",
            "Default: ~") { |arg| options[:weight_p] = arg }

    opts.on("-B", "--Height INCHES", String,
            "How hight you are.",
            "Pleace introduce your height in inches.",
            "Default: ~") { |arg| options[:height_i] = arg }

    opts.on("-h", "--help",
            "Show this help message.") { puts opts; exit }


    error = false
    arguments.each do|v|
     unless v.match(/-[w|b|W|B|h]/) || v.match(/\d/)
        error = true
        puts "Use '-h' or '--help' for more help"
     end
    end

    unless (arguments[0] == "-h") || (arguments[0] == "--help")
    error = true unless (arguments.count == 4)
    end

    opts.parse!(arguments) unless error

    if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? }
      puts opts; exit
    end
  end

  return options
end