visionmedia-commander 3.1.2 → 3.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,4 +1,19 @@
1
1
 
2
+ === 3.1.4 / 2009-03-20
3
+
4
+ * Gemspec typo
5
+
6
+ === 3.1.3 / 2009-03-20
7
+
8
+ * Added #remove_global_options
9
+ * Added core global option descriptions
10
+ * Added display of global options
11
+ * Removed small core switches (-v, -h) because they are implicitly set
12
+ * Fixed issue with option switchs failing when named as common methods like --send, or --password
13
+ * Fixed bug causing InvalidOption error when global options do not abort the process.
14
+ This was due to these options remaining in the arguments array, in turn being parsed
15
+ by the sub-command's option parser, issuing the error. This is fixed by #remove_global_options.
16
+
2
17
  === 3.1.2 / 2009-03-16
3
18
 
4
19
  * Added support for global options via #global_option
data/Manifest CHANGED
@@ -1,6 +1,7 @@
1
1
  bin/commander
2
2
  commander.gemspec
3
3
  History.rdoc
4
+ lib/commander/blank.rb
4
5
  lib/commander/command.rb
5
6
  lib/commander/core_ext/array.rb
6
7
  lib/commander/core_ext/object.rb
data/README.rdoc CHANGED
@@ -211,6 +211,16 @@ Which will output the rest of the help doc, along with:
211
211
 
212
212
  TJ Holowaychuk <tj@vision-media.ca>
213
213
 
214
+ === Global Options
215
+
216
+ Although most switches will be at the sub-command level, several are available
217
+ by default at the global level, such as --version, and --help. Using #global_option
218
+ you can add additional global options:
219
+
220
+ global_option('-c', '--config FILE', 'Load config data for your commands to use') { |file| ... }
221
+
222
+ This method accepts the same syntax as Commander::Command#option so check it out for documentation.
223
+
214
224
  == ASCII Tables
215
225
 
216
226
  For feature rich ASCII tables for your terminal app check out visionmedia's terminal-table gem at
@@ -222,9 +232,14 @@ http://github.com/visionmedia/terminal-table
222
232
  | | | | | get it while its hot! |
223
233
  +----------+-------+----+--------+-----------------------+
224
234
 
235
+ == Contrib
236
+
237
+ Feel free to fork and request a pull, or submit a ticket
238
+ http://visionmedia.lighthouseapp.com/projects/27643-commander
239
+
225
240
  == Known Issues
226
241
 
227
- * none
242
+ * None
228
243
 
229
244
  == License
230
245
 
data/Rakefile CHANGED
@@ -1,12 +1,16 @@
1
1
 
2
- %w( rubygems rake echoe ./lib/commander.rb ).each { |lib| require lib }
2
+ $:.unshift 'lib'
3
+ require 'commander'
4
+ require 'rubygems'
5
+ require 'rake'
6
+ require 'echoe'
3
7
 
4
- Echoe.new("commander", Commander::VERSION) do |p|
8
+ Echoe.new "commander", Commander::VERSION do |p|
5
9
  p.author = "TJ Holowaychuk"
6
10
  p.email = "tj@vision-media.ca"
7
11
  p.summary = "The complete solution for Ruby command-line executables"
8
- p.url = "http://github.com/visionmedia/commander"
9
- p.runtime_dependencies = ["highline >=1.5.0"]
12
+ p.url = "http://visionmedia.github.com/commander"
13
+ p.runtime_dependencies << "highline >=1.5.0"
10
14
  end
11
15
 
12
16
  Dir['tasks/**/*.rake'].sort.each { |lib| load lib }
data/Todo.rdoc CHANGED
@@ -1,13 +1,13 @@
1
1
 
2
2
  == Major
3
3
 
4
- * Prevent global options from overriding sub-command level options
5
4
  * Finish global --trace
6
5
  * 1.9.x compatability
7
6
  * Publish RDoc on mini page / screencasts
