Show
Ignore:
Timestamp:
07/20/08 22:46:52 (5 months ago)
Author:
athomas
Message:

Allow help() to return bare strings. These will be auto-formatted is possible.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cly/trunk/cly/parser.py

    r577 r578  
    3030        self.node = node 
    3131 
     32        def parse_help(node): 
     33            help = node.help(context) 
     34            if isinstance(help, basestring): 
     35                if node.name == node.pattern: 
     36                    return [(node.name, help)] 
     37                else: 
     38                    return [('<%s>' % node.name, help)] 
     39            else: 
     40                return help 
     41 
    3242        def add_help(node): 
    33             node_help = sorted(node.help(context)) 
     43            node_help = sorted(parse_help(node)) 
    3444            for help in node_help: 
    3545                self.help.append((node.group, node.order, help[0], help[1])) 
     
    4656        >>> from cly.builder import Grammar, Node, Help 
    4757        >>> context = Context(None, None) 
     58        >>> class Test(Node): 
     59        ...   def help(self, context): 
     60        ...     return 'HELP!' 
    4861        >>> help = HelpParser(context, Grammar( 
    4962        ...     one=Node(help='1'), 
    50         ...     two=Node(help=Help.pair('<two>', '2'), group=2))) 
     63        ...     two=Node(help=Help.pair('<two>', '2'), group=2), 
     64        ...     three=Test(help='HELP!'), 
     65        ...     )) 
    5166        >>> list(help) 
    52         [(0, 'one', '1'), (2, '<two>', '2')] 
     67        [(0, 'one', '1'), (0, 'three', 'HELP!'), (2, '<two>', '2')] 
    5368        """ 
    5469 
     
    357372        if where is None: 
    358373            assert hasattr(grammar, 'graft'), \ 
    359                 'need either an explicit "where" or a "graft" attribute on ' 
     374                'need either an explicit "where" or a "graft" attribute on ' \ 
    360375                'the <grammar> root' 
    361376            where = grammar.graft