Class: Como::Spec
- Inherits:
-
ComoCommon
- Object
- ComoCommon
- Como::Spec
- Defined in:
- lib/como.rb
Overview
User interface for Como.
Constant Summary collapse
- @@argv =
Command line options source.
ARGV
Class Method Summary collapse
- .ArgCheck(cond, str) ⇒ Object
-
.checkAlso(opt, error, &check) ⇒ Object
Additional option check.
-
.checkRule(opt = nil, &rule) ⇒ Object
Check option combination rules.
-
.command(prog, author, year, defs, config = {}) ⇒ Object
The primary entry point to Como.
-
.defineCheck(prog, author, year, defs, config = {}) ⇒ Object
Same as “defineCheckHelp” except without automatic “help” option processing.
-
.defineCheckHelp(prog, author, year, defs, config = {}) ⇒ Object
Alias to Spec.command.
-
.program(author, year, config = nil) { ... } ⇒ Object
Create specification for program with subcmds.
-
.setArgv(newArgv) ⇒ Object
Set command line options source, i.e.
-
.setUsageFooter(str) ⇒ Object
Set optional footer for “usage”.
-
.setUsageHeader(str) ⇒ Object
Set optional header for “usage”.
-
.specify(table) ⇒ Object
Specify and check options spec.
-
.usage ⇒ Object
Display program usage (and optionally exit).
Instance Method Summary collapse
-
#checkRule(opt = nil, &rule) ⇒ Object
Check option combination rules.
-
#initialize(author, year) ⇒ Spec
constructor
Create Spec object that can handle subcmd definitions.
-
#subcmd(cmd, defs, config = {}) ⇒ Object
Define subcommand options.
Methods inherited from ComoCommon
Constructor Details
#initialize(author, year) ⇒ Spec
Create Spec object that can handle subcmd definitions.
494 495 496 497 498 499 500 |
# File 'lib/como.rb', line 494 def initialize( , year ) @author = @year = year Spec.ArgCheck( .class == String, "Author name is not a String" ) Spec.ArgCheck( year.class == String, "Year is not a String" ) end |
Class Method Details
.ArgCheck(cond, str) ⇒ Object
674 675 676 |
# File 'lib/como.rb', line 674 def Spec.ArgCheck( cond, str ) raise( ArgumentError, str ) unless cond end |
.checkAlso(opt, error, &check) ⇒ Object
Additional option check.
667 668 669 |
# File 'lib/como.rb', line 667 def Spec.checkAlso( opt, error, &check ) Opt.main.checkAlso( opt, error, &check ) end |
.checkRule(opt = nil, &rule) ⇒ Object
Check option combination rules.
637 638 639 640 641 642 643 644 645 |
# File 'lib/como.rb', line 637 def Spec.checkRule( opt = nil, &rule ) if opt opt = Opt[ opt ] else opt = Opt.current end opt.setRuleCheck( &rule ) opt.checkRule end |
.command(prog, author, year, defs, config = {}) ⇒ Object
The primary entry point to Como. Defines the command switches and parses the command line. Performs “usage” display if “help” was selected.
471 472 473 474 |
# File 'lib/como.rb', line 471 def Spec.command( prog, , year, defs, config = {} ) Spec.defineCheck( prog, , year, defs, config ) # Spec.usage if Opt['help'].given end |
.defineCheck(prog, author, year, defs, config = {}) ⇒ Object
Same as “defineCheckHelp” except without automatic “help” option processing.
483 484 485 486 487 |
# File 'lib/como.rb', line 483 def Spec.defineCheck( prog, , year, defs, config = {} ) spec = Spec.new( , year ) spec.subcmd( prog, defs, config ) Opt.main.check( ArgsParseState.new( @@argv ) ) end |
.defineCheckHelp(prog, author, year, defs, config = {}) ⇒ Object
Alias to Spec.command.
477 478 479 |
# File 'lib/como.rb', line 477 def Spec.defineCheckHelp( prog, , year, defs, config = {} ) Spec.command( prog, , year, defs, config ) end |
.program(author, year, config = nil) { ... } ⇒ Object
Create specification for program with subcmds.
451 452 453 454 455 456 457 458 |
# File 'lib/como.rb', line 451 def Spec.program( , year, config = nil, &defs ) if config Opt.configOverlay( config ) end spec = Spec.new( , year ) spec.instance_eval( &defs ) Opt.main.check( ArgsParseState.new( @@argv ) ) end |
.setArgv(newArgv) ⇒ Object
Set command line options source, i.e. @@argv (default: ARGV).
611 612 613 |
# File 'lib/como.rb', line 611 def Spec.setArgv( newArgv ) @@argv = newArgv end |
.setUsageFooter(str) ⇒ Object
Set optional footer for “usage”.
627 628 629 |
# File 'lib/como.rb', line 627 def Spec.( str ) Opt.main.( str ) end |
.setUsageHeader(str) ⇒ Object
Set optional header for “usage”.
621 622 623 |
# File 'lib/como.rb', line 621 def Spec.setUsageHeader( str ) Opt.main.setUsageHeader( str ) end |
.specify(table) ⇒ Object
Specify and check options spec.
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/como.rb', line 544 def Spec.specify( table ) = {} subcmds = {} # Type checks for valid user input. Spec.ArgCheck( table.class == Array, "Option table is not an Array" ) table.each_index do |idx| i = table[ idx ] Spec.ArgCheck( i.class == Array, "Option table entry is not an Array" ) if i[0] == :default && i.length == 2 # Add 2 dummy entries for :default type if needed. table[ idx ] = [ i[0], nil, nil, i[1] ] elsif i[0] == :subcmd && i.length == 3 # Add 1 dummy entry for :subcmd type if needed. table[ idx ] = [ i[0], i[1], nil, i[2] ] end Spec.ArgCheck( table[ idx ].length == 4, "Option table entry length not 4" ) end table.each do |e| if e[0] == :subcmd subcmds[ e[1] ] = Opt.subcmd( e[1], e[3] ) else option = nil case e[0] when :switch, :exclusive, :silent, :single, :multi, :opt_single, :opt_multi, :opt_any option = Opt.full( e[1], e[2], e[0], e[3] ) when :default option = Opt.defaultOpt( e[3] ) else raise "Unknown option type: \"#{e[0]}\"..." end [ option.name ] = option end end [ .values, subcmds.values ] end |
Instance Method Details
#checkRule(opt = nil, &rule) ⇒ Object
Check option combination rules.
653 654 655 656 657 658 659 660 |
# File 'lib/como.rb', line 653 def checkRule( opt = nil, &rule ) if opt opt = Opt[ opt ] else opt = Opt.current end opt.setRuleCheck( &rule ) end |
#subcmd(cmd, defs, config = {}) ⇒ Object
Define subcommand options.
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
# File 'lib/como.rb', line 508 def subcmd( cmd, defs, config = {} ) unless Opt.main main = MainOpt.new( @author, @year, cmd, nil, :subcmd, nil ) Opt.setMain( main ) subcmd = main else subcmd = Opt.findOpt( cmd ) Opt.setSubcmd( subcmd ) Spec.ArgCheck( false, "Subcommand \"#{cmd}\" not defined." ) unless subcmd end # Overlay user config on top of default. subcmd.applyConfig( config ) if subcmd.config[ :autohelp ] # Automatically add the help option. defs.insert( 0, [ :silent, "help", "-h", "Display usage info." ] ) end subcmd.setSubopt( *Spec.specify( defs ) ) subcmd end |