app-ctx 0.1.2 → 0.1.3
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/CHANGELOG +62 -0
- data/README +117 -108
- data/examples/boolean.rb +15 -0
- data/examples/conversions.rb +17 -0
- data/examples/ruby_conv.rb +16 -0
- data/examples/run_with_block.rb +29 -0
- data/examples/run_with_class.rb +51 -0
- data/examples/set_default_values.rb +23 -0
- data/lib/app-ctx.rb +1 -1
- data/test/t_app-ctx.rb +9 -0
- metadata +12 -4
data/CHANGELOG
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
Sat Oct 21 11:41:31 CEST 2006 dirk@sebrink.de
|
2
|
+
* more examples, blocks with set_default_values
|
3
|
+
|
4
|
+
Sat Oct 21 09:51:25 CEST 2006 dirk@sebrink.de
|
5
|
+
* add, mult, sub cleanup
|
6
|
+
|
7
|
+
Sat Oct 21 09:42:12 CEST 2006 dirk@sebrink.de
|
8
|
+
* add arguement type conversions and test
|
9
|
+
|
10
|
+
Sat Oct 21 09:17:44 CEST 2006 dirk@sebrink.de
|
11
|
+
* add, sub, mult
|
12
|
+
|
13
|
+
Sat Oct 21 09:06:08 CEST 2006 dirk@sebrink.de
|
14
|
+
* .vim-session
|
15
|
+
|
16
|
+
Fri Oct 20 20:48:02 CEST 2006 dirk@sebrink.de
|
17
|
+
* darcs changes output added
|
18
|
+
|
19
|
+
Fri Oct 20 20:46:15 CEST 2006 dirk@sebrink.de
|
20
|
+
* typo: args
|
21
|
+
|
22
|
+
Thu Oct 19 05:48:47 CEST 2006 dirk@sebrink.de
|
23
|
+
* 0.1.3: = README
|
24
|
+
|
25
|
+
Sat Sep 16 10:08:42 CEST 2006 dirk@sebrink.de
|
26
|
+
* doc
|
27
|
+
|
28
|
+
Sat Sep 16 09:44:32 CEST 2006 dirk@sebrink.de
|
29
|
+
tagged 0.1.2-set_default_values
|
30
|
+
|
31
|
+
Sat Sep 16 09:40:08 CEST 2006 dirk@sebrink.de
|
32
|
+
* set_default_values added
|
33
|
+
|
34
|
+
Tue Sep 5 00:21:41 CEST 2006 dirk@sebrink.de
|
35
|
+
* example/comments
|
36
|
+
|
37
|
+
Mon Sep 4 23:10:13 CEST 2006 dirk@sebrink.de
|
38
|
+
* .gem ignored
|
39
|
+
|
40
|
+
Mon Sep 4 11:56:56 CEST 2006 dirk@sebrink.de
|
41
|
+
* 0.1.1
|
42
|
+
|
43
|
+
Mon Sep 4 01:46:30 CEST 2006 dirk@sebrink.de
|
44
|
+
* removed config_path from test
|
45
|
+
|
46
|
+
Mon Sep 4 01:36:49 CEST 2006 dirk@sebrink.de
|
47
|
+
* App::run with class and closure + comment refactoring
|
48
|
+
|
49
|
+
Sat Sep 2 19:18:25 CEST 2006 dirk@sebrink.de
|
50
|
+
* IoC comments and '...have a nice day'
|
51
|
+
|
52
|
+
Tue Aug 29 18:48:49 CEST 2006 wolfger schramm <wolfger.schramm@idmedia.com>
|
53
|
+
* setter= config
|
54
|
+
|
55
|
+
Thu Aug 24 18:08:38 CEST 2006 dirk@sebrink.de
|
56
|
+
* default boring file
|
57
|
+
|
58
|
+
Thu Aug 24 18:05:58 CEST 2006 dirk@sebrink.de
|
59
|
+
* alpha version: ported and refactored for gemspec
|
60
|
+
|
61
|
+
Wed Aug 23 12:12:00 CEST 2006 dirk@sebrink.de
|
62
|
+
* new repository
|
data/README
CHANGED
@@ -1,109 +1,118 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
1
|
+
#
|
2
|
+
# = Application Context
|
3
|
+
#
|
4
|
+
# Every application needs configuration and app-ctx provides a concise way of
|
5
|
+
# doing it.
|
6
|
+
#
|
7
|
+
# For all applications (you are not a mouseclicker, are u?), once in a while
|
8
|
+
# you need to supply some configuration values to overrule the built-in
|
9
|
+
# defaults. The app-ctx gem does unify and organize built-in constants,
|
10
|
+
# config files and commandline option with a clearly defined priority, from
|
11
|
+
# low to high:
|
12
|
+
#
|
13
|
+
# - procedural: App::Config#set_default_values
|
14
|
+
# - default values YAML file from next to the $0 script
|
15
|
+
# - user supplied configuration file, eg.: --config=/tmp/foo.yml
|
16
|
+
# - command line options and flags: --foo --bar=foo
|
17
|
+
#
|
18
|
+
# But for your application it is of no interesst from where the values are
|
19
|
+
# coming: command line option: "--port=1234", a user configuration file or
|
20
|
+
# from the applications built-in default values. Therefor +app-ctx+ combines
|
21
|
+
# value settings from various sources into a single configuration hash.
|
22
|
+
#
|
23
|
+
# basically you have two ways to use it:
|
24
|
+
#
|
25
|
+
# require 'app-ctx' # of course,and than...
|
26
|
+
#
|
27
|
+
# 1. closures (see examples/run_with_block.rb)
|
28
|
+
#
|
29
|
+
# App::ctx.run do |context| ... end
|
30
|
+
#
|
31
|
+
# where context object provides:
|
32
|
+
#
|
33
|
+
# values: the combined key and value settings
|
34
|
+
# argv: remaining argument(not the options) of the command line
|
35
|
+
# defaults_path: full path to the defaults file
|
36
|
+
#
|
37
|
+
#
|
38
|
+
# 2. with a mainclass(see examples/run_with_class.rb)
|
39
|
+
#
|
40
|
+
# App::ctx.run YourClassHere
|
41
|
+
#
|
42
|
+
# for the second case(with a mainclass) an application instance of this class
|
43
|
+
# is created. The first argument is than taken as method name and executed,
|
44
|
+
# again with a context oject provided.
|
45
|
+
#
|
46
|
+
# $ ruby example/run_with_class show
|
47
|
+
#
|
48
|
+
# will result in the :show method beeing called:
|
49
|
+
#
|
50
|
+
# context = Config.new...
|
51
|
+
# ...
|
52
|
+
# app = Simple.new...
|
53
|
+
# app.show(context)
|
54
|
+
#
|
55
|
+
#
|
56
|
+
# == Conversions
|
57
|
+
#
|
58
|
+
# Commandline options are strings only, but sometimes you need strongly typed
|
59
|
+
# primitive values. +app-ctx+ does automatically convert integer and float
|
60
|
+
# values, see "examples/conversions.rb" for:
|
61
|
+
#
|
62
|
+
# $ ./examples/conversions.rb
|
63
|
+
# {:i=>23, :f=>3.14}
|
64
|
+
# typeof 'i': Fixnum
|
65
|
+
# typeof 'f': Float
|
66
|
+
#
|
67
|
+
# $ ./examples/conversions.rb -i=17 -f=2.12
|
68
|
+
# {:i=>17, :f=>2.12}
|
69
|
+
# typeof 'i': Fixnum
|
70
|
+
# typeof 'f': Float
|
71
|
+
#
|
72
|
+
# $ ./examples/conversions.rb -i=i -f=f
|
73
|
+
# {:i=>"i", :f=>"f"}
|
74
|
+
# typeof 'i': String
|
75
|
+
# typeof 'f': String
|
76
|
+
#
|
77
|
+
#
|
78
|
+
# == Flags/Boolean values
|
79
|
+
#
|
80
|
+
# Flags(options without values) are converted to boolean values, see
|
81
|
+
# examples/boolean.rb:
|
82
|
+
#
|
83
|
+
# $ ruby ./examples/boolean.rb
|
84
|
+
# {:bool=>false}
|
85
|
+
# typeof 'bool': FalseClass
|
86
|
+
#
|
87
|
+
# $ ruby ./examples/boolean.rb --bool
|
88
|
+
# {:bool=>true}
|
89
|
+
# typeof 'bool': TrueClass
|
90
|
+
#
|
91
|
+
#
|
92
|
+
# == Ruby Conversions
|
93
|
+
#
|
94
|
+
# When Fixnum, Float and boolean conversion are not enough, as a last resort,
|
95
|
+
# you can use ruby code directly for evaluation of option values. Replacing
|
96
|
+
# '=' with ':' results in assigning the ruby evaluation value to the key, see
|
97
|
+
# examples/ruby_conv.rb:
|
98
|
+
#
|
99
|
+
# $ ruby ./examples/ruby_conv.rb
|
100
|
+
# {:r=>1..10, :a=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
|
101
|
+
# typeof 'r': Range
|
102
|
+
# typeof 'a': Array
|
103
|
+
#
|
104
|
+
# $ ruby ./examples/ruby_conv.rb -r:2..3 -a:'(-1..1).to_a'
|
105
|
+
# {:r=>2..3, :a=>[-1, 0, 1]}
|
106
|
+
# typeof 'r': Range
|
107
|
+
# typeof 'a': Array
|
108
|
+
#
|
109
|
+
# $ ruby ./examples/ruby_conv.rb -r:2..3 -a:\(-1..1\).to_a
|
110
|
+
# {:r=>2..3, :a=>[-1, 0, 1]}
|
111
|
+
# typeof 'r': Range
|
112
|
+
# typeof 'a': Array
|
113
|
+
#
|
114
|
+
#
|
115
|
+
# have fun
|
116
|
+
# dirk
|
117
|
+
#
|
109
118
|
# vim: set tw=80 syntax=txt nosmartindent:
|
data/examples/boolean.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
require 'app-ctx'
|
7
|
+
|
8
|
+
Defaults = {:bool=>false}
|
9
|
+
|
10
|
+
App::run do |context|
|
11
|
+
h = Defaults.update(context.values)
|
12
|
+
puts h.inspect
|
13
|
+
puts "typeof 'bool': #{h[:bool].class}"
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
require 'app-ctx'
|
7
|
+
|
8
|
+
Defaults = {:i => 23, :f => 3.14,:bool=>false}
|
9
|
+
|
10
|
+
App::run do |context|
|
11
|
+
h = Defaults.update(context.values)
|
12
|
+
puts h.inspect
|
13
|
+
puts "typeof 'i': #{h[:i].class}"
|
14
|
+
puts "typeof 'f': #{h[:f].class}"
|
15
|
+
puts "typeof 'bool': #{h[:bool].class}"
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
require 'app-ctx'
|
7
|
+
|
8
|
+
Defaults = {:r => 1..10, :a=>(1..10).to_a}
|
9
|
+
|
10
|
+
App::run do |context|
|
11
|
+
h = Defaults.update(context.values)
|
12
|
+
puts h.inspect
|
13
|
+
puts "typeof 'r': #{h[:r].class}"
|
14
|
+
puts "typeof 'a': #{h[:a].class}"
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
require 'app-ctx'
|
7
|
+
|
8
|
+
class Simple
|
9
|
+
# $ ./examples/run_with_block.rb add 1 5
|
10
|
+
# 1 + 5 = 6
|
11
|
+
# $
|
12
|
+
def add a, b
|
13
|
+
puts "#{a} + #{b} = #{a + b}"
|
14
|
+
end
|
15
|
+
|
16
|
+
# $ ./examples/run_with_block.rb add 1 5
|
17
|
+
# 1 + 5 = 6
|
18
|
+
# $
|
19
|
+
def sub a, b
|
20
|
+
puts "#{a} - #{b} = #{a - b}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
App::run do |context|
|
26
|
+
puts context
|
27
|
+
Simple.new.send(context.argv.shift, *context.argv)
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
require 'app-ctx'
|
7
|
+
|
8
|
+
class Simple
|
9
|
+
|
10
|
+
# $ ./examples/run_with_class.rb add 1 5
|
11
|
+
# 1 + 5 = 6
|
12
|
+
# $
|
13
|
+
def add context
|
14
|
+
a, b = context.argv
|
15
|
+
puts "#{a} + #{b} = #{a + b}"
|
16
|
+
end
|
17
|
+
|
18
|
+
# $ ./examples/run_with_class.rb mult 1 5
|
19
|
+
# 1 * 5 = 5
|
20
|
+
# $
|
21
|
+
def mult context
|
22
|
+
a, b = context.argv
|
23
|
+
puts "#{a} * #{b} = #{a * b}"
|
24
|
+
end
|
25
|
+
|
26
|
+
# $ ./examples/run_with_class.rb sub 1 5
|
27
|
+
# 1 - 5 = -4
|
28
|
+
# $
|
29
|
+
def sub context
|
30
|
+
a, b = context.argv
|
31
|
+
puts "#{a} - #{b} = #{a - b}"
|
32
|
+
end
|
33
|
+
|
34
|
+
# $ ./examples/run_with_class.rb --foo --bar=1 show like no other
|
35
|
+
# args : ["like", "no", "other"]
|
36
|
+
# values : {:foo=>true, :bar=>1}
|
37
|
+
# defaults : '/Users/dluesebrink/ruby/app-ctx/examples/run_with_class.yml'
|
38
|
+
# user config : ''
|
39
|
+
# arguments : like, no, other
|
40
|
+
# $
|
41
|
+
def show context
|
42
|
+
puts context
|
43
|
+
puts "arguments: #{context.argv.join(", ")}"
|
44
|
+
end
|
45
|
+
|
46
|
+
def some_func context
|
47
|
+
puts "some_func called: #{context.argv.join(", ")}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
App::run :class => Simple
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
require 'app-ctx'
|
7
|
+
|
8
|
+
class Simple
|
9
|
+
|
10
|
+
def initialize(context)
|
11
|
+
context.set_default_values({
|
12
|
+
:desc => "programatically setting default values",
|
13
|
+
:port => 1234,
|
14
|
+
})
|
15
|
+
end
|
16
|
+
|
17
|
+
def show context
|
18
|
+
puts context
|
19
|
+
puts "arguments: #{context.argv.join(", ")}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
App::run :class => Simple
|
data/lib/app-ctx.rb
CHANGED
data/test/t_app-ctx.rb
CHANGED
@@ -144,6 +144,15 @@ class AppConfigTest < Test::Unit::TestCase
|
|
144
144
|
assert ! cfg[:b]
|
145
145
|
end
|
146
146
|
|
147
|
+
def test_parse_command_line_argument_conversion
|
148
|
+
puts "\n --> #{self.name}"
|
149
|
+
|
150
|
+
c = Config.new :argv => ["string", "5", "2.34"]
|
151
|
+
cfg, av = c.values, c.argv
|
152
|
+
assert_equal({}, cfg)
|
153
|
+
assert_equal ["string", 5, 2.34], av
|
154
|
+
end
|
155
|
+
|
147
156
|
def test_parse_command_line_int_float_flags
|
148
157
|
puts "\n --> #{self.name}"
|
149
158
|
|
metadata
CHANGED
@@ -3,13 +3,13 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: app-ctx
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2006-
|
6
|
+
version: 0.1.3
|
7
|
+
date: 2006-10-21 00:00:00 +02:00
|
8
8
|
summary: command line application startup and config context
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
|
-
email: dirk
|
12
|
-
homepage: http://www.
|
11
|
+
email: dirk@sebrink.de
|
12
|
+
homepage: http://www.sofasportler.de/dirk.blog, http://www.sebrink.de/
|
13
13
|
rubyforge_project:
|
14
14
|
description:
|
15
15
|
autorequire:
|
@@ -28,9 +28,16 @@ cert_chain:
|
|
28
28
|
authors:
|
29
29
|
- Dirk Luesebrink
|
30
30
|
files:
|
31
|
+
- examples/boolean.rb
|
32
|
+
- examples/conversions.rb
|
33
|
+
- examples/ruby_conv.rb
|
34
|
+
- examples/run_with_block.rb
|
35
|
+
- examples/run_with_class.rb
|
36
|
+
- examples/set_default_values.rb
|
31
37
|
- lib/app-ctx.rb
|
32
38
|
- lib/app-ctx/dlog.rb
|
33
39
|
- test/t_app-ctx.rb
|
40
|
+
- CHANGELOG
|
34
41
|
- README
|
35
42
|
test_files:
|
36
43
|
- test/t_app-ctx.rb
|
@@ -38,6 +45,7 @@ rdoc_options:
|
|
38
45
|
- --main
|
39
46
|
- README
|
40
47
|
extra_rdoc_files:
|
48
|
+
- CHANGELOG
|
41
49
|
- README
|
42
50
|
executables: []
|
43
51
|
|