Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'attributes': 0.05; 'char': 0.07; 'indeed,': 0.07; '>>>>': 0.09; 'header:In-reply-to:1': 0.09; 'minus': 0.09; 'subject:parsing': 0.09; 'am,': 0.13; 'wrote:': 0.15; "'-'": 0.16; 'brackets': 0.16; 'brackets.': 0.16; 'from:addr:free.fr': 0.16; '\xe9crit': 0.16; 'class,': 0.16; 'true,': 0.19; 'command': 0.19; "doesn't": 0.22; 'parse': 0.23; 'cheers': 0.23; 'tried': 0.27; 'work.': 0.28; 'charset:iso-8859-15': 0.28; 'thu,': 0.28; 'object': 0.30; 'not.': 0.30; '+0200,': 0.30; 'nested': 0.30; "won't": 0.32; 'closing': 0.33; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'there': 0.34; 'nobody': 0.34; 'but': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'closed': 0.62; 'saw': 0.64; 'received:62': 0.67; 'dans': 0.77; '09:24': 0.84; '18:37': 0.84; 'expects': 0.84 Date: Fri, 29 Jul 2011 11:37:34 +0200 From: Karim Subject: Re: shlex parsing In-reply-to: <4e32618d$0$23657$426a74cc@news.free.fr> To: python-list@python.org MIME-version: 1.0 Content-type: text/plain; charset=ISO-8859-15; format=flowed Content-transfer-encoding: 8BIT References: <4e3184d3$0$29530$426a74cc@news.free.fr> <4e32618d$0$23657$426a74cc@news.free.fr> User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Thunderbird/3.1.11 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311932260 news.xs4all.nl 23932 [2001:888:2000:d::a6]:60748 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10506 On 07/29/2011 09:24 AM, Web Dreamer wrote: > Nobody a écrit ce jeudi 28 juillet 2011 18:37 dans > : > >> On Thu, 28 Jul 2011 17:48:34 +0200, Web Dreamer wrote: >> >>>> I would like to parse this TCL command line with shlex: >>>> >>>> '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG' >>>>>> s = s.replace('[','"[') >>>>>> s = s.replace(']',']"') >> Note that this approach won't work if you have nested brackets or braces. >> That would require a real parser. > True, > I tried with the shlex class, but adding '[]' to a shlexobject.quotes > doesn't work. > Indeed, shlex expects an opening and closing quote to be the same, and [ is > different from ] so shlex therefore expects an opening [ to be closed with [ > and not ] > > My solution indeed only works as long as there are not any nested brackets. > Yeah I saw that shlex object behavior is slighty different from shlex.split() fonction. the char minus by default is taken as a individual word and in split it is not. get_token() method return '-' alone and you have to set object attributes wordchars += '-' to have the same behavior as shlex.split(). Cheers Karim