8
7
 
9
8
  == Minor
10
9
 
10
+ * Prevent global options from overriding sub-command level options
11
11
  * Add arbitrary help blocks to sub-commands as well
12
12
  * Spec for changing out formatters (mock formatter)
13
13
  * Add optional default command when none is present
data/commander.gemspec CHANGED
@@ -2,19 +2,19 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{commander}
5
- s.version = "3.1.2"
5
+ s.version = "3.1.4"
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-16}
9
+ s.date = %q{2009-03-20}
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
- 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/foo", "test/pbar", "test/ui", "Todo.rdoc"]
14
+ s.extra_rdoc_files = ["bin/commander", "lib/commander/blank.rb", "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/blank.rb", "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
- s.homepage = %q{http://github.com/visionmedia/commander}
17
+ s.homepage = %q{http://visionmedia.github.com/commander}
18
18
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Commander", "--main", "README.rdoc"]
19
19
  s.require_paths = ["lib"]
20
20
  s.rubyforge_project = %q{commander}
data/lib/commander.rb CHANGED
@@ -21,11 +21,9 @@
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  #++
23
23
 
24
- $:.unshift File.dirname(__FILE__)
25
-
26
- require 'rubygems'
27
24
  require 'highline/import'
28
25
  require 'commander/version'
26
+ require 'commander/blank'
29
27
  require 'commander/user_interaction'
30
28
  require 'commander/core_ext'
31
29
  require 'commander/runner'
@@ -0,0 +1,6 @@
1
+
2
+ # TODO: remove when 1.9.x compatible, utilize BasicObject conditionally
3
+
4
+ class BlankSlate
5
+ instance_methods.each { |m| undef_method m unless m =~ /^__/ }
6
+ end
@@ -1,6 +1,5 @@
1
1
 
2
2
  require 'optparse'
3
- require 'ostruct'
4
3
 
5
4
  module Commander
6
5
  class Command
@@ -10,11 +9,20 @@ module Commander
10
9
 
11
10
  ##
12
11
  # Options struct.
13
-
14
- class Options < OpenStruct
12
+
13
+ class Options < BlankSlate
14
+ def method_missing meth, *args, &block
15
+ if meth.to_s =~ /=$/
16
+ metaclass = class << self; self end
17
+ metaclass.send :define_method, meth.to_s.chop do
18
+ args.first
19
+ end
20
+ end
21
+ end
22
+
15
23
  def default defaults = {}
16
24
  defaults.each do |key, value|
17
- send "#{key}=", value if send(key).nil?
25
+ __send__ :"#{key}=", value if __send__(key).nil?
18
26
  end
19
27
  end
20
28
  end
@@ -195,7 +203,7 @@ module Commander
195
203
 
196
204
  def proxy_option_struct
197
205
  @proxy_options.inject Options.new do |options, (option, value)|
198
- options.send :"#{option}=", value if option
206
+ options.__send__ :"#{option}=", value
199
207
  options
200
208
  end
201
209
  end
@@ -17,6 +17,12 @@
17
17
  <%= $terminal.color "ALIASES", :bold %>:
18
18
  <% @aliases.each do |alias_name, args| %>
19
19
  <%= "%-20s %s %s" % [alias_name, command(alias_name).name, args.join(' ')] -%>
20
+ <% end %>
21
+ <% end %>
22
+ <% unless @options.empty? -%>
23
+ <%= $terminal.color "GLOBAL OPTIONS", :bold %>:
24
+ <% @options.each do |(args, proc)| %>
25
+ <%= "%-20s %s" % [args.shift, args.last.is_a?(String) ? args.last : ''] -%>
20
26
  <% end -%>
21
27
  <% end %>
22
28
  <% if program :help -%>
@@ -11,7 +11,15 @@ module Commander
11
11
  class CommandError < StandardError; end
12
12
  class InvalidCommandError < CommandError; end
13
13
 
14
- attr_reader :commands, :options
14
+ ##
15
+ # Array of commands.
16
+
17
+ attr_reader :commands
18
+
19
+ ##
20
+ # Global options.
21
+
22
+ attr_reader :options
15
23
 
16
24
  ##
17
25
  # Initialize a new command runner. Optionally
@@ -28,9 +36,10 @@ module Commander
28
36
 
29
37
  def run!
30
38
  require_program :name, :version, :description
31
- global_option('-h', '--help') { command(:help).run *@args[1..-1]; return }
32
- global_option('-v', '--version') { say version; return }
39
+ global_option('--help', 'Display help documentation') { command(:help).run *@args[1..-1]; return }
40
+ global_option('--version', 'Display version information') { say version; return }
33
41
  parse_global_options
42
+ remove_global_options
34
43
  call_active_command
35
44
  rescue InvalidCommandError
36
45
  say 'invalid command. Use --help for more information'
@@ -231,13 +240,33 @@ module Commander
231
240
  end
232
241
  end
233
242
  end
243
+
244
+ ##
245
+ # Removes global options from args. This prevents an invalid
246
+ # option error from ocurring when options are parsed
247
+ # again for the sub-command.
248
+
249
+ def remove_global_options
250
+ # TODO: refactor with flipflop
251
+ options.each do |(args, proc)|
252
+ switch, has_arg = args.first.split
253
+ past_switch, arg_removed = false, false
254
+ @args.delete_if do |arg|
255
+ if arg == switch
256
+ past_switch, arg_removed = true, false
257
+ true
258
+ elsif past_switch && !arg_removed && arg !~ /^-/
259
+ arg_removed = true
260
+ else
261
+ arg_removed = true
262
+ false
263
+ end
264
+ end
265
+ end
266
+ end
234
267
 
235
268
  ##
236
269
  # Parse global command options.
237
- #
238
- # These options are used by commander itself
239
- # as well as allowing your program to specify
240
- # global commands such as '--verbose'.
241
270
 
242
271
  def parse_global_options
243
272
  options.inject OptionParser.new do |options, (args, proc)|
@@ -78,24 +78,24 @@ module Commander
78
78
  #
79
79
  # === Options:
80
80
  #
81
- # :title Title, defaults to "Progress"
82
- # :width Width of :progress_bar
83
- # :progress_str Progress string, defaults to "="
84
- # :incomplete_str Incomplete bar string, defaults to '.'
85
- # :format Defaults to ":title |:progress_bar| :percent_complete% complete "
86
- # :tokens Additional tokens replaced within the format string
87
- # :complete_message Defaults to "Process complete"
81
+ # :title Title, defaults to "Progress"
82
+ # :width Width of :progress_bar
83
+ # :progress_str Progress string, defaults to "="
84
+ # :incomplete_str Incomplete bar string, defaults to '.'
85
+ # :format Defaults to ":title |:progress_bar| :percent_complete% complete "
86
+ # :tokens Additional tokens replaced within the format string
87
+ # :complete_message Defaults to "Process complete"
88
88
  #
89
89
  # === Tokens:
90
90
  #
91
- # :title
92
- # :percent_complete
93
- # :progress_bar
94
- # :step
95
- # :steps_remaining
96
- # :total_steps
97
- # :time_elapsed
98
- # :time_remaining
91
+ # :title
92
+ # :percent_complete
93
+ # :progress_bar
94
+ # :step
95
+ # :steps_remaining
96
+ # :total_steps
97
+ # :time_elapsed
98
+ # :time_remaining
99
99
  #
100
100
 
101
101
  def initialize total, options = {}
@@ -212,10 +212,10 @@ module Commander
212
212
  #
213
213
  # === Example:
214
214
  #
215
- # uris = %w( http://vision-media.ca http://google.com )
216
- # ProgressBar.progress uris, :format => "Remaining: :time_remaining" do |uri|
217
- # res = open uri
218
- # end
215
+ # uris = %w( http://vision-media.ca http://google.com )
216
+ # ProgressBar.progress uris, :format => "Remaining: :time_remaining" do |uri|
217
+ # res = open uri
218
+ # end
219
219
  #
220
220
  # === See:
221
221
  #
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Commander
3
- VERSION = '3.1.2'
3
+ VERSION = '3.1.4'
4
4
  end
data/spec/command_spec.rb CHANGED
@@ -6,6 +6,24 @@ describe Commander::Command do
6
6
  create_test_command
7
7
  end
8
8
 
9
+ describe 'Options' do
10
+ before :each do
11
+ @options = Commander::Command::Options.new
12
+ end
13
+
14
+ it "should act like an open struct" do
15
+ @options.send = 'mail'
16
+ @options.password = 'foobar'
17
+ @options.send.should == 'mail'
18
+ @options.password.should == 'foobar'
19
+ end
20
+
21
+ it "should allow __send__ to function as always" do
22
+ @options.send = 'foo'
23
+ @options.__send__(:send).should == 'foo'
24
+ end
25
+ end
26
+
9
27
  describe "#seperate_switches_from_description" do
10
28
  it "should seperate switches and description returning both" do
11
29
  switches, description = *@command.seperate_switches_from_description('-h', '--help', 'display help')
@@ -23,6 +41,12 @@ describe Commander::Command do
23
41
  @command.option('--recursive') { |recursive| recursive.should be_true }
24
42
  @command.run '--recursive'
25
43
  end
44
+
45
+ it "should allow usage of common method names" do
46
+ @command.option '--password STRING'
47
+ @command.when_called { |_, options| options.password.should == 'foo' }
48
+ @command.run '--password', 'foo'
49
+ end
26
50
  end
27
51
 
28
52
  describe "#options" do
@@ -53,14 +77,14 @@ describe Commander::Command do
53
77
 
54
78
  it "calling the #call method by default when an object is called" do
55
79
  object = mock 'Object'
56
- object.should_receive(:call).with(an_instance_of(Array), an_instance_of(Commander::Command::Options)).once
80
+ object.should_receive(:call).once
57
81
  @command.when_called object
58
82
  @command.run 'foo'
59
83
  end
60
84
 
61
85
  it "calling an arbitrary method when an object is called" do
62
86
  object = mock 'Object'
63
- object.should_receive(:foo).with(an_instance_of(Array), an_instance_of(Commander::Command::Options)).once
87
+ object.should_receive(:foo).once
64
88
  @command.when_called object, :foo
65
89
  @command.run 'foo'
66
90
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ require 'rubygems'
2
3
  require 'commander'
3
4
  require 'stringio'
4
5
 
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.1.2
4
+ version: 3.1.4
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-16 00:00:00 -07:00
12
+ date: 2009-03-20 00:00:00 -07:00
13
13
  default_executable: commander
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,6 +30,7 @@ extensions: []
30
30
 
31
31
  extra_rdoc_files:
32
32
  - bin/commander
33
+ - lib/commander/blank.rb
33
34
  - lib/commander/command.rb
34
35
  - lib/commander/core_ext/array.rb
35
36
  - lib/commander/core_ext/object.rb
@@ -52,6 +53,7 @@ files:
52
53
  - bin/commander
53
54
  - commander.gemspec
54
55
  - History.rdoc
56
+ - lib/commander/blank.rb
55
57
  - lib/commander/command.rb
56
58
  - lib/commander/core_ext/array.rb
57
59
  - lib/commander/core_ext/object.rb
@@ -85,7 +87,7 @@ files:
85
87
  - test/ui
86
88
  - Todo.rdoc
87
89
  has_rdoc: true
88
- homepage: http://github.com/visionmedia/commander
90
+ homepage: http://visionmedia.github.com/commander
89
91
  post_install_message:
90
92
  rdoc_options:
91
93
  - --line-numbers