-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Thor2, a class with less unexpected defaults
This new class allows having more expected defaults without breaking backward compatibility. Documentation can then recommend using Thor2 for new CLIs which will be more intuitive for users.
- Loading branch information
Showing
5 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -507,3 +507,5 @@ def help(command = nil, subcommand = false) | |
end | ||
end | ||
end | ||
|
||
require "thor/thor2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Thor | ||
class Thor2 < Thor | ||
# This is a class to use instead of Thor when declaring your CLI | ||
# This alternative works the same way as Thor, but has more common defaults: | ||
# * If there is a failure in the argument parsing and other Thor-side | ||
# things, the exit code will be non-zero | ||
# * Things that look like options but are not valid options will | ||
# will show an error of being unknown option instead of being | ||
# used as arguments. | ||
# * Make sure the default value of options is of the correct type | ||
# For backward compatibility reasons, these cannot be made default in | ||
# the regular `Thor` class | ||
# | ||
# This class is available in the top-level as Thor2, so you can do | ||
# class MyCli < Thor2 | ||
# ... | ||
# end | ||
|
||
# Fail on unknown options instead of treating them as argument | ||
check_unknown_options! | ||
|
||
# Make sure the default value of options is of the correct type | ||
check_default_type! | ||
|
||
# All failures should result in non-zero error code | ||
def self.exit_on_failure? | ||
true | ||
end | ||
end | ||
end | ||
|
||
::Thor2 = Thor::Thor2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "thor" | ||
|
||
class MySimpleThor2 < Thor2 | ||
class_option "verbose", :type => :boolean | ||
class_option "mode", :type => :string | ||
|
||
desc "checked", "a command with checked" | ||
def checked(*args) | ||
puts [options, args].inspect | ||
[options, args] | ||
end | ||
end | ||
|
||
MySimpleThor2.start(ARGV) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require "helper" | ||
|
||
describe Thor2 do | ||
describe "#check_unknown_options!" do | ||
it "still accept options and arguments" do | ||
stdout, _, status = run_thor_fixture_standalone('thor2', %w(checked command --verbose)) | ||
|
||
expect(stdout.strip).to eq [{"verbose" => true}, %w[command]].inspect | ||
expect(status.exitstatus).to eq(0) | ||
end | ||
|
||
it "does not accept if non-option that looks like an option is after an argument and exits with code 1" do | ||
_stdout, stderr, status = run_thor_fixture_standalone('thor2', %w(checked command --foo --bar)) | ||
expect(stderr.strip).to eq("Unknown switches '--foo, --bar'") | ||
expect(status.exitstatus).to eq(1) | ||
end | ||
end if RUBY_VERSION > "1.8.7" | ||
|
||
it "checks the default type" do | ||
expect do | ||
Class.new(Thor2) do | ||
option "bar", :type => :numeric, :default => "foo" | ||
end | ||
end.to raise_error(ArgumentError, "Expected numeric default value for '--bar'; got \"foo\" (string)") | ||
end | ||
end |