#2983: Proposal to add util.parseargs
-------------------------+--------------------------------------------------
 Reporter:  athomas      |        Owner:  cboos
     Type:  enhancement  |       Status:  new  
 Priority:  normal       |    Milestone:  0.11 
Component:  wiki         |      Version:  devel
 Severity:  normal       |   Resolution:       
 Keywords:  macro        |  
-------------------------+--------------------------------------------------
Changes (by cboos):

  * owner:  athomas => cboos
  * component:  general => wiki

Comment:

 What about this simpler `parse_args()` ?
 The rules would be:
  - every "," character inside a macro call is an argument separator,
 unless escaped by a "\"
  - every identifier directly followed by an "=" corresponds to an
 assignement to a named argument

 The point being that Wiki authors won't necessarily want to follow Python
 syntactic rules when writing macros, and that the simple rules above are
 backward compatible, unambiguous, and simple to implement.

 {{{
 #!python
 def parse_args(args):
     largs, kwargs = [], {}
     if args:
         for arg in re.split(r'(?<!\\),', args):
             m = re.match(r'\s*[a-zA-Z_]\w=', arg)
             if m:
                 kwargs[arg[:m.end()-1]] = arg[m.end():]
             else:
                 largs.append(arg)
     return largs, kwargs
 }}}

 Typical use, e.g. for the last arguments of the !TicketQuery macro
 (the first one being always the query itself):
 {{{
 >>> parse_args('format=compact, order=priority, title=Priority List')
 ([], {' title': 'Priority List', ' order': 'priority', 'format':
 'compact'})
 }}}

-- 
Ticket URL: <http://trac.edgewall.org/ticket/2983#comment:17>
The Trac Project <http://trac.edgewall.com/>
_______________________________________________
Trac-Tickets mailing list
[email protected]
http://lists.edgewall.com/mailman/listinfo/trac-tickets

Reply via email to