Skip to content

Commit b37b3fc

Browse files
committed
Fix for invalid subcommand
1 parent 8996b7f commit b37b3fc

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

lib/iruby/application.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "rbconfig"
55
require "singleton"
66

7+
require_relative "error"
78
require_relative "kernel_app"
89

910
module IRuby
@@ -56,7 +57,13 @@ def setup(argv=nil)
5657
argv = ["console", *argv]
5758
end
5859

59-
parse_sub_command(argv) if argv.length > 0
60+
begin
61+
parse_sub_command(argv) if argv.length > 0
62+
rescue InvalidSubcommandError => err
63+
$stderr.puts err.message
64+
print_help(opts, $stderr)
65+
abort
66+
end
6067
end
6168

6269
SUB_COMMANDS = {
@@ -75,17 +82,15 @@ def setup(argv=nil)
7582
@sub_cmd = sub_cmd.to_sym
7683
@sub_argv = sub_argv
7784
else
78-
$stderr.puts "Invalid sub-command name: #{sub_cmd}"
79-
print_help(opts, $stderr)
80-
abort
85+
raise InvalidSubcommandError.new(sub_cmd, sub_argv)
8186
end
8287
end
8388

8489
private def print_help(opts, out=$stdout)
8590
out.puts opts.help
8691
out.puts
87-
out.puts "Sub-commands"
88-
out.puts "============"
92+
out.puts "Subcommands"
93+
out.puts "==========="
8994
SUB_COMMANDS.each do |name, description|
9095
out.puts "#{name}"
9196
out.puts " #{description}"

lib/iruby/error.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module IRuby
2+
class Error < StandardError
3+
end
4+
5+
class InvalidSubcommandError < Error
6+
def initialize(name, argv)
7+
@name = name
8+
@argv = argv
9+
super("Invalid subcommand name: #{@name}")
10+
end
11+
end
12+
end

test/iruby/application/application_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,12 @@ def test_version
2121
assert status.success?
2222
assert_match(/\bIRuby\s+#{Regexp.escape(IRuby::VERSION)}\b/, out)
2323
end
24+
25+
def test_unknown_subcommand
26+
out, status = Open3.capture2e(*iruby_command("matz"))
27+
refute status.success?
28+
assert_match(/^Invalid subcommand name: matz$/, out)
29+
assert_match(/^Subcommands$/, out)
30+
end
2431
end
2532
end

0 commit comments

Comments
 (0)