visionmedia-commander 3.0.3 → 3.1.0

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.
data/History.rdoc CHANGED
@@ -1,4 +1,11 @@
1
1
 
2
+ === 3.1.0 / 2009-03-13
3
+
4
+ * Added Command#inspect
5
+ * Added displaying of aliases in global help
6
+ * Added support for alias args
7
+ * Added #default_command
8
+
2
9
  === 3.0.3 / 2009-03-12
3
10
 
4
11
  * Added #alias_command
data/Manifest CHANGED
@@ -29,6 +29,7 @@ spec/spec_helper.rb
29
29
  tasks/docs.rake
30
30
  tasks/gemspec.rake
31
31
  tasks/spec.rake
32
+ test/foo
32
33
  test/pbar
33
34
  test/ui
34
35
  Todo.rdoc
data/README.rdoc CHANGED
@@ -138,6 +138,79 @@ simplify common tasks using the following methods:
138
138
  # 'Log' action to stdout
139
139
  log "create", "path/to/file.rb"
140
140
 
141
+ == Commander Goodies
142
+
143
+ === Option Defaults
144
+
145
+ The options struct passed to #when_called provides a #default method, allowing you
146
+ to set defaults in a clean manor to options which have not been set.
147
+
148
+ command :foo do |c|
149
+ c.option '--interval SECONDS', Integer, 'Interval in seconds'
150
+ c.option '--timeout SECONDS', Integer, 'Timeout in seconds'
151
+ c.when_called do |args, options|
152
+ options.default \
153
+ :interval => 2,
154
+ :timeout => 60
155
+ end
156
+ end
157
+
158
+ === Command Aliasing
159
+
160
+ Aliases can be created using the #alias_command method like below:
161
+
162
+ command :'install gem' do |c|
163
+ c.when_called { puts 'foo' }
164
+ end
165
+ alias_command :'gem install', :'install gem'
166
+
167
+ Or more complicated aliases can be made, passing any arguments
168
+ as if it was invoked via the command line:
169
+
170
+ command :'install gem' do |c|
171
+ c.syntax = 'install gem <name> [options]'
172
+ c.option '--dest DIR', String, 'Destination directory'
173
+ c.when_called { |args, options| puts "installing #{args.first} to #{options.dest}" }
174
+ end
175
+ alias_command :update, :'install gem', 'rubygems', '--dest', 'some_path'
176
+
177
+ $ foo update
178
+ # => installing rubygems to some_path
179
+
180
+ === Command Defaults
181
+
182
+ Although working with a sub-command executable framework provides many
183
+ benefits over a single command implementation, sometimes you still
184
+ want the ability to create a terse syntax for your command. With that
185
+ in mind we may use #default_command to help with this. Considering
186
+ our previous :'install gem' example:
187
+
188
+ default_command :update
189
+
190
+ $ foo
191
+ # => installing rubygems to some_path
192
+
193
+ Keeping in mind that commander searches for the longest possible match
194
+ when considering a sub-command, so if you were to pass arguments to foo
195
+ like below, expecting them to be passed to :update, this would be incorrect,
196
+ and would end up calling :'install gem', so be careful that the users do
197
+ not need to use command names within the arguments.
198
+
199
+ $ foo install gem
200
+ # => installing to
201
+
202
+ === Additional Global Help
203
+
204
+ Arbitrary help can be added using the following #program symbol:
205
+
206
+ program :help, 'Author', 'TJ Holowaychuk <tj@vision-media.ca>'
207
+
208
+ Which will output the rest of the help doc, along with:
209
+
210
+ AUTHOR:
211
+
212
+ TJ Holowaychuk <tj@vision-media.ca>
213
+
141
214
  == ASCII Tables
142
215
 
143
216
  For feature rich ASCII tables for your terminal app check out visionmedia's terminal-table gem at
data/Todo.rdoc CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  == Minor
8
8
 
9
+ * Add arbitrary help blocks to sub-commands as well
10
+ * Spec for changing out formatters (mock formatter)
9
11
  * Add optional default command when none is present
10
12
  * Fix multi-word command template creation via `commander init`
11
13
  * Consider global #color method again
data/commander.gemspec CHANGED
@@ -2,17 +2,17 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{commander}
5
- s.version = "3.0.3"
5
+ s.version = "3.1.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["TJ Holowaychuk"]
9
- s.date = %q{2009-03-12}
9
+ s.date = %q{2009-03-13}
10
10
  s.default_executable = %q{commander}
11
11
  s.description = %q{The complete solution for Ruby command-line executables}
12
12
  s.email = %q{tj@vision-media.ca}
13
13
  s.executables = ["commander"]
