applix 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/applix.rb +30 -46
  3. data/spec/applix_spec.rb +20 -0
  4. metadata +3 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.6
1
+ 0.3.7
data/lib/applix.rb CHANGED
@@ -4,8 +4,11 @@ class Applix
4
4
  def self.main argv, defaults = {}, &blk
5
5
  app = Applix.new
6
6
  app.instance_eval(&blk)
7
- app.run(argv, defaults)
7
+ app.run(argv, defaults, &blk)
8
+ end
8
9
 
10
+ def self.main! argv, defaults = {}, &blk
11
+ self.main argv, defaults, &blk
9
12
  rescue => e
10
13
  puts <<-EOT
11
14
 
@@ -25,14 +28,30 @@ usage: #{$0} <args...>
25
28
  # pre handle, can modify args & options
26
29
  @prolog_cb.call(args, options) unless @prolog_cb.nil?
27
30
 
28
- # it's either :any
29
- if task = tasks[:any]
30
- rc = task[:code].call(*args, options)
31
- else # or the task defined by the first argument
32
- (name = args.shift) or (raise "no task")
33
- (task = tasks[name.to_sym]) or (raise "no such task: '#{name}'")
34
- rc = task[:code].call(*args, options)
35
- end
31
+
32
+ # logic table for dispatching the command line onto an action
33
+ #
34
+ # id | name exits? any | action
35
+ # -- | -----------------+--------------
36
+ # 1 | - - | error: no any, mapped to #3 with name == :any
37
+ # 2 | - x | -> any
38
+ # 3 | x - - | error: no any
39
+ # 4 | x - x | -> any
40
+ # 5 | x x - | -> task
41
+ # 6 | x x x | -> task
42
+ #
43
+ # having name with no task is the same as no name with no any task..
44
+ name = (args.shift || :any).to_sym
45
+ # shoose existing task or :any
46
+ task = tasks[name] || tasks[:any]
47
+ task or (raise "no such task: '#{name}'")
48
+
49
+ # case #4: we must un-shift the name back into the args list to lets any
50
+ # see it as its first argument,
51
+ (args.unshift name.to_s) if(name != :any && task[:name] == :any)
52
+
53
+ # do the call
54
+ rc = task[:code].call(*args, options)
36
55
 
37
56
  # post handle
38
57
  unless @epilog_cb.nil?
@@ -53,49 +72,14 @@ usage: #{$0} <args...>
53
72
  end
54
73
 
55
74
  def any &blk
56
- tasks[:any] = { :code => blk }
75
+ tasks[:any] = { :name => :any, :code => blk }
57
76
  end
58
77
 
59
78
  def handle name, &blk
60
- tasks[name.to_sym] = { :code => blk }
79
+ tasks[name.to_sym] = { :name => name, :code => blk }
61
80
  end
62
81
 
63
82
  def tasks
64
83
  @tasks ||= {}
65
84
  end
66
85
  end
67
-
68
- __END__
69
- #
70
- def main args, options = {}
71
- options = (Defaults.merge options)
72
- options[:date] = Date.parse(options[:date]) # up-type string date
73
-
74
- action = args.shift or raise "no action"
75
-
76
- # account is an command line arg but password is prompted, never have that in
77
- # a config or on the command line!
78
- #
79
- username = args.shift # or raise "no username"
80
- password = prompt_for_password
81
-
82
- # which method to run depend on first command line argument..
83
- send action, username, password, options
84
- end
85
-
86
- params = Hash.from_argv ARGV
87
- begin
88
- main params[:args], params
89
- rescue => e
90
- puts <<-EOT
91
-
92
- ## #{e}
93
-
94
- usage: #{__FILE__} <task> <username>
95
-
96
- --- #{e.backtrace.join "\n "}
97
- EOT
98
- end
99
-
100
- __END__
101
-
data/spec/applix_spec.rb CHANGED
@@ -75,6 +75,26 @@ describe "Applix" do
75
75
  (t_handle < $t_post_handle).should == true
76
76
  end
77
77
 
78
+ it 'supports :any as fallback on command lines without matching task' do
79
+ Applix.main(%w(--opt1 foo param1 param2), {:opt2 => false}) do
80
+ handle(:not_called) { raise "can't possible happen" }
81
+ any do |*args, options|
82
+ args.should == ["foo", "param1", "param2"]
83
+ options.should == {:opt1 => true, :opt2 => false}
84
+ end
85
+ end
86
+ end
87
+
88
+ it 'any does not shadow existing tasks' do
89
+ Applix.main(['--opt1', 'foo', "param1", "param2"], {:opt2 => false}) do
90
+ handle(:foo) do |*args, options|
91
+ args.should == ["param1", "param2"]
92
+ options.should == {:opt1 => true, :opt2 => false}
93
+ end
94
+ any { raise "can't possible happen" }
95
+ end
96
+ end
97
+
78
98
  it 'supports :any when task does not depend on first arguments' do
79
99
  %w(bla fasel laber red).each do |name|
80
100
  Applix.main(['--opt1', name, "param1", "param2"], {:opt2 => false}) do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: applix
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.6
5
+ version: 0.3.7
6
6
  platform: ruby
7
7
  authors:
8
8
  - dirk luesebrink
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-08-17 00:00:00 +02:00
13
+ date: 2011-08-22 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -139,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
139
139
  requirements:
140
140
  - - ">="
141
141
  - !ruby/object:Gem::Version
142
- hash: 819130283461250046
142
+ hash: 3100699751538142693
143
143
  segments:
144
144
  - 0
145
145
  version: "0"