[IPython-dev] Re: [IPython-user] pysh is in CVS

Fernando Perez Fernando.Perez at colorado.edu
Mon Jun 28 19:58:53 EDT 2004


Ville Vainio wrote:
>>get rid of it (plus, it's expensive as you noted).  If anyone can 
>>provide me with a pure python function which does:
>>
>>argv_list = parse_string(argv_str)
>>
>>which takes a string (with arbitrary quotes, parens, etc.) and returns 
>>a python list which would be the correct sys.argv seen by a command 
>>line consisting of this argv_str, I'd be very happy.
> 
> 
> 
> Well, shlex.split(s) does arbitrary quotes, but not parens:
> 
> 
> In [5]: shlex.split('hi "very much stuff" (a b c) hai')
> Out[5]: ['hi', 'very much stuff', '(a', 'b', 'c)', 'hai']
> 
> Still, it's an ok solution for most cases, dont you think?

Cool!  I didn't even know about shlex.  The stdlib is really quite the 
treasure trove (and I _did_ google around for a while when I faced this 
problem, but somehow I missed shlex).

I'll look into this tonight in detail, but it does sound quite reasonable. 
Esp. considering that parens don't typically belong naked on a shell line to 
begin with, and that shlex handles them gracefully if inside a string:

planck[~]|37> shlex.split('hi "very much (stuff)" (a b c) hai')
          <37> ['hi', 'very much (stuff)', '(a', 'b', 'c)', 'hai']

This means I'll most likely remove that ugly shell call to a full-blown python 
interpeter, which I hated since day 1 (but I really wanted to provide true 
sys.argv in @run).

Many thanks!

f




More information about the IPython-dev mailing list