class Mongify::Mongoid::CLI::Options

Used to parse the options for an application

Public Class Methods

new(argv) click to toggle source
# 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

Public Instance Methods

banner() click to toggle source

Banner for help output

parse() click to toggle source

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

# 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_options() click to toggle source

Sets the options for CLI Also used for help output

# 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

Private Instance Methods

output_dir(argv=@argv) click to toggle source

Returns the config file

# File lib/mongify/mongoid/cli/options.rb, line 74
def output_dir(argv=@argv)
  @output_dir if @output_dir && File.exist?(@output_dir) && File.directory?(@output_dir)
end
parse_options() click to toggle source

option parser, ensuring parse_options is only called once

# File lib/mongify/mongoid/cli/options.rb, line 79
def parse_options
  @parser.parse!(@argv)
rescue OptionParser::InvalidOption => er
  raise Mongify::InvalidOption, er.message, er.backtrace
end
translation_file(argv=@argv) click to toggle source

Returns the translation_file or nil

# File lib/mongify/mongoid/cli/options.rb, line 69
def translation_file(argv=@argv)
  argv[0] if argv.length >= 1
end