class CommandLineArgumentParser

Constants

DOMAIN_CRAWLER
WEB_CRAWLER

Attributes

crawl_depth[R]
crawl_type[R]
page_limit[R]
url_file[R]

Public Class Methods

new() click to toggle source
# File lib/command_line_argument_parser.rb, line 8
def initialize
        unless ARGV.length >= 1
                display_usage
                exit
        end

        # initial argument
        @opts = GetoptLong.new(
                ["--crawl", "-c", GetoptLong::REQUIRED_ARGUMENT],
                ["--crawl-depth", "-d", GetoptLong::OPTIONAL_ARGUMENT],
                ["--page-limit", "-p", GetoptLong::OPTIONAL_ARGUMENT],
                ["--url-file", "-f", GetoptLong::OPTIONAL_ARGUMENT]
        )
        @crawl_type = "data.txt"
        @crawl_depth = 3
        @page_limit = 100
        @url_file = 'urls.txt'
end

Public Instance Methods

display_usage() click to toggle source
# File lib/command_line_argument_parser.rb, line 27
def display_usage
        p "Sample usage:"
        p "ruby resay_crawler.rb -c web -d 3 -p 100 -f 'urls.txt'"
        p "-c must be either 'web' or 'domain', will default to 'web' is you type garbage "
end
ensure_crawl_type_correct(value) click to toggle source
# File lib/command_line_argument_parser.rb, line 48
def ensure_crawl_type_correct(value)
        if value != WEB_CRAWLER && value != DOMAIN_CRAWLER
                @crawl_type = WEB_CRAWLER
        else
                @crawl_type = value
        end
end
parse_arguments() click to toggle source
# File lib/command_line_argument_parser.rb, line 33
def parse_arguments
        @opts.each do |opt, arg|
                case opt
                when '--crawl'
                        ensure_crawl_type_correct(arg)
                when '--crawl-depth'
                        @crawl_depth = arg.to_i
                when '--page-limit'
                        @page_limit = arg.to_i
                when '--url-file'
                        @url_file = arg
                end
        end
end