<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    On 07/28/2011 12:11 AM, Dan Stromberg wrote:
    <blockquote
cite="mid:CAGGBd_qXDTKK9hkv-VteS+=d6Rnxa0M+qvL3rRHY56AAOPd44g@mail.gmail.com"
      type="cite"><br>
      You could probably use a recursive descent parser with the
      standard library.<br>
      <br>
      But if your management is OK with pyparsing, that might be easier,
      and a bit more clear as well.<br>
    </blockquote>
    <br>
    Yes, I thought to use str method partition in a recursive way but
    using pyParsing still be easer.<br>
    <br>
    Thanks<br>
    Cheers<br>
    <br>
    <blockquote
cite="mid:CAGGBd_qXDTKK9hkv-VteS+=d6Rnxa0M+qvL3rRHY56AAOPd44g@mail.gmail.com"
      type="cite"><br>
      <div class="gmail_quote">On Wed, Jul 27, 2011 at 2:08 PM, Karim <span
          dir="ltr"><<a moz-do-not-send="true"
            href="mailto:karim.liateni@free.fr">karim.liateni@free.fr</a>></span>
        wrote:<br>
        <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
          0.8ex; border-left: 1px solid rgb(204, 204, 204);
          padding-left: 1ex;">
          <div bgcolor="#ffffff" text="#000000"> <br>
            Thank you Dan for answering.<br>
            <br>
            I ended with this and gave up with shlex:<br>
            <br>
            split = ['-option1', '[get_rule', 'A1', 'B2]', '-option2',
            '$VAR', '-option3', 'TAG']<br>
            <br>
            procedure_found = False<br>
            result          = []<br>
            <br>
            for token in split:<br>
                if not token.startswith('[') and not token.endswith(']')
            and not procedure_found:<br>
                    result.append(token)<br>
                elif token.startswith('['):<br>
                    procedure_found = True<br>
                    _token = token<br>
                elif token.endswith(']'):<br>
                    procedure_found = False<br>
                    _token += ' ' + token<br>
                    result.append(_token)<br>
                else:<br>
                    _token += ' ' + token<br>
            <br>
            print split<br>
            print result<br>
            <br>
            which gives the desired values:<br>
            <br>
            ['-option1', '[get_rule', 'A1', 'B2]', '-option2', '$VAR',
            '-option3', 'TAG']<br>
            ['-option1', '[get_rule A1 B2]', '-option2', '$VAR',
            '-option3', 'TAG']<br>
            <br>
            <br>
            Sure pyParsing seems to be pretty simple but my constraint
            is to use<br>
            standard lib (at maximum). To bad it is not part of python
            standard libs.<br>
            On the other hand, I will have to regroup expression like
            '-option1 $VAL == $CONSTRAINT'<br>
            in ['-option1', '$VAL == $CONSTRAINT'].<br>
            <br>
            So it seems that I have no others choicse and have to use a
            parser like pyParsing.<br>
            <br>
            Regards<br>
            <font color="#888888"> Karim</font>
            <div>
              <div class="h5"><br>
                <br>
                On 07/27/2011 10:44 PM, Dan Stromberg wrote:
                <blockquote type="cite"><br>
                  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.<br>
                  <br>
                  I've used pyparsing for stuff a bit similar to this.<br>
                  <br>
                  Or here's a list:<br>
                  <a moz-do-not-send="true"
                    href="http://wiki.python.org/moin/LanguageParsing"
                    target="_blank">http://wiki.python.org/moin/LanguageParsing</a><br>
                  <br>
                  <div class="gmail_quote"> On Wed, Jul 27, 2011 at
                    12:30 PM, Karim <span dir="ltr"><<a
                        moz-do-not-send="true"
                        href="mailto:karim.liateni@free.fr"
                        target="_blank">karim.liateni@free.fr</a>></span>
                    wrote:<br>
                    <blockquote class="gmail_quote" style="margin: 0pt
                      0pt 0pt 0.8ex; border-left: 1px solid rgb(204,
                      204, 204); padding-left: 1ex;"> <br>
                      Hello All,<br>
                      <br>
                      I would like to parse this TCL command line with
                      shlex:<br>
                      <br>
                      '-option1 [get_rule A1 B2] -option2 $VAR -option3
                      TAG'<br>
                      <br>
                      And I want to get the splitted list:<br>
                      <br>
                      ['-option1', '[get_rule A1 B2]', '-option2',
                       '$VAR', '-option3',  'TAG']<br>
                      <br>
                      Then I will gather in tuple 2 by 2 the arguments.<br>
                      <br>
                      I tried to the shlec properties attributes
                      'quotes', 'whitespace', etc...<br>
                      <br>
                      But I make 'choux blanc'.<br>
                      <br>
                      If somebody has complex experiences with  this
                      module I am in.<br>
                      <br>
                      Cheers<br>
                      Karim<br>
                      <font color="#888888"> -- <br>
                        <a moz-do-not-send="true"
                          href="http://mail.python.org/mailman/listinfo/python-list"
                          target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
                      </font></blockquote>
                  </div>
                  <br>
                </blockquote>
                <br>
              </div>
            </div>
          </div>
        </blockquote>
      </div>
      <br>
    </blockquote>
    <br>
  </body>
</html>