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
  
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
# File 'lib/mongify/cli/options.rb', line 18

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

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

Examples:

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

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mongify/cli/options.rb', line 54

def parse
  parse_options
  
  if @command_class == HelpCommand
    HelpCommand.new(@parser)
  elsif @command_class == VersionCommand
    VersionCommand.new(@parser.program_name)
  else
    raise ConfigurationFileNotFound, "You need to provide a configuration file location #{@config_file}" unless @config_file
    #TODO: In the future, request sql_connection and nosql_connection from user input
    config = Configuration.parse(@config_file)
    
    WorkerCommand.new(action, config, translation_file, @parser)
  end
end

#set_optionsObject

Sets the options for CLI Also used for help output



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

def set_options
  @parser.banner = banner
  @parser.separator "Common options:"
  @parser.on("-h", "--help", "Show this message") do
    @command_class = HelpCommand
  end
  @parser.on("-v", "--version", "Show version") do
    @command_class = VersionCommand
  end
  @parser.on('-c', '--config FILE', "Configuration File to use") do |file|
    @config_file = file
  end
end