class ImgFetcher::CommandLine

Constants

DEFAULT_DIRECTORY

Public Class Methods

new() click to toggle source
# File lib/img_fetcher/command_line.rb, line 6
def initialize
  @arguments = {}
  @options = OptionParser.new
  initialize_options
end

Public Instance Methods

parse!() click to toggle source
# File lib/img_fetcher/command_line.rb, line 12
def parse!
  @options.parse!
  check_required_arguments
  @arguments
end

Private Instance Methods

build_and_check_output_directory() click to toggle source

Checks that exists, be a valid Directory (or places the default directory), and appends at the end a slash (/)

# File lib/img_fetcher/command_line.rb, line 74
def build_and_check_output_directory
  directory = @arguments[:output_directory]

  if directory && Dir.exist?(directory)
    directory[-1] == '/' ? directory : "#{directory}/"
  else
    DEFAULT_DIRECTORY
  end
end
check_required_arguments() click to toggle source
# File lib/img_fetcher/command_line.rb, line 65
def check_required_arguments
  # Check if file_path is present and is a valid system file
  raise MissingOptionError unless @arguments[:file_path] && File.file?(@arguments[:file_path])
  # Build & check output_directory
  @arguments[:output_directory] = build_and_check_output_directory
end
file_path_option() click to toggle source
# File lib/img_fetcher/command_line.rb, line 29
def file_path_option
  @options.on(
    '-f FILE_PATH', '--file FILE_PATH',
    '[REQUIRED] Fetch and store the images from each line from the given file'
  ) do |file_path|
    @arguments[:file_path] = file_path
  end
end
initialize_options() click to toggle source
# File lib/img_fetcher/command_line.rb, line 20
def initialize_options
  @options.banner = 'Usage: img_fetcher -f <file_path> [options...]'
  file_path_option
  output_directory_option
  version_option
  verbose_option
  threaded_option
end
output_directory_option() click to toggle source
# File lib/img_fetcher/command_line.rb, line 38
def output_directory_option
  @options.on(
    '-o OUTPUT_DIRECTORY', '--output OUTPUT_DIRECTORY', 'Specify the output directory'
  ) do |output_directory|
    @arguments[:output_directory] = output_directory
  end
end
threaded_option() click to toggle source
# File lib/img_fetcher/command_line.rb, line 59
def threaded_option
  @options.on('-t', '--threaded', 'Run the command with multiple threads') do
    @arguments[:threaded] = true
  end
end
verbose_option() click to toggle source
# File lib/img_fetcher/command_line.rb, line 53
def verbose_option
  @options.on('-v', '--verbose', 'Make the operation more talkative') do
    @arguments[:verbose] = true
  end
end
version_option() click to toggle source
# File lib/img_fetcher/command_line.rb, line 46
def version_option
  @options.on('-V', '--version', 'Show version number and quit') do
    puts ImgFetcher::VERSION
    exit
  end
end