Class: ValidateWebsite::Parser

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

Overview

Internal class for parse command line args

Constant Summary collapse

DEFAULT_OPTS_ALL =
{
  :markup_validation => true,
  # crawler: log not found url (404 status code)
  # static: log not found url (not on filesystem, pwd considered as root « / »)
  :not_found         => false,
  :quiet             => false,
  :file              => nil,
  # regex to ignore certain validation errors
  :ignore_errors     => nil,
  :color             => true,
  # internal verbose for ValidateWebsite
  :validate_verbose  => false,
}
DEFAULT_OPTS_CRAWL =
{
  :site              => 'http://localhost:3000/',
  :exclude           => nil,
}.merge(DEFAULT_OPTS_ALL)
DEFAULT_OPTS_STATIC =
{
  :site              => 'http://www.example.com/',
  :pattern           => '**/*.html',
}.merge(DEFAULT_OPTS_ALL)

Class Method Summary collapse

Class Method Details

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



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

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



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
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
# File 'lib/validate_website/option_parser.rb', line 44

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("-i", "--ignore-errors 'IGNORE'", String,
         "Validation errors to ignore (regex)") { |v|
      options[:ignore_errors] = 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.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



106
107
108
109
110
111
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
# File 'lib/validate_website/option_parser.rb', line 106

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("-i", "--ignore-errors 'IGNORE'", String,
         "Validation errors to ignore (regex)") { |v|
      options[:ignore_errors] = 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



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

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