Class: CliParser

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

Overview

The command line interface parser

Class Method Summary collapse

Class Method Details

.call_subcommand(command) ⇒ Object

Tries to call subcommand extracted from hash

Parameters:

  • command (String)

    subcommand to call



115
116
117
118
119
120
121
# File 'lib/cli_parser.rb', line 115

def self.call_subcommand(command)
  if command.nil?
    abort(@banner)
  else
    command.call
  end
end

.parse_argvObject

Parse the arguments in the command line



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cli_parser.rb', line 99

def self.parse_argv
  client = Client.new
  subcommands = {
    recipe: -> { client.recipe @options },
    register: -> { client.register ARGV[1], ARGV[2] },
    login: -> { client. ARGV[1], ARGV[2] },
    last: -> { client.last },
    review: -> { client.review ARGV[1] },
    user: -> { client.user }
  }
  call_subcommand subcommands[ARGV[0].to_sym]
end

.parse_optsObject

Parse the options in command line



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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cli_parser.rb', line 27

def self.parse_opts
  OptionParser.new do |opts|
    opts.banner = @banner

    # help message option
    opts.on('--help',
            'Prints help message') do
      puts opts
      exit
    end

    # version message option
    opts.on('--version',
            'Prints version') do
      puts "yum #{Yum::VERSION}"
      exit
    end

    # phrase -----------------------------------------------------------------
    # include recipe with phrase
    opts.on('-p', '--phrase PHRASE', @help[:phrase]) do |phrase|
      @options[:q] = phrase
    end

    # holiday ----------------------------------------------------------------
    # include holiday recipes
    opts.on('-h', '--holiday HOLIDAY', @help[:holiday]) do |holiday|
      @options[:allowedHoliday] = holiday
    end

    # exclude holiday recipes
    opts.on('--eh', '--excluded-holiday HOLIDAY', @help[:holiday]) do |hol|
      @options[:excludedHoliday] = hol
    end

    # ingredients ------------------------------------------------------------
    # include ingredients in recipes
    opts.on('-i', '--ingredients INGREDIENT', @help[:ing]) do |ing|
      @options[:allowedIngredient] = ing
    end

    # exclude holiday in recipes
    opts.on('--ei', '--excluded-ingredient INGREDIENT', @help[:ing]) do |ing|
      @options[:excludedIngredient] = ing
    end

    # cuisines ---------------------------------------------------------------
    # include cuisines in recipes
    opts.on('-c', '--cuisine CUISINE', @help[:cui]) do |cui|
      @options[:allowedCuisine] = cui
    end

    # exclude cuisines in recipes
    opts.on('--ec', '--excluded-cuisine CUISINE', @help[:cui]) do |cui|
      @options[:excludedCuisine] = cui
    end
    # diets ------------------------------------------------------------------
    # include diets in recipes
    opts.on('-d', '--diet DIET', @help[:diet]) do |diet|
      @options[:allowedDiet] = diet
    end

    # exclude diets in recipes
    opts.on('--ed', '--excluded-diet DIET', @help[:diet]) do |diet|
      @options[:excludedDiet] = diet
    end
  end.parse!
rescue OptionParser::InvalidOption => e
  abort(e)
end