14
14
  s.extra_rdoc_files = ["bin/commander", "lib/commander/command.rb", "lib/commander/core_ext/array.rb", "lib/commander/core_ext/object.rb", "lib/commander/core_ext/string.rb", "lib/commander/core_ext.rb", "lib/commander/help_formatters/base.rb", "lib/commander/help_formatters/terminal/command_help.erb", "lib/commander/help_formatters/terminal/help.erb", "lib/commander/help_formatters/terminal.rb", "lib/commander/help_formatters.rb", "lib/commander/runner.rb", "lib/commander/user_interaction.rb", "lib/commander/version.rb", "lib/commander.rb", "README.rdoc", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
15
- s.files = ["bin/commander", "commander.gemspec", "History.rdoc", "lib/commander/command.rb", "lib/commander/core_ext/array.rb", "lib/commander/core_ext/object.rb", "lib/commander/core_ext/string.rb", "lib/commander/core_ext.rb", "lib/commander/help_formatters/base.rb", "lib/commander/help_formatters/terminal/command_help.erb", "lib/commander/help_formatters/terminal/help.erb", "lib/commander/help_formatters/terminal.rb", "lib/commander/help_formatters.rb", "lib/commander/runner.rb", "lib/commander/user_interaction.rb", "lib/commander/version.rb", "lib/commander.rb", "Manifest", "Rakefile", "README.rdoc", "spec/command_spec.rb", "spec/core_ext/array_spec.rb", "spec/core_ext/object_spec.rb", "spec/core_ext/string_spec.rb", "spec/help_formatters/base_spec.rb", "spec/help_formatters/terminal_spec.rb", "spec/runner_spec.rb", "spec/spec_helper.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "test/pbar", "test/ui", "Todo.rdoc"]
15
+ s.files = ["bin/commander", "commander.gemspec", "History.rdoc", "lib/commander/command.rb", "lib/commander/core_ext/array.rb", "lib/commander/core_ext/object.rb", "lib/commander/core_ext/string.rb", "lib/commander/core_ext.rb", "lib/commander/help_formatters/base.rb", "lib/commander/help_formatters/terminal/command_help.erb", "lib/commander/help_formatters/terminal/help.erb", "lib/commander/help_formatters/terminal.rb", "lib/commander/help_formatters.rb", "lib/commander/runner.rb", "lib/commander/user_interaction.rb", "lib/commander/version.rb", "lib/commander.rb", "Manifest", "Rakefile", "README.rdoc", "spec/command_spec.rb", "spec/core_ext/array_spec.rb", "spec/core_ext/object_spec.rb", "spec/core_ext/string_spec.rb", "spec/help_formatters/base_spec.rb", "spec/help_formatters/terminal_spec.rb", "spec/runner_spec.rb", "spec/spec_helper.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "test/foo", "test/pbar", "test/ui", "Todo.rdoc"]
16
16
  s.has_rdoc = true
17
17
  s.homepage = %q{http://github.com/visionmedia/commander}
18
18
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Commander", "--main", "README.rdoc"]
@@ -211,5 +211,9 @@ module Commander
211
211
  end
212
212
  end
213
213
 
214
+ def inspect #:nodoc:
215
+ "<Commander::Command:#{name}>"
216
+ end
217
+
214
218
  end
215
219
  end
@@ -16,7 +16,7 @@ class Object
16
16
  extend Forwardable
17
17
  include Commander::UI
18
18
 
19
- def_delegators :$command_runner, :add_command, :command, :program, :run!, :commands, :alias_command
19
+ def_delegators :$command_runner, :add_command, :command, :program, :run!, :commands, :alias_command, :default_command
20
20
  def_delegators Commander::UI::ProgressBar, :progress
21
21
 
22
22
  ##
@@ -8,14 +8,24 @@
8
8
  <%= program :description %>
9
9
 
10
10
  <%= $terminal.color "SUB-COMMANDS", :bold %>:
11
- <% @commands.each_pair do |name, command| %>
11
+ <% @commands.each_pair do |name, command| -%>
12
+ <% unless alias? name %>
12
13
  <%= "%-20s %s" % [command.name, command.summary || command.description] -%>
13
- <% end %>
14
+ <% end -%>
15
+ <% end -%>
16
+ <% unless @aliases.empty? %>
17
+
18
+ <%= $terminal.color "ALIASES", :bold %>:
19
+ <% @aliases.each do |alias_name, args| %>
20
+ <%= "%-20s %s %s" % [alias_name, command(alias_name).name, args.join(' ')] -%>
21
+ <% end -%>
22
+ <% end -%>
14
23
  <% if program :help -%>
24
+
15
25
  <% program(:help).each_pair do |title, body| %>
16
26
  <%= $terminal.color title.to_s.upcase, :bold %>:
17
-
27
+
18
28
  <%= body %>
19
- <% end %>
29
+ <% end -%>
20
30
  <% end -%>
21
31
 
@@ -26,7 +26,7 @@ module Commander
26
26
  # supplying +args+ for mocking, or arbitrary usage.
27
27
 
28
28
  def initialize args = ARGV
29
- @args, @commands, @options = args, {}, {}
29
+ @args, @commands, @options, @aliases = args, {}, {}, {}
30
30
  @program = program_defaults
31
31
  create_default_commands
32
32
  parse_global_options
@@ -40,7 +40,12 @@ module Commander
40
40
  case
41
41
  when options[:version] ; $terminal.say "#{program(:name)} #{program(:version)}"
42
42
  when options[:help] ; command(:help).run(*@args[1..-1])
43
- else active_command.run *args_without_command_name
43
+ else
44
+ if alias? command_name_from_args
45
+ active_command.run *(@aliases[command_name_from_args.to_s] + args_without_command_name)
46
+ else
47
+ active_command.run *args_without_command_name
48
+ end
44
49
  end
45
50
  rescue InvalidCommandError
46
51
  $terminal.say 'invalid command. Use --help for more information'
@@ -107,10 +112,20 @@ module Commander
107
112
  end
108
113
 
109
114
  ##
110
- # Alias command +name+ with +alias_name+.
115
+ # Alias command +name+ with +alias_name+. Optionallry +args+ may be passed
116
+ # as if they were being passed straight to the original command via the command-line.
111
117
 
112
- def alias_command alias_name, name
118
+ def alias_command alias_name, name, *args
113
119
  @commands[alias_name.to_s] = command name
120
+ @aliases[alias_name.to_s] = args
121
+ end
122
+
123
+ ##
124
+ # Default command +name+ to be used when no other
125
+ # command is found in the arguments.
126
+
127
+ def default_command name
128
+ @default_command = name
114
129
  end
115
130
 
116
131
  ##
@@ -120,6 +135,13 @@ module Commander
120
135
  @commands[command.name] = command
121
136
  end
122
137
 
138
+ ##
139
+ # Check if command +name+ is an alias.
140
+
141
+ def alias? name
142
+ @aliases.include? name.to_s
143
+ end
144
+
123
145
  ##
124
146
  # Check if a command +name+ exists.
125
147
 
@@ -139,7 +161,7 @@ module Commander
139
161
  # Supports multi-word commands, using the largest possible match.
140
162
 
141
163
  def command_name_from_args
142
- @__command_name_from_args ||= valid_command_names_from(*@args.dup).sort.last
164
+ @__command_name_from_args ||= (valid_command_names_from(*@args.dup).sort.last || @default_command)
143
165
  end
144
166
 
145
167
  ##
@@ -162,7 +184,7 @@ module Commander
162
184
 
163
185
  def args_without_command_name
164
186
  removed = []
165
- parts = command_name_from_args.split
187
+ parts = command_name_from_args.split rescue []
166
188
  @args.dup.delete_if do |arg|
167
189
  removed << arg if parts.include?(arg) and not removed.include?(arg)
168
190
  end
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Commander
3
- VERSION = '3.0.3'
3
+ VERSION = '3.1.0'
4
4
  end
@@ -5,13 +5,6 @@ describe Commander::HelpFormatter do
5
5
  mock_terminal
6
6
  end
7
7
 
8
- def run *args
9
- new_command_runner *args do
10
- program :help_formatter, Commander::HelpFormatter::Base
11
- end.run!
12
- @output.string
13
- end
14
-
15
8
  it "should display global help using --help switch" do
16
9
  run('--help').should == "Implement global help here\n"
17
10
  end
data/spec/runner_spec.rb CHANGED
@@ -35,46 +35,50 @@ describe Commander do
35
35
  end
36
36
 
37
37
  describe "#alias_command" do
38
- it "should allow aliasing of any command" do
38
+ it "should alias a command" do
39
39
  alias_command :foo, :test
40
40
  command(:foo).should == command(:test)
41
41
  end
42
+
43
+ it "should pass arguments passed to the alias when called" do
44
+ new_command_runner 'install', 'gem', 'commander' do
45
+ command :install do |c|
46
+ c.option '--gem-name NAME', 'Install a gem'
47
+ c.when_called { |_, options| options.gem_name.should == 'commander' }
48
+ end
49
+ alias_command :'install gem', :install, '--gem-name'
50
+ command(:install).should_receive(:run).once
51
+ end.run!
52
+ end
42
53
  end
43
54
 
44
55
  describe "--version" do
45
56
  it "should output program version" do
46
- new_command_runner '--version' do
47
- program :help_formatter, Commander::HelpFormatter::Base
48
- end.run!
49
- @output.string.should == "test 1.2.3\n"
57
+ run('--version').should == "test 1.2.3\n"
50
58
  end
51
59
  end
52
60
 
53
61
  describe "--help" do
54
62
  it "should not output an invalid command message" do
55
- new_command_runner('--help').run!
56
- @output.string.should_not == "invalid command. Use --help for more information\n"
63
+ run('--help').should_not == "invalid command. Use --help for more information\n"
57
64
  end
58
65
  end
59
66
 
60
67
  describe "with invalid options" do
61
68
  it "should output an invalid option message" do
62
- new_command_runner('test', '--invalid-option').run!
63
- @output.string.should == "invalid option: --invalid-option\n"
69
+ run('test', '--invalid-option').should == "invalid option: --invalid-option\n"
64
70
  end
65
71
  end
66
72
 
67
73
  describe "with invalid sub-command passed" do
68
74
  it "should output an invalid command message" do
69
- new_command_runner('foo').run!
70
- @output.string.should == "invalid command. Use --help for more information\n"
75
+ run('foo').should == "invalid command. Use --help for more information\n"
71
76
  end
72
77
  end
73
78
 
74
79
  describe "with invalid sub-command passed to help" do
75
80
  it "should output an invalid command message" do
76
- new_command_runner('help', 'does_not_exist').run!
77
- @output.string.should == "invalid command. Use --help for more information\n"
81
+ run('help', 'does_not_exist').should == "invalid command. Use --help for more information\n"
78
82
  end
79
83
  end
80
84
 
@@ -132,6 +136,41 @@ describe Commander do
132
136
  end
133
137
  end
134
138
 
139
+ describe "#default_command" do
140
+ it "should allow you to default any command when one is not explicitly passed" do
141
+ new_command_runner '--trace' do
142
+ default_command :test
143
+ command(:test).should_receive(:run).once
144
+ command_runner.active_command.should == command(:test)
145
+ end.run!
146
+ end
147
+
148
+ it "should not prevent other commands from being called" do
149
+ new_command_runner 'foo', 'bar', '--trace' do
150
+ default_command :test
151
+ command(:'foo bar'){}
152
+ command(:'foo bar').should_receive(:run).once
153
+ command_runner.active_command.should == command(:'foo bar')
154
+ end.run!
155
+ end
156
+
157
+ it "should not prevent longer commands to use the same words as the default" do
158
+ new_command_runner 'foo', 'bar', 'something'
159
+ default_command :'foo bar'
160
+ command(:'foo bar'){}
161
+ command(:'foo bar something'){}
162
+ command_runner.active_command.should == command(:'foo bar something')
163
+ end
164
+
165
+ it "should allow defaulting of command aliases" do
166
+ new_command_runner '--trace' do
167
+ default_command :foobar
168
+ alias_command :foobar, :test
169
+ command(:test).should_receive(:run).once
170
+ end.run!
171
+ end
172
+ end
173
+
135
174
  describe "should function correctly" do
136
175
  it "when options are passed before the command name" do
137
176
  new_command_runner '--trace', 'test', 'foo', 'bar' do
@@ -175,6 +214,15 @@ describe Commander do
175
214
  command_runner.args_without_command_name.should == ['--trace', 'something', 'my', 'command']
176
215
  end.run!
177
216
  end
217
+
218
+ it "when using multi-word commands with other commands using the same words" do
219
+ new_command_runner '--trace', 'my', 'command', 'something', 'my', 'command' do
220
+ command('my command') {}
221
+ command('my command something') {}
222
+ command_runner.command_name_from_args.should == 'my command something'
223
+ command_runner.args_without_command_name.should == ['--trace', 'my', 'command']
224
+ end.run!
225
+ end
178
226
  end
179
227
 
180
228
  end
data/spec/spec_helper.rb CHANGED
@@ -37,4 +37,11 @@ def new_command_runner *args, &block
37
37
  create_test_command
38
38
  yield if block
39
39
  command_runner
40
+ end
41
+
42
+ def run *args
43
+ new_command_runner *args do
44
+ program :help_formatter, Commander::HelpFormatter::Base
45
+ end.run!
46
+ @output.string
40
47
  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: 3.0.3
4
+ version: 3.1.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-12 00:00:00 -07:00
12
+ date: 2009-03-13 00:00:00 -07:00
13
13
  default_executable: commander
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -80,6 +80,7 @@ files:
80
80
  - tasks/docs.rake
81
81
  - tasks/gemspec.rake
82
82
  - tasks/spec.rake
83
+ - test/foo
83
84
  - test/pbar
84
85
  - test/ui
85
86
  - Todo.rdoc