5
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
|
# File 'lib/uchardet/cli.rb', line 5
def self.execute(stdout, args=[])
@stdout = stdout
@options = {
:input_filtered => false,
:declared_encoding => nil,
:detect_all => false,
:path => nil
}
parser = OptionParser.new do |opts|
opts.banner = " Usage: \#{File.basename($0)} [options] file\n BANNER\n \n opts.on(\"-l\", \"--list\",\n \"Display list of detectable character sets.\"\n ) { self.list; exit }\n opts.on(\"-s\", \"--strip\",\n \"Strip HTML or XML markup before detection.\"\n ) { @options[:input_filtered] = true }\n opts.on(\"-e\", \"--encoding\",\n \"Hint the charset detector about possible encoding.\"\n ) { |arg| @options[:declared_encoding] = arg }\n opts.on(\"-a\", \"--all\",\n \"Show all matching encodings.\"\n ) { @options[:detect_all] = true }\n opts.on(\"-h\", \"--help\",\n \"Show this help message.\"\n ) { @stdout.puts opts; exit }\n \n if args.empty?\n @stdout.puts opts\n else\n begin\n opts.parse!(args)\n rescue OptionParser::ParseError => ex\n STDERR.puts \"ERROR: \#{ex.to_s}. See \#{File.basename($0)} --help\"\n exit\n end\n \n @options[:path] = args.last\n if @options[:path].nil? || @options[:path].empty?\n @stdout.puts opts\n STDERR.puts \"ERROR: please specify a file path.\"\n exit\n end\n \n self.detect\n end\n end\nend\n".gsub(/^\s*/,'')
|