Class: ValidateWebsite::Parser

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

Constant Summary collapse

DEFAULT_OPTS_CRAWL =
{
  :site              => 'http://localhost:3000/',
  :markup_validation => true,
  :exclude           => nil,
  :file              => nil,
  # log not found url (404 status code)
  :not_found         => false,
  # internal verbose for ValidateWebsite
  :validate_verbose  => false,
  :quiet             => false,

  # Anemone options see anemone/lib/anemone/core.rb
  :verbose           => false,
  :cookies           => nil,
  :accept_cookies    => true,
  :redirect_limit    => 0,
  :color             => true,
}
DEFAULT_OPTS_STATIC =
{
  :site              => 'http://www.example.com/',
  :pattern           => '**/*.html',
  :file              => nil,
  :validate_verbose  => false,
  :quiet             => false,
  :markup_validation => true,
  # log not found url (not on filesystem, pwd considered as root « / »)
  :not_found         => false,
  :color             => true,
}

Class Method Summary collapse

Class Method Details

.command_line_parse!(opts, args, options) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/validate_website/option_parser.rb', line 153

def self.command_line_parse!(opts, args, options)
  begin
    opts.parse!(args)
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument
    puts $!.to_s
    puts opts
    exit 128
  end
  @@default_opts.merge(options)
end

.command_line_parse_crawl(args) ⇒ Object



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/validate_website/option_parser.rb', line 50

def self.command_line_parse_crawl(args)
  options = {}
  opts = OptionParser.new do |o|
    o.set_summary_indent('  ')
    o.banner =    'Usage: validate-website [OPTIONS]'
    o.define_head 'validate-website - Web crawler for checking the ' +
      'validity of your documents'
    o.separator   ''

    o.on("-s", "--site 'SITE'", String,
         "Website to crawl (Default: #{@@default_opts[:site]})") { |v|
      options[:site] = v
    }
    o.on("-u", "--user-agent 'USERAGENT'", String,
         "Change user agent") { |v|
      options[:user_agent] = v
    }
    o.on("-e", "--exclude 'EXCLUDE'", String,
         "Url to exclude (ex: 'redirect|news')") { |v|
      options[:exclude] = v
    }
    o.on("-f", "--file 'FILE'", String,
         "Save not well formed or not found urls") { |v|
      options[:file] = v
    }

    o.on("-c", "--cookies 'COOKIES'", String,
         "Set defaults cookies") { |v|
      options[:cookies] = v
    }

    o.on("-m", "--[no-]markup-validation",
         "Markup validation (Default: #{@@default_opts[:markup_validation]})") { |v|
      options[:markup_validation] = v
    }
    o.on("-n", "--not-found",
         "Log not found url (Default: #{@@default_opts[:not_found]})") { |v|
      options[:not_found] = v
    }
    o.on("--[no-]color",
         "Show colored output (Default: #{@@default_opts[:color]})") { |v|
      options[:color] = v
    }
    o.on("-v", "--verbose",
         "Show validator errors (Default: #{@@default_opts[:validate_verbose]})") { |v|
      options[:validate_verbose] = v
    }
    o.on("-q", "--quiet",
         "Only report errors (Default: #{@@default_opts[:quiet]})") { |v|
      options[:quiet] = v
    }
    o.on("-d", "--debug",
         "Show anemone log (Default: #{@@default_opts[:verbose]})") { |v|
      options[:verbose] = v
    }

    o.separator ""
    o.on_tail("-h", "--help", "Show this help message.") { puts o; exit }
  end
  command_line_parse!(opts, args, options)
end

.command_line_parse_static(args) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/validate_website/option_parser.rb', line 112

def self.command_line_parse_static(args)
  options = {}
  opts = OptionParser.new do |o|
    o.set_summary_indent('  ')
    o.banner =    'Usage: validate-website-static [OPTIONS]'
    o.define_head 'validate-website-static - check the validity of ' +
      'your documents'
    o.separator   ''

    o.on("-s", "--site 'SITE'", String,
         "Where static files will be hosted (Default: #{@@default_opts[:site]})") { |v|
      options[:site] = v
    }
    o.on("-p", "--pattern 'PATTERN'", String,
         "Change filenames pattern (Default: #{@@default_opts[:pattern]})") { |v|
      options[:pattern] = v.strip
    }
    o.on("-f", "--file 'FILE'", String,
         "Save not well formed urls") { |v|
      options[:file] = v
    }
    o.on("-m", "--[no-]markup-validation",
         "Markup validation (Default: #{@@default_opts[:markup_validation]})") { |v|
      options[:markup_validation] = v
    }
    o.on("-n", "--not-found",
         "Log files not on filesystem, pwd considered as root « / » (Default: #{@@default_opts[:not_found]})") { |v|
      options[:not_found] = v
    }
    o.on("-v", "--verbose",
         "Show validator errors (Default: #{@@default_opts[:validate_verbose]})") { |v|
      options[:validate_verbose] = v
    }
    o.on("-q", "--quiet",
         "Only report errors (Default: #{@@default_opts[:quiet]})") { |v|
      options[:quiet] = v
    }
  end
  command_line_parse!(opts, args, options)
end

.parse(options, type) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/validate_website/option_parser.rb', line 37

def self.parse(options, type)
  if const_defined?("DEFAULT_OPTS_#{type.to_s.upcase}")
    @@default_opts = const_get("DEFAULT_OPTS_#{type.to_s.upcase}")
    if Array === options
      send("command_line_parse_#{type}", options)
    else
      @@default_opts.merge(options)
    end
  else
    raise ArgumentError, "Unknown options type : #{type}"
  end
end