como 0.1.3 → 0.1.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.
- checksums.yaml +7 -0
- data/CHANGELOG.rdoc +7 -0
- data/doc/Como/ArgsParseState.html +52 -52
- data/doc/Como/ComoCommon.html +128 -33
- data/doc/Como/MainOpt.html +52 -55
- data/doc/Como/Opt/ErrorWithData.html +15 -15
- data/doc/Como/Opt/InvalidOption.html +1 -1
- data/doc/Como/Opt/MissingArgument.html +1 -1
- data/doc/Como/Opt.html +1211 -821
- data/doc/Como/RuleCheck.html +143 -92
- data/doc/Como/RuleDisplay.html +96 -92
- data/doc/Como/Spec.html +636 -273
- data/doc/Como.html +56 -36
- data/doc/_index.html +1 -1
- data/doc/file.CHANGELOG.html +12 -2
- data/doc/file.README.html +1 -1
- data/doc/index.html +1 -1
- data/doc/method_list.html +113 -71
- data/doc/top-level-namespace.html +1 -1
- data/lib/como.rb +217 -102
- data/lib/version.rb +2 -7
- metadata +60 -67
- data/Rakefile +0 -59
data/lib/como.rb
CHANGED
@@ -41,7 +41,7 @@
|
|
41
41
|
#
|
42
42
|
# First Como is required and Como module is included.
|
43
43
|
#
|
44
|
-
#
|
44
|
+
# {Spec.command} method takes 4 arguments:
|
45
45
|
# [progname] Name of the program (or command).
|
46
46
|
# [author] Author of the program.
|
47
47
|
# [year] Year (or any date) for the program.
|
@@ -63,7 +63,7 @@
|
|
63
63
|
# The option name also doubles as long option format, i.e. one could
|
64
64
|
# use "--file <filename>" on the command line.
|
65
65
|
#
|
66
|
-
# Existence of optional options can be tested using the
|
66
|
+
# Existence of optional options can be tested using the {Opt#given}
|
67
67
|
# method. For example
|
68
68
|
# Opt['debug'].given
|
69
69
|
# would return "true" if "-d" was given on the command line.
|
@@ -165,12 +165,12 @@
|
|
165
165
|
# when 'rm'; puts " Removing file \"#{subcmd['file'].value}\"..."
|
166
166
|
# end
|
167
167
|
#
|
168
|
-
#
|
168
|
+
# {Spec.program} method defines a program (command) with
|
169
169
|
# subcommands. The author and date are provided as parameters, and the
|
170
170
|
# program and subcommand options are defined in block.
|
171
171
|
#
|
172
|
-
# The first
|
173
|
-
# (
|
172
|
+
# The first {Spec#command} (or {Spec#subcmd}) method call defines the main command
|
173
|
+
# ({Opt.main}) which represents the program. It has two "subcmd"
|
174
174
|
# options ("add" and "rm").
|
175
175
|
#
|
176
176
|
# The rest of the "subcmd" methods define subcommands for the parent
|
@@ -187,14 +187,14 @@
|
|
187
187
|
# or alternatively
|
188
188
|
# Opt['como_subcmd']
|
189
189
|
#
|
190
|
-
# The subcommands can be referenced through
|
190
|
+
# The subcommands can be referenced through {Opt.main} (etc.)
|
191
191
|
# Opt.main['add']
|
192
192
|
# Opt['como_subcmd']['add']
|
193
193
|
#
|
194
|
-
# or directly from
|
194
|
+
# or directly from {Opt} if subcommand names do not collide:
|
195
195
|
# Opt['add']
|
196
196
|
#
|
197
|
-
# The given subcommand can be accessed with
|
197
|
+
# The given subcommand can be accessed with {Opt#givenSubcmd} method from
|
198
198
|
# each parent command.
|
199
199
|
#
|
200
200
|
# === Subcommand example executions
|
@@ -262,7 +262,8 @@
|
|
262
262
|
# [:single] Mandatory single argument option.
|
263
263
|
# [:multi] Mandatory multiple argument option (one or many). Option
|
264
264
|
# values in array.
|
265
|
-
# [:opt_single] Optional single argument option.
|
265
|
+
# [:opt_single] Optional single argument option. Value is nil when
|
266
|
+
# option is not given.
|
266
267
|
# [:opt_multi] Optional multiple argument option (one or many). Option
|
267
268
|
# values in array.
|
268
269
|
# [:opt_any] Optional multiple argument option (also none
|
@@ -271,7 +272,7 @@
|
|
271
272
|
# String values can be left out, since only the document
|
272
273
|
# string is used. Default option is referred with
|
273
274
|
# ":default" or "nil".
|
274
|
-
# [:exclusive] Option that does not
|
275
|
+
# [:exclusive] Option that does not co-exist with other
|
275
276
|
# options. :exclusive can have arguments as with
|
276
277
|
# :opt_any, however :exclusive is documented like
|
277
278
|
# :switch.
|
@@ -303,8 +304,8 @@
|
|
303
304
|
# Option behavior can be controlled with several configuration options.
|
304
305
|
#
|
305
306
|
# The configuration options are provided in a Hash. These are the
|
306
|
-
# passed as the last regular parameter for both
|
307
|
-
#
|
307
|
+
# passed as the last regular parameter for both {Spec.command} and
|
308
|
+
# {Spec.program} methods. Setting the configuration at {Spec.program}
|
308
309
|
# will propagate the config options to all the subcommands as
|
309
310
|
# well. Configuration can be given to each subcommand separately to
|
310
311
|
# override the inherited config values. Subcommand settings are not
|
@@ -335,9 +336,9 @@
|
|
335
336
|
# tested whether they are specified on the command line using:
|
336
337
|
# Opt['name'].given
|
337
338
|
#
|
338
|
-
# The
|
339
|
+
# The {Opt#given} method takes optionally a block argument. When block
|
339
340
|
# argument is used, the block is supplied with option value and the
|
340
|
-
# block is executed if the option has been set (See: Opt#given).
|
341
|
+
# block is executed if the option has been set (See: {Opt#given}).
|
341
342
|
#
|
342
343
|
# Provided value is returned by:
|
343
344
|
# Opt['name'].value
|
@@ -378,7 +379,7 @@
|
|
378
379
|
# == Subcommand options
|
379
380
|
#
|
380
381
|
# The given subcommand for the parent command is return by
|
381
|
-
#
|
382
|
+
# {Opt#givenSubcmd}. Commonly the program creator should just check
|
382
383
|
# directly which subcommand has been selected and check for any
|
383
384
|
# subcommand options set. For example:
|
384
385
|
# if Opt['como_subcmd']['add'].given
|
@@ -387,7 +388,7 @@
|
|
387
388
|
# == Program external options
|
388
389
|
#
|
389
390
|
# If the user gives the "--" option (double-dash), the arguments after
|
390
|
-
# that option are returned as an Array with
|
391
|
+
# that option are returned as an Array with {Opt.external}.
|
391
392
|
#
|
392
393
|
#
|
393
394
|
#
|
@@ -415,8 +416,8 @@
|
|
415
416
|
#
|
416
417
|
# = Customization
|
417
418
|
#
|
418
|
-
# A user specific customization file can be referenced through
|
419
|
-
# "COMO" environment variable. If environment variable "COMO" is
|
419
|
+
# A Como user specific customization file can be referenced through
|
420
|
+
# the "COMO" environment variable. If environment variable "COMO" is
|
420
421
|
# defined, the referenced file is read in as Ruby file as a last phase
|
421
422
|
# when Como is loaded from the program (require). Proposed naming
|
422
423
|
# convention for the customization is:
|
@@ -426,9 +427,9 @@
|
|
426
427
|
# User can define a pre and a post action hook in the file.
|
427
428
|
#
|
428
429
|
# The pre-hook can be used for example to change the Como config
|
429
|
-
# defaults. It is run before the body of
|
430
|
-
#
|
431
|
-
# been passed to
|
430
|
+
# defaults. It is run before the body of {Spec.command} or
|
431
|
+
# {Spec.program} is executed. It is passed all the parameters that has
|
432
|
+
# been passed to {Spec.command} or {Spec.program}, only collected into
|
432
433
|
# a Hash. The Hash keys are method parameter names as symbols.
|
433
434
|
#
|
434
435
|
# Example:
|
@@ -449,7 +450,16 @@
|
|
449
450
|
# end
|
450
451
|
#
|
451
452
|
# There is no predefined use cases for post-hook. Post-hook is passed
|
452
|
-
# the Opt.main as parameter.
|
453
|
+
# the {Opt.main} as parameter.
|
454
|
+
#
|
455
|
+
# {Spec.program} and {Spec.command} both process and check options in
|
456
|
+
# one pass. Como user can separate the definition and checking
|
457
|
+
# phase. Definition phase is performed by executing
|
458
|
+
# {Spec.defineProgram} or {Spec.defineCommand}. After definition phase
|
459
|
+
# the user can for example programmatically add new subcommands or
|
460
|
+
# options, in addition to existing options. When the subcommands and
|
461
|
+
# options are complete, {Spec.execute} should be called to perform
|
462
|
+
# options checking.
|
453
463
|
#
|
454
464
|
# If the provided customization facilities are not satisfactory,
|
455
465
|
# changes can be implemented simply by overloading the existing
|
@@ -481,10 +491,18 @@ module Como
|
|
481
491
|
@@io
|
482
492
|
end
|
483
493
|
|
494
|
+
# Set hook content.
|
495
|
+
#
|
496
|
+
# @param name [String] Hook name.
|
497
|
+
# @yield code Hook code.
|
484
498
|
def ComoCommon.setHook( name, &code )
|
485
499
|
@@hook[ name ] = code
|
486
500
|
end
|
487
501
|
|
502
|
+
# Run hook.
|
503
|
+
#
|
504
|
+
# @param name [String] Hook name.
|
505
|
+
# @yield code Hook arguments.
|
488
506
|
def ComoCommon.runHook( name, args )
|
489
507
|
@@hook[ name ].yield( args ) if @@hook[ name ]
|
490
508
|
end
|
@@ -507,13 +525,41 @@ module Como
|
|
507
525
|
# User interface for Como.
|
508
526
|
class Spec < ComoCommon
|
509
527
|
|
528
|
+
|
510
529
|
# Create specification for program with subcmds.
|
511
530
|
#
|
512
531
|
# @param author [String] Program author.
|
513
532
|
# @param year [String] Year (or dates) for program.
|
514
533
|
# @yield [] Subcmd definitions.
|
515
534
|
def Spec.program( author, year, config = nil, &defs )
|
535
|
+
Spec.defineProgram( author, year, config, &defs )
|
536
|
+
Spec.execute
|
537
|
+
end
|
538
|
+
|
516
539
|
|
540
|
+
# The primary entry point to Como. Defines the command
|
541
|
+
# switches and parses the command line. Performs "usage"
|
542
|
+
# display if "help" was selected.
|
543
|
+
#
|
544
|
+
# @param prog [String] Program (i.e. command) name.
|
545
|
+
# @param author [String] Author of the program.
|
546
|
+
# @param year [String] Year (or dates) for program.
|
547
|
+
# @param defs [Array<Array>] Option definitions.
|
548
|
+
# @param config [Hash] Option definition's behavioral config
|
549
|
+
# (changes @@config defaults).
|
550
|
+
def Spec.command( prog, author, year, defs = [], config = {} )
|
551
|
+
Spec.defineCommand( prog, author, year, defs, config )
|
552
|
+
Spec.execute
|
553
|
+
end
|
554
|
+
|
555
|
+
|
556
|
+
# Define options specification for program. User should
|
557
|
+
# perform {Spec.execute} separately.
|
558
|
+
#
|
559
|
+
# @param author [String] Program author.
|
560
|
+
# @param year [String] Year (or dates) for program.
|
561
|
+
# @yield [] Subcmd definitions.
|
562
|
+
def Spec.defineProgram( author, year, config = nil, &defs )
|
517
563
|
preHookArgs = {
|
518
564
|
:author => author,
|
519
565
|
:year => year,
|
@@ -526,17 +572,14 @@ module Como
|
|
526
572
|
if config
|
527
573
|
Opt.configOverlay( config )
|
528
574
|
end
|
575
|
+
|
529
576
|
spec = Spec.new( author, year )
|
530
577
|
spec.instance_eval( &defs )
|
531
|
-
Opt.main.check( ArgsParseState.new( @@argv ) )
|
532
|
-
|
533
|
-
ComoCommon.runHook( :postHook, Opt.main )
|
534
578
|
end
|
535
579
|
|
536
580
|
|
537
|
-
#
|
538
|
-
#
|
539
|
-
# display if "help" was selected.
|
581
|
+
# Define options specification for command. User should
|
582
|
+
# perform {Spec.execute} separately.
|
540
583
|
#
|
541
584
|
# @param prog [String] Program (i.e. command) name.
|
542
585
|
# @param author [String] Author of the program.
|
@@ -544,7 +587,7 @@ module Como
|
|
544
587
|
# @param defs [Array<Array>] Option definitions.
|
545
588
|
# @param config [Hash] Option definition's behavioral config
|
546
589
|
# (changes @@config defaults).
|
547
|
-
def Spec.
|
590
|
+
def Spec.defineCommand( prog, author, year, defs, config = {} )
|
548
591
|
|
549
592
|
preHookArgs = {
|
550
593
|
:prog => prog,
|
@@ -556,24 +599,24 @@ module Como
|
|
556
599
|
|
557
600
|
ComoCommon.runHook( :preHook, preHookArgs )
|
558
601
|
|
559
|
-
Spec.
|
560
|
-
|
561
|
-
ComoCommon.runHook( :postHook, Opt.main )
|
602
|
+
spec = Spec.new( author, year )
|
603
|
+
spec.subcmd( prog, defs, config )
|
562
604
|
end
|
563
605
|
|
564
606
|
|
565
|
-
# Alias
|
607
|
+
# Alias for {Spec.command}.
|
608
|
+
#
|
609
|
+
# NOTE: This method is deprecated and will be removed in
|
610
|
+
# future releases.
|
566
611
|
def Spec.defineCheckHelp( prog, author, year, defs, config = {} )
|
567
612
|
Spec.command( prog, author, year, defs, config )
|
568
613
|
end
|
569
614
|
|
570
615
|
|
571
|
-
#
|
572
|
-
|
573
|
-
def Spec.defineCheck( prog, author, year, defs, config = {} )
|
574
|
-
spec = Spec.new( author, year )
|
575
|
-
spec.subcmd( prog, defs, config )
|
616
|
+
# Perform command line options checking.
|
617
|
+
def Spec.execute
|
576
618
|
Opt.main.check( ArgsParseState.new( @@argv ) )
|
619
|
+
ComoCommon.runHook( :postHook, Opt.main )
|
577
620
|
end
|
578
621
|
|
579
622
|
|
@@ -595,7 +638,7 @@ module Como
|
|
595
638
|
# @param cmd [String] Subcmd name.
|
596
639
|
# @param defs [Array<Array>] Option definition table.
|
597
640
|
# @param config [] Configuration options.
|
598
|
-
def subcmd( cmd, defs, config = {} )
|
641
|
+
def subcmd( cmd, defs = [], config = {} )
|
599
642
|
|
600
643
|
unless Opt.main
|
601
644
|
|
@@ -622,8 +665,7 @@ module Como
|
|
622
665
|
defs.insert( 0, [ :silent, "help", "-h", "Display usage info." ] )
|
623
666
|
end
|
624
667
|
|
625
|
-
|
626
|
-
subcmd
|
668
|
+
Spec.specify( subcmd, defs )
|
627
669
|
|
628
670
|
end
|
629
671
|
|
@@ -632,10 +674,12 @@ module Como
|
|
632
674
|
alias command subcmd
|
633
675
|
|
634
676
|
|
635
|
-
#
|
677
|
+
# Check/fix options specs and create option objects for the
|
678
|
+
# whole table.
|
636
679
|
#
|
637
|
-
# @param
|
638
|
-
|
680
|
+
# @param subcmd [Opt] Subcommand target.
|
681
|
+
# @param table [Array<Array>] Option definition table for subcommand.
|
682
|
+
def Spec.specify( subcmd, table )
|
639
683
|
|
640
684
|
options = {}
|
641
685
|
subcmds = {}
|
@@ -643,58 +687,67 @@ module Como
|
|
643
687
|
# Type checks for valid user input.
|
644
688
|
Spec.ArgCheck( table.class == Array, "Option table is not an Array" )
|
645
689
|
|
646
|
-
table.
|
690
|
+
table.each do |e|
|
691
|
+
os = Spec.specifyOptOrSub( e )
|
692
|
+
case os.type
|
693
|
+
when :subcmd; subcmds[ os.name ] = os
|
694
|
+
else options[ os.name ] = os
|
695
|
+
end
|
696
|
+
end
|
697
|
+
|
698
|
+
subcmd.setOptionSubcmd( options.values, subcmds.values )
|
699
|
+
subcmd
|
700
|
+
end
|
647
701
|
|
648
|
-
i = table[ idx ]
|
649
702
|
|
650
|
-
Spec.ArgCheck( i.class == Array, "Option table entry is not an Array" )
|
651
703
|
|
652
|
-
|
704
|
+
# Check/fix options specs and create option objects for the
|
705
|
+
# whole table.
|
706
|
+
#
|
707
|
+
# @param opt_or_sub [Array<Array>] Option definition table.
|
708
|
+
def Spec.specifyOptOrSub( opt_or_sub )
|
653
709
|
|
654
|
-
|
655
|
-
table[ idx ] = [ i[0], nil, nil, i[1] ]
|
710
|
+
# Fix the table entries if needed.
|
656
711
|
|
657
|
-
|
712
|
+
Spec.ArgCheck( opt_or_sub.class == Array, "Option table entry is not an Array" )
|
658
713
|
|
659
|
-
|
660
|
-
table[ idx ] = [ i[0], i[1], nil, i[2] ]
|
661
|
-
end
|
714
|
+
if opt_or_sub[0] == :default && opt_or_sub.length == 2
|
662
715
|
|
663
|
-
|
664
|
-
|
665
|
-
end
|
716
|
+
# Add 2 dummy entries for :default type if needed.
|
717
|
+
opt_or_sub = [ opt_or_sub[0], nil, nil, opt_or_sub[1] ]
|
666
718
|
|
719
|
+
elsif opt_or_sub[0] == :subcmd && opt_or_sub.length == 3
|
667
720
|
|
668
|
-
|
721
|
+
# Add 1 dummy entry for :subcmd type if needed.
|
722
|
+
opt_or_sub = [ opt_or_sub[0], opt_or_sub[1], nil, opt_or_sub[2] ]
|
669
723
|
|
670
|
-
|
724
|
+
end
|
671
725
|
|
672
|
-
|
726
|
+
Spec.ArgCheck( opt_or_sub.length == 4,
|
727
|
+
"Option table entry length not 4" )
|
673
728
|
|
674
|
-
|
729
|
+
if opt_or_sub[0] == :subcmd
|
675
730
|
|
676
|
-
|
731
|
+
Opt.subcmd( opt_or_sub[1], opt_or_sub[3] )
|
677
732
|
|
678
|
-
|
733
|
+
else
|
679
734
|
|
680
|
-
|
681
|
-
:opt_single, :opt_multi, :opt_any
|
682
|
-
option = Opt.full( e[1], e[2], e[0], e[3] )
|
735
|
+
case opt_or_sub[0]
|
683
736
|
|
684
|
-
|
685
|
-
|
737
|
+
when :switch, :exclusive, :silent, :single, :multi,
|
738
|
+
:opt_single, :opt_multi, :opt_any
|
739
|
+
Opt.full( opt_or_sub[1], opt_or_sub[2], opt_or_sub[0], opt_or_sub[3] )
|
686
740
|
|
687
|
-
|
688
|
-
|
689
|
-
end
|
741
|
+
when :default
|
742
|
+
Opt.defaultOpt( opt_or_sub[3] )
|
690
743
|
|
691
|
-
|
744
|
+
else
|
745
|
+
raise "Unknown option type: \"#{opt_or_sub[0]}\"..."
|
692
746
|
|
693
747
|
end
|
694
748
|
|
695
749
|
end
|
696
750
|
|
697
|
-
[ options.values, subcmds.values ]
|
698
751
|
end
|
699
752
|
|
700
753
|
|
@@ -762,6 +815,7 @@ module Como
|
|
762
815
|
|
763
816
|
|
764
817
|
# Additional option check.
|
818
|
+
#
|
765
819
|
# @param opt [String] Option name.
|
766
820
|
# @param error [String] Error string for false return values (from check).
|
767
821
|
# @param check [Proc] Checker proc run for the option. Either
|
@@ -773,6 +827,7 @@ module Como
|
|
773
827
|
|
774
828
|
private
|
775
829
|
|
830
|
+
# Argument checking assertion.
|
776
831
|
def Spec.ArgCheck( cond, str )
|
777
832
|
raise( ArgumentError, str ) unless cond
|
778
833
|
end
|
@@ -790,9 +845,13 @@ module Como
|
|
790
845
|
class Opt < ComoCommon
|
791
846
|
|
792
847
|
|
793
|
-
# Create exception with capability to pass arbitrary data
|
848
|
+
# Create exception with capability to pass arbitrary data.
|
794
849
|
class ErrorWithData < StandardError
|
850
|
+
|
851
|
+
# Exception data.
|
795
852
|
attr_reader :data
|
853
|
+
|
854
|
+
# Create error exception.
|
796
855
|
def initialize( message = nil, data = nil )
|
797
856
|
super( message )
|
798
857
|
@data = data
|
@@ -1023,10 +1082,10 @@ module Como
|
|
1023
1082
|
attr_reader :rules
|
1024
1083
|
|
1025
1084
|
|
1026
|
-
# Create Opt object
|
1027
|
-
# [
|
1028
|
-
# [
|
1029
|
-
# [
|
1085
|
+
# Create Opt object.
|
1086
|
+
# @param name [String] Option name.
|
1087
|
+
# @param opt [String] Switch.
|
1088
|
+
# @param type [Symbol] Option type. One of:
|
1030
1089
|
# * :switch
|
1031
1090
|
# * :single
|
1032
1091
|
# * :multi
|
@@ -1036,9 +1095,8 @@ module Como
|
|
1036
1095
|
# * :default
|
1037
1096
|
# * :exclusive
|
1038
1097
|
# * :silent
|
1039
|
-
# [
|
1040
|
-
# [
|
1041
|
-
|
1098
|
+
# @param doc [String] Option documentation.
|
1099
|
+
# @param value [Object] Default value.
|
1042
1100
|
def initialize( name, opt, type, doc, value = nil )
|
1043
1101
|
@parent = nil
|
1044
1102
|
@name = name
|
@@ -1046,11 +1104,16 @@ module Como
|
|
1046
1104
|
@longOpt = "--#{name}"
|
1047
1105
|
@type = type
|
1048
1106
|
@value = value
|
1107
|
+
|
1108
|
+
if hasMany && value == nil
|
1109
|
+
@value = []
|
1110
|
+
end
|
1111
|
+
|
1049
1112
|
@doc = doc
|
1050
1113
|
# Whether option was set or not.
|
1051
1114
|
@given = false
|
1052
|
-
@subopt =
|
1053
|
-
@subcmd =
|
1115
|
+
@subopt = []
|
1116
|
+
@subcmd = []
|
1054
1117
|
@rules = nil
|
1055
1118
|
|
1056
1119
|
@config = @@config.dup
|
@@ -1059,20 +1122,35 @@ module Como
|
|
1059
1122
|
end
|
1060
1123
|
|
1061
1124
|
|
1062
|
-
# Set subcommand suboptions.
|
1125
|
+
# Set command (subcommand) suboptions and subcmds.
|
1063
1126
|
#
|
1064
1127
|
# @param opts [Array<Opt>]
|
1065
|
-
def
|
1128
|
+
def setOptionSubcmd( opts, subs )
|
1066
1129
|
opts.each do |i|
|
1067
|
-
i
|
1130
|
+
addOption( i )
|
1068
1131
|
end
|
1069
1132
|
|
1070
1133
|
subs.each do |i|
|
1071
|
-
i
|
1134
|
+
addSubcmd( i )
|
1072
1135
|
end
|
1136
|
+
end
|
1073
1137
|
|
1074
|
-
|
1075
|
-
|
1138
|
+
|
1139
|
+
# Add subcommand option.
|
1140
|
+
#
|
1141
|
+
# @param opt [Option] Option to add.
|
1142
|
+
def addOption( opt )
|
1143
|
+
opt.parent = self
|
1144
|
+
@subopt.push opt
|
1145
|
+
end
|
1146
|
+
|
1147
|
+
|
1148
|
+
# Add subcommand subcmd.
|
1149
|
+
#
|
1150
|
+
# @param cmd [Option] Subcmd to add.
|
1151
|
+
def addSubcmd( cmd )
|
1152
|
+
cmd.parent = self
|
1153
|
+
@subcmd.push cmd
|
1076
1154
|
end
|
1077
1155
|
|
1078
1156
|
|
@@ -1101,8 +1179,7 @@ module Como
|
|
1101
1179
|
# Start at top.
|
1102
1180
|
top = self
|
1103
1181
|
|
1104
|
-
|
1105
|
-
|
1182
|
+
parse = Proc.new do
|
1106
1183
|
# Parse and check for invalid arguments.
|
1107
1184
|
begin
|
1108
1185
|
top = top.parse( argsState, top.config[ :check_invalid ] )
|
@@ -1110,10 +1187,10 @@ module Como
|
|
1110
1187
|
|
1111
1188
|
# Check for any missing valid arguments.
|
1112
1189
|
checkMissing
|
1190
|
+
end
|
1113
1191
|
|
1114
|
-
|
1115
|
-
|
1116
|
-
error( err.to_s )
|
1192
|
+
error = Proc.new do |err|
|
1193
|
+
errornl( err.to_s )
|
1117
1194
|
|
1118
1195
|
# Display subcmd specific usage info.
|
1119
1196
|
err.data.usage
|
@@ -1121,6 +1198,12 @@ module Como
|
|
1121
1198
|
exit( 1 )
|
1122
1199
|
end
|
1123
1200
|
|
1201
|
+
begin
|
1202
|
+
parse.call
|
1203
|
+
rescue Opt::MissingArgument, Opt::InvalidOption => err
|
1204
|
+
error.call( err )
|
1205
|
+
end
|
1206
|
+
|
1124
1207
|
# Revert back to top after hierarchy travelsal.
|
1125
1208
|
usageIfHelp
|
1126
1209
|
|
@@ -1332,7 +1415,7 @@ module Como
|
|
1332
1415
|
|
1333
1416
|
rescue Opt::MissingArgument, Opt::InvalidOption => err
|
1334
1417
|
@@io.puts
|
1335
|
-
|
1418
|
+
errornl( err.to_s )
|
1336
1419
|
|
1337
1420
|
usage( nil, true )
|
1338
1421
|
|
@@ -1341,6 +1424,7 @@ module Como
|
|
1341
1424
|
|
1342
1425
|
|
1343
1426
|
# Additional option check.
|
1427
|
+
#
|
1344
1428
|
# @param opt [String] Option name.
|
1345
1429
|
# @param error [String] Error string for false return values (from check).
|
1346
1430
|
# @param check [Proc] Checker proc run for the option. Either
|
@@ -1352,7 +1436,7 @@ module Como
|
|
1352
1436
|
end
|
1353
1437
|
rescue Opt::MissingArgument, Opt::InvalidOption => err
|
1354
1438
|
@@io.puts
|
1355
|
-
|
1439
|
+
errornl( err.to_s )
|
1356
1440
|
err.data.usage
|
1357
1441
|
exit( 1 )
|
1358
1442
|
end
|
@@ -1422,7 +1506,11 @@ module Como
|
|
1422
1506
|
# Example usage: fileName = Opt["file"].apply( "no_name.txt" )
|
1423
1507
|
def apply( default = nil )
|
1424
1508
|
if given
|
1425
|
-
|
1509
|
+
if @type == :switch
|
1510
|
+
true
|
1511
|
+
else
|
1512
|
+
value
|
1513
|
+
end
|
1426
1514
|
else
|
1427
1515
|
default
|
1428
1516
|
end
|
@@ -1538,7 +1626,7 @@ module Como
|
|
1538
1626
|
# Option requires many arguments?
|
1539
1627
|
def hasMany
|
1540
1628
|
case @type
|
1541
|
-
when :multi, :opt_multi, :opt_any, :exclusive; true
|
1629
|
+
when :multi, :opt_multi, :opt_any, :exclusive, :default; true
|
1542
1630
|
else false
|
1543
1631
|
end
|
1544
1632
|
end
|
@@ -1619,7 +1707,7 @@ module Como
|
|
1619
1707
|
|
1620
1708
|
# Display program usage (and optionally exit).
|
1621
1709
|
def usageIfHelp
|
1622
|
-
if self['help'].given
|
1710
|
+
if self.argByName( 'help' ) && self['help'].given
|
1623
1711
|
usage
|
1624
1712
|
elsif hasSubcmd && givenSubcmd
|
1625
1713
|
givenSubcmd.usageIfHelp
|
@@ -1746,11 +1834,24 @@ module Como
|
|
1746
1834
|
|
1747
1835
|
|
1748
1836
|
# Como error printout.
|
1749
|
-
def error( str )
|
1750
|
-
|
1837
|
+
def error( str, nl = false )
|
1838
|
+
nl = nl ? "\n" : ""
|
1839
|
+
STDERR.puts( "#{nl}#{Opt.progname} error: #{str}" )
|
1840
|
+
end
|
1841
|
+
|
1842
|
+
|
1843
|
+
# Como error printout with pre-newline.
|
1844
|
+
def errornl( str )
|
1845
|
+
error( str, true )
|
1751
1846
|
end
|
1752
1847
|
|
1753
1848
|
|
1849
|
+
# Como error printout with immediate exit.
|
1850
|
+
def fatal( str )
|
1851
|
+
error( str )
|
1852
|
+
exit( false )
|
1853
|
+
end
|
1854
|
+
|
1754
1855
|
|
1755
1856
|
|
1756
1857
|
# ------------------------------------------------------------
|
@@ -1853,8 +1954,12 @@ module Como
|
|
1853
1954
|
attr_reader :author, :year
|
1854
1955
|
|
1855
1956
|
|
1957
|
+
# Create program main option.
|
1856
1958
|
def initialize( author, year,
|
1857
|
-
|
1959
|
+
name, opt,
|
1960
|
+
type, doc,
|
1961
|
+
value = nil )
|
1962
|
+
|
1858
1963
|
@author = author
|
1859
1964
|
@year = year
|
1860
1965
|
@external = nil
|
@@ -1892,6 +1997,8 @@ module Como
|
|
1892
1997
|
# Command argument parsing state.
|
1893
1998
|
class ArgsParseState
|
1894
1999
|
|
2000
|
+
# Create parse state.
|
2001
|
+
#
|
1895
2002
|
# @param list [Array<String>] List of Command Line Arguments
|
1896
2003
|
# (default: ARGV).
|
1897
2004
|
def initialize( list )
|
@@ -1973,11 +2080,17 @@ module Como
|
|
1973
2080
|
# end
|
1974
2081
|
class RuleCheck
|
1975
2082
|
|
2083
|
+
# Perform rule checking options.
|
2084
|
+
#
|
2085
|
+
# @param opt [Object] Options to check.
|
2086
|
+
# @yield rule Checking conditions.
|
1976
2087
|
def RuleCheck.check( opt, &rule )
|
1977
2088
|
rc = RuleCheck.new( opt )
|
1978
2089
|
rc.instance_eval( &rule )
|
1979
2090
|
end
|
1980
2091
|
|
2092
|
+
|
2093
|
+
# Build Rule checker.
|
1981
2094
|
def initialize( opt, &rule )
|
1982
2095
|
@opt = opt
|
1983
2096
|
end
|
@@ -2096,11 +2209,13 @@ module Como
|
|
2096
2209
|
rd.evalAndDisplay( &rule )
|
2097
2210
|
end
|
2098
2211
|
|
2212
|
+
# Create rule displayer.
|
2099
2213
|
def initialize( prefixStr )
|
2100
2214
|
# Prefix string for lines. Rules add/rm from it.
|
2101
2215
|
@prefixStr = prefixStr
|
2102
2216
|
end
|
2103
2217
|
|
2218
|
+
# Display method.
|
2104
2219
|
def evalAndDisplay( &rule )
|
2105
2220
|
printRule( instance_eval( &rule ) )
|
2106
2221
|
end
|