applix 0.3.0 → 0.3.4
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/README.mkd +29 -1
- data/VERSION +1 -1
- data/lib/applix.rb +32 -5
- data/spec/applix_spec.rb +73 -0
- metadata +3 -3
data/README.mkd
CHANGED
@@ -1,6 +1,34 @@
|
|
1
1
|
# applix
|
2
2
|
|
3
|
-
|
3
|
+
Building command line apps like:
|
4
|
+
|
5
|
+
Defaults = { :verbose => false }
|
6
|
+
|
7
|
+
Applix.main(ARGV, Defaults) do
|
8
|
+
handle(:one) do |*args, opts|
|
9
|
+
if opts[:verbose]
|
10
|
+
puts "arguments: #{args.inspect}"
|
11
|
+
puts "options: #{opts.inspect}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
handle(:two) do |*args, opts|
|
16
|
+
if opts[:verbose]
|
17
|
+
puts "arguments: #{args.inspect}"
|
18
|
+
puts "options: #{opts.inspect}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
can be called like:
|
24
|
+
|
25
|
+
$ app --verbose one 1234 x y z
|
26
|
+
arguments: ["1234", "x", "y", "z"]
|
27
|
+
options: {:verbose => true}
|
28
|
+
$
|
29
|
+
|
30
|
+
Command line options will be processed with Hash#from_argv. `Hash#from_argv`
|
31
|
+
builds a hash from ARGV like argument vector according to
|
4
32
|
following examples:
|
5
33
|
|
6
34
|
'-f' --> { :f => true }
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.4
|
data/lib/applix.rb
CHANGED
@@ -5,12 +5,13 @@ class Applix
|
|
5
5
|
app = Applix.new
|
6
6
|
app.instance_eval(&blk)
|
7
7
|
app.run(argv, defaults)
|
8
|
+
|
8
9
|
rescue => e
|
9
10
|
puts <<-EOT
|
10
11
|
|
11
12
|
## #{e}
|
12
13
|
|
13
|
-
usage: #{
|
14
|
+
usage: #{$0} <args...>
|
14
15
|
|
15
16
|
--- #{e.backtrace.join "\n "}
|
16
17
|
EOT
|
@@ -21,14 +22,40 @@ usage: #{__FILE__} <args...>
|
|
21
22
|
options = (defaults.merge options)
|
22
23
|
args = (options.delete :args)
|
23
24
|
|
24
|
-
#
|
25
|
-
(
|
26
|
-
|
27
|
-
|
25
|
+
# pre handle
|
26
|
+
@prolog_cb.call(*args, options) unless @prolog_cb.nil?
|
27
|
+
|
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
|
36
|
+
|
37
|
+
# post handle
|
38
|
+
unless @epilog_cb.nil?
|
39
|
+
rc = @epilog_cb.call(rc, *args, options)
|
40
|
+
end
|
41
|
+
|
42
|
+
rc # return result code from handle callbacks, not the epilog_cb
|
28
43
|
end
|
29
44
|
|
30
45
|
private
|
31
46
|
|
47
|
+
def prolog &blk
|
48
|
+
@prolog_cb = blk
|
49
|
+
end
|
50
|
+
|
51
|
+
def epilog &blk
|
52
|
+
@epilog_cb = blk
|
53
|
+
end
|
54
|
+
|
55
|
+
def any &blk
|
56
|
+
tasks[:any] = { :code => blk }
|
57
|
+
end
|
58
|
+
|
32
59
|
def handle name, &blk
|
33
60
|
tasks[name.to_sym] = { :code => blk }
|
34
61
|
end
|
data/spec/applix_spec.rb
CHANGED
@@ -2,6 +2,79 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe "Applix" do
|
4
4
|
|
5
|
+
it 'prolog has read/write access to args and options' do
|
6
|
+
Applix.main(['func']) do
|
7
|
+
prolog { |*args, options|
|
8
|
+
args.should == ['func']
|
9
|
+
options[:prolog] = Time.now
|
10
|
+
}
|
11
|
+
|
12
|
+
handle(:func) { |*_, options|
|
13
|
+
options[:prolog]
|
14
|
+
}
|
15
|
+
end.should_not == nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'epilog has access to task handler results' do
|
19
|
+
Applix.main(['func']) do
|
20
|
+
# @epilog will NOT make it into the handle invocation
|
21
|
+
epilog { |rc, *_|
|
22
|
+
rc.should == [1, 2, 3]
|
23
|
+
rc.reverse
|
24
|
+
}
|
25
|
+
handle(:func) { [1, 2, 3] }
|
26
|
+
|
27
|
+
end.should == [3, 2, 1]
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'runs before callback before handle calls' do
|
31
|
+
Applix.main(['func']) do
|
32
|
+
|
33
|
+
# @prolog will be available in handle invocations
|
34
|
+
prolog {
|
35
|
+
@prolog = :prolog
|
36
|
+
}
|
37
|
+
|
38
|
+
# @epilog will NOT make it into the handle invocation
|
39
|
+
epilog { |rc, *_|
|
40
|
+
@epilog = :epilog
|
41
|
+
rc
|
42
|
+
}
|
43
|
+
|
44
|
+
handle(:func) {
|
45
|
+
[@prolog, @epilog]
|
46
|
+
}
|
47
|
+
end.should == [:prolog, nil]
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'runs epilog callback after handle' do
|
51
|
+
t_handle = Applix.main([:func]) do
|
52
|
+
epilog { |rc, *_|
|
53
|
+
$t_post_handle = Time.now
|
54
|
+
rc
|
55
|
+
}
|
56
|
+
handle(:func) {
|
57
|
+
# epilog block should not have been executed yet
|
58
|
+
$t_post_handle.should == nil
|
59
|
+
Time.now
|
60
|
+
}
|
61
|
+
end
|
62
|
+
t_handle.should_not == nil
|
63
|
+
$t_post_handle.should_not == nil
|
64
|
+
(t_handle < $t_post_handle).should == true
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'supports :any when task does not depend on first arguments' do
|
68
|
+
%w(bla fasel laber red).each do |name|
|
69
|
+
Applix.main(['--opt1', name, "param1", "param2"], {:opt2 => false}) do
|
70
|
+
any do |*args, options|
|
71
|
+
args.should == [name, "param1", "param2"]
|
72
|
+
options.should == {:opt1 => true, :opt2 => false}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
5
78
|
it 'should call actions by first argument names' do
|
6
79
|
argv = ['func']
|
7
80
|
Applix.main(argv) 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.
|
5
|
+
version: 0.3.4
|
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-
|
13
|
+
date: 2011-08-16 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:
|
142
|
+
hash: 2582567187063183630
|
143
143
|
segments:
|
144
144
|
- 0
|
145
145
|
version: "0"
|