Class: Mongify::Mongoid::CLI::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/mongify/mongoid/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.



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

def initialize(argv)
  @parsed = false
  @argv = argv
  @parser = OptionParser.new
  @options = {}
  set_options
  parse_options
end

Instance Method Details

Banner for help output



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongify/mongoid/cli/options.rb', line 19

def banner
  progname = @parser.program_name
  return <<EOB
  Usage: #{progname} translation_file.rb [--output ~/output_dir]

  Examples:
  #{progname} database_translation.rb
  #{progname} database_translation.rb -O ~/output_dir

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

EOB
end

#parseObject

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



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

def parse
  case 
    when @command_class == Command::Help
      Command::Help.new(@parser)
    when @command_class == Command::Version
      Command::Version.new(@parser.program_name)
    else
      Command::Worker.new(translation_file, output_dir, @options)
  end
end

#set_optionsObject

Sets the options for CLI Also used for help output



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

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
  @parser.on("-O", "--output DIR", "Output Directory") do |dir|
    @output_dir = dir
  end
  @parser.on("-F", "--force", "Force overwrite of Output Directory") do
    @options[:overwrite] = true
  end
end