Class: Mongify::CLI::Options

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

Overview

Used to parse the options for an application

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Options

Returns a new instance of Options.



8
9
10
11
12
13
14
15
# File 'lib/mongify/cli/options.rb', line 8

def initialize(argv)
  @parsed = false
  @argv = argv
  @parser = OptionParser.new
  #@command_class = ReekCommand
  set_options
  parse_options
end

Instance Method Details

Banner for help output



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mongify/cli/options.rb', line 18

def banner
  progname = @parser.program_name
  return <<EOB
Usage: #{progname} command database.config [database_translation.rb]

Commands:
#{Mongify::CLI::Command::Worker.list_commands.join("\n")}

Examples:

#{progname} check database.config
#{progname} translation datbase.config > database_translation.rb
#{progname} process database.config database_translation.rb
#{progname} sync database.config database_translation.rb

See http://github.com/anlek/mongify for more details

EOB
end

#parseObject

Parses CLI passed attributes and figures out what command user is trying to run



52
53
54
55
56
57
58
59
60
# File 'lib/mongify/cli/options.rb', line 52

def parse
  if @command_class == Command::Help
    Command::Help.new(@parser)
  elsif @command_class == Command::Version
    Command::Version.new(@parser.program_name)
  else
    Command::Worker.new(action, config_file, translation_file, @parser)
  end
end

#set_optionsObject

Sets the options for CLI Also used for help output



40
41
42
43
44
45
46
47
48
49
# File 'lib/mongify/cli/options.rb', line 40

def set_options
  @parser.banner = banner
  @parser.separator "Common options:"
  @parser.on("-h", "--help", "Show this message") do
    @command_class = Command::Help
  end
  @parser.on("-v", "--version", "Show version") do
    @command_class = Command::Version
  end
end