shlex parsing

Karim karim.liateni at
Wed Jul 27 23:08:38 CEST 2011

Thank you Dan for answering.

I ended with this and gave up with shlex:

split = ['-option1', '[get_rule', 'A1', 'B2]', '-option2', '$VAR', 
'-option3', 'TAG']

procedure_found = False
result          = []

for token in split:
     if not token.startswith('[') and not token.endswith(']') and not 
     elif token.startswith('['):
         procedure_found = True
         _token = token
     elif token.endswith(']'):
         procedure_found = False
         _token += ' ' + token
         _token += ' ' + token

print split
print result

which gives the desired values:

['-option1', '[get_rule', 'A1', 'B2]', '-option2', '$VAR', '-option3', 
['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG']

Sure pyParsing seems to be pretty simple but my constraint is to use
standard lib (at maximum). To bad it is not part of python standard libs.
On the other hand, I will have to regroup expression like '-option1 $VAL 
in ['-option1', '$VAL == $CONSTRAINT'].

So it seems that I have no others choicse and have to use a parser like 


On 07/27/2011 10:44 PM, Dan Stromberg wrote:
> I've not used the shlex module, but this feels more like an issue to 
> address with a parser than for a lexical analyzer - or perhaps even 
> both, since you're splitting on whitespace sometimes, and matching 
> square brackets sometimes.
> I've used pyparsing for stuff a bit similar to this.
> Or here's a list:
> On Wed, Jul 27, 2011 at 12:30 PM, Karim <karim.liateni at 
> <mailto:karim.liateni at>> wrote:
>     Hello All,
>     I would like to parse this TCL command line with shlex:
>     '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
>     And I want to get the splitted list:
>     ['-option1', '[get_rule A1 B2]', '-option2',  '$VAR', '-option3',
>      'TAG']
>     Then I will gather in tuple 2 by 2 the arguments.
>     I tried to the shlec properties attributes 'quotes', 'whitespace',
>     etc...
>     But I make 'choux blanc'.
>     If somebody has complex experiences with  this module I am in.
>     Cheers
>     Karim
>     -- 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list