thor 0.16.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +15 -0
- data/README.md +23 -6
- data/bin/thor +1 -1
- data/lib/thor/actions/create_file.rb +34 -35
- data/lib/thor/actions/create_link.rb +9 -5
- data/lib/thor/actions/directory.rb +33 -23
- data/lib/thor/actions/empty_directory.rb +75 -85
- data/lib/thor/actions/file_manipulation.rb +103 -36
- data/lib/thor/actions/inject_into_file.rb +46 -36
- data/lib/thor/actions.rb +90 -68
- data/lib/thor/base.rb +302 -244
- data/lib/thor/command.rb +142 -0
- data/lib/thor/core_ext/hash_with_indifferent_access.rb +52 -24
- data/lib/thor/error.rb +90 -10
- data/lib/thor/group.rb +70 -74
- data/lib/thor/invocation.rb +63 -55
- data/lib/thor/line_editor/basic.rb +37 -0
- data/lib/thor/line_editor/readline.rb +88 -0
- data/lib/thor/line_editor.rb +17 -0
- data/lib/thor/nested_context.rb +29 -0
- data/lib/thor/parser/argument.rb +24 -28
- data/lib/thor/parser/arguments.rb +110 -102
- data/lib/thor/parser/option.rb +53 -15
- data/lib/thor/parser/options.rb +174 -97
- data/lib/thor/parser.rb +4 -4
- data/lib/thor/rake_compat.rb +12 -11
- data/lib/thor/runner.rb +159 -155
- data/lib/thor/shell/basic.rb +216 -93
- data/lib/thor/shell/color.rb +53 -40
- data/lib/thor/shell/html.rb +61 -58
- data/lib/thor/shell.rb +29 -36
- data/lib/thor/util.rb +231 -213
- data/lib/thor/version.rb +1 -1
- data/lib/thor.rb +303 -166
- data/thor.gemspec +27 -24
- metadata +36 -226
- data/.gitignore +0 -44
- data/.rspec +0 -2
- data/.travis.yml +0 -7
- data/CHANGELOG.rdoc +0 -134
- data/Gemfile +0 -15
- data/Thorfile +0 -30
- data/bin/rake2thor +0 -86
- data/lib/thor/core_ext/dir_escape.rb +0 -0
- data/lib/thor/core_ext/file_binary_read.rb +0 -9
- data/lib/thor/core_ext/ordered_hash.rb +0 -100
- data/lib/thor/task.rb +0 -132
- data/spec/actions/create_file_spec.rb +0 -170
- data/spec/actions/create_link_spec.rb +0 -81
- data/spec/actions/directory_spec.rb +0 -149
- data/spec/actions/empty_directory_spec.rb +0 -130
- data/spec/actions/file_manipulation_spec.rb +0 -370
- data/spec/actions/inject_into_file_spec.rb +0 -135
- data/spec/actions_spec.rb +0 -331
- data/spec/base_spec.rb +0 -279
- data/spec/core_ext/hash_with_indifferent_access_spec.rb +0 -43
- data/spec/core_ext/ordered_hash_spec.rb +0 -115
- data/spec/exit_condition_spec.rb +0 -19
- data/spec/fixtures/application.rb +0 -2
- data/spec/fixtures/app{1}/README +0 -3
- data/spec/fixtures/bundle/execute.rb +0 -6
- data/spec/fixtures/bundle/main.thor +0 -1
- data/spec/fixtures/doc/%file_name%.rb.tt +0 -1
- data/spec/fixtures/doc/COMMENTER +0 -10
- data/spec/fixtures/doc/README +0 -3
- data/spec/fixtures/doc/block_helper.rb +0 -3
- data/spec/fixtures/doc/components/.empty_directory +0 -0
- data/spec/fixtures/doc/config.rb +0 -1
- data/spec/fixtures/doc/config.yaml.tt +0 -1
- data/spec/fixtures/enum.thor +0 -10
- data/spec/fixtures/group.thor +0 -114
- data/spec/fixtures/invoke.thor +0 -112
- data/spec/fixtures/path with spaces +0 -0
- data/spec/fixtures/script.thor +0 -190
- data/spec/fixtures/task.thor +0 -10
- data/spec/group_spec.rb +0 -216
- data/spec/invocation_spec.rb +0 -100
- data/spec/parser/argument_spec.rb +0 -53
- data/spec/parser/arguments_spec.rb +0 -66
- data/spec/parser/option_spec.rb +0 -202
- data/spec/parser/options_spec.rb +0 -330
- data/spec/rake_compat_spec.rb +0 -72
- data/spec/register_spec.rb +0 -135
- data/spec/runner_spec.rb +0 -241
- data/spec/shell/basic_spec.rb +0 -300
- data/spec/shell/color_spec.rb +0 -81
- data/spec/shell/html_spec.rb +0 -32
- data/spec/shell_spec.rb +0 -47
- data/spec/spec_helper.rb +0 -59
- data/spec/task_spec.rb +0 -80
- data/spec/thor_spec.rb +0 -418
- data/spec/util_spec.rb +0 -196
data/spec/fixtures/invoke.thor
DELETED
@@ -1,112 +0,0 @@
|
|
1
|
-
class A < Thor
|
2
|
-
include Thor::Actions
|
3
|
-
|
4
|
-
desc "one", "invoke one"
|
5
|
-
def one
|
6
|
-
p 1
|
7
|
-
invoke :two
|
8
|
-
invoke :three
|
9
|
-
end
|
10
|
-
|
11
|
-
desc "two", "invoke two"
|
12
|
-
def two
|
13
|
-
p 2
|
14
|
-
invoke :three
|
15
|
-
end
|
16
|
-
|
17
|
-
desc "three", "invoke three"
|
18
|
-
def three
|
19
|
-
p 3
|
20
|
-
end
|
21
|
-
|
22
|
-
desc "four", "invoke four"
|
23
|
-
def four
|
24
|
-
p 4
|
25
|
-
invoke "defined:five"
|
26
|
-
end
|
27
|
-
|
28
|
-
desc "five N", "check if number is equal 5"
|
29
|
-
def five(number)
|
30
|
-
number == 5
|
31
|
-
end
|
32
|
-
|
33
|
-
desc "invoker", "invoke a b task"
|
34
|
-
def invoker(*args)
|
35
|
-
invoke :b, :one, ["Jose"]
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
class B < Thor
|
40
|
-
class_option :last_name, :type => :string
|
41
|
-
|
42
|
-
desc "one FIRST_NAME", "invoke one"
|
43
|
-
def one(first_name)
|
44
|
-
"#{options.last_name}, #{first_name}"
|
45
|
-
end
|
46
|
-
|
47
|
-
desc "two", "invoke two"
|
48
|
-
def two
|
49
|
-
options
|
50
|
-
end
|
51
|
-
|
52
|
-
desc "three", "invoke three"
|
53
|
-
def three
|
54
|
-
self
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
class C < Thor::Group
|
59
|
-
include Thor::Actions
|
60
|
-
|
61
|
-
def one
|
62
|
-
p 1
|
63
|
-
end
|
64
|
-
|
65
|
-
def two
|
66
|
-
p 2
|
67
|
-
end
|
68
|
-
|
69
|
-
def three
|
70
|
-
p 3
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
class Defined < Thor::Group
|
75
|
-
class_option :unused, :type => :boolean, :desc => "This option has no use"
|
76
|
-
|
77
|
-
def one
|
78
|
-
p 1
|
79
|
-
invoke "a:two"
|
80
|
-
invoke "a:three"
|
81
|
-
invoke "a:four"
|
82
|
-
invoke "defined:five"
|
83
|
-
end
|
84
|
-
|
85
|
-
def five
|
86
|
-
p 5
|
87
|
-
end
|
88
|
-
|
89
|
-
def print_status
|
90
|
-
say_status :finished, :counting
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
class E < Thor::Group
|
95
|
-
invoke Defined
|
96
|
-
end
|
97
|
-
|
98
|
-
class F < Thor::Group
|
99
|
-
invoke "b:one" do |instance, klass, task|
|
100
|
-
instance.invoke klass, task, [ "Jose" ], :last_name => "Valim"
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
class G < Thor::Group
|
105
|
-
class_option :invoked, :type => :string, :default => "defined"
|
106
|
-
invoke_from_option :invoked
|
107
|
-
end
|
108
|
-
|
109
|
-
class H < Thor::Group
|
110
|
-
class_option :defined, :type => :boolean, :default => true
|
111
|
-
invoke_from_option :defined
|
112
|
-
end
|
File without changes
|
data/spec/fixtures/script.thor
DELETED
@@ -1,190 +0,0 @@
|
|
1
|
-
class MyScript < Thor
|
2
|
-
check_unknown_options! :except => :with_optional
|
3
|
-
|
4
|
-
attr_accessor :some_attribute
|
5
|
-
attr_writer :another_attribute
|
6
|
-
attr_reader :another_attribute
|
7
|
-
|
8
|
-
group :script
|
9
|
-
default_task :example_default_task
|
10
|
-
|
11
|
-
map "-T" => :animal, ["-f", "--foo"] => :foo
|
12
|
-
|
13
|
-
map "animal_prison" => "zoo"
|
14
|
-
|
15
|
-
desc "zoo", "zoo around"
|
16
|
-
def zoo
|
17
|
-
true
|
18
|
-
end
|
19
|
-
|
20
|
-
desc "animal TYPE", "horse around"
|
21
|
-
|
22
|
-
no_tasks do
|
23
|
-
def this_is_not_a_task
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def animal(type)
|
28
|
-
[type]
|
29
|
-
end
|
30
|
-
|
31
|
-
map "hid" => "hidden"
|
32
|
-
|
33
|
-
desc "hidden TYPE", "this is hidden", :hide => true
|
34
|
-
def hidden(type)
|
35
|
-
[type]
|
36
|
-
end
|
37
|
-
|
38
|
-
map "fu" => "zoo"
|
39
|
-
|
40
|
-
desc "foo BAR", <<END
|
41
|
-
do some fooing
|
42
|
-
This is more info!
|
43
|
-
Everyone likes more info!
|
44
|
-
END
|
45
|
-
method_option :force, :type => :boolean, :desc => "Force to do some fooing"
|
46
|
-
def foo(bar)
|
47
|
-
[bar, options]
|
48
|
-
end
|
49
|
-
|
50
|
-
desc "example_default_task", "example!"
|
51
|
-
method_options :with => :string
|
52
|
-
def example_default_task
|
53
|
-
options.empty? ? "default task" : options
|
54
|
-
end
|
55
|
-
|
56
|
-
desc "call_myself_with_wrong_arity", "get the right error"
|
57
|
-
def call_myself_with_wrong_arity
|
58
|
-
call_myself_with_wrong_arity(4)
|
59
|
-
end
|
60
|
-
|
61
|
-
desc "call_unexistent_method", "Call unexistent method inside a task"
|
62
|
-
def call_unexistent_method
|
63
|
-
boom!
|
64
|
-
end
|
65
|
-
|
66
|
-
desc "long_description", "a" * 80
|
67
|
-
long_desc <<-D
|
68
|
-
This is a really really really long description.
|
69
|
-
Here you go. So very long.
|
70
|
-
|
71
|
-
It even has two paragraphs.
|
72
|
-
D
|
73
|
-
def long_description
|
74
|
-
end
|
75
|
-
|
76
|
-
desc "name-with-dashes", "Ensure normalization of task names"
|
77
|
-
def name_with_dashes
|
78
|
-
end
|
79
|
-
|
80
|
-
method_options :all => :boolean
|
81
|
-
method_option :lazy, :lazy_default => "yes"
|
82
|
-
method_option :lazy_numeric, :type => :numeric, :lazy_default => 42
|
83
|
-
method_option :lazy_array, :type => :array, :lazy_default => %w[eat at joes]
|
84
|
-
method_option :lazy_hash, :type => :hash, :lazy_default => {'swedish' => 'meatballs'}
|
85
|
-
desc "with_optional NAME", "invoke with optional name"
|
86
|
-
def with_optional(name=nil, *args)
|
87
|
-
[ name, options, args ]
|
88
|
-
end
|
89
|
-
|
90
|
-
class AnotherScript < Thor
|
91
|
-
desc "baz", "do some bazing"
|
92
|
-
def baz
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
private
|
97
|
-
|
98
|
-
def method_missing(meth, *args)
|
99
|
-
if meth == :boom!
|
100
|
-
super
|
101
|
-
else
|
102
|
-
[meth, args]
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
desc "what", "what"
|
107
|
-
def what
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
class MyChildScript < MyScript
|
112
|
-
remove_task :bar
|
113
|
-
|
114
|
-
method_options :force => :boolean, :param => :numeric
|
115
|
-
def initialize(*args)
|
116
|
-
super
|
117
|
-
end
|
118
|
-
|
119
|
-
desc "zoo", "zoo around"
|
120
|
-
method_options :param => :required
|
121
|
-
def zoo
|
122
|
-
options
|
123
|
-
end
|
124
|
-
|
125
|
-
desc "animal TYPE", "horse around"
|
126
|
-
def animal(type)
|
127
|
-
[type, options]
|
128
|
-
end
|
129
|
-
method_option :other, :type => :string, :default => "method default", :for => :animal
|
130
|
-
desc "animal KIND", "fish around", :for => :animal
|
131
|
-
|
132
|
-
desc "boom", "explodes everything"
|
133
|
-
def boom
|
134
|
-
end
|
135
|
-
|
136
|
-
remove_task :boom, :undefine => true
|
137
|
-
end
|
138
|
-
|
139
|
-
class Barn < Thor
|
140
|
-
desc "open [ITEM]", "open the barn door"
|
141
|
-
def open(item = nil)
|
142
|
-
if item == "shotgun"
|
143
|
-
puts "That's going to leave a mark."
|
144
|
-
else
|
145
|
-
puts "Open sesame!"
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
desc "paint [COLOR]", "paint the barn"
|
150
|
-
method_option :coats, :type => :numeric, :default => 2, :desc => 'how many coats of paint'
|
151
|
-
def paint(color='red')
|
152
|
-
puts "#{options[:coats]} coats of #{color} paint"
|
153
|
-
end
|
154
|
-
|
155
|
-
end
|
156
|
-
|
157
|
-
module Scripts
|
158
|
-
class MyScript < MyChildScript
|
159
|
-
argument :accessor, :type => :string
|
160
|
-
class_options :force => :boolean
|
161
|
-
method_option :new_option, :type => :string, :for => :example_default_task
|
162
|
-
|
163
|
-
def zoo
|
164
|
-
self.accessor
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
class MyDefaults < Thor
|
169
|
-
check_unknown_options!
|
170
|
-
|
171
|
-
namespace :default
|
172
|
-
desc "cow", "prints 'moo'"
|
173
|
-
def cow
|
174
|
-
puts "moo"
|
175
|
-
end
|
176
|
-
|
177
|
-
desc "task_conflict", "only gets called when prepended with a colon"
|
178
|
-
def task_conflict
|
179
|
-
puts "task"
|
180
|
-
end
|
181
|
-
|
182
|
-
desc "barn", "commands to manage the barn"
|
183
|
-
subcommand "barn", Barn
|
184
|
-
end
|
185
|
-
|
186
|
-
class ChildDefault < Thor
|
187
|
-
namespace "default:child"
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
data/spec/fixtures/task.thor
DELETED
data/spec/group_spec.rb
DELETED
@@ -1,216 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe Thor::Group do
|
4
|
-
describe "task" do
|
5
|
-
it "allows to use private methods from parent class as tasks" do
|
6
|
-
ChildGroup.start.should == ["bar", "foo", "baz"]
|
7
|
-
ChildGroup.new.baz("bar").should == "bar"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
describe "#start" do
|
12
|
-
it "invokes all the tasks under the Thor group" do
|
13
|
-
MyCounter.start(["1", "2", "--third", "3"]).should == [ 1, 2, 3 ]
|
14
|
-
end
|
15
|
-
|
16
|
-
it "uses argument default value" do
|
17
|
-
MyCounter.start(["1", "--third", "3"]).should == [ 1, 2, 3 ]
|
18
|
-
end
|
19
|
-
|
20
|
-
it "invokes all the tasks in the Thor group and his parents" do
|
21
|
-
BrokenCounter.start(["1", "2", "--third", "3"]).should == [ nil, 2, 3, false, 5 ]
|
22
|
-
end
|
23
|
-
|
24
|
-
it "raises an error if a required argument is added after a non-required" do
|
25
|
-
lambda {
|
26
|
-
MyCounter.argument(:foo, :type => :string)
|
27
|
-
}.should raise_error(ArgumentError, 'You cannot have "foo" as required argument after the non-required argument "second".')
|
28
|
-
end
|
29
|
-
|
30
|
-
it "raises when an exception happens within the task call" do
|
31
|
-
lambda { BrokenCounter.start(["1", "2", "--fail"]) }.should raise_error
|
32
|
-
end
|
33
|
-
|
34
|
-
it "raises an error when a Thor group task expects arguments" do
|
35
|
-
lambda { WhinyGenerator.start }.should raise_error(ArgumentError, /thor wrong_arity takes 1 argument, but it should not/)
|
36
|
-
end
|
37
|
-
|
38
|
-
it "invokes help message if any of the shortcuts is given" do
|
39
|
-
MyCounter.should_receive(:help)
|
40
|
-
MyCounter.start(["-h"])
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe "#desc" do
|
45
|
-
it "sets the description for a given class" do
|
46
|
-
MyCounter.desc.should == "Description:\n This generator runs three tasks: one, two and three.\n"
|
47
|
-
end
|
48
|
-
|
49
|
-
it "can be inherited" do
|
50
|
-
BrokenCounter.desc.should == "Description:\n This generator runs three tasks: one, two and three.\n"
|
51
|
-
end
|
52
|
-
|
53
|
-
it "can be nil" do
|
54
|
-
WhinyGenerator.desc.should be_nil
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
describe "#help" do
|
59
|
-
before do
|
60
|
-
@content = capture(:stdout){ MyCounter.help(Thor::Base.shell.new) }
|
61
|
-
end
|
62
|
-
|
63
|
-
it "provides usage information" do
|
64
|
-
@content.should =~ /my_counter N \[N\]/
|
65
|
-
end
|
66
|
-
|
67
|
-
it "shows description" do
|
68
|
-
@content.should =~ /Description:/
|
69
|
-
@content.should =~ /This generator runs three tasks: one, two and three./
|
70
|
-
end
|
71
|
-
|
72
|
-
it "shows options information" do
|
73
|
-
@content.should =~ /Options/
|
74
|
-
@content.should =~ /\[\-\-third=THREE\]/
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
describe "#invoke" do
|
79
|
-
before do
|
80
|
-
@content = capture(:stdout){ E.start }
|
81
|
-
end
|
82
|
-
|
83
|
-
it "allows to invoke a class from the class binding" do
|
84
|
-
@content.should =~ /1\n2\n3\n4\n5\n/
|
85
|
-
end
|
86
|
-
|
87
|
-
it "shows invocation information to the user" do
|
88
|
-
@content.should =~ /invoke Defined/
|
89
|
-
end
|
90
|
-
|
91
|
-
it "uses padding on status generated by the invoked class" do
|
92
|
-
@content.should =~ /finished counting/
|
93
|
-
end
|
94
|
-
|
95
|
-
it "allows invocation to be configured with blocks" do
|
96
|
-
capture(:stdout) do
|
97
|
-
F.start.should == ["Valim, Jose"]
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
it "shows invoked options on help" do
|
102
|
-
content = capture(:stdout){ E.help(Thor::Base.shell.new) }
|
103
|
-
content.should =~ /Defined options:/
|
104
|
-
content.should =~ /\[--unused\]/
|
105
|
-
content.should =~ /# This option has no use/
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
describe "#invoke_from_option" do
|
110
|
-
describe "with default type" do
|
111
|
-
before do
|
112
|
-
@content = capture(:stdout){ G.start }
|
113
|
-
end
|
114
|
-
|
115
|
-
it "allows to invoke a class from the class binding by a default option" do
|
116
|
-
@content.should =~ /1\n2\n3\n4\n5\n/
|
117
|
-
end
|
118
|
-
|
119
|
-
it "does not invoke if the option is nil" do
|
120
|
-
capture(:stdout){ G.start(["--skip-invoked"]) }.should_not =~ /invoke/
|
121
|
-
end
|
122
|
-
|
123
|
-
it "prints a message if invocation cannot be found" do
|
124
|
-
content = capture(:stdout){ G.start(["--invoked", "unknown"]) }
|
125
|
-
content.should =~ /error unknown \[not found\]/
|
126
|
-
end
|
127
|
-
|
128
|
-
it "allows to invoke a class from the class binding by the given option" do
|
129
|
-
content = capture(:stdout){ G.start(["--invoked", "e"]) }
|
130
|
-
content.should =~ /invoke e/
|
131
|
-
end
|
132
|
-
|
133
|
-
it "shows invocation information to the user" do
|
134
|
-
@content.should =~ /invoke defined/
|
135
|
-
end
|
136
|
-
|
137
|
-
it "uses padding on status generated by the invoked class" do
|
138
|
-
@content.should =~ /finished counting/
|
139
|
-
end
|
140
|
-
|
141
|
-
it "shows invoked options on help" do
|
142
|
-
content = capture(:stdout){ G.help(Thor::Base.shell.new) }
|
143
|
-
content.should =~ /defined options:/
|
144
|
-
content.should =~ /\[--unused\]/
|
145
|
-
content.should =~ /# This option has no use/
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
describe "with boolean type" do
|
150
|
-
before do
|
151
|
-
@content = capture(:stdout){ H.start }
|
152
|
-
end
|
153
|
-
|
154
|
-
it "allows to invoke a class from the class binding by a default option" do
|
155
|
-
@content.should =~ /1\n2\n3\n4\n5\n/
|
156
|
-
end
|
157
|
-
|
158
|
-
it "does not invoke if the option is false" do
|
159
|
-
capture(:stdout){ H.start(["--no-defined"]) }.should_not =~ /invoke/
|
160
|
-
end
|
161
|
-
|
162
|
-
it "shows invocation information to the user" do
|
163
|
-
@content.should =~ /invoke defined/
|
164
|
-
end
|
165
|
-
|
166
|
-
it "uses padding on status generated by the invoked class" do
|
167
|
-
@content.should =~ /finished counting/
|
168
|
-
end
|
169
|
-
|
170
|
-
it "shows invoked options on help" do
|
171
|
-
content = capture(:stdout){ H.help(Thor::Base.shell.new) }
|
172
|
-
content.should =~ /defined options:/
|
173
|
-
content.should =~ /\[--unused\]/
|
174
|
-
content.should =~ /# This option has no use/
|
175
|
-
end
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
describe "edge-cases" do
|
180
|
-
it "can handle boolean options followed by arguments" do
|
181
|
-
klass = Class.new(Thor::Group) do
|
182
|
-
desc "say hi to name"
|
183
|
-
argument :name, :type => :string
|
184
|
-
class_option :loud, :type => :boolean
|
185
|
-
|
186
|
-
def hi
|
187
|
-
name.upcase! if options[:loud]
|
188
|
-
"Hi #{name}"
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
klass.start(["jose"]).should == ["Hi jose"]
|
193
|
-
klass.start(["jose", "--loud"]).should == ["Hi JOSE"]
|
194
|
-
klass.start(["--loud", "jose"]).should == ["Hi JOSE"]
|
195
|
-
end
|
196
|
-
|
197
|
-
it "provides extra args as `args`" do
|
198
|
-
klass = Class.new(Thor::Group) do
|
199
|
-
desc "say hi to name"
|
200
|
-
argument :name, :type => :string
|
201
|
-
class_option :loud, :type => :boolean
|
202
|
-
|
203
|
-
def hi
|
204
|
-
name.upcase! if options[:loud]
|
205
|
-
out = "Hi #{name}"
|
206
|
-
out << ": " << args.join(", ") unless args.empty?
|
207
|
-
out
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
klass.start(["jose"]).should == ["Hi jose"]
|
212
|
-
klass.start(["jose", "--loud"]).should == ["Hi JOSE"]
|
213
|
-
klass.start(["--loud", "jose"]).should == ["Hi JOSE"]
|
214
|
-
end
|
215
|
-
end
|
216
|
-
end
|
data/spec/invocation_spec.rb
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
require 'thor/base'
|
3
|
-
|
4
|
-
describe Thor::Invocation do
|
5
|
-
describe "#invoke" do
|
6
|
-
it "invokes a task inside another task" do
|
7
|
-
capture(:stdout){ A.new.invoke(:two) }.should == "2\n3\n"
|
8
|
-
end
|
9
|
-
|
10
|
-
it "invokes a task just once" do
|
11
|
-
capture(:stdout){ A.new.invoke(:one) }.should == "1\n2\n3\n"
|
12
|
-
end
|
13
|
-
|
14
|
-
it "invokes a task just once even if they belongs to different classes" do
|
15
|
-
capture(:stdout){ Defined.new.invoke(:one) }.should == "1\n2\n3\n4\n5\n"
|
16
|
-
end
|
17
|
-
|
18
|
-
it "invokes a task with arguments" do
|
19
|
-
A.new.invoke(:five, [5]).should be_true
|
20
|
-
A.new.invoke(:five, [7]).should be_false
|
21
|
-
end
|
22
|
-
|
23
|
-
it "invokes the default task if none is given to a Thor class" do
|
24
|
-
content = capture(:stdout){ A.new.invoke("b") }
|
25
|
-
content.should =~ /Tasks/
|
26
|
-
content.should =~ /LAST_NAME/
|
27
|
-
end
|
28
|
-
|
29
|
-
it "accepts a class as argument without a task to invoke" do
|
30
|
-
content = capture(:stdout){ A.new.invoke(B) }
|
31
|
-
content.should =~ /Tasks/
|
32
|
-
content.should =~ /LAST_NAME/
|
33
|
-
end
|
34
|
-
|
35
|
-
it "accepts a class as argument with a task to invoke" do
|
36
|
-
base = A.new([], :last_name => "Valim")
|
37
|
-
base.invoke(B, :one, ["Jose"]).should == "Valim, Jose"
|
38
|
-
end
|
39
|
-
|
40
|
-
it "allows customized options to be given" do
|
41
|
-
base = A.new([], :last_name => "Wrong")
|
42
|
-
base.invoke(B, :one, ["Jose"], :last_name => "Valim").should == "Valim, Jose"
|
43
|
-
end
|
44
|
-
|
45
|
-
it "reparses options in the new class" do
|
46
|
-
A.start(["invoker", "--last-name", "Valim"]).should == "Valim, Jose"
|
47
|
-
end
|
48
|
-
|
49
|
-
it "shares initialize options with invoked class" do
|
50
|
-
A.new([], :foo => :bar).invoke("b:two").should == { "foo" => :bar }
|
51
|
-
end
|
52
|
-
|
53
|
-
it "dump configuration values to be used in the invoked class" do
|
54
|
-
base = A.new
|
55
|
-
base.invoke("b:three").shell.should == base.shell
|
56
|
-
end
|
57
|
-
|
58
|
-
it "allow extra configuration values to be given" do
|
59
|
-
base, shell = A.new, Thor::Base.shell.new
|
60
|
-
base.invoke("b:three", [], {}, :shell => shell).shell.should == shell
|
61
|
-
end
|
62
|
-
|
63
|
-
it "invokes a Thor::Group and all of its tasks" do
|
64
|
-
capture(:stdout){ A.new.invoke(:c) }.should == "1\n2\n3\n"
|
65
|
-
end
|
66
|
-
|
67
|
-
it "does not invoke a Thor::Group twice" do
|
68
|
-
base = A.new
|
69
|
-
silence(:stdout){ base.invoke(:c) }
|
70
|
-
capture(:stdout){ base.invoke(:c) }.should be_empty
|
71
|
-
end
|
72
|
-
|
73
|
-
it "does not invoke any of Thor::Group tasks twice" do
|
74
|
-
base = A.new
|
75
|
-
silence(:stdout){ base.invoke(:c) }
|
76
|
-
capture(:stdout){ base.invoke("c:one") }.should be_empty
|
77
|
-
end
|
78
|
-
|
79
|
-
it "raises Thor::UndefinedTaskError if the task can't be found" do
|
80
|
-
lambda do
|
81
|
-
A.new.invoke("foo:bar")
|
82
|
-
end.should raise_error(Thor::UndefinedTaskError)
|
83
|
-
end
|
84
|
-
|
85
|
-
it "raises Thor::UndefinedTaskError if the task can't be found even if all tasks where already executed" do
|
86
|
-
base = C.new
|
87
|
-
silence(:stdout){ base.invoke_all }
|
88
|
-
|
89
|
-
lambda do
|
90
|
-
base.invoke("foo:bar")
|
91
|
-
end.should raise_error(Thor::UndefinedTaskError)
|
92
|
-
end
|
93
|
-
|
94
|
-
it "raises an error if a non Thor class is given" do
|
95
|
-
lambda do
|
96
|
-
A.new.invoke(Object)
|
97
|
-
end.should raise_error(RuntimeError, "Expected Thor class, got Object")
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
require 'thor/parser'
|
3
|
-
|
4
|
-
describe Thor::Argument do
|
5
|
-
|
6
|
-
def argument(name, options={})
|
7
|
-
@argument ||= Thor::Argument.new(name, options)
|
8
|
-
end
|
9
|
-
|
10
|
-
describe "errors" do
|
11
|
-
it "raises an error if name is not supplied" do
|
12
|
-
lambda {
|
13
|
-
argument(nil)
|
14
|
-
}.should raise_error(ArgumentError, "Argument name can't be nil.")
|
15
|
-
end
|
16
|
-
|
17
|
-
it "raises an error if type is unknown" do
|
18
|
-
lambda {
|
19
|
-
argument(:task, :type => :unknown)
|
20
|
-
}.should raise_error(ArgumentError, "Type :unknown is not valid for arguments.")
|
21
|
-
end
|
22
|
-
|
23
|
-
it "raises an error if argument is required and have default values" do
|
24
|
-
lambda {
|
25
|
-
argument(:task, :type => :string, :default => "bar", :required => true)
|
26
|
-
}.should raise_error(ArgumentError, "An argument cannot be required and have default value.")
|
27
|
-
end
|
28
|
-
|
29
|
-
it "raises an error if enum isn't an array" do
|
30
|
-
lambda {
|
31
|
-
argument(:task, :type => :string, :enum => "bar")
|
32
|
-
}.should raise_error(ArgumentError, "An argument cannot have an enum other than an array.")
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
describe "#usage" do
|
37
|
-
it "returns usage for string types" do
|
38
|
-
argument(:foo, :type => :string).usage.should == "FOO"
|
39
|
-
end
|
40
|
-
|
41
|
-
it "returns usage for numeric types" do
|
42
|
-
argument(:foo, :type => :numeric).usage.should == "N"
|
43
|
-
end
|
44
|
-
|
45
|
-
it "returns usage for array types" do
|
46
|
-
argument(:foo, :type => :array).usage.should == "one two three"
|
47
|
-
end
|
48
|
-
|
49
|
-
it "returns usage for hash types" do
|
50
|
-
argument(:foo, :type => :hash).usage.should == "key:value"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|