Skip to content

Commit 88ccad0

Browse files
authored
Print deprecation warning for help command (#567)
* Give show_doc its own command class * Print deprecation warning for `help` command
1 parent 070a332 commit 88ccad0

4 files changed

Lines changed: 91 additions & 57 deletions

File tree

lib/irb/cmd/help.rb

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,23 @@
1-
# frozen_string_literal: false
2-
#
3-
# help.rb - helper using ri
4-
#
1+
# frozen_string_literal: true
52

6-
require_relative "nop"
3+
require_relative "show_doc"
74

85
module IRB
9-
# :stopdoc:
10-
116
module ExtendCommand
12-
class Help < Nop
13-
class << self
14-
def transform_args(args)
15-
# Return a string literal as is for backward compatibility
16-
if args.empty? || string_literal?(args)
17-
args
18-
else # Otherwise, consider the input as a String for convenience
19-
args.strip.dump
20-
end
21-
end
22-
end
23-
7+
class Help < ShowDoc
248
category "Context"
25-
description "Enter the mode to look up RI documents."
26-
27-
def execute(*names)
28-
require 'rdoc/ri/driver'
29-
30-
unless self.class.const_defined?(:Ri)
31-
opts = RDoc::RI::Driver.process_args([])
32-
self.class.const_set(:Ri, RDoc::RI::Driver.new(opts))
33-
end
9+
description "[DEPRECATED] Enter the mode to look up RI documents."
3410

35-
if names.empty?
36-
Ri.interactive
37-
else
38-
names.each do |name|
39-
begin
40-
Ri.display_name(name.to_s)
41-
rescue RDoc::RI::Error
42-
puts $!.message
43-
end
44-
end
45-
end
11+
DEPRECATION_MESSAGE = <<~MSG
12+
[Deprecation] The `help` command will be repurposed to display command help in the future.
13+
For RI document lookup, please use the `show_doc` command instead.
14+
For command help, please use `show_cmds` for now.
15+
MSG
4616

47-
nil
48-
rescue LoadError, SystemExit
49-
warn "Can't display document because `rdoc` is not installed."
17+
def execute(*names)
18+
warn DEPRECATION_MESSAGE
19+
super
5020
end
5121
end
5222
end
53-
54-
# :startdoc:
5523
end

lib/irb/cmd/show_doc.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "nop"
4+
5+
module IRB
6+
module ExtendCommand
7+
class ShowDoc < Nop
8+
class << self
9+
def transform_args(args)
10+
# Return a string literal as is for backward compatibility
11+
if args.empty? || string_literal?(args)
12+
args
13+
else # Otherwise, consider the input as a String for convenience
14+
args.strip.dump
15+
end
16+
end
17+
end
18+
19+
category "Context"
20+
description "Enter the mode to look up RI documents."
21+
22+
def execute(*names)
23+
require 'rdoc/ri/driver'
24+
25+
unless ShowDoc.const_defined?(:Ri)
26+
opts = RDoc::RI::Driver.process_args([])
27+
ShowDoc.const_set(:Ri, RDoc::RI::Driver.new(opts))
28+
end
29+
30+
if names.empty?
31+
Ri.interactive
32+
else
33+
names.each do |name|
34+
begin
35+
Ri.display_name(name.to_s)
36+
rescue RDoc::RI::Error
37+
puts $!.message
38+
end
39+
end
40+
end
41+
42+
nil
43+
rescue LoadError, SystemExit
44+
warn "Can't display document because `rdoc` is not installed."
45+
end
46+
end
47+
end
48+
end

lib/irb/extend-command.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,14 @@ def irb_context
157157

158158
[
159159
:irb_help, :Help, "cmd/help",
160-
[:show_doc, NO_OVERRIDE],
161160
[:help, NO_OVERRIDE],
162161
],
163162

163+
[
164+
:irb_show_doc, :ShowDoc, "cmd/show_doc",
165+
[:show_doc, NO_OVERRIDE],
166+
],
167+
164168
[
165169
:irb_info, :IrbInfo, "cmd/irb_info"
166170
],

test/irb/test_cmd.rb

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -783,19 +783,33 @@ def test_ls_with_no_singleton_class
783783
end
784784

785785
class ShowDocTest < CommandTestCase
786-
def test_help_and_show_doc
787-
["help", "show_doc"].each do |cmd|
788-
out, err = execute_lines(
789-
"#{cmd} String#gsub\n",
790-
"\n",
791-
)
786+
def test_help
787+
out, err = execute_lines(
788+
"help String#gsub\n",
789+
"\n",
790+
)
792791

793-
# the former is what we'd get without document content installed, like on CI
794-
# the latter is what we may get locally
795-
possible_rdoc_output = [/Nothing known about String#gsub/, /gsub\(pattern\)/]
796-
assert_empty err
797-
assert(possible_rdoc_output.any? { |output| output.match?(out) }, "Expect the `#{cmd}` command to match one of the possible outputs. Got:\n#{out}")
798-
end
792+
# the former is what we'd get without document content installed, like on CI
793+
# the latter is what we may get locally
794+
possible_rdoc_output = [/Nothing known about String#gsub/, /gsub\(pattern\)/]
795+
assert_include err, "[Deprecation] The `help` command will be repurposed to display command help in the future.\n"
796+
assert(possible_rdoc_output.any? { |output| output.match?(out) }, "Expect the `help` command to match one of the possible outputs. Got:\n#{out}")
797+
ensure
798+
# this is the only way to reset the redefined method without coupling the test with its implementation
799+
EnvUtil.suppress_warning { load "irb/cmd/help.rb" }
800+
end
801+
802+
def test_show_doc
803+
out, err = execute_lines(
804+
"show_doc String#gsub\n",
805+
"\n",
806+
)
807+
808+
# the former is what we'd get without document content installed, like on CI
809+
# the latter is what we may get locally
810+
possible_rdoc_output = [/Nothing known about String#gsub/, /gsub\(pattern\)/]
811+
assert_not_include err, "[Deprecation]"
812+
assert(possible_rdoc_output.any? { |output| output.match?(out) }, "Expect the `show_doc` command to match one of the possible outputs. Got:\n#{out}")
799813
ensure
800814
# this is the only way to reset the redefined method without coupling the test with its implementation
801815
EnvUtil.suppress_warning { load "irb/cmd/help.rb" }

0 commit comments

Comments
 (0)