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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/asciidoctor/reducer/cli.rb', line 9
def parse args
options = { attributes: {}, log_level: LOG_LEVELS['warn'], safe: :unsafe }
opt_parser = ::OptionParser.new do |opts|
opts.program_name = 'asciidoctor-reducer'
opts.banner = <<~END
Usage: #{opts.program_name} [OPTION]... FILE
#{::Gem.loaded_specs['asciidoctor-reducer'].summary}
END
opts.on '-a KEY[=VALUE]', '--attribute=KEY[=VALUE]',
'set a document attribute in the AsciiDoc document: [key, key!, key=value]',
'may be specified multiple times' do |attr|
key, val = attr.split '=', 2
val ||= ''
options[:attributes][key] = val
end
opts.on '--log-level LEVEL', %w(debug info warn error fatal),
'set the minimum level of messages to log: [debug, info, warn, error, fatal] (default: warn)' do |level|
options[:log_level] = level
end
opts.on '-o FILE', '--output=FILE', 'set the output filename or stream' do |file|
options[:output_file] = file
end
opts.on '--preserve-conditionals', 'preserve preprocessor conditional directives in the reduced source' do
options[:preserve_conditionals] = true
end
opts.on '-q', '--quiet', 'suppress all application log messages' do
options[:log_level] = nil
end
opts.on '-rLIBRARY', '--require LIBRARY', 'require the specified library or libraries before reducing',
'may be specified multiple times' do |path|
(options[:requires] ||= []).concat path.split ','
end
opts.on '-S', '--safe-mode SAFE_MODE', ['unsafe', 'safe', 'server', 'secure'],
'set safe mode level: [unsafe, safe, server, secure] (default: unsafe)' do |name|
options[:safe] = name.to_sym
end
opts.on '--trace', 'trace the cause of application errors (default: false)' do
options[:trace] = true
end
opts.on '-v', '--version', 'display the version information and exit' do
print_version opts
return 0
end
opts.on '-h', '--help', 'display this help text and exit' do
print_help opts
return 0
end
end
if (args = opt_parser.parse args).empty?
opt_parser.warn 'Please specify an AsciiDoc file to reduce.'
print_help opt_parser
1
elsif args.size == 1
if (requires = options.delete :requires)
requires.uniq.each do |path|
require path
rescue ::LoadError
$stderr.puts %(#{opt_parser.program_name}: '#{path}' could not be required (reason: #{$!.message}))
return 1
end
end
options[:input_file] = args[0]
options[:output_file] = '-' unless options[:output_file]
[0, options]
else
opt_parser.warn %(extra arguments detected (unparsed arguments: #{(args.drop 1).join ' '}))
print_help opt_parser
1
end
rescue ::OptionParser::InvalidOption
$stderr.puts %(#{opt_parser.program_name}: #{$!.message})
print_help opt_parser
1
end
|