Class: CommandLineArgumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/command_line_argument_parser.rb

Constant Summary collapse

WEB_CRAWLER =
'web'
DOMAIN_CRAWLER =
'domain'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandLineArgumentParser

Returns a new instance of CommandLineArgumentParser.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/command_line_argument_parser.rb', line 8

def initialize
  unless ARGV.length >= 1
    display_usage
    exit
  end
  @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

Instance Attribute Details

#crawl_depthObject (readonly)

Returns the value of attribute crawl_depth.



6
7
8
# File 'lib/command_line_argument_parser.rb', line 6

def crawl_depth
  @crawl_depth
end

#crawl_typeObject (readonly)

Returns the value of attribute crawl_type.



6
7
8
# File 'lib/command_line_argument_parser.rb', line 6

def crawl_type
  @crawl_type
end

#page_limitObject (readonly)

Returns the value of attribute page_limit.



6
7
8
# File 'lib/command_line_argument_parser.rb', line 6

def page_limit
  @page_limit
end

#url_fileObject (readonly)

Returns the value of attribute url_file.



6
7
8
# File 'lib/command_line_argument_parser.rb', line 6

def url_file
  @url_file
end

Instance Method Details

#display_usageObject



25
26
27
28
29
# File 'lib/command_line_argument_parser.rb', line 25

def display_usage
  p "Sample usage:"
  p "ruby dk_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) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/command_line_argument_parser.rb', line 46

def ensure_crawl_type_correct(value)
  if value != WEB_CRAWLER && value != DOMAIN_CRAWLER
    @crawl_type = WEB_CRAWLER
  else
    @crawl_type = value
  end
end

#parse_argumentsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/command_line_argument_parser.rb', line 31

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