Class: Como::Opt

Inherits:
ComoCommon show all
Defined in:
lib/como.rb

Overview

Opt includes all options spec information and parsed options and their values. Option instance is accessed with “Opt”. The option status (Opt instance) can be queried with for example “given” and “value” methods.

Direct Known Subclasses

MainOpt

Defined Under Namespace

Classes: ErrorWithData, InvalidOption, MissingArgument

Constant Summary collapse

@@main =

Program i.e. highest level subcommand.

nil
@@opts =

List of parsed option specs and option values.

[]
@@subcmd =

Current subcommand recorded.

nil
@@config =

Set of default configs for printout.

{
    :autohelp      => true,
    :rulehelp      => false,
    :header        => nil,
    :footer        => nil,
    :subcheck      => true,
    :check_missing => true,
    :check_invalid => true,
    :tab           => 12,
    :help_exit     => true,
    :copyright     => true,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ComoCommon

getIo, runHook, setHook, setIo

Constructor Details

#initialize(name, opt, type, doc, value = nil) ⇒ Opt

Create Opt object.

Parameters:

  • name (String)

    Option name.

  • opt (String)

    Switch.

  • type (Array<Symbol>, Symbol)

    Option type in primitives (or :subcmd).

  • doc (String)

    Option documentation.

  • value (Object) (defaults to: nil)

    Default value.



1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
# File 'lib/como.rb', line 1223

def initialize( name, opt, type, doc, value = nil )
    @parent = nil
    @name = name
    @shortOpt = opt
    @longOpt = "--#{name}"
    @type = type

    @value = value

    if @type != :subcmd
        if prim?( :many ) && value == nil
            @value = []
        end
    end

    @doc = doc
    # Whether option was set or not.
    @given = false
    @subopt = []
    @subcmd = []
    @rules = nil

    @config = @@config.dup

    Opt.addOpt( self )
end

Instance Attribute Details

#configObject (readonly)

Opt configuration.



1210
1211
1212
# File 'lib/como.rb', line 1210

def config
  @config
end

#docObject

Option documentation string.



1198
1199
1200
# File 'lib/como.rb', line 1198

def doc
  @doc
end

#given(optArg = false, &prog) ⇒ Object Also known as: given?

Returns true if option is given, and block is not present. When block is present, the block is executed (with value as parameter) if option has been given.

Parameters:

  • optArg (Boolean) (defaults to: false)

    Pass Opt to block instead of its value.



1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
# File 'lib/como.rb', line 1665

def given( optArg = false, &prog )
    if block_given?
        if @given
            if optArg
                yield( self )
            else
                yield( @value )
            end
        else
            false
        end
    else
        @given
    end
end

#longOptObject

Long option string.



1189
1190
1191
# File 'lib/como.rb', line 1189

def longOpt
  @longOpt
end

#nameObject

Option name.



1183
1184
1185
# File 'lib/como.rb', line 1183

def name
  @name
end

#parentObject

Subcommand parent (i.e. host).



1179
1180
1181
# File 'lib/como.rb', line 1179

def parent
  @parent
end

#rulesObject (readonly)

Opt rules.



1213
1214
1215
# File 'lib/como.rb', line 1213

def rules
  @rules
end

#shortOptObject

Short option string.



1186
1187
1188
# File 'lib/como.rb', line 1186

def shortOpt
  @shortOpt
end

#subcmdObject (readonly)

List of subcommands.



1207
1208
1209
# File 'lib/como.rb', line 1207

def subcmd
  @subcmd
end

#suboptObject (readonly)

List of suboptions.



1204
1205
1206
# File 'lib/como.rb', line 1204

def subopt
  @subopt
end

#typeObject

Option type as array of primitives (or :subcmd).



1192
1193
1194
# File 'lib/como.rb', line 1192

def type
  @type
end

#valueObject

Option value.



1195
1196
1197
# File 'lib/como.rb', line 1195

def value
  @value
end

Class Method Details

.[](str) ⇒ Object

Select option object by name. Main is searched first and then the flattened list of all options.



1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
# File 'lib/como.rb', line 1062

def Opt.[](str)

    # Search Main first.
    ret = Opt.main.argByName( str )

    unless ret
        ret = Opt.findOpt( str )
        unless ret
            raise RuntimeError, "Option \"#{str}\" does not exist..."
        end
    end

    ret
end

.addOpt(opt) ⇒ Object

Add option to options list.



1026
1027
1028
# File 'lib/como.rb', line 1026

def Opt.addOpt( opt )
    @@opts.push opt
end

.authorObject

Return author.



1091
1092
1093
# File 'lib/como.rb', line 1091

def Opt.author
    @@main.author
end

.configGetObject

Return default config for Como options.



1144
1145
1146
# File 'lib/como.rb', line 1144

def Opt.configGet
    @@config
end

.configOverlay(config) ⇒ Object

Overlay Opt default configuration options.



1138
1139
1140
# File 'lib/como.rb', line 1138

def Opt.configOverlay( config )
    @@config.merge!( config )
end

.configSet(config) ⇒ Object

Set default config for Como options. User can manipulate the defaults with “preHook”.



1151
1152
1153
# File 'lib/como.rb', line 1151

def Opt.configSet( config )
    @@config = config
end

.currentObject

Current subcmd processed.



1038
1039
1040
# File 'lib/como.rb', line 1038

def Opt.current
    @@subcmd
end

.defaultObject

Return arguments (options) that have no switch.



1104
1105
1106
# File 'lib/como.rb', line 1104

def Opt.default
    Opt.main.default
end

.defaultOpt(doc = "No doc.") ⇒ Object

Create default option spec, no switch.



1120
1121
1122
# File 'lib/como.rb', line 1120

def Opt.defaultOpt( doc = "No doc." )
    new( "<default>", "<default>", [ :default, :none, :one, :many, :opt ], doc, [] )
end

.each(&blk) ⇒ Object

Options iterator for all options.



1126
1127
1128
# File 'lib/como.rb', line 1126

def Opt.each( &blk )
    Opt.main.each( &blk )
end

.each_given(&blk) ⇒ Object

Options iterator for given options.



1132
1133
1134
# File 'lib/como.rb', line 1132

def Opt.each_given( &blk )
    Opt.main.each_given( &blk )
end

.error(str, nl = false) ⇒ Object

Issue non-fatal user error. See #error.



1157
1158
1159
# File 'lib/como.rb', line 1157

def Opt.error( str, nl = false )
    Opt.main.error( str, nl )
end

.externalObject

Return arguments (options) that are specified as command external (i.e. after '–').



1098
1099
1100
# File 'lib/como.rb', line 1098

def Opt.external
    Opt.main.external
end

.fatal(str) ⇒ Object

Issue fatal user error. See #fatal.



1163
1164
1165
# File 'lib/como.rb', line 1163

def Opt.fatal( str )
    Opt.main.fatal( str )
end

.findOpt(name) ⇒ Object

Find option by name.



1044
1045
1046
1047
1048
1049
1050
1051
# File 'lib/como.rb', line 1044

def Opt.findOpt( name )
    idx = @@opts.index do |i| i.name == name end
    if idx
        @@opts[ idx ]
    else
        nil
    end
end

.full(name, opt, type, doc = "No doc.") ⇒ Object

Create option spec.



1110
1111
1112
# File 'lib/como.rb', line 1110

def Opt.full( name, opt, type, doc = "No doc." )
    new( name, opt, type, doc )
end

.mainObject

Get main option.



1020
1021
1022
# File 'lib/como.rb', line 1020

def Opt.main
    @@main
end

.prognameObject

Return program name.



1079
1080
1081
# File 'lib/como.rb', line 1079

def Opt.progname
    @@main.name
end

.resetObject

Reset “dynamic” class members.



1055
1056
1057
# File 'lib/como.rb', line 1055

def Opt.reset
    @@opts = []
end

.setMain(main) ⇒ Object

Set main option.



1014
1015
1016
1017
# File 'lib/como.rb', line 1014

def Opt.setMain( main )
    @@main = main
    Opt.setSubcmd( main )
end

.setSubcmd(opt) ⇒ Object

Set current subcmd.



1032
1033
1034
# File 'lib/como.rb', line 1032

def Opt.setSubcmd( opt )
    @@subcmd = opt
end

.subcmd(name, doc = "No doc.") ⇒ Object

Create sub-command option spec.



1115
1116
1117
# File 'lib/como.rb', line 1115

def Opt.subcmd( name, doc = "No doc." )
    new( name, nil, :subcmd, doc, false )
end

.warn(str, nl = false) ⇒ Object

Issue user warning. See #warn.



1169
1170
1171
# File 'lib/como.rb', line 1169

def Opt.warn( str, nl = false )
    Opt.main.warn( str, nl )
end

.yearObject

Return program year.



1085
1086
1087
# File 'lib/como.rb', line 1085

def Opt.year
    @@main.year
end

Instance Method Details

#[](str) ⇒ Object

Select option object by name operator.



1591
1592
1593
1594
1595
1596
1597
# File 'lib/como.rb', line 1591

def []( str )
    ret = argByName( str )
    unless ret
        raise RuntimeError, "Subopt \"#{str}\" does not exist for \"#{@name}\"!"
    end
    ret
end

#addOption(opt) ⇒ Object

Add subcommand option.

Parameters:

  • opt (Option)

    Option to add.



1268
1269
1270
1271
# File 'lib/como.rb', line 1268

def addOption( opt )
    opt.parent = self
    @subopt.push opt
end

#addSubcmd(cmd) ⇒ Object

Add subcommand subcmd.

Parameters:

  • cmd (Option)

    Subcmd to add.



1277
1278
1279
1280
# File 'lib/como.rb', line 1277

def addSubcmd( cmd )
    cmd.parent = self
    @subcmd.push cmd
end

#apply(default = nil) ⇒ Object

Return option value if given otherwise the default. Example usage: fileName = Opt.apply( “no_name.txt” )



1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
# File 'lib/como.rb', line 1646

def apply( default = nil )
    if given
        if prim?( :none ) && !( prim?( :one ) || prim?( :many ) )
            true
        else
            value
        end
    else
        default
    end
end

#applyConfig(config) ⇒ Object

Merge config to base config.

Parameters:

  • config (Hash)

    Configuration Hash to merge.



1286
1287
1288
# File 'lib/como.rb', line 1286

def applyConfig( config )
    @config.merge!( config )
end

#argById(str) ⇒ Object

Select option object by name/opt/longOpt.



1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
# File 'lib/como.rb', line 1737

def argById( str )
    if str == nil || str == :default
        @subopt.each do |o|
            if o.prim?( :default )
                return o
            end
        end
        nil
    else
        suball.each do |o|
            if str == o.name || str == o.opt || str == o.longOpt
                return o
            end
        end
        nil
    end
end

#argByName(str) ⇒ Object

Select option object by name.



1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
# File 'lib/como.rb', line 1717

def argByName( str )
    if str == nil || str == :default
        @subopt.each do |o|
            if o.prim?( :default )
                return o
            end
        end
        nil
    else
        suball.each do |o|
            if str == o.name
                return o
            end
        end
        nil
    end
end

#check(argsState) ⇒ Object

Check provided args.



1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
# File 'lib/como.rb', line 1303

def check( argsState )

    # Start at top.
    top = self

    parse = Proc.new do
        # Parse and check for invalid arguments.
        begin
            top = top.parse( argsState, top.config[ :check_invalid ] )
        end while( top )

        # Check for any missing valid arguments.
        checkMissing
    end

    error = Proc.new do |err|
        errornl( err.to_s )

        # Display subcmd specific usage info.
        err.data.usage

        exit( 1 )
    end

    begin
        parse.call
    rescue Opt::MissingArgument, Opt::InvalidOption => err
        error.call( err )
    end

    # Revert back to top after hierarchy travelsal.
    usageIfHelp

    # Check rules.
    cur = self
    while cur
        cur.checkRule
        cur = cur.givenSubcmd
    end

    self
end

#checkAlso(opt, error, &check) ⇒ Object

Additional option check.

Parameters:

  • opt (String)

    Option name.

  • error (String)

    Error string for false return values (from check).

  • check (Proc)

    Checker proc run for the option. Either

Returns:

  • false or generate an exception when errors found.



1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
# File 'lib/como.rb', line 1571

def checkAlso( opt, error, &check )
    begin
        if self[opt].evalCheck( &check ) != true
            raise Opt::InvalidOption.new( error, self )
        end
    rescue Opt::MissingArgument, Opt::InvalidOption => err
        @@io.puts
        errornl( err.to_s )
        err.data.usage
        exit( 1 )
    end
end

#checkMissingObject

Check for any non-given required arguments recursively through hierarchy of subcommands. MissingArgument Exception is generated if argument is missing.

Raises:



1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
# File 'lib/como.rb', line 1491

def checkMissing

    return unless config[ :check_missing ]

    # Full cmd name.
    cmd = ( getParents.map do |i| i.name end ).join( ' ' )

    # Check for any exclusive args first.
    @subopt.each do |o|
        if o.prim?( :mutex ) && o.given
            return
        end
    end


    # Check for required arguments for this level before
    # subcmds.
    @subopt.each do |o|
        if !o.prim?( :opt )
            unless o.given
                raise MissingArgument.new(
                    "Option \"#{o.opt}\" missing for \"#{cmd}\"...",
                    self )
            end
        end
    end

    if hasSubcmd
        if @config[ :subcheck ]
            # Compulsory Subcommand checking enabled.
            subcmdMissing = true
        else
            subcmdMissing = false
        end
    else
        subcmdMissing = false
    end

    # Check for any subcmd args.
    sub = givenSubcmd
    if sub
        subcmdMissing = false
        # Use recursion to examine the next level.
        return sub.checkMissing
    end

    # If no subcmds are given, issue error.
    raise MissingArgument.new(
        "Subcommand required for \"#{cmd}\"...",
        self ) if subcmdMissing

end

#checkRuleObject

Check option combination rules.



1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
# File 'lib/como.rb', line 1547

def checkRule

    return unless @rules

    begin
        raise Opt::InvalidOption.new( "Option combination mismatch!", self ) unless
            RuleCheck.check( self, &@rules )

    rescue Opt::MissingArgument, Opt::InvalidOption => err
        @@io.puts
        errornl( err.to_s )

        usage( nil, true )

    end
end

#cmdlineObject

Return cmdline usage strings for options in an Array.



1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
# File 'lib/como.rb', line 1857

def cmdline
    opts = []

    @subopt.each do |o|

        next if o.prim?( :hidden )

        prural = nil
        if o.prim?( :none ) && o.prim?( :many )
            prural = "*"
        elsif o.prim?( :one ) && !o.prim?( :many ) && o.prim?( :repeat )
            prural = "#"
        elsif o.prim?( :one ) && o.prim?( :many )
            prural = "+"
        else
            prural = ""
        end

        if !( o.hasSwitchStyleDoc )
            name = " <#{o.name}>#{prural}"
        else
            name = ""
        end

        if o.shortOpt == nil
            opt = o.longOpt
        else
            opt = o.shortOpt
        end

        if !o.prim?( :opt )
            opts.push "#{opt}#{name}"
        else
            opts.push "[#{opt}#{name}]"
        end
    end


    if hasSubcmd
        opts.push "<<subcommand>>"
    end

    opts

end

#defaultObject

Return default options.



1711
1712
1713
# File 'lib/como.rb', line 1711

def default
    argByName( nil )
end

#each(&blk) ⇒ Object

Options list iterator.



1613
1614
1615
1616
1617
# File 'lib/como.rb', line 1613

def each( &blk )
    suball.each do |o|
        yield o
    end
end

#each_given(&blk) ⇒ Object

Options iterator for given options.



1621
1622
1623
1624
1625
# File 'lib/como.rb', line 1621

def each_given( &blk )
    suball.each do |o|
        yield o if o.given
    end
end

#error(str, nl = false) ⇒ Object

Como error printout.

Parameters:

  • str (String)

    Error message.

  • nl (Boolean) (defaults to: false)

    Prepend msg with newline.



1983
1984
1985
1986
# File 'lib/como.rb', line 1983

def error( str, nl = false )
    nl = nl ? "\n" : ""
    STDERR.puts( "#{nl}#{Opt.progname} error: #{str}" )
end

#errornl(str) ⇒ Object

Como error printout with pre-newline.



1990
1991
1992
# File 'lib/como.rb', line 1990

def errornl( str )
    error( str, true )
end

#evalCheck(&check) ⇒ Object

Custom check for the option. User has to know some Como internals.



2014
2015
2016
# File 'lib/como.rb', line 2014

def evalCheck( &check )
    instance_eval( &check )
end

#fatal(str) ⇒ Object

Como error printout with immediate exit.



1996
1997
1998
1999
# File 'lib/como.rb', line 1996

def fatal( str )
    error( str )
    exit( false )
end

#findOpt(str) ⇒ Object

Find option object by option str.



1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
# File 'lib/como.rb', line 1930

def findOpt( str )
    if str == nil
        subopt.detect { |i| i.prim?( :default ) }
    elsif str[0..1] == "--"
        subopt.detect { |i| i.longOpt == str }
    elsif str[0..0] == "-"
        subopt.detect { |i| i.opt == str }
    else
        suball.detect { |i| i.name == str }
    end
end

#givenCountObject

Number of given options.



1629
1630
1631
1632
1633
1634
1635
# File 'lib/como.rb', line 1629

def givenCount
    cnt = 0
    each_given do |i|
        cnt += 1
    end
    cnt
end

#givenSubcmdObject

Return the selected subcommand.



1687
1688
1689
# File 'lib/como.rb', line 1687

def givenSubcmd
    ( @subcmd.select do |o| o.given end )[0]
end

#hasSwitchStyleDocObject

Test if option is of switch type.



1763
1764
1765
1766
1767
1768
1769
1770
# File 'lib/como.rb', line 1763

def hasSwitchStyleDoc
    if ( prim?( :none ) && !prim?( :many ) ) ||
       prim?( :default )
        true
    else
        false
    end
end

#optObject

Option's opt id. Short if exists otherwise long.



1601
1602
1603
# File 'lib/como.rb', line 1601

def opt
    @shortOpt ? @shortOpt : @longOpt
end

#paramsHash

Returns Hash of option value parameters. Example command line content:

-p rounds=10 length=5

Option value content in this case would be (Array of param settings):

[ "rounds=10", "length=5" ]

Returns:

  • (Hash)

    Parameter settings included in the option.



1699
1700
1701
1702
1703
1704
1705
1706
1707
# File 'lib/como.rb', line 1699

def params
    map = {}
    @value.each do |i|
        name, value = i.split('=')
        value = str_to_num( value )
        map[ name ] = value
    end
    map
end

#parse(args, checkInvalids = true) ⇒ Object

Parse cmdline options from args.



1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
# File 'lib/como.rb', line 1349

def parse( args, checkInvalids = true )

    while args.get

        # puts "Opt.parse (#{@name}): #{args.get}"

        if args.isOptTerm

            # Rest of the args do not belong to this program.
            args.next
            Opt.main.external = args.rest
            break

        elsif args.isOpt

            o = findOpt( args.get )

            if !o
                if checkInvalids
                    raise \
                    InvalidOption.new( "Unknown option \"#{args.get}\"...",
                        self )
                else
                    o = findOpt( nil )
                    if !o
                        raise \
                        InvalidOption.new(
                            "No default option specified to allow \"#{args.get}\"...",
                           self )
                    else
                        # Default option.
                        o.value.push args.toValue
                        args.next
                    end
                end

            elsif o && ( o.prim?( :one ) || o.prim?( :many ) )

                args.next

                if ( !args.get || args.isOpt ) &&
                   !o.prim?( :none )

                    raise MissingArgument.new(
                        "No argument given for \"#{o.opt}\"...",
                        self )

                else

                    if o.given && !o.prim?( :repeat )
                        raise \
                        InvalidOption.new(
                            "Option specified too multiple times (\"#{o.name}\")...",
                            self )
                    end

                    if o.prim?( :many )

                        # Get all argument for multi-option.
                        o.value = [] if !o.given
                        while args.get && !args.isOpt
                            o.value.push args.toValue
                            args.next
                        end

                    else

                        # Get one argument for single-option.

                        if o.prim?( :repeat )
                            o.value = [] if !o.given
                            o.value.push args.toValue
                        else
                            o.value = args.toValue
                        end

                        args.next
                    end
                end

                o.given = true

            else

                if !o
                    raise InvalidOption.new( "No valid options specified...",
                        self )
                else
                    o.given = true
                    args.next
                end
            end

        else

            # Subcmd or default. Check for Subcmd first.

            # Search for Subcmd.
            o = findOpt( args.get )

            if !o

                # Search for default option.
                o = findOpt( nil )
                if !o
                    if @subcmd.any?
                        raise \
                            InvalidOption.new(
                            "Unknown subcmd: \"#{args.get}\"...",
                            self )
                    else
                        raise \
                            InvalidOption.new(
                            "No default option specified to allow \"#{args.get}\"...",
                            self )
                    end
                else
                    # Default option.
                    o.given = true
                    o.value.push args.toValue
                    args.next
                end

            else

                # Subcmd.
                o.given = true
                args.next
                return o

            end

        end
    end

    nil
end

#prim?(prim) ⇒ Boolean

Check for primitive.

Returns:

  • (Boolean)


1757
1758
1759
# File 'lib/como.rb', line 1757

def prim?( prim )
    @type.index( prim )
end

#setOptionSubcmd(opts, subs) ⇒ Object

Set command (subcommand) suboptions and subcmds.

Parameters:

  • opts (Array<Opt>)


1254
1255
1256
1257
1258
1259
1260
1261
1262
# File 'lib/como.rb', line 1254

def setOptionSubcmd( opts, subs )
    opts.each do |i|
        addOption( i )
    end

    subs.each do |i|
        addSubcmd( i )
    end
end

#setRuleCheck(&rule) ⇒ Object

Set rule checks for the option.

Parameters:

  • rule (Proc)

    Rule to check after command line parsing.



1294
1295
1296
# File 'lib/como.rb', line 1294

def setRuleCheck( &rule )
    @rules = rule
end

#setUsageFooter(str) ⇒ Object

Set optional footer for “usage”.



1783
1784
1785
# File 'lib/como.rb', line 1783

def setUsageFooter( str )
    @config[ :footer ] = str
end

#setUsageHeader(str) ⇒ Object

Set optional header for “usage”.



1777
1778
1779
# File 'lib/como.rb', line 1777

def setUsageHeader( str )
    @config[ :header ] = str
end

#suballObject

All subcommand options, options and subcommands.



1607
1608
1609
# File 'lib/como.rb', line 1607

def suball
    @subopt + @subcmd
end

#suboptDocObject

Return document strings for options.



1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
# File 'lib/como.rb', line 1905

def suboptDoc

    str = ""
    # format = Proc.new do |s,d| ( "  %-#{@config[ :tab ]}s%s\n" % [ s, d ] ) end

    str += "\n"

    str += "  Options:\n" if hasSubcmd && hasVisibleOptions

    @subopt.each do |o|
        next if o.prim?( :hidden )
        str += suboptDocFormat( o.opt, o.doc )
    end

    str += "\n" + suboptDocFormat( "Subcommands:", "" ) if hasSubcmd

    @subcmd.each do |o|
        str += suboptDocFormat( o.name, o.doc )
    end

    str
end

#to_hashObject

Convert to hash representation.

Keys are symbols: name, type, given, value, subopt, subcmd.



1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
# File 'lib/como.rb', line 1946

def to_hash
    h = {}
    h[ :name ] = @name
    h[ :type ] = @type
    h[ :given ] = @given
    h[ :value ] = @value
    h[ :subopt ] = @subopt.map{|i| i.to_hash }
    h[ :subcmd ] = @subcmd.map{|i| i.to_hash }
    h
end

#to_hoptObject

Convert to hash representation with opt name keys.

Keys are symbols: name, type, given, value, subopt, subcmd.



1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
# File 'lib/como.rb', line 1961

def to_hopt
    h = {}
    h[ :name ] = @name
    h[ :type ] = @type
    h[ :given ] = @given
    h[ :value ] = @value
    h[ :subopt ] = {}
    @subopt.each do |i|
        h[ :subopt ][ i.name ] = i.to_hopt
    end
    h[ :subcmd ] = {}
    @subcmd.each do |i|
        h[ :subcmd ][ i.name ] = i.to_hopt
    end
    h
end

#usage(doExit = nil, ruleHelp = nil) ⇒ Object

Display program usage (and optionally exit).

Parameters:

  • doExit (Boolean) (defaults to: nil)

    Exit program after help display. Default to help_exit config if nil.

  • ruleHelp (Boolean) (defaults to: nil)

    Include rule help to help display. Default to rulehelp config if nil.



1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
# File 'lib/como.rb', line 1796

def usage( doExit = nil, ruleHelp = nil )

    doExit = @config[ :help_exit ] if doExit == nil
    ruleHelp = @config[ :rulehelp ] if ruleHelp == nil

    @@io.puts usageNormal

    if ruleHelp
        @@io.puts "\n  Option Combinations:"
        @@io.puts RuleDisplay.print( &@rules )
    end

    exit( 1 ) if doExit
end

#usageCommandObject

Usage printout for command.



1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
# File 'lib/como.rb', line 1823

def usageCommand
    str = ""
    str += "\
  Subcommand \"#{@name}\" usage:
    #{fullCommand} #{cmdline.join(" ")}
"
    str += suboptDoc

    str += "\n"
end

#usageIfHelpObject

Display program usage (and optionally exit).



1813
1814
1815
1816
1817
1818
1819
# File 'lib/como.rb', line 1813

def usageIfHelp
    if self.argByName( 'help' ) && self['help'].given
        usage
    elsif hasSubcmd && givenSubcmd
        givenSubcmd.usageIfHelp
    end
end

#usageNormalObject

Usage info for Opt:s.



1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
# File 'lib/como.rb', line 1835

def usageNormal
    str = ""

    if @config[ :header ]
        str += @config[ :header ]
    else
        str += "\n"
    end

    str += usageCommand

    if @config[ :footer ]
        str += @config[ :footer ]
    else
        str += "\n"
    end

    str
end

#warn(str, nl = false) ⇒ Object

Como warning printout.

Parameters:

  • str (String)

    Warning message.

  • nl (Boolean) (defaults to: false)

    Prepend msg with newline.



2006
2007
2008
2009
# File 'lib/como.rb', line 2006

def warn( str, nl = false )
    nl = nl ? "\n" : ""
    STDERR.puts( "#{nl}#{Opt.progname} warning: #{str}" )
end

#~Object

Short syntax for value reference. Example: “~Opt”.



1639
1640
1641
# File 'lib/como.rb', line 1639

def ~()
    @value
end