optitron 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- optitron (0.1.6)
4
+ optitron (0.1.7)
5
5
  callsite (= 0.0.4)
6
6
  ruby2ruby (= 1.2.4)
7
7
  ruby_parser (>= 2.0)
@@ -232,3 +232,28 @@ If you try parsing invalid parameters, you can get back friendly error messages
232
232
  => ["File is required"]
233
233
  @parser.parse(%w(kill --pid=something)).error_messages
234
234
  => ["Pid is invalid"]
235
+
236
+ The response from #parse can also be used to dispatch. All that's required is the target object be able to respond to <tt>params=</tt>.
237
+
238
+ For example
239
+
240
+ class Runner
241
+ attr_accessor :params
242
+ def install(file)
243
+ puts "I'm installing #{file} with #{params.inspect}"
244
+ end
245
+ end
246
+
247
+ parser = Optitron.new {
248
+ help
249
+ opt 'verbose', "Be very loud"
250
+ cmd "install", "This installs things" do
251
+ arg "file", "The file to install"
252
+ end
253
+ }
254
+
255
+ parser.parse(%w(install this_file -v)).dispatch(Runner.new)
256
+
257
+ Will output
258
+
259
+ I'm installing this_file with {"help"=>false, "verbose"=>true}
@@ -49,7 +49,7 @@ class Optitron
49
49
  cmd.args.each do |arg|
50
50
  cmd_line << " " << help_line_for_arg(arg)
51
51
  end
52
- cmds << [cmd_line, cmd.desc]
52
+ cmds << [cmd_line, cmd]
53
53
  cmd.options.each do |opt|
54
54
  cmds.assoc(cmd_line) << help_line_for_opt(opt)
55
55
  end
@@ -72,8 +72,14 @@ class Optitron
72
72
  help_output << "Commands\n\n" + cmds.map do |(cmd, *opts)|
73
73
  cmd_text = ""
74
74
  cmd_text << "%-#{longest_line}s " % cmd
75
- cmd_desc = opts.shift
76
- cmd_text << "# #{cmd_desc}" if cmd_desc
75
+ cmd_obj = opts.shift
76
+ cmd_text << "# #{cmd_obj.desc}" if cmd_obj.desc
77
+ cmd_obj.args.each do |arg|
78
+ if arg.desc
79
+ cmd_text << "\n%-#{longest_line}s " % ""
80
+ cmd_text << "# #{arg.name} -- #{arg.desc}"
81
+ end
82
+ end
77
83
  opts.each do |opt|
78
84
  cmd_text << "\n %-#{longest_line}s " % opt.first
79
85
  cmd_text << "# #{opt.last}" if opt.last
@@ -62,5 +62,14 @@ class Optitron
62
62
  def valid?
63
63
  @errors.empty?
64
64
  end
65
+
66
+ def dispatch(target)
67
+ if valid?
68
+ target.params = params
69
+ target.send(command.to_sym, *args)
70
+ else
71
+ puts response.error_messages.join("\n")
72
+ end
73
+ end
65
74
  end
66
75
  end
@@ -1,3 +1,3 @@
1
1
  class Optitron
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Optitron::Response dispatch" do
4
+ it "should dispatch" do
5
+ target = mock('target')
6
+ target.should_receive(:params=).with({'verbose' => false})
7
+ target.should_receive(:install).with('thefile')
8
+ @parser = Optitron.new {
9
+ opt 'verbose', "Be very loud"
10
+ cmd "install", "This installs things" do
11
+ arg "file", "The file to install"
12
+ end
13
+ }
14
+ @parser.parse(%w(install thefile)).dispatch(target)
15
+ end
16
+ end
@@ -20,7 +20,7 @@ describe "Optitron::Parser help" do
20
20
  arg "thing", "Stuff to join", :type => :greedy, :required => true
21
21
  end
22
22
  }
23
- @parser.help.should == "Commands\n\ninstall [file] # This installs things\nshow [first] <second> # This shows things\nkill # This kills things\n -p/--pids=[ARRAY] # A list of pids to kill\n -P/--pid=[NUMERIC] # A pid to kill\n -n/--names=[HASH] # Some sort of hash\njoin [thing1 thing2 ...] # This joins things\n\nGlobal options\n\n-v/--verbose # Be very loud"
23
+ @parser.help.should == "Commands\n\ninstall [file] # This installs things\n # file -- The file to install\nshow [first] <second> # This shows things\n # first -- The first thing to show\n # second -- The second optional thing to show\nkill # This kills things\n -p/--pids=[ARRAY] # A list of pids to kill\n -P/--pid=[NUMERIC] # A pid to kill\n -n/--names=[HASH] # Some sort of hash\njoin [thing1 thing2 ...] # This joins things\n # thing -- Stuff to join\n\nGlobal options\n\n-v/--verbose # Be very loud"
24
24
  end
25
25
 
26
26
  it "generate help for non-command parsers" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optitron
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 7
10
- version: 0.1.7
9
+ - 8
10
+ version: 0.1.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-27 00:00:00 -05:00
18
+ date: 2010-08-29 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -157,6 +157,7 @@ files:
157
157
  - spec/arg_spec.rb
158
158
  - spec/cli_spec.rb
159
159
  - spec/default_spec.rb
160
+ - spec/dispatch_spec.rb
160
161
  - spec/errors_spec.rb
161
162
  - spec/help_spec.rb
162
163
  - spec/option_spec.rb