visionmedia-commander 2.5.7 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,16 @@
2
2
  require 'commander'
3
3
  require 'stringio'
4
4
 
5
+ # Mock terminal IO streams so we can spec against them
6
+
7
+ def mock_terminal
8
+ @input = StringIO.new
9
+ @output = StringIO.new
10
+ $terminal = HighLine.new @input, @output
11
+ end
12
+
13
+ # Create test command for usage within several specs
14
+
5
15
  def create_test_command
6
16
  command :test do |c|
7
17
  c.syntax = "test [options] <file>"
@@ -14,12 +24,17 @@ def create_test_command
14
24
  "test %s" % args.join
15
25
  end
16
26
  end
27
+ @command = command :test
17
28
  end
18
29
 
19
- def new_command_runner *args
30
+ # Create a new global command runner
31
+
32
+ def new_command_runner *args, &block
20
33
  $command_runner = Commander::Runner.new args
21
- program :name, "test"
22
- program :version, "1.2.3"
23
- program :description, "something"
34
+ program :name, 'test'
35
+ program :version, '1.2.3'
36
+ program :description, 'something'
24
37
  create_test_command
38
+ yield if block_given?
39
+ command_runner
25
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visionmedia-commander
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.7
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Holowaychuk
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-11 00:00:00 -07:00
12
+ date: 2009-03-12 00:00:00 -07:00
13
13
  default_executable: commander
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,17 +32,14 @@ extra_rdoc_files:
32
32
  - bin/commander
33
33
  - lib/commander/command.rb
34
34
  - lib/commander/core_ext/array.rb
35
- - lib/commander/core_ext/kernel.rb
36
35
  - lib/commander/core_ext/object.rb
37
36
  - lib/commander/core_ext/string.rb
38
37
  - lib/commander/core_ext.rb
39
- - lib/commander/fileutils.rb
40
38
  - lib/commander/help_formatters/base.rb
41
39
  - lib/commander/help_formatters/terminal/command_help.erb
42
40
  - lib/commander/help_formatters/terminal/help.erb
43
41
  - lib/commander/help_formatters/terminal.rb
44
42
  - lib/commander/help_formatters.rb
45
- - lib/commander/import.rb
46
43
  - lib/commander/runner.rb
47
44
  - lib/commander/user_interaction.rb
48
45
  - lib/commander/version.rb
@@ -57,17 +54,14 @@ files:
57
54
  - History.rdoc
58
55
  - lib/commander/command.rb
59
56
  - lib/commander/core_ext/array.rb
60
- - lib/commander/core_ext/kernel.rb
61
57
  - lib/commander/core_ext/object.rb
62
58
  - lib/commander/core_ext/string.rb
63
59
  - lib/commander/core_ext.rb
64
- - lib/commander/fileutils.rb
65
60
  - lib/commander/help_formatters/base.rb
66
61
  - lib/commander/help_formatters/terminal/command_help.erb
67
62
  - lib/commander/help_formatters/terminal/help.erb
68
63
  - lib/commander/help_formatters/terminal.rb
69
64
  - lib/commander/help_formatters.rb
70
- - lib/commander/import.rb
71
65
  - lib/commander/runner.rb
72
66
  - lib/commander/user_interaction.rb
73
67
  - lib/commander/version.rb
@@ -75,14 +69,18 @@ files:
75
69
  - Manifest
76
70
  - Rakefile
77
71
  - README.rdoc
78
- - spec/commander_spec.rb
72
+ - spec/command_spec.rb
73
+ - spec/core_ext/array_spec.rb
74
+ - spec/core_ext/object_spec.rb
79
75
  - spec/core_ext/string_spec.rb
80
76
  - spec/help_formatter_spec.rb
81
- - spec/import_spec.rb
77
+ - spec/runner_spec.rb
82
78
  - spec/spec_helper.rb
83
79
  - tasks/docs.rake
84
80
  - tasks/gemspec.rake
85
81
  - tasks/spec.rake
82
+ - test/pbar
83
+ - test/ui
86
84
  - Todo.rdoc
87
85
  has_rdoc: true
88
86
  homepage: http://github.com/visionmedia/commander
@@ -1,12 +0,0 @@
1
-
2
- require 'forwardable'
3
-
4
- ##
5
- # Make the following methods globally accessable:
6
- #
7
- # * HighLine#color
8
-
9
- module Kernel
10
- extend Forwardable
11
- def_delegators :$terminal, :color
12
- end
@@ -1,30 +0,0 @@
1
-
2
- require 'fileutils'
3
-
4
- module VerboseFileUtils
5
-
6
- include FileUtils
7
-
8
- ##
9
- # Wrap _methods_ with _action_ log message.
10
-
11
- def self.log action, *methods
12
- methods.each do |meth|
13
- define_method meth do |*args|
14
- Commander::UI.log "#{action}", *args
15
- super
16
- end
17
- end
18
- end
19
-
20
- log "remove", :rm, :rm_r, :rm_rf, :rmdir
21
- log "create", :touch, :mkdir, :mkdir_p
22
- log "copy", :cp, :cp_r
23
- log "move", :mv
24
- log "change", :cd
25
- log "link", :ln, :ln_s
26
- log "install", :install
27
-
28
- end
29
-
30
- include VerboseFileUtils
@@ -1,31 +0,0 @@
1
-
2
- require "forwardable"
3
-
4
- ##
5
- # Makes the following Commander methods globally available:
6
- #
7
- # * Commander::Runner#add_command
8
- # * Commander::Runner#get_command
9
- # * Commander::Runner#command
10
- # * Commander::Runner#commands
11
- # * Commander::Runner#program
12
- # * Commander::UI::ProgressBar#progress
13
-
14
- module Kernel
15
- extend Forwardable
16
- def_delegators :$command_runner, :add_command, :get_command, :command, :program, :run!, :commands
17
- def_delegators Commander::UI::ProgressBar, :progress
18
-
19
- def command_runner #:nodoc:
20
- $command_runner
21
- end
22
-
23
- alias_method :original_method_missing, :method_missing
24
- def method_missing meth, *args, &block
25
- if meth.to_s =~ /^ask_for_([\w]+)/
26
- $terminal.ask args.first, eval($1.camelcase)
27
- else
28
- original_method_missing(meth, *args, &block)
29
- end
30
- end
31
- end
@@ -1,241 +0,0 @@
1
-
2
- describe Commander do
3
-
4
- before :each do
5
- @input = StringIO.new
6
- @output = StringIO.new
7
- $terminal = HighLine.new @input, @output
8
- create_test_command
9
- end
10
-
11
- it "should allow access to all commands using #commands" do
12
- commands.length.should eql(2) # our test command as well as help default
13
- end
14
-
15
- it "should set program information using #program" do
16
- program :name, "test"
17
- program :version, "1.2.3"
18
- program :description, "just a test."
19
- program(:name).should eql("test")
20
- program(:version).should eql("1.2.3")
21
- program(:description).should eql("just a test.")
22
- end
23
-
24
- it "should allow arbitrary blocks of global help documentation" do
25
- program :help, 'Copyright', 'TJ Holowaychuk'
26
- program(:help)['Copyright'].should eql('TJ Holowaychuk')
27
- end
28
-
29
- it "should raise an error when crutial program info is not set" do
30
- new_command_runner '--help'
31
- program :name, ''
32
- lambda { run! }.should raise_error(Commander::Runner::CommandError)
33
- end
34
-
35
- it "should output program version using --version switch" do
36
- new_command_runner '--version'
37
- program :help_formatter, Commander::HelpFormatter::Base
38
- command_runner.run!
39
- @output.string.should eql("test 1.2.3\n")
40
- end
41
-
42
- it "should get command instances using #get_command" do
43
- get_command(:test).name.should eql('test')
44
- end
45
-
46
- it "should assign options" do
47
- get_command(:test).options.length.should eql(2)
48
- end
49
-
50
- it "should invoke #when_called with arguments properly" do
51
- get_command(:test).call([1, 2]).should eql("test 12")
52
- end
53
-
54
- it "should output invalid option message when invalid options passed to command" do
55
- new_command_runner 'test', '--invalid-option'
56
- command_runner.run!
57
- @output.string.should eql("invalid option: --invalid-option\n")
58
- end
59
-
60
- it "should output invalid command message when help sub-command does not exist" do
61
- new_command_runner 'help', 'does_not_exist'
62
- command_runner.run!
63
- @output.string.should eql("invalid command. Use --help for more information\n")
64
- end
65
-
66
- it "should locate command within arbitrary arguments passed" do
67
- new_command_runner '--help', '--arbitrary', 'test'
68
- command_runner.command_name_from_args.should eql('test')
69
- end
70
-
71
- it "should resolve active command from global options passed" do
72
- new_command_runner '--help', 'test'
73
- command_runner.active_command.should be_instance_of(Commander::Command)
74
- end
75
-
76
- it "should distinguish between switches and descriptions passed to #option" do
77
- get_command(:test).options[0][:description].should eql("trace description")
78
- get_command(:test).options[0][:switches].should eql(["-t", "--trace"])
79
- get_command(:test).options[1][:description].should eql("verbose description")
80
- get_command(:test).options[1][:switches].should eql(["--verbose"])
81
- get_command(:test).option "--test"
82
- get_command(:test).options[2][:description].should be_nil
83
- get_command(:test).options[2][:switches].should eql(["--test"])
84
- end
85
-
86
- it "should generate a symbol from switches" do
87
- get_command(:test).sym_from_switch("--trace").should eql(:trace)
88
- get_command(:test).sym_from_switch("--foo-bar").should eql(:foo_bar)
89
- get_command(:test).sym_from_switch("--[no]-feature").should eql(:feature)
90
- get_command(:test).sym_from_switch("--[no]-feature ARG").should eql(:feature)
91
- get_command(:test).sym_from_switch("--file [ARG]").should eql(:file)
92
- get_command(:test).sym_from_switch("--colors colors").should eql(:colors)
93
- end
94
-
95
- it "should resolve active command from invalid options passed" do
96
- new_command_runner '--help', 'test', '--arbitrary'
97
- command_runner.active_command.should be_instance_of(Commander::Command)
98
- end
99
-
100
- it "should raise invalid command error when the command is not found in the arguments passed"do
101
- new_command_runner '--help'
102
- lambda { command_runner.active_command }.should raise_error(Commander::Runner::InvalidCommandError)
103
- end
104
-
105
- it "should add options to previously created commands" do
106
- get_command(:test).option("--recursive") {}
107
- get_command(:test).options.length.should eql(3)
108
- end
109
-
110
- it "should call command option procs" do
111
- recursive = false
112
- get_command(:test).option("--recursive") { recursive = true }
113
- get_command(:test).run ['--recursive']
114
- recursive.should be_true
115
- end
116
-
117
- it "should call the #when_called proc when #run" do
118
- result = nil
119
- get_command(:test).when_called { |args, options| result = args.join(' ') }
120
- get_command(:test).run ['--trace', 'just', 'some', 'args']
121
- result.should eql("just some args")
122
- end
123
-
124
- it "should handle boolean options" do
125
- opts = nil
126
- get_command(:test).when_called { |args, options| opts = options }
127
- get_command(:test).run ['--trace', 'foo', 'bar']
128
- opts.trace.should be_true
129
- end
130
-
131
- it "should handle toggle options" do
132
- options = nil
133
- get_command(:test).when_called { |args, opts| options = opts }
134
- get_command(:test).option "--[no-]toggle"
135
- get_command(:test).run ['--no-toggle']
136
- options.toggle.should be_false
137
- end
138
-
139
- it "should handle manditory arg options" do
140
- options = nil
141
- get_command(:test).when_called { |args, opts| options = opts }
142
- get_command(:test).option "--manditory ARG"
143
- get_command(:test).run ['--manditory', "foo"]
144
- options.manditory.should eql("foo")
145
- lambda { get_command(:test).run ['--manditory'] }.should raise_error(OptionParser::MissingArgument)
146
- end
147
-
148
- it "should handle optional arg options" do
149
- options = nil
150
- get_command(:test).when_called { |args, opts| options = opts }
151
- get_command(:test).option "--optional [ARG]"
152
- get_command(:test).run ['--optional', "foo"]
153
- options.optional.should eql("foo")
154
- get_command(:test).run ['--optional']
155
- options.optiona.should be_nil
156
- end
157
-
158
- it "should handle list options" do
159
- options = nil
160
- get_command(:test).when_called { |args, opts| options = opts }
161
- get_command(:test).option "--list words", Array
162
- get_command(:test).run ['--list', "im,a,list"]
163
- options.list.should eql(["im", "a", "list"])
164
- end
165
-
166
- it "should initialize and call object when a class is passed to #when_called" do
167
- $_when_called_value = nil
168
- class HandleWhenCalled1
169
- def initialize(args, options) $_when_called_value = args.join('-') end
170
- end
171
- get_command(:test).when_called HandleWhenCalled1
172
- get_command(:test).call(["hello", "world"])
173
- $_when_called_value.should eql("hello-world")
174
- end
175
-
176
- it "should initialize and call object when a class is passed to #when_called with an arbitrary method" do
177
- class HandleWhenCalled2
178
- def arbitrary_method(args, options) args.join('-') end
179
- end
180
- get_command(:test).when_called HandleWhenCalled2, :arbitrary_method
181
- get_command(:test).call(["hello", "world"]).should eql("hello-world")
182
- end
183
-
184
- it "should call object when passed to #when_called " do
185
- class HandleWhenCalled3
186
- def arbitrary_method(args, options) args.join('-') end
187
- end
188
- get_command(:test).when_called HandleWhenCalled3.new, :arbitrary_method
189
- get_command(:test).call(["hello", "world"]).should eql("hello-world")
190
- end
191
-
192
- it "should populate options when passed before command name" do
193
- options = nil
194
- new_command_runner '--foo', 'test', 'some', 'args'
195
- get_command(:test).option "--foo"
196
- get_command(:test).when_called { |args, opts| options = opts }
197
- command_runner.run!
198
- options.foo.should be_true
199
- end
200
-
201
- it "should populate options when passed after command name" do
202
- options = nil
203
- new_command_runner 'test', '--foo', 'some', 'args'
204
- get_command(:test).option "--foo"
205
- get_command(:test).when_called { |args, opts| options = opts }
206
- command_runner.run!
207
- options.foo.should be_true
208
- end
209
-
210
- it "should allow multi-word strings as command names to be called correctly" do
211
- arguments = nil
212
- options = nil
213
- new_command_runner 'foo', 'bar', 'something', 'i', 'like', '--i-like', 'cookies'
214
- command 'foo bar something' do |c|
215
- c.option '--i-like WHAT'
216
- c.when_called { |args, opts| arguments, options = args, opts }
217
- end
218
- command_runner.command_name_from_args.should eql('foo bar something')
219
- command_runner.args_without_command.should eql(['i', 'like', '--i-like', 'cookies'])
220
- command_runner.run!
221
- arguments.should eql(['i', 'like'])
222
- options.i_like.should eql('cookies')
223
- end
224
-
225
- it "should allow multi-word strings as command names to be called correctly, with options before command name" do
226
- arguments = nil
227
- options = nil
228
- new_command_runner '--something', 'foo', 'bar', 'random_arg'
229
- command 'foo bar' do |c|
230
- c.option '--something'
231
- c.when_called { |args, opts| arguments, options = args, opts }
232
- end
233
- command_runner.command_name_from_args.should eql('foo bar')
234
- command_runner.args_without_command.should eql(['--something', 'random_arg'])
235
- command_runner.run!
236
- arguments.should eql(['random_arg'])
237
- options.something.should be_true
238
- end
239
-
240
-
241
- end