visionmedia-commander 3.1.1 → 3.1.2

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,10 @@
1
1
 
2
+ === 3.1.2 / 2009-03-16
3
+
4
+ * Added support for global options via #global_option
5
+ * Added #call_active_command and #say to clean things up internally
6
+ * Misc refactoring
7
+
2
8
  === 3.1.1 / 2009-03-13
3
9
 
4
10
  * Fixed some terminal formatter spacing issues
data/Todo.rdoc CHANGED
@@ -1,6 +1,8 @@
1
1
 
2
2
  == Major
3
3
 
4
+ * Prevent global options from overriding sub-command level options
5
+ * Finish global --trace
4
6
  * 1.9.x compatability
5
7
  * Publish RDoc on mini page / screencasts
6
8
 
@@ -16,7 +18,6 @@
16
18
  * Add optional command to be executed when none is specified
17
19
  * Add global options... change runner implementations as well as displaying in terminal formatter, OpenStruct inherit these options?
18
20
  * Add highline paging ... clean it up because its ugly
19
- * Add aliasing of commands / args / options
20
21
  * Add option copying / merging capabilities
21
22
  * Change; require that users explicitly require 'commander/import' (which in turn will highlight/import as well)
22
23
  * Release to RubyForge as well
@@ -25,7 +26,6 @@
25
26
 
26
27
  == Brainstorming
27
28
 
28
- * Add global --trace method
29
29
  * Add global trapping of INT to end process with less ugly output
30
30
  * Add global --options switch for loading options from a filepath
31
31
  * Add argument parser / defaults
data/commander.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{commander}
5
- s.version = "3.1.1"
5
+ s.version = "3.1.2"
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-13}
9
+ s.date = %q{2009-03-16}
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}
@@ -16,8 +16,9 @@ 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, :default_command
20
19
  def_delegators Commander::UI::ProgressBar, :progress
20
+ def_delegators :$command_runner, :add_command, :command, :program, :run!,
21
+ :commands, :alias_command, :default_command, :global_option
21
22
 
22
23
  ##
23
24
  # Return the current binding.
@@ -11,25 +11,16 @@ module Commander
11
11
  class CommandError < StandardError; end
12
12
  class InvalidCommandError < CommandError; end
13
13
 
14
- ##
15
- # Commands within the runner.
16
-
17
- attr_reader :commands
18
-
19
- ##
20
- # Global options.
21
-
22
- attr_reader :options
23
-
14
+ attr_reader :commands, :options
15
+
24
16
  ##
25
17
  # Initialize a new command runner. Optionally
26
18
  # supplying +args+ for mocking, or arbitrary usage.
27
19
 
28
20
  def initialize args = ARGV
29
- @args, @commands, @options, @aliases = args, {}, {}, {}
21
+ @args, @commands, @aliases, @options = args, {}, {}, []
30
22
  @program = program_defaults
31
23
  create_default_commands
32
- parse_global_options
33
24
  end
34
25
 
35
26
  ##
@@ -37,23 +28,35 @@ module Commander
37
28
 
38
29
  def run!
39
30
  require_program :name, :version, :description
40
- case
41
- when options[:version] ; $terminal.say "#{program(:name)} #{program(:version)}"
42
- when options[:help] ; command(:help).run(*@args[1..-1])
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
49
- end
31
+ global_option('-h', '--help') { command(:help).run *@args[1..-1]; return }
32
+ global_option('-v', '--version') { say version; return }
33
+ parse_global_options
34
+ call_active_command
50
35
  rescue InvalidCommandError
51
- $terminal.say 'invalid command. Use --help for more information'
36
+ say 'invalid command. Use --help for more information'
52
37
  rescue \
53
38
  OptionParser::InvalidOption,
54
39
  OptionParser::InvalidArgument,
55
40
  OptionParser::MissingArgument => e
56
- $terminal.say e
41
+ say e
42
+ end
43
+
44
+ ##
45
+ # Return program version.
46
+
47
+ def version
48
+ '%s %s' % [program(:name), program(:version)]
49
+ end
50
+
51
+ ##
52
+ # Invoke the active command.
53
+
54
+ def call_active_command
55
+ if alias? command_name_from_args
56
+ active_command.run *(@aliases[command_name_from_args.to_s] + args_without_command_name)
57
+ else
58
+ active_command.run *args_without_command_name
59
+ end
57
60
  end
58
61
 
59
62
  ##
@@ -111,6 +114,15 @@ module Commander
111
114
  @commands[name.to_s] or raise InvalidCommandError, "invalid command '#{ name || 'nil' }'", caller
112
115
  end
113
116
 
117
+ ##
118
+ # Add a global option; follows the same syntax as
119
+ # Command#option. This would be used for switches such
120
+ # as --version, --trace, etc.
121
+
122
+ def global_option *args, &block
123
+ @options << [args, block]
124
+ end
125
+
114
126
  ##
115
127
  # Alias command +name+ with +alias_name+. Optionallry +args+ may be passed
116
128
  # as if they were being passed straight to the original command via the command-line.
@@ -212,9 +224,9 @@ module Commander
212
224
  c.example "Display help for 'foo'", 'command help foo'
213
225
  c.when_called do |args, options|
214
226
  if args.empty?
215
- $terminal.say help_formatter.render
227
+ say help_formatter.render
216
228
  else
217
- $terminal.say help_formatter.render_command(command(args.join(' ')))
229
+ say help_formatter.render_command(command(args.join(' ')))
218
230
  end
219
231
  end
220
232
  end
@@ -228,10 +240,9 @@ module Commander
228
240
  # global commands such as '--verbose'.
229
241
 
230
242
  def parse_global_options
231
- opts = OptionParser.new
232
- opts.on('--help') { @options[:help] = true }
233
- opts.on('--version') { @options[:version] = true }
234
- opts.parse! @args.dup
243
+ options.inject OptionParser.new do |options, (args, proc)|
244
+ options.on *args, &proc
245
+ end.parse! @args.dup
235
246
  rescue OptionParser::InvalidOption
236
247
  # Ignore invalid options since options will be further
237
248
  # parsed by our sub commands.
@@ -246,5 +257,11 @@ module Commander
246
257
  end
247
258
  end
248
259
 
260
+ private
261
+
262
+ def say *args #:nodoc:
263
+ $termina.say *args
264
+ end
265
+
249
266
  end
250
267
  end
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Commander
3
- VERSION = '3.1.1'
3
+ VERSION = '3.1.2'
4
4
  end
data/spec/runner_spec.rb CHANGED
@@ -52,6 +52,33 @@ describe Commander do
52
52
  end
53
53
  end
54
54
 
55
+ describe "#global_option" do
56
+ it "should be invoked when used in the args list" do
57
+ file = ''
58
+ new_command_runner 'test', '--config', 'foo' do
59
+ global_option('--config FILE') { |f| file = f }
60
+ end.run!
61
+ file.should == 'foo'
62
+ end
63
+ end
64
+
65
+ # describe "--trace" do
66
+ # it "should display pretty errors by default" do
67
+ # new_command_runner 'test' do
68
+ # raise 'cookies!'
69
+ # end.run!
70
+ # @output.should == 'error: cookies!. use --trace to view backtrace'
71
+ # end
72
+ #
73
+ # it "should display callstack when using this switch" do
74
+ # lambda {
75
+ # new_command_runner 'test', '--trace' do
76
+ # raise 'cookies!'
77
+ # end.run!
78
+ # }.should raise_error
79
+ # end
80
+ # end
81
+
55
82
  describe "--version" do
56
83
  it "should output program version" do
57
84
  run('--version').should == "test 1.2.3\n"
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.1
4
+ version: 3.1.2
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-13 00:00:00 -07:00
12
+ date: 2009-03-16 00:00:00 -07:00
13
13
  default_executable: commander
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency