Changeset 1722
- Timestamp:
- 11/02/08 17:47:21 (2 months ago)
- Location:
- cobra/trunk/Source
- Files:
-
- 2 modified
-
CommandLine.cobra (modified) (10 diffs)
-
Utils.cobra (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
cobra/trunk/Source/CommandLine.cobra
r1721 r1722 23 23 var _hasDefault = false 24 24 var _default = '' 25 var _choices = List<of String>() 25 26 26 27 get isUnpacked from var … … 32 33 get type as String 33 34 require .isUnpacked 35 ensure result in ['accumulator', 'args-list', 'bool', 'int', 'main', 'menu', 'string'] 34 36 return _type 35 37 … … 45 47 """ 46 48 Check .hasDefault before using this property for anything useful. 49 """ 50 51 get choices from var 52 """ 53 The choices for options that are type 'menu'. 47 54 """ 48 55 … … 66 73 else 67 74 this['type'] = 'string' 75 assert _type in ['accumulator', 'args-list', 'bool', 'int','main', 'menu', 'string'] 68 76 if .containsKey('synonyms') 69 77 for syn in this['synonyms'] … … 74 82 _isAccumulator = this['isAccumulator'] to bool 75 83 if .containsKey('default') 76 _default = this['default'] to String84 _default = this['default'] 77 85 _hasDefault = true 86 if .containsKey('choices') 87 for choice in this['choices'] 88 _choices.add(choice) 78 89 _isUnpacked = true 90 assert .type=='menu' implies .choices.count > 0 91 assert .choices.count <> 0 implies .type == 'menu' 79 92 80 93 … … 433 446 434 447 def parseArgs(args as IList<of String>, options as out OptionValues?, paths as out List<of String>?) 435 _argParser.parseArgs(args, out options, out paths) 448 try 449 _argParser.parseArgs(args, out options, out paths) 450 catch ape as ArgParseException 451 options = nil 452 paths = nil 453 .error(ape.message) 436 454 _verbosity = _argParser.verbosity 437 455 CobraMain.willTimeIt = _argParser.willTimeIt … … 804 822 805 823 824 class ArgParseException 825 inherits Exception 826 827 def init(msg as String) 828 base.init(msg) 829 830 806 831 class ArgParser 807 832 """ … … 907 932 value = args[argn:] 908 933 if value is nil 909 _error('Cannot process value "[valueStr]" for option "[name]".') 934 errMsg = 'Cannot parse value "[valueStr]" for option "[name]".' 935 branch spec.type 936 on 'bool', errMsg += ' Possible values include yes, no, y, n, true, false, t, f, 1, 0, + and -.' 937 on 'menu', errMsg += ' Possible values are [Utils.join(", ", " and ", spec.choices)].' 938 _error(errMsg) 910 939 valueDict[name] = value to ! 911 940 didSpecify[name] = true … … 1148 1177 if valueStr.length==0 1149 1178 value = nil 1150 if not (spec['choices'] to System.Collections.IList).contains(valueStr)1179 if not valueStr in spec.choices 1151 1180 value = nil 1152 1181 else … … 1163 1192 1164 1193 def _error(msg as String) 1165 if msg.length 1166 print 'cobra: error: [msg]' 1167 print 'Run Cobra without options to get full usage information.' 1168 Environment.exit(1) 1194 throw ArgParseException(msg) 1169 1195 1170 1196 -
cobra/trunk/Source/Utils.cobra
r1675 r1722 166 166 sb.append(part.toString) 167 167 s = sep 168 return sb.toString 169 170 # CC: make join an extension method of String which already has a join() except that it only works on arrays of strings 171 def join(sep as String, lastSep as String, parts as System.Collections.IEnumerable) as String 172 test 173 assert Utils.join(', ', ' and ', ['a', 'b'])=='a and b' 174 assert Utils.join(', ', ' and ', ['a', 'b', 'c'])=='a, b and c' 175 assert Utils.join(', ', ' and ', ['a', 'b', 'c', 'd'])=='a, b, c and d' 176 body 177 sb = StringBuilder() 178 s = '' 179 partsList = [] 180 for part in parts, partsList.add(part) 181 count = partsList.count 182 for i in count 183 sb.append(s) 184 sb.append(partsList[i].toString) 185 s = if(i==count-2, lastSep, sep) 168 186 return sb.toString 169 187
