1
2
3
4
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
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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# We handle the parsing of options, and subsequently as a singleton
# object to be queried for option values
module RI
VERSION_STRING = "alpha 0.1"
class Options
require 'singleton'
require 'getoptlong'
include Singleton
# No not use a pager. Writable, because ri sets it if it
# can't find a pager
attr_accessor :use_stdout
# should we just display a class list and exit
attr_reader :list_classes
# The width of the output line
attr_reader :width
# the formatting we apply to the output
attr_reader :formatter
module OptionList
OPTION_LIST = [
[ "--help", "-h", nil,
"you're looking at it" ],
[ "--classes", "-c", nil,
"Display the names of classes and modules we\n" +
"know about"],
[ "--format", "-f", "<name>",
"Format to use when displaying output:\n" +
" " + RI::TextFormatter.list + "\n" +
"Use 'bs' (backspace) with most pager programs.\n" +
"To use ANSI, either also use the -T option, or\n\n" +
"tell your pager to allow control characters\n" +
"(for example using the -R option to less)"],
[ "--no-pager", "-T", nil,
"Send output directly to stdout."
],
[ "--width", "-w", "output width",
"set the width of the output" ],
]
def OptionList.options
OPTION_LIST.map do |long, short, arg,|
[ long,
short,
arg ? GetoptLong::REQUIRED_ARGUMENT : GetoptLong::NO_ARGUMENT
]
end
end
def OptionList.strip_output(text)
text =~ /^\s+/
leading_spaces = $&
text.gsub!(/^#{leading_spaces}/, '')
$stdout.puts text
end
# Show an error and exit
def OptionList.error(msg)
$stderr.puts
$stderr.puts msg
$stderr.puts "\nFor help on options, try 'ri --help'\n\n"
exit 1
end
# Show usage and exit
def OptionList.usage(short_form=false)
puts
puts(RI::VERSION_STRING)
puts
name = File.basename($0)
OptionList.strip_output(<<-EOT)
Usage:
#{name} [options] [names...]
Display information on Ruby classes, modules, and methods.
Give the names of classes or methods to see their documentation.
Partial names may be given: if the names match more than
one entity, a list will be shown, otherwise details on
that entity will be displayed.
Nested classes and modules can be specified using the normal
Name::Name notation, and instance methods can be distinguished
from class methods using "." (or "#") instead of "::".
For example:
ri File
ri File.new
ri F.n
ri zip
Note that shell quoting may be required for method names
containing puncuation:
ri 'Array.[]'
ri compact\\!
EOT
if short_form
puts "For help on options, type 'ri -h'"
puts "For a list of classes I know about, type 'ri -c'"
else
puts "Options:\n\n"
OPTION_LIST.each do |long, short, arg, desc|
opt = sprintf("%20s", "#{long}, #{short}")
oparg = sprintf("%-7s", arg)
print "#{opt} #{oparg}"
desc = desc.split("\n")
if arg.nil? || arg.length < 7
puts desc.shift
else
puts
end
desc.each do |line|
puts(" "*28 + line)
end
puts
end
exit 0
end
end
end
# Parse command line options.
def parse
@use_stdout = !STDOUT.tty?
@width = 72
@formatter = RI::TextFormatter.for("plain")
@list_classes = false
begin
go = GetoptLong.new(*OptionList.options)
go.quiet = true
go.each do |opt, arg|
case opt
when "--help" then OptionList.usage
when "--no-pager" then @use_stdout = true
when "--classes" then @list_classes = true
when "--format"
@formatter = RI::TextFormatter.for(arg)
unless @formatter
$stderr.print "Invalid formatter (should be one of "
$stderr.puts RI::TextFormatter.list + ")"
exit 1
end
when "--width"
begin
@width = Integer(arg)
rescue
$stderr.puts "Invalid width: '#{arg}'"
exit 1
end
end
end
rescue GetoptLong::InvalidOption, GetoptLong::MissingArgument => error
OptionList.error(error.message)
end
end
end
end
|