Class: Como::Opt
- Inherits:
-
ComoCommon
- Object
- ComoCommon
- Como::Opt
- 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
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
-
#config ⇒ Object
readonly
Opt configuration.
-
#doc ⇒ Object
Option documentation string.
-
#given(yieldOpt = false, &prog) ⇒ Object
(also: #given?)
Returns true if option is given, and block is not present.
-
#longOpt ⇒ Object
Long option string.
-
#name ⇒ Object
Option name.
-
#parent ⇒ Object
Subcommand parent (i.e. host).
-
#rules ⇒ Object
readonly
Opt rules.
-
#shortOpt ⇒ Object
Short option string.
-
#subcmd ⇒ Object
readonly
List of subcommands.
-
#subopt ⇒ Object
readonly
List of suboptions.
-
#type ⇒ Object
Option type.
-
#value ⇒ Object
Option value.
Class Method Summary collapse
-
.[](str) ⇒ Object
Select option object by name.
-
.addOpt(opt) ⇒ Object
Add option to options list.
-
.author ⇒ Object
Return author.
-
.configGet ⇒ Object
Return default config for Como options.
-
.configOverlay(config) ⇒ Object
Overlay Opt default configuration options.
-
.configSet(config) ⇒ Object
Set default config for Como options.
-
.current ⇒ Object
Current subcmd processed.
-
.default ⇒ Object
Return arguments (options) that have no switch.
-
.defaultOpt(doc = "No doc.") ⇒ Object
Create default option spec, no switch.
-
.each(&blk) ⇒ Object
Options iterator for all options.
-
.each_given(&blk) ⇒ Object
Options iterator for given options.
-
.external ⇒ Object
Return arguments (options) that are specified as command external (i.e. after ‘–’).
-
.findOpt(name) ⇒ Object
Find option by name.
-
.full(name, opt, type, doc = "No doc.") ⇒ Object
Create option spec.
-
.main ⇒ Object
Get main option.
-
.progname ⇒ Object
Return program name.
-
.reset ⇒ Object
Reset “dynamic” class members.
-
.setMain(main) ⇒ Object
Set main option.
-
.setSubcmd(opt) ⇒ Object
Set current subcmd.
-
.subcmd(name, doc = "No doc.") ⇒ Object
Create sub-command option spec.
-
.year ⇒ Object
Return program year.
Instance Method Summary collapse
-
#[](str) ⇒ Object
Select option object by name operator.
-
#addOption(opt) ⇒ Object
Add subcommand option.
-
#addSubcmd(cmd) ⇒ Object
Add subcommand subcmd.
-
#apply(default = nil) ⇒ Object
Return option value if given otherwise the default.
-
#applyConfig(config) ⇒ Object
Merge config to base config.
-
#argById(str) ⇒ Object
Select option object by name/opt/longOpt.
-
#argByName(str) ⇒ Object
Select option object by name.
-
#check(argsState) ⇒ Object
Check provided args.
-
#checkAlso(opt, error, &check) ⇒ Object
Additional option check.
-
#checkMissing ⇒ Object
Check for any non-given required arguments recursively through hierarchy of subcommands.
-
#checkRule ⇒ Object
Check option combination rules.
-
#cmdline ⇒ Object
Return cmdline usage strings for options in an Array.
-
#default ⇒ Object
Return default options.
-
#each(&blk) ⇒ Object
Options list iterator.
-
#each_given(&blk) ⇒ Object
Options iterator for given options.
-
#error(str, nl = false) ⇒ Object
Como error printout.
-
#errornl(str) ⇒ Object
Como error printout with pre-newline.
-
#fatal(str) ⇒ Object
Como error printout with immediate exit.
-
#findOpt(str) ⇒ Object
Find option object by option str.
-
#givenCount ⇒ Object
Number of given options.
-
#givenSubcmd ⇒ Object
Return the selected subcommand.
-
#hasArg ⇒ Object
Option requires argument?.
-
#hasMany ⇒ Object
Option requires many arguments?.
-
#initialize(name, opt, type, doc, value = nil) ⇒ Opt
constructor
Create Opt object.
-
#isExclusive ⇒ Object
Test if option is exclusive.
-
#isRequired ⇒ Object
Is mandatory argument?.
-
#isSwitch ⇒ Object
Test if option is of switch type.
-
#opt ⇒ Object
Option’s opt id.
-
#params ⇒ Hash
Returns Hash of option value parameters.
-
#parse(args, checkInvalids = true) ⇒ Object
Parse cmdline options from args.
-
#setOptionSubcmd(opts, subs) ⇒ Object
Set command (subcommand) suboptions and subcmds.
-
#setRuleCheck(&rule) ⇒ Object
Set rule checks for the option.
-
#setUsageFooter(str) ⇒ Object
Set optional footer for “usage”.
-
#setUsageHeader(str) ⇒ Object
Set optional header for “usage”.
-
#silent? ⇒ Boolean
Test if option is silent.
-
#suball ⇒ Object
All subcommand options, options and subcommands.
-
#suboptDoc ⇒ Object
Return document strings for options.
-
#usage(doExit = nil, ruleHelp = nil) ⇒ Object
Display program usage (and optionally exit).
-
#usageCommand ⇒ Object
Usage printout for command.
-
#usageIfHelp ⇒ Object
Display program usage (and optionally exit).
-
#usageNormal ⇒ Object
Usage info for Opt:s.
-
#~ ⇒ Object
Short syntax for value reference.
Methods inherited from ComoCommon
getIo, runHook, setHook, setIo
Constructor Details
#initialize(name, opt, type, doc, value = nil) ⇒ Opt
Create Opt object.
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 |
# File 'lib/como.rb', line 1100 def initialize( name, opt, type, doc, value = nil ) @parent = nil @name = name @shortOpt = opt @longOpt = "--#{name}" @type = type @value = value if hasMany && value == nil @value = [] 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
#config ⇒ Object (readonly)
Opt configuration.
1079 1080 1081 |
# File 'lib/como.rb', line 1079 def config @config end |
#doc ⇒ Object
Option documentation string.
1067 1068 1069 |
# File 'lib/como.rb', line 1067 def doc @doc end |
#given(yieldOpt = 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.
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 |
# File 'lib/como.rb', line 1526 def given( yieldOpt = false, &prog ) if block_given? if @given if yieldOpt yield( self ) else yield( @value ) end else false end else @given end end |
#longOpt ⇒ Object
Long option string.
1058 1059 1060 |
# File 'lib/como.rb', line 1058 def longOpt @longOpt end |
#name ⇒ Object
Option name.
1052 1053 1054 |
# File 'lib/como.rb', line 1052 def name @name end |
#parent ⇒ Object
Subcommand parent (i.e. host).
1048 1049 1050 |
# File 'lib/como.rb', line 1048 def parent @parent end |
#rules ⇒ Object (readonly)
Opt rules.
1082 1083 1084 |
# File 'lib/como.rb', line 1082 def rules @rules end |
#shortOpt ⇒ Object
Short option string.
1055 1056 1057 |
# File 'lib/como.rb', line 1055 def shortOpt @shortOpt end |
#subcmd ⇒ Object (readonly)
List of subcommands.
1076 1077 1078 |
# File 'lib/como.rb', line 1076 def subcmd @subcmd end |
#subopt ⇒ Object (readonly)
List of suboptions.
1073 1074 1075 |
# File 'lib/como.rb', line 1073 def subopt @subopt end |
#type ⇒ Object
Option type.
1061 1062 1063 |
# File 'lib/como.rb', line 1061 def type @type end |
#value ⇒ Object
Option value.
1064 1065 1066 |
# File 'lib/como.rb', line 1064 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.
948 949 950 951 952 953 954 955 956 957 958 959 960 961 |
# File 'lib/como.rb', line 948 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.
912 913 914 |
# File 'lib/como.rb', line 912 def Opt.addOpt( opt ) @@opts.push opt end |
.author ⇒ Object
Return author.
977 978 979 |
# File 'lib/como.rb', line 977 def Opt. @@main. end |
.configGet ⇒ Object
Return default config for Como options.
1030 1031 1032 |
# File 'lib/como.rb', line 1030 def Opt.configGet @@config end |
.configOverlay(config) ⇒ Object
Overlay Opt default configuration options.
1024 1025 1026 |
# File 'lib/como.rb', line 1024 def Opt.configOverlay( config ) @@config.merge!( config ) end |
.configSet(config) ⇒ Object
Set default config for Como options. User can manipulate the defaults with “preHook”.
1037 1038 1039 |
# File 'lib/como.rb', line 1037 def Opt.configSet( config ) @@config = config end |
.current ⇒ Object
Current subcmd processed.
924 925 926 |
# File 'lib/como.rb', line 924 def Opt.current @@subcmd end |
.default ⇒ Object
Return arguments (options) that have no switch.
990 991 992 |
# File 'lib/como.rb', line 990 def Opt.default Opt.main.default end |
.defaultOpt(doc = "No doc.") ⇒ Object
Create default option spec, no switch.
1006 1007 1008 |
# File 'lib/como.rb', line 1006 def Opt.defaultOpt( doc = "No doc." ) new( "<default>", "<default>", :default, doc, [] ) end |
.each(&blk) ⇒ Object
Options iterator for all options.
1012 1013 1014 |
# File 'lib/como.rb', line 1012 def Opt.each( &blk ) Opt.main.each &blk end |
.each_given(&blk) ⇒ Object
Options iterator for given options.
1018 1019 1020 |
# File 'lib/como.rb', line 1018 def Opt.each_given( &blk ) Opt.main.each_given( &blk ) end |
.external ⇒ Object
Return arguments (options) that are specified as command external (i.e. after ‘–’).
984 985 986 |
# File 'lib/como.rb', line 984 def Opt.external Opt.main.external end |
.findOpt(name) ⇒ Object
Find option by name.
930 931 932 933 934 935 936 937 |
# File 'lib/como.rb', line 930 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.
996 997 998 |
# File 'lib/como.rb', line 996 def Opt.full( name, opt, type, doc = "No doc." ) new( name, opt, type, doc ) end |
.main ⇒ Object
Get main option.
906 907 908 |
# File 'lib/como.rb', line 906 def Opt.main @@main end |
.progname ⇒ Object
Return program name.
965 966 967 |
# File 'lib/como.rb', line 965 def Opt.progname @@main.name end |
.reset ⇒ Object
Reset “dynamic” class members.
941 942 943 |
# File 'lib/como.rb', line 941 def Opt.reset @@opts = [] end |
.setMain(main) ⇒ Object
Set main option.
900 901 902 903 |
# File 'lib/como.rb', line 900 def Opt.setMain( main ) @@main = main Opt.setSubcmd( main ) end |
.setSubcmd(opt) ⇒ Object
Set current subcmd.
918 919 920 |
# File 'lib/como.rb', line 918 def Opt.setSubcmd( opt ) @@subcmd = opt end |
.subcmd(name, doc = "No doc.") ⇒ Object
Create sub-command option spec.
1001 1002 1003 |
# File 'lib/como.rb', line 1001 def Opt.subcmd( name, doc = "No doc." ) new( name, nil, :subcmd, doc, false ) end |
.year ⇒ Object
Return program year.
971 972 973 |
# File 'lib/como.rb', line 971 def Opt.year @@main.year end |
Instance Method Details
#[](str) ⇒ Object
Select option object by name operator.
1452 1453 1454 1455 1456 1457 1458 |
# File 'lib/como.rb', line 1452 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.
1142 1143 1144 1145 |
# File 'lib/como.rb', line 1142 def addOption( opt ) opt.parent = self @subopt.push opt end |
#addSubcmd(cmd) ⇒ Object
Add subcommand subcmd.
1151 1152 1153 1154 |
# File 'lib/como.rb', line 1151 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” )
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 |
# File 'lib/como.rb', line 1507 def apply( default = nil ) if given if @type == :switch true else value end else default end end |
#applyConfig(config) ⇒ Object
Merge config to base config.
1160 1161 1162 |
# File 'lib/como.rb', line 1160 def applyConfig( config ) @config.merge!( config ) end |
#argById(str) ⇒ Object
Select option object by name/opt/longOpt.
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 |
# File 'lib/como.rb', line 1598 def argById( str ) if str == nil || str == :default @subopt.each do |o| if o.type == :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.
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 |
# File 'lib/como.rb', line 1578 def argByName( str ) if str == nil || str == :default @subopt.each do |o| if o.type == :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.
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 |
# File 'lib/como.rb', line 1177 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.
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 |
# File 'lib/como.rb', line 1432 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 |
#checkMissing ⇒ Object
Check for any non-given required arguments recursively through hierarchy of subcommands. MissingArgument Exception is generated if argument is missing.
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 |
# File 'lib/como.rb', line 1352 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.isExclusive && o.given return end end # Check for required arguments for this level before # subcmds. @subopt.each do |o| if o.isRequired 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 |
#checkRule ⇒ Object
Check option combination rules.
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 |
# File 'lib/como.rb', line 1408 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 |
#cmdline ⇒ Object
Return cmdline usage strings for options in an Array.
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 |
# File 'lib/como.rb', line 1753 def cmdline opts = [] @subopt.each do |o| next if o.silent? prural = nil case o.type when :multi, :opt_multi; prural = "+" when :opt_any; prural = "*" else prural = "" end if !( o.isSwitch ) name = " <#{o.name}>#{prural}" else name = "" end if o.shortOpt == nil opt = o.longOpt else opt = o.shortOpt end if o.isRequired opts.push "#{opt}#{name}" else opts.push "[#{opt}#{name}]" end end if hasSubcmd opts.push "<<subcommand>>" end opts end |
#default ⇒ Object
Return default options.
1572 1573 1574 |
# File 'lib/como.rb', line 1572 def default argByName( nil ) end |
#each(&blk) ⇒ Object
Options list iterator.
1474 1475 1476 1477 1478 |
# File 'lib/como.rb', line 1474 def each( &blk ) suball.each do |o| yield o end end |
#each_given(&blk) ⇒ Object
Options iterator for given options.
1482 1483 1484 1485 1486 |
# File 'lib/como.rb', line 1482 def each_given( &blk ) suball.each do |o| yield o if o.given end end |
#error(str, nl = false) ⇒ Object
Como error printout.
1837 1838 1839 1840 |
# File 'lib/como.rb', line 1837 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.
1844 1845 1846 |
# File 'lib/como.rb', line 1844 def errornl( str ) error( str, true ) end |
#fatal(str) ⇒ Object
Como error printout with immediate exit.
1850 1851 1852 1853 |
# File 'lib/como.rb', line 1850 def fatal( str ) error( str ) exit( false ) end |
#findOpt(str) ⇒ Object
Find option object by option str.
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 |
# File 'lib/como.rb', line 1823 def findOpt( str ) if str == nil suball.detect { |i| i.type == :default } elsif str[0..1] == "--" suball.detect { |i| i.longOpt == str } elsif str[0..0] != "-" suball.detect { |i| i.name == str } else suball.detect { |i| i.opt == str } end end |
#givenCount ⇒ Object
Number of given options.
1490 1491 1492 1493 1494 1495 1496 |
# File 'lib/como.rb', line 1490 def givenCount cnt = 0 each_given do |i| cnt += 1 end cnt end |
#givenSubcmd ⇒ Object
Return the selected subcommand.
1548 1549 1550 |
# File 'lib/como.rb', line 1548 def givenSubcmd ( @subcmd.select do |o| o.given end )[0] end |
#hasArg ⇒ Object
Option requires argument?
1618 1619 1620 1621 1622 1623 |
# File 'lib/como.rb', line 1618 def hasArg case @type when :single, :multi, :opt_single, :opt_multi, :opt_any, :exclusive; true else false end end |
#hasMany ⇒ Object
Option requires many arguments?
1627 1628 1629 1630 1631 1632 |
# File 'lib/como.rb', line 1627 def hasMany case @type when :multi, :opt_multi, :opt_any, :exclusive, :default; true else false end end |
#isExclusive ⇒ Object
Test if option is exclusive. In addition :exclusive also :silent is exclusive.
1652 1653 1654 1655 1656 1657 |
# File 'lib/como.rb', line 1652 def isExclusive case @type when :exclusive, :silent; true else false end end |
#isRequired ⇒ Object
Is mandatory argument?
1636 1637 1638 1639 1640 1641 |
# File 'lib/como.rb', line 1636 def isRequired case @type when :single, :multi; true else false end end |
#isSwitch ⇒ Object
Test if option is of switch type.
1661 1662 1663 1664 1665 1666 |
# File 'lib/como.rb', line 1661 def isSwitch case @type when :switch, :exclusive, :default; true else false end end |
#opt ⇒ Object
Option’s opt id. Short if exists otherwise long.
1462 1463 1464 |
# File 'lib/como.rb', line 1462 def opt @shortOpt ? @shortOpt : @longOpt end |
#params ⇒ Hash
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" ]
1560 1561 1562 1563 1564 1565 1566 1567 1568 |
# File 'lib/como.rb', line 1560 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.
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 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 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 1345 1346 |
# File 'lib/como.rb', line 1223 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.hasArg args.next if ( !args.get || args.isOpt ) && o.type != :opt_any && o.type != :exclusive raise MissingArgument.new( "No argument given for \"#{o.opt}\"...", self ) else if o.hasMany # 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.given raise \ InvalidOption.new( "Too many arguments for option (\"#{o.name}\")...", self ) 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 raise \ InvalidOption.new( "No default option specified to allow \"#{args.get}\"...", self ) 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 |
#setOptionSubcmd(opts, subs) ⇒ Object
Set command (subcommand) suboptions and subcmds.
1128 1129 1130 1131 1132 1133 1134 1135 1136 |
# File 'lib/como.rb', line 1128 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.
1168 1169 1170 |
# File 'lib/como.rb', line 1168 def setRuleCheck( &rule ) @rules = rule end |
#setUsageFooter(str) ⇒ Object
Set optional footer for “usage”.
1679 1680 1681 |
# File 'lib/como.rb', line 1679 def ( str ) @config[ :footer ] = str end |
#setUsageHeader(str) ⇒ Object
Set optional header for “usage”.
1673 1674 1675 |
# File 'lib/como.rb', line 1673 def setUsageHeader( str ) @config[ :header ] = str end |
#silent? ⇒ Boolean
Test if option is silent.
1645 1646 1647 |
# File 'lib/como.rb', line 1645 def silent? @type == :silent end |
#suball ⇒ Object
All subcommand options, options and subcommands.
1468 1469 1470 |
# File 'lib/como.rb', line 1468 def suball @subopt + @subcmd end |
#suboptDoc ⇒ Object
Return document strings for options.
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 |
# File 'lib/como.rb', line 1798 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.silent? 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 |
#usage(doExit = nil, ruleHelp = nil) ⇒ Object
Display program usage (and optionally exit).
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 |
# File 'lib/como.rb', line 1692 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 |
#usageCommand ⇒ Object
Usage printout for command.
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 |
# File 'lib/como.rb', line 1719 def usageCommand str = "" str += "\ Subcommand \"#{@name}\" usage: #{fullCommand} #{cmdline.join(" ")} " str += suboptDoc str += "\n" end |
#usageIfHelp ⇒ Object
Display program usage (and optionally exit).
1709 1710 1711 1712 1713 1714 1715 |
# File 'lib/como.rb', line 1709 def usageIfHelp if self.argByName( 'help' ) && self['help'].given usage elsif hasSubcmd && givenSubcmd givenSubcmd.usageIfHelp end end |
#usageNormal ⇒ Object
Usage info for Opt:s.
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 |
# File 'lib/como.rb', line 1731 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 |
#~ ⇒ Object
Short syntax for value reference. Example: “~Opt”.
1500 1501 1502 |
# File 'lib/como.rb', line 1500 def ~() @value end |