File tree Expand file tree Collapse file tree 4 files changed +71
-2
lines changed Expand file tree Collapse file tree 4 files changed +71
-2
lines changed Original file line number Diff line number Diff line change @@ -507,3 +507,5 @@ def help(command = nil, subcommand = false)
507507 end
508508 end
509509end
510+
511+ require "thor/thor2"
Original file line number Diff line number Diff line change @@ -138,7 +138,8 @@ def attr_accessor(*) #:nodoc:
138138 end
139139
140140 # If you want to raise an error for unknown options, call check_unknown_options!
141- # This is disabled by default to allow dynamic invocations.
141+ # This is disabled by default in the Thor class to allow dynamic invocations.
142+ # This is enabled by default in the Thor2 class
142143 def check_unknown_options!
143144 @check_unknown_options = true
144145 end
@@ -153,7 +154,8 @@ def check_unknown_options?(config) #:nodoc:
153154
154155 # If you want to raise an error when the default value of an option does not match
155156 # the type call check_default_type!
156- # This is disabled by default for compatibility.
157+ # This is disabled by default in the Thor class for compatibility.
158+ # This is enabled by default in the Thor2 class
157159 def check_default_type!
158160 @check_default_type = true
159161 end
Original file line number Diff line number Diff line change 1+ class Thor
2+ class Thor2 < Thor
3+ # This is a class to use instead of Thor when declaring your CLI
4+ # This alternative works the same way as Thor, but has more common defaults:
5+ # * If there is a failure in the argument parsing and other Thor-side
6+ # things, the exit code will be non-zero
7+ # * Things that look like options but are not valid options will
8+ # will show an error of being unknown option instead of being
9+ # used as arguments.
10+ # * Make sure the default value of options is of the correct type
11+ # For backward compatibility reasons, these cannot be made default in
12+ # the regular `Thor` class
13+ #
14+ # This class is available in the top-level as Thor2, so you can do
15+ # class MyCli < Thor2
16+ # ...
17+ # end
18+
19+ # Fail on unknown options instead of treating them as argument
20+ check_unknown_options!
21+
22+ # Make sure the default value of options is of the correct type
23+ check_default_type!
24+
25+ # All failures should result in non-zero error code
26+ def self . exit_on_failure?
27+ true
28+ end
29+ end
30+ end
31+
32+ ::Thor2 = Thor ::Thor2
Original file line number Diff line number Diff line change 1+ require "helper"
2+
3+ describe Thor2 do
4+ my_script = Class . new ( Thor2 ) do
5+ class_option "verbose" , :type => :boolean
6+ class_option "mode" , :type => :string
7+
8+ desc "checked" , "a command with checked"
9+ def checked ( *args )
10+ [ options , args ]
11+ end
12+ end
13+
14+ describe "#check_unknown_options!" do
15+ it "still accept options and arguments" do
16+ expect ( my_script . start ( %w[ checked command --verbose ] ) ) . to eq [ { "verbose" => true } , %w[ command ] ]
17+ end
18+
19+ it "does not accept if non-option that looks like an option is after an argument" do
20+ expect ( capture ( :stderr ) do
21+ my_script . start ( %w[ checked command --foo --bar ] )
22+ end . strip ) . to eq ( "Unknown switches '--foo, --bar'" )
23+ end
24+ end
25+
26+ it "checks the default type" do
27+ expect do
28+ Class . new ( Thor2 ) do
29+ option "bar" , :type => :numeric , :default => "foo"
30+ end
31+ end . to raise_error ( ArgumentError , "Expected numeric default value for '--bar'; got \" foo\" (string)" )
32+ end
33+ end
You can’t perform that action at this time.
0 commit comments