[Ml-yokadi] YokadiOptionParser (was Re: yokadi upstream)

Aurélien Gâteau aurelien.gateau
Jeu 20 Nov 11:42:04 CET 2008


Sébastien Renard wrote:

>> We could add a parser_foo() function which would return a parser for the
>> foo command. This way do_foo() can use it, and we can override do_help()
>> to do something like this:
>>
>> if function parser_foo exist:
>> 	parser = self.parser_foo()
>> 	parser.print_usage()
>> else:
>> 	cmd.do_help(self, "foo")
> 
> It is much more clear and simple indeed. But how to extract parser for foo 
> function wihtout centralise parser definition in a dict or something like 
> this ?

Here is a more complete example of what I have in mind:

class MyCmd(cmd):
   def parser_foo(self):
     parser = YokadiOptionParser()
     parser.add_option(...)
     ...
     return parser

   def do_foo(self, line):
     parser = self.parser_foo()
     options, args = parser.parse_args(line)
     ...


   def do_help(self, arg):
     parserMethod = getattr(self, "parser_" + arg)
     if parserMethod:
       parser = parserMethod()
       parser.print_usage()
     else:
       Cmd.do_help(self, arg)

You just need to add a "parser_foo" method which returns a parser and 
use this function in "do_foo".

>> I just stumbled on a problem with the parser though: it split the line
>> in a string list using shlex.split(), which causes single and double
>> quotes to be interpreted. As a result, calling this:
>>
>> t_add prj can't read .bar files
>>
>> Will raise a ValueError("No closing quotation") exception. You now need
>> to escape the quote.
> 
> Argh. We need something simple. Escaping quote is not yoakdi spirit (simple 
> and intuitive tool).
> 
>> I don't like this, but on the other hand having a real parser make it
>> possible to implement much more powerful commands. Any idea on this?
> 
> We need quote for : 
> - task title and definition (language quote)
> - argument delimiting when they have space
> 
> Can't we forbid space on argument and so allow free language quote in string ? 
> For that, we could automagically escape all quotes before parsing.

That's what I did this morning. There was no need to escape quotes in 
fact: splitting with the standard split() string method instead of 
shlex.split() does the job.

Aurélien


Plus d'informations sur la liste de diffusion Ml-yokadi