-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
129 lines (101 loc) · 3.04 KB
/
README
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
= Configurable
Class configurations for the command line and web.
== Description
Configurable adds methods to declare class configurations. Configurations are
inheritable, delegate to methods, and have hash-like access. Configurable
constructs configs such that they easily map to config files, web forms, and
the command line.
==== Usage
Declare configurations using the config method. Config generates accessors
that initialize with the default value.
class ConfigClass
include Configurable
config :flag, false # a flag
config :switch, true # an on/off switch
config :num, 3.14 # a number
config :lst, [1,2,3] # a list of integers
config :str, 'one' # a string
end
c = ConfigClass.new
c.str # => 'one'
Configs may also be accessed through config (a kind of delegating hash):
c.str = 'two'
c.config[:str] # => 'two'
c.config[:str] = 'three'
c.str # => 'three'
c.config.to_hash
# => {
# :flag => false,
# :switch => true,
# :num => 3.14,
# :lst => [1, 2, 3],
# :str => 'three'
# }
Configs may be imported and exported as simple objects which easily translate
to and from user interfaces, be they config files, web forms, or the command
line.
Config files:
c.config.import(
'flag' => true,
'num' => 6.022
)
c.config.export
# => {
# 'flag' => true,
# 'switch' => true,
# 'num' => 6.022,
# 'lst' => [1, 2, 3],
# 'str' => 'three'
# }
Web forms:
params = {
'flag' => 'true', # checkbox
'switch' => 'true', # radio button
'num' => '2.71', # text input
'lst' => ['2', '6'] # list input (lst[]=2&lst[]=6)
}
c.config.import(params).to_hash
# => {
# :flag => true,
# :switch => true,
# :num => 2.71,
# :lst => [2, 6],
# :str => 'three'
# }
Command Line:
c.config.parse %w{a --flag --no-switch --num 6.022 --lst 7 --lst 8,9 b c}
# => ['a', 'b', 'c']
c.config.to_hash
# => {
# :flag => true,
# :switch => false,
# :num => 6.022,
# :lst => [7, 8, 9],
# :str => 'three'
# }
stdout = []
parser = c.config.parser do |psr|
psr.on '-h', '--help', 'print help' do
stdout << "options:"
stdout << psr
end
end
parser.parse('--help')
"\n" + stdout.join("\n")
# => %q{
# options:
# --flag a flag
# -h, --help print help
# --lst LST... a list of integers (1,2,3)
# --num NUM a number (3.14)
# --str STR a string (one)
# --[no-]switch an on/off switch
# }
Configurable supports custom data types, nested configs, and config modules.
See the examples and documentation for details.
== Installation
Configurable is available as a gem[http://rubygems.org/gems/configurable].
% gem install configurable
== Info
Developer:: {Simon Chiang}[http://github.com/thinkerbot]
License:: {MIT-Style}[link:files/MIT-LICENSE.html]