Class: Staticizer::Command

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

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object

Parse command line arguments and print out any errors



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/staticizer/command.rb', line 6

def Command.parse(args)
  options = {}
  initial_page = nil

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: staticizer initial_url [options]\nExample: staticizer http://squaremill.com --output-dir=/tmp/crawl"

    opts.separator ""
    opts.separator "Specific options:"

    opts.on("--aws-s3-bucket [STRING]", "Name of S3 bucket to write to") do |v|
      options[:aws] ||= {}
      options[:aws][:bucket_name] = v
    end

    opts.on("--aws-region [STRING]", "AWS Region of S3 bucket") do |v|
      options[:aws] ||= {}
      options[:aws][:region] = v
    end

    opts.on("--aws-access-key [STRING]", "AWS Access Key ID") do |v|
      options[:aws] ||= {}
      options[:aws][:access_key_id] = v
    end

    opts.on("--aws-secret-key [STRING]", "AWS Secret Access Key") do |v|
      options[:aws] ||= {}
      options[:aws][:secret_access_key] = v
    end

    opts.on("-d", "--output-dir [DIRECTORY]", "Write crawl to disk in this directory, will be created if it does not exist") do |v|
      options[:output_dir] = v
    end

    opts.on("-v", "--verbose", "Run verbosely (sets log level to Logger::DEBUG)") do |v|
      options[:log_level] = Logger::DEBUG
    end

    opts.on("--log-level [NUMBER]", "Set log level 0 = most verbose to 4 = least verbose") do |v|
      options[:log_level] = v.to_i
    end

    opts.on("--log-file [PATH]", "Log file to write to") do |v|
      options[:logger] = Logger.new(v)
    end

    opts.on("--skip-write [PATH]", "Don't write out files to disk or s3") do |v|
      options[:skip_write] = true
    end

    opts.on("--valid-domains x,y,z", Array, "Comma separated list of domains that should be crawled, other domains will be ignored") do |v|
      options[:valid_domains] = v
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts "test"
      puts opts
      exit
    end
  end

  begin
    parser.parse!(args)
    initial_page = ARGV.pop
    raise ArgumentError, "Need to specify an initial URL to start the crawl" unless initial_page
  rescue StandardError => e
    puts e
    puts parser
    exit(1)
  end

  return options, initial_page
end