main 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +220 -3
- data/README.tmpl +219 -3
- data/lib/main.rb +17 -10
- data/lib/main/arrayfields.rb +347 -0
- data/lib/main/attributes.rb +54 -0
- data/lib/main/base.rb +112 -44
- data/lib/main/factories.rb +6 -5
- data/lib/main/parameter.rb +2 -1
- data/lib/main/proxy.rb +54 -0
- data/test/main.rb +116 -1
- metadata +5 -4
- data/a.rb +0 -33
- data/main-0.0.1.gem +0 -0
data/README
CHANGED
@@ -5,12 +5,10 @@ SYNOPSIS
|
|
5
5
|
a class factory and dsl for generating real main programs real quick
|
6
6
|
|
7
7
|
URI
|
8
|
-
|
9
8
|
http://rubyforge.org/projects/codeforpeople/
|
10
9
|
http://codeforpeople.com/lib/ruby/
|
11
10
|
|
12
11
|
INSTALL
|
13
|
-
|
14
12
|
$sudo gem install main
|
15
13
|
|
16
14
|
DESCRIPTION
|
@@ -310,7 +308,7 @@ SAMPLES
|
|
310
308
|
|
311
309
|
true
|
312
310
|
[40.0, 2.0]
|
313
|
-
|
311
|
+
nil
|
314
312
|
false
|
315
313
|
|
316
314
|
~ > ruby samples/d.rb --help
|
@@ -332,12 +330,231 @@ SAMPLES
|
|
332
330
|
|
333
331
|
DOCS
|
334
332
|
test/main.rb
|
333
|
+
|
335
334
|
find lib|xargs -n1 vi -R
|
336
335
|
|
336
|
+
API section below
|
337
|
+
|
337
338
|
HISTORY
|
339
|
+
0.0.2
|
340
|
+
- removed dependancy on attributes/arrayfields. main now has zero gem
|
341
|
+
dependancies.
|
342
|
+
|
343
|
+
- added support for io redirection. redirection of stdin, stdout, and
|
344
|
+
stderr can be done to any io like object or object that can be
|
345
|
+
inerpreted as a pathname (object.to_s)
|
346
|
+
|
347
|
+
- main objects can now easily be created and run on demand, which makes
|
348
|
+
testing a breeze
|
349
|
+
|
350
|
+
def test_unit_goodness!
|
351
|
+
main =
|
352
|
+
Main.new{
|
353
|
+
stdout StringIO.new
|
354
|
+
stderr '/dev/null'
|
355
|
+
|
356
|
+
def run
|
357
|
+
puts 42
|
358
|
+
end
|
359
|
+
}
|
360
|
+
|
361
|
+
main.run
|
362
|
+
main.stdout.rewind
|
363
|
+
|
364
|
+
assert main.stdout.read == "42\n"
|
365
|
+
end
|
366
|
+
|
367
|
+
- added API section to readme and called it 'docs'
|
368
|
+
|
369
|
+
- wrote a bunch more tests. there are now 42 of them.
|
370
|
+
|
338
371
|
0.0.1
|
339
372
|
|
340
373
|
initial version. this version extracts much of the functionality of alib's
|
341
374
|
(gen install alib) Alib.script main program generator and also some of jim's
|
342
375
|
freeze's excellent CommandLine::Aplication into what i hope is a simpler and
|
343
376
|
more unified interface
|
377
|
+
|
378
|
+
API
|
379
|
+
|
380
|
+
Main {
|
381
|
+
|
382
|
+
###########################################################################
|
383
|
+
# CLASS LEVEL API #
|
384
|
+
###########################################################################
|
385
|
+
#
|
386
|
+
# the name of the program, auto-set and used in usage
|
387
|
+
#
|
388
|
+
program 'foo.rb'
|
389
|
+
#
|
390
|
+
# a short description of program functionality, auto-set and used in usage
|
391
|
+
#
|
392
|
+
synopsis "foo.rb arg [options]+"
|
393
|
+
#
|
394
|
+
# long description of program functionality, used in usage iff set
|
395
|
+
#
|
396
|
+
description <<-hdoc
|
397
|
+
this text will automatically be indented to the right level.
|
398
|
+
|
399
|
+
it should describe how the program works in detail
|
400
|
+
hdoc
|
401
|
+
#
|
402
|
+
# used in usage iff set
|
403
|
+
#
|
404
|
+
author 'ara.t.howard@gmail.com'
|
405
|
+
#
|
406
|
+
# used in usage
|
407
|
+
#
|
408
|
+
version '0.0.42'
|
409
|
+
#
|
410
|
+
# stdin/out/err can be anthing which responds to read/write or a string
|
411
|
+
# which will be opened as in the appropriate mode
|
412
|
+
#
|
413
|
+
stdin '/dev/null'
|
414
|
+
stdout '/dev/null'
|
415
|
+
stderr open('/dev/null', 'w')
|
416
|
+
#
|
417
|
+
# the logger should be a Logger object, something 'write'-able, or a string
|
418
|
+
# which will be used to open the logger. the logger_level specifies the
|
419
|
+
# initalize verbosity setting, the default is Logger::INFO
|
420
|
+
#
|
421
|
+
logger(( program + '.log' ))
|
422
|
+
logger_level Logger::DEBUG
|
423
|
+
#
|
424
|
+
# you can configure exit codes. the defaults are shown
|
425
|
+
#
|
426
|
+
exit_success # 0
|
427
|
+
exit_failure # 1
|
428
|
+
exit_warn # 42
|
429
|
+
#
|
430
|
+
# the usage object is rather complex. by default it's an object which can
|
431
|
+
# be built up in sections using the
|
432
|
+
#
|
433
|
+
# usage["BUGS"] = "something about bugs'
|
434
|
+
#
|
435
|
+
# syntax to append sections onto the already pre-built usage message which
|
436
|
+
# contains program, synopsis, parameter descriptions and the like
|
437
|
+
#
|
438
|
+
# however, you always replace the usage object wholesale with one of your
|
439
|
+
# chosing like so
|
440
|
+
#
|
441
|
+
usage <<-txt
|
442
|
+
my own usage message
|
443
|
+
txt
|
444
|
+
|
445
|
+
|
446
|
+
###########################################################################
|
447
|
+
# PARAMETER API #
|
448
|
+
###########################################################################
|
449
|
+
#
|
450
|
+
# all the parameter types of argument|keyword|option|environment share this
|
451
|
+
# api. you must specify the type when the parameter method is used.
|
452
|
+
# alternatively used one of the shortcut methods
|
453
|
+
# argument|keyword|option|environment. in otherwords
|
454
|
+
#
|
455
|
+
# parameter('foo'){ type :option }
|
456
|
+
#
|
457
|
+
# is synonymous with
|
458
|
+
#
|
459
|
+
# option('foo'){ }
|
460
|
+
#
|
461
|
+
option 'foo' {
|
462
|
+
#
|
463
|
+
# required - whether this paramter must by supplied on the command line.
|
464
|
+
# note that you can create 'required' options with this keyword
|
465
|
+
#
|
466
|
+
required # or required true
|
467
|
+
#
|
468
|
+
# argument_required - applies only to options.
|
469
|
+
#
|
470
|
+
argument_required # argument :required
|
471
|
+
#
|
472
|
+
# argument_optional - applies only to options.
|
473
|
+
#
|
474
|
+
argument_optional # argument :optional
|
475
|
+
#
|
476
|
+
# cast - should be either a lambda taking one argument, or a symbol
|
477
|
+
# designation one of the built in casts defined in Main::Cast. supported
|
478
|
+
# types are :boolean|:integer|:float|:numeric|:string|:uri. built-in
|
479
|
+
# casts can be abbreviated
|
480
|
+
#
|
481
|
+
cast :int
|
482
|
+
#
|
483
|
+
# validate - should be a lambda taking one argument and returning
|
484
|
+
# true|false
|
485
|
+
#
|
486
|
+
validate{|int| int == 42}
|
487
|
+
#
|
488
|
+
# synopsis - should be a concise characterization of the paramter. a
|
489
|
+
# default synopsis is built automatically from the parameter. this
|
490
|
+
# information is displayed in the usage message
|
491
|
+
#
|
492
|
+
synopsis '--foo'
|
493
|
+
#
|
494
|
+
# description - a longer description of the paramter. it appears in the
|
495
|
+
# usage also.
|
496
|
+
#
|
497
|
+
description 'a long description of foo'
|
498
|
+
#
|
499
|
+
# arity - indicates how many times the parameter should appear on the
|
500
|
+
# command line. the default is one.
|
501
|
+
#
|
502
|
+
arity 2
|
503
|
+
#
|
504
|
+
# default - you can provide a default value in case none is given. the
|
505
|
+
# alias 'defaults' reads a bit nicer when you are giving a list of
|
506
|
+
# defaults for paramters of > 1 arity
|
507
|
+
#
|
508
|
+
defaults 40, 2
|
509
|
+
}
|
510
|
+
|
511
|
+
###########################################################################
|
512
|
+
# INSTANCE LEVEL API #
|
513
|
+
###########################################################################
|
514
|
+
#
|
515
|
+
# you must define a run method. it is the only method you must define.
|
516
|
+
#
|
517
|
+
def run
|
518
|
+
#
|
519
|
+
# all parameters are available in the 'params' hash and via the alias
|
520
|
+
# 'param'. it can be indexed via string or symbol. the values are all
|
521
|
+
# Main::Parameter objects
|
522
|
+
#
|
523
|
+
foo = params['foo']
|
524
|
+
#
|
525
|
+
# the given? method indicates whether or not the parameter was given on
|
526
|
+
# the commandline/environment, etc. in particular this will not be true
|
527
|
+
# when a default value was specified but no parameter was given
|
528
|
+
#
|
529
|
+
foo.given?
|
530
|
+
#
|
531
|
+
# the list of all values can be retrieved via 'values'. note that this
|
532
|
+
# is always an array.
|
533
|
+
#
|
534
|
+
p foo.values
|
535
|
+
#
|
536
|
+
# the __first__ value can be retrieved via 'value'. note that this
|
537
|
+
# never an array.
|
538
|
+
#
|
539
|
+
p foo.value
|
540
|
+
#
|
541
|
+
# the methods debug|info|warn|error|fatal are delegated to the logger
|
542
|
+
# object
|
543
|
+
#
|
544
|
+
info{ "this goes to the log" }
|
545
|
+
#
|
546
|
+
# you can set the exit_status at anytime. this status is used when
|
547
|
+
# exiting the program. exceptions cause this to be ext_failure if, and
|
548
|
+
# only if, the current value was exit_success. in otherwords an
|
549
|
+
# un-caught exception always results in a failing exit_status
|
550
|
+
#
|
551
|
+
exit_status exit_failure
|
552
|
+
#
|
553
|
+
# a few shortcuts both set the exit_status and exit the program.
|
554
|
+
#
|
555
|
+
exit_success!
|
556
|
+
exit_failure!
|
557
|
+
exit_warn!
|
558
|
+
end
|
559
|
+
|
560
|
+
}
|
data/README.tmpl
CHANGED
@@ -5,12 +5,10 @@ SYNOPSIS
|
|
5
5
|
a class factory and dsl for generating real main programs real quick
|
6
6
|
|
7
7
|
URI
|
8
|
-
|
9
8
|
http://rubyforge.org/projects/codeforpeople/
|
10
9
|
http://codeforpeople.com/lib/ruby/
|
11
10
|
|
12
11
|
INSTALL
|
13
|
-
|
14
12
|
$sudo gem install main
|
15
13
|
|
16
14
|
DESCRIPTION
|
@@ -138,17 +136,235 @@ DESCRIPTION
|
|
138
136
|
a user defined synopsis
|
139
137
|
|
140
138
|
SAMPLES
|
141
|
-
|
142
139
|
@samples
|
143
140
|
|
144
141
|
DOCS
|
145
142
|
test/main.rb
|
143
|
+
|
146
144
|
find lib|xargs -n1 vi -R
|
147
145
|
|
146
|
+
API section below
|
147
|
+
|
148
148
|
HISTORY
|
149
|
+
0.0.2
|
150
|
+
- removed dependancy on attributes/arrayfields. main now has zero gem
|
151
|
+
dependancies.
|
152
|
+
|
153
|
+
- added support for io redirection. redirection of stdin, stdout, and
|
154
|
+
stderr can be done to any io like object or object that can be
|
155
|
+
inerpreted as a pathname (object.to_s)
|
156
|
+
|
157
|
+
- main objects can now easily be created and run on demand, which makes
|
158
|
+
testing a breeze
|
159
|
+
|
160
|
+
def test_unit_goodness!
|
161
|
+
main =
|
162
|
+
Main.new{
|
163
|
+
stdout StringIO.new
|
164
|
+
stderr '/dev/null'
|
165
|
+
|
166
|
+
def run
|
167
|
+
puts 42
|
168
|
+
end
|
169
|
+
}
|
170
|
+
|
171
|
+
main.run
|
172
|
+
main.stdout.rewind
|
173
|
+
|
174
|
+
assert main.stdout.read == "42\n"
|
175
|
+
end
|
176
|
+
|
177
|
+
- added API section to readme and called it 'docs'
|
178
|
+
|
179
|
+
- wrote a bunch more tests. there are now 42 of them.
|
180
|
+
|
149
181
|
0.0.1
|
150
182
|
|
151
183
|
initial version. this version extracts much of the functionality of alib's
|
152
184
|
(gen install alib) Alib.script main program generator and also some of jim's
|
153
185
|
freeze's excellent CommandLine::Aplication into what i hope is a simpler and
|
154
186
|
more unified interface
|
187
|
+
|
188
|
+
API
|
189
|
+
|
190
|
+
Main {
|
191
|
+
|
192
|
+
###########################################################################
|
193
|
+
# CLASS LEVEL API #
|
194
|
+
###########################################################################
|
195
|
+
#
|
196
|
+
# the name of the program, auto-set and used in usage
|
197
|
+
#
|
198
|
+
program 'foo.rb'
|
199
|
+
#
|
200
|
+
# a short description of program functionality, auto-set and used in usage
|
201
|
+
#
|
202
|
+
synopsis "foo.rb arg [options]+"
|
203
|
+
#
|
204
|
+
# long description of program functionality, used in usage iff set
|
205
|
+
#
|
206
|
+
description <<-hdoc
|
207
|
+
this text will automatically be indented to the right level.
|
208
|
+
|
209
|
+
it should describe how the program works in detail
|
210
|
+
hdoc
|
211
|
+
#
|
212
|
+
# used in usage iff set
|
213
|
+
#
|
214
|
+
author 'ara.t.howard@gmail.com'
|
215
|
+
#
|
216
|
+
# used in usage
|
217
|
+
#
|
218
|
+
version '0.0.42'
|
219
|
+
#
|
220
|
+
# stdin/out/err can be anthing which responds to read/write or a string
|
221
|
+
# which will be opened as in the appropriate mode
|
222
|
+
#
|
223
|
+
stdin '/dev/null'
|
224
|
+
stdout '/dev/null'
|
225
|
+
stderr open('/dev/null', 'w')
|
226
|
+
#
|
227
|
+
# the logger should be a Logger object, something 'write'-able, or a string
|
228
|
+
# which will be used to open the logger. the logger_level specifies the
|
229
|
+
# initalize verbosity setting, the default is Logger::INFO
|
230
|
+
#
|
231
|
+
logger(( program + '.log' ))
|
232
|
+
logger_level Logger::DEBUG
|
233
|
+
#
|
234
|
+
# you can configure exit codes. the defaults are shown
|
235
|
+
#
|
236
|
+
exit_success # 0
|
237
|
+
exit_failure # 1
|
238
|
+
exit_warn # 42
|
239
|
+
#
|
240
|
+
# the usage object is rather complex. by default it's an object which can
|
241
|
+
# be built up in sections using the
|
242
|
+
#
|
243
|
+
# usage["BUGS"] = "something about bugs'
|
244
|
+
#
|
245
|
+
# syntax to append sections onto the already pre-built usage message which
|
246
|
+
# contains program, synopsis, parameter descriptions and the like
|
247
|
+
#
|
248
|
+
# however, you always replace the usage object wholesale with one of your
|
249
|
+
# chosing like so
|
250
|
+
#
|
251
|
+
usage <<-txt
|
252
|
+
my own usage message
|
253
|
+
txt
|
254
|
+
|
255
|
+
|
256
|
+
###########################################################################
|
257
|
+
# PARAMETER API #
|
258
|
+
###########################################################################
|
259
|
+
#
|
260
|
+
# all the parameter types of argument|keyword|option|environment share this
|
261
|
+
# api. you must specify the type when the parameter method is used.
|
262
|
+
# alternatively used one of the shortcut methods
|
263
|
+
# argument|keyword|option|environment. in otherwords
|
264
|
+
#
|
265
|
+
# parameter('foo'){ type :option }
|
266
|
+
#
|
267
|
+
# is synonymous with
|
268
|
+
#
|
269
|
+
# option('foo'){ }
|
270
|
+
#
|
271
|
+
option 'foo' {
|
272
|
+
#
|
273
|
+
# required - whether this paramter must by supplied on the command line.
|
274
|
+
# note that you can create 'required' options with this keyword
|
275
|
+
#
|
276
|
+
required # or required true
|
277
|
+
#
|
278
|
+
# argument_required - applies only to options.
|
279
|
+
#
|
280
|
+
argument_required # argument :required
|
281
|
+
#
|
282
|
+
# argument_optional - applies only to options.
|
283
|
+
#
|
284
|
+
argument_optional # argument :optional
|
285
|
+
#
|
286
|
+
# cast - should be either a lambda taking one argument, or a symbol
|
287
|
+
# designation one of the built in casts defined in Main::Cast. supported
|
288
|
+
# types are :boolean|:integer|:float|:numeric|:string|:uri. built-in
|
289
|
+
# casts can be abbreviated
|
290
|
+
#
|
291
|
+
cast :int
|
292
|
+
#
|
293
|
+
# validate - should be a lambda taking one argument and returning
|
294
|
+
# true|false
|
295
|
+
#
|
296
|
+
validate{|int| int == 42}
|
297
|
+
#
|
298
|
+
# synopsis - should be a concise characterization of the paramter. a
|
299
|
+
# default synopsis is built automatically from the parameter. this
|
300
|
+
# information is displayed in the usage message
|
301
|
+
#
|
302
|
+
synopsis '--foo'
|
303
|
+
#
|
304
|
+
# description - a longer description of the paramter. it appears in the
|
305
|
+
# usage also.
|
306
|
+
#
|
307
|
+
description 'a long description of foo'
|
308
|
+
#
|
309
|
+
# arity - indicates how many times the parameter should appear on the
|
310
|
+
# command line. the default is one.
|
311
|
+
#
|
312
|
+
arity 2
|
313
|
+
#
|
314
|
+
# default - you can provide a default value in case none is given. the
|
315
|
+
# alias 'defaults' reads a bit nicer when you are giving a list of
|
316
|
+
# defaults for paramters of > 1 arity
|
317
|
+
#
|
318
|
+
defaults 40, 2
|
319
|
+
}
|
320
|
+
|
321
|
+
###########################################################################
|
322
|
+
# INSTANCE LEVEL API #
|
323
|
+
###########################################################################
|
324
|
+
#
|
325
|
+
# you must define a run method. it is the only method you must define.
|
326
|
+
#
|
327
|
+
def run
|
328
|
+
#
|
329
|
+
# all parameters are available in the 'params' hash and via the alias
|
330
|
+
# 'param'. it can be indexed via string or symbol. the values are all
|
331
|
+
# Main::Parameter objects
|
332
|
+
#
|
333
|
+
foo = params['foo']
|
334
|
+
#
|
335
|
+
# the given? method indicates whether or not the parameter was given on
|
336
|
+
# the commandline/environment, etc. in particular this will not be true
|
337
|
+
# when a default value was specified but no parameter was given
|
338
|
+
#
|
339
|
+
foo.given?
|
340
|
+
#
|
341
|
+
# the list of all values can be retrieved via 'values'. note that this
|
342
|
+
# is always an array.
|
343
|
+
#
|
344
|
+
p foo.values
|
345
|
+
#
|
346
|
+
# the __first__ value can be retrieved via 'value'. note that this
|
347
|
+
# never an array.
|
348
|
+
#
|
349
|
+
p foo.value
|
350
|
+
#
|
351
|
+
# the methods debug|info|warn|error|fatal are delegated to the logger
|
352
|
+
# object
|
353
|
+
#
|
354
|
+
info{ "this goes to the log" }
|
355
|
+
#
|
356
|
+
# you can set the exit_status at anytime. this status is used when
|
357
|
+
# exiting the program. exceptions cause this to be ext_failure if, and
|
358
|
+
# only if, the current value was exit_success. in otherwords an
|
359
|
+
# un-caught exception always results in a failing exit_status
|
360
|
+
#
|
361
|
+
exit_status exit_failure
|
362
|
+
#
|
363
|
+
# a few shortcuts both set the exit_status and exit the program.
|
364
|
+
#
|
365
|
+
exit_success!
|
366
|
+
exit_failure!
|
367
|
+
exit_warn!
|
368
|
+
end
|
369
|
+
|
370
|
+
}
|