Changeset 1722

Show
Ignore:
Timestamp:
11/02/08 17:47:21 (2 months ago)
Author:
Chuck.Esterbrook
Message:

Command line: Tell the user what the possible choices are when a 'bool' or 'menu' option cannot be parsed.

Location:
cobra/trunk/Source
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Source/CommandLine.cobra

    r1721 r1722  
    2323        var _hasDefault = false 
    2424        var _default = '' 
     25        var _choices = List<of String>() 
    2526 
    2627        get isUnpacked from var 
     
    3233        get type as String 
    3334                require .isUnpacked 
     35                ensure result in ['accumulator', 'args-list', 'bool', 'int', 'main', 'menu', 'string'] 
    3436                return _type 
    3537 
     
    4547                """ 
    4648                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'. 
    4754                """ 
    4855                         
     
    6673                        else 
    6774                                this['type'] = 'string' 
     75                assert _type in ['accumulator', 'args-list', 'bool', 'int','main', 'menu', 'string'] 
    6876                if .containsKey('synonyms') 
    6977                        for syn in this['synonyms'] 
     
    7482                        _isAccumulator = this['isAccumulator'] to bool 
    7583                if .containsKey('default') 
    76                         _default = this['default'] to String 
     84                        _default = this['default'] 
    7785                        _hasDefault = true 
     86                if .containsKey('choices') 
     87                        for choice in this['choices'] 
     88                                _choices.add(choice) 
    7889                _isUnpacked = true 
     90                assert .type=='menu' implies .choices.count > 0 
     91                assert .choices.count <> 0 implies .type == 'menu' 
    7992 
    8093 
     
    433446 
    434447        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) 
    436454                _verbosity = _argParser.verbosity 
    437455                CobraMain.willTimeIt = _argParser.willTimeIt 
     
    804822         
    805823 
     824class ArgParseException 
     825        inherits Exception 
     826 
     827        def init(msg as String) 
     828                base.init(msg) 
     829 
     830 
    806831class ArgParser 
    807832        """ 
     
    907932                                                value = args[argn:] 
    908933                                        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) 
    910939                                        valueDict[name] = value to ! 
    911940                                        didSpecify[name] = true 
     
    11481177                                if valueStr.length==0 
    11491178                                        value = nil 
    1150                                 if not (spec['choices'] to System.Collections.IList).contains(valueStr) 
     1179                                if not valueStr in spec.choices 
    11511180                                        value = nil 
    11521181                                else 
     
    11631192                 
    11641193        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) 
    11691195 
    11701196 
  • cobra/trunk/Source/Utils.cobra

    r1675 r1722  
    166166                                        sb.append(part.toString) 
    167167                                        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) 
    168186                                return sb.toString 
    169187