main 0.0.1

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 ADDED
@@ -0,0 +1,343 @@
1
+ NAME
2
+ main.rb
3
+
4
+ SYNOPSIS
5
+ a class factory and dsl for generating real main programs real quick
6
+
7
+ URI
8
+
9
+ http://rubyforge.org/projects/codeforpeople/
10
+ http://codeforpeople.com/lib/ruby/
11
+
12
+ INSTALL
13
+
14
+ $sudo gem install main
15
+
16
+ DESCRIPTION
17
+ main.rb is a library which simplifies and unifies the details of creating
18
+ command line programs. for instance, this program
19
+
20
+ require 'main'
21
+
22
+ Main {
23
+ argument 'foo'
24
+ option 'bar'
25
+
26
+ def run
27
+ p params['foo']
28
+ p params['bar']
29
+ exit_success!
30
+ end
31
+ }
32
+
33
+ sets up a program which requires one argument, 'bar', and which may accept one
34
+ command line switch, '--foo' in addition to the single option which is always
35
+ accepted and handled appropriately: '--help', '-h'.
36
+
37
+ for simple programs this is a real time saver but it's for more complex
38
+ applications where main.rb's unification of parameter parsing, class
39
+ configuration dsl, and auto-generation of usage messages can really streamline
40
+ command line application development. for example the following 'a.rb'
41
+ program:
42
+
43
+ require 'main'
44
+
45
+ Main {
46
+ argument('foo'){
47
+ cast :int
48
+ }
49
+ keyword('bar'){
50
+ arity 2
51
+ cast :float
52
+ defaults 0.0, 1.0
53
+ }
54
+ option('foobar'){
55
+ argument :optional
56
+ description 'the foobar option is very handy'
57
+ }
58
+ environment('BARFOO'){
59
+ cast :list_of_bool
60
+ synopsis 'export barfoo=value'
61
+ }
62
+
63
+ def run
64
+ p params['foo'].value
65
+ p params['bar'].values
66
+ p params['foobar'].value
67
+ p params['BARFOO'].value
68
+ end
69
+ }
70
+
71
+ when run with a command line of
72
+
73
+ BARFOO=true,false,false ruby a.rb 42 bar=40 bar=2 --foobar=a
74
+
75
+ will produce
76
+
77
+ 42
78
+ [40.0, 2.0]
79
+ "a"
80
+ [true, false, false]
81
+
82
+ while a command line of
83
+
84
+ ruby a.rb --help
85
+
86
+ will produce
87
+
88
+ NAME
89
+ a.rb
90
+
91
+ SYNOPSIS
92
+ a.rb foo [bar=bar] [options]+
93
+
94
+ PARAMETERS
95
+ * foo [ 1 -> int(foo) ]
96
+
97
+ * bar=bar [ 2 ~> float(bar=0.0,1.0) ]
98
+
99
+ * --foobar=[foobar] [ 1 ~> foobar ]
100
+ the foobar option is very handy
101
+
102
+ * --help, -h
103
+
104
+ * export barfoo=value
105
+
106
+ and this shows how all of argument, keyword, option, and environment parsing
107
+ can be declartively dealt with in a unified fashion - the dsl for all
108
+ parameter types is the same - and how auto synopsis and usage generation saves
109
+ keystrokes. the parameter synopsis is compact and can be read as
110
+
111
+ * foo [ 1 -> int(foo) ]
112
+
113
+ 'one argument will get processed via int(argument_name)'
114
+
115
+ 1 : one argument
116
+ -> : will get processed (the argument is required)
117
+ int(foo) : the cast is int, the arg name is foo
118
+
119
+ * bar=bar [ 2 ~> float(bar=0.0,1.0) ]
120
+
121
+ 'two keyword arguments might be processed via float(bar=0.0,1.0)'
122
+
123
+ 2 : two arguments
124
+ ~> : might be processed (the argument is optional)
125
+ float(bar=0.0,1.0) : the cast will be float, the default values are
126
+ 0.0 and 1.0
127
+
128
+ * --foobar=[foobar] [ 1 ~> foobar ]
129
+
130
+ 'one option with optional argument may be given directly'
131
+
132
+ * --help, -h
133
+
134
+ no synopsis, simple switch takes no args and is not required
135
+
136
+ * export barfoo=value
137
+
138
+ a user defined synopsis
139
+
140
+ SAMPLES
141
+
142
+ <========< samples/a.rb >========>
143
+
144
+ ~ > cat samples/a.rb
145
+
146
+ require 'main'
147
+
148
+ ARGV.replace %w( 42 ) if ARGV.empty?
149
+
150
+ Main {
151
+ argument('foo'){
152
+ required # this is the default
153
+ cast :int # value cast to Fixnum
154
+ validate{|foo| foo == 42} # raises error in failure case
155
+ description 'the foo param' # shown in --help
156
+ }
157
+
158
+ def run
159
+ p params['foo'].given?
160
+ p params['foo'].value
161
+ end
162
+ }
163
+
164
+ ~ > ruby samples/a.rb
165
+
166
+ true
167
+ 42
168
+
169
+ ~ > ruby samples/a.rb --help
170
+
171
+ NAME
172
+ a.rb
173
+
174
+ SYNOPSIS
175
+ a.rb foo [options]+
176
+
177
+ PARAMETERS
178
+ * foo [ 1 -> int(foo) ]
179
+ the foo param
180
+
181
+ * --help, -h
182
+
183
+
184
+
185
+ <========< samples/b.rb >========>
186
+
187
+ ~ > cat samples/b.rb
188
+
189
+ require 'main'
190
+
191
+ ARGV.replace %w( 40 1 1 ) if ARGV.empty?
192
+
193
+ Main {
194
+ argument('foo'){
195
+ arity 3 # foo will given three times
196
+ cast :int # value cast to Fixnum
197
+ validate{|foo| [40,1].include? foo} # raises error in failure case
198
+ description 'the foo param' # shown in --help
199
+ }
200
+
201
+ def run
202
+ p params['foo'].given?
203
+ p params['foo'].values
204
+ end
205
+ }
206
+
207
+ ~ > ruby samples/b.rb
208
+
209
+ true
210
+ [40, 1, 1]
211
+
212
+ ~ > ruby samples/b.rb --help
213
+
214
+ NAME
215
+ b.rb
216
+
217
+ SYNOPSIS
218
+ b.rb foo [options]+
219
+
220
+ PARAMETERS
221
+ * foo [ 3 -> int(foo) ]
222
+ the foo param
223
+
224
+ * --help, -h
225
+
226
+
227
+
228
+ <========< samples/c.rb >========>
229
+
230
+ ~ > cat samples/c.rb
231
+
232
+ require 'main'
233
+
234
+ ARGV.replace %w( foo=40 foo=2 bar=false ) if ARGV.empty?
235
+
236
+ Main {
237
+ keyword('foo'){
238
+ required # by default keywords are not required
239
+ arity 2
240
+ cast :float
241
+ }
242
+ keyword('bar'){
243
+ cast :bool
244
+ }
245
+
246
+ def run
247
+ p params['foo'].given?
248
+ p params['foo'].values
249
+ p params['bar'].given?
250
+ p params['bar'].value
251
+ end
252
+ }
253
+
254
+ ~ > ruby samples/c.rb
255
+
256
+ true
257
+ [40.0, 2.0]
258
+ true
259
+ false
260
+
261
+ ~ > ruby samples/c.rb --help
262
+
263
+ NAME
264
+ c.rb
265
+
266
+ SYNOPSIS
267
+ c.rb foo=foo [bar=bar] [options]+
268
+
269
+ PARAMETERS
270
+ * foo=foo [ 2 -> float(foo) ]
271
+
272
+ * bar=bar [ 1 ~> bool(bar) ]
273
+
274
+ * --help, -h
275
+
276
+
277
+
278
+ <========< samples/d.rb >========>
279
+
280
+ ~ > cat samples/d.rb
281
+
282
+ require 'main'
283
+
284
+ ARGV.replace %w( --foo=40 -f2 ) if ARGV.empty?
285
+
286
+ Main {
287
+ option('foo', 'f'){
288
+ required # by default options are not required, we could use 'foo=foo'
289
+ # above as a shortcut
290
+ argument_required
291
+ arity 2
292
+ cast :float
293
+ }
294
+
295
+ option('bar=[bar]', 'b'){ # note shortcut syntax for optional args
296
+ # argument_optional # we could also use this method
297
+ cast :bool
298
+ default false
299
+ }
300
+
301
+ def run
302
+ p params['foo'].given?
303
+ p params['foo'].values
304
+ p params['bar'].given?
305
+ p params['bar'].value
306
+ end
307
+ }
308
+
309
+ ~ > ruby samples/d.rb
310
+
311
+ true
312
+ [40.0, 2.0]
313
+ true
314
+ false
315
+
316
+ ~ > ruby samples/d.rb --help
317
+
318
+ NAME
319
+ d.rb
320
+
321
+ SYNOPSIS
322
+ d.rb --foo=foo [options]+
323
+
324
+ PARAMETERS
325
+ * --foo=foo, -f [ 2 -> float(foo) ]
326
+
327
+ * --bar=[bar], -b [ 1 ~> bool(bar=false) ]
328
+
329
+ * --help, -h
330
+
331
+
332
+
333
+ DOCS
334
+ test/main.rb
335
+ find lib|xargs -n1 vi -R
336
+
337
+ HISTORY
338
+ 0.0.1
339
+
340
+ initial version. this version extracts much of the functionality of alib's
341
+ (gen install alib) Alib.script main program generator and also some of jim's
342
+ freeze's excellent CommandLine::Aplication into what i hope is a simpler and
343
+ more unified interface
@@ -0,0 +1,154 @@
1
+ NAME
2
+ main.rb
3
+
4
+ SYNOPSIS
5
+ a class factory and dsl for generating real main programs real quick
6
+
7
+ URI
8
+
9
+ http://rubyforge.org/projects/codeforpeople/
10
+ http://codeforpeople.com/lib/ruby/
11
+
12
+ INSTALL
13
+
14
+ $sudo gem install main
15
+
16
+ DESCRIPTION
17
+ main.rb is a library which simplifies and unifies the details of creating
18
+ command line programs. for instance, this program
19
+
20
+ require 'main'
21
+
22
+ Main {
23
+ argument 'foo'
24
+ option 'bar'
25
+
26
+ def run
27
+ p params['foo']
28
+ p params['bar']
29
+ exit_success!
30
+ end
31
+ }
32
+
33
+ sets up a program which requires one argument, 'bar', and which may accept one
34
+ command line switch, '--foo' in addition to the single option which is always
35
+ accepted and handled appropriately: '--help', '-h'.
36
+
37
+ for simple programs this is a real time saver but it's for more complex
38
+ applications where main.rb's unification of parameter parsing, class
39
+ configuration dsl, and auto-generation of usage messages can really streamline
40
+ command line application development. for example the following 'a.rb'
41
+ program:
42
+
43
+ require 'main'
44
+
45
+ Main {
46
+ argument('foo'){
47
+ cast :int
48
+ }
49
+ keyword('bar'){
50
+ arity 2
51
+ cast :float
52
+ defaults 0.0, 1.0
53
+ }
54
+ option('foobar'){
55
+ argument :optional
56
+ description 'the foobar option is very handy'
57
+ }
58
+ environment('BARFOO'){
59
+ cast :list_of_bool
60
+ synopsis 'export barfoo=value'
61
+ }
62
+
63
+ def run
64
+ p params['foo'].value
65
+ p params['bar'].values
66
+ p params['foobar'].value
67
+ p params['BARFOO'].value
68
+ end
69
+ }
70
+
71
+ when run with a command line of
72
+
73
+ BARFOO=true,false,false ruby a.rb 42 bar=40 bar=2 --foobar=a
74
+
75
+ will produce
76
+
77
+ 42
78
+ [40.0, 2.0]
79
+ "a"
80
+ [true, false, false]
81
+
82
+ while a command line of
83
+
84
+ ruby a.rb --help
85
+
86
+ will produce
87
+
88
+ NAME
89
+ a.rb
90
+
91
+ SYNOPSIS
92
+ a.rb foo [bar=bar] [options]+
93
+
94
+ PARAMETERS
95
+ * foo [ 1 -> int(foo) ]
96
+
97
+ * bar=bar [ 2 ~> float(bar=0.0,1.0) ]
98
+
99
+ * --foobar=[foobar] [ 1 ~> foobar ]
100
+ the foobar option is very handy
101
+
102
+ * --help, -h
103
+
104
+ * export barfoo=value
105
+
106
+ and this shows how all of argument, keyword, option, and environment parsing
107
+ can be declartively dealt with in a unified fashion - the dsl for all
108
+ parameter types is the same - and how auto synopsis and usage generation saves
109
+ keystrokes. the parameter synopsis is compact and can be read as
110
+
111
+ * foo [ 1 -> int(foo) ]
112
+
113
+ 'one argument will get processed via int(argument_name)'
114
+
115
+ 1 : one argument
116
+ -> : will get processed (the argument is required)
117
+ int(foo) : the cast is int, the arg name is foo
118
+
119
+ * bar=bar [ 2 ~> float(bar=0.0,1.0) ]
120
+
121
+ 'two keyword arguments might be processed via float(bar=0.0,1.0)'
122
+
123
+ 2 : two arguments
124
+ ~> : might be processed (the argument is optional)
125
+ float(bar=0.0,1.0) : the cast will be float, the default values are
126
+ 0.0 and 1.0
127
+
128
+ * --foobar=[foobar] [ 1 ~> foobar ]
129
+
130
+ 'one option with optional argument may be given directly'
131
+
132
+ * --help, -h
133
+
134
+ no synopsis, simple switch takes no args and is not required
135
+
136
+ * export barfoo=value
137
+
138
+ a user defined synopsis
139
+
140
+ SAMPLES
141
+
142
+ @samples
143
+
144
+ DOCS
145
+ test/main.rb
146
+ find lib|xargs -n1 vi -R
147
+
148
+ HISTORY
149
+ 0.0.1
150
+
151
+ initial version. this version extracts much of the functionality of alib's
152
+ (gen install alib) Alib.script main program generator and also some of jim's
153
+ freeze's excellent CommandLine::Aplication into what i hope is a simpler and
154
+ more unified interface