File tree Expand file tree Collapse file tree 3 files changed +30
-6
lines changed Expand file tree Collapse file tree 3 files changed +30
-6
lines changed Original file line number Diff line number Diff line change 44require "rbconfig"
55require "singleton"
66
7+ require_relative "error"
78require_relative "kernel_app"
89
910module 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 } "
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change @@ -21,5 +21,12 @@ def test_version
2121 assert status . success?
2222 assert_match ( /\b IRuby\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
2532end
You can’t perform that action at this time.
0 commit comments