55
Python Module of the Week, Release 1.132
’Escaped \e Character not in quotes’,
’Escaped "\e" Character in double quotes’,
"Escaped ’\e’ Character in single quotes",
r"Escaped ’\’’ \"\’\" single quote",
r’Escaped "\"" \’\"\’ double quote’,
"\"’Strip extra layer of quotes’\"",
]:
print ’ORIGINAL :’, repr(s)
print ’non-POSIX:’,
non_posix_lexer = shlex.shlex(s, posix=False)
try:
print repr(list(non_posix_lexer))
except ValueError, err:
print ’error(%s)’ % err
print ’POSIX
:’,
posix_lexer = shlex.shlex(s, posix=True)
try:
print repr(list(posix_lexer))
except ValueError, err:
print ’error(%s)’ % err
print
Here are a few examples ofthe differences in parsing behavior:
$ python shlex_posix.py
ORIGINAL : ’Do"Not"Separate’
non-POSIX: [’Do"Not"Separate’]
POSIX
: [’DoNotSeparate’]
ORIGINAL : ’"Do"Separate’
non-POSIX: [’"Do"’, ’Separate’]
POSIX
: [’DoSeparate’]
ORIGINAL : ’Escaped \\e Character not in quotes’
non-POSIX: [’Escaped’, ’\\’, ’e’, ’Character’, ’not’, ’in’, ’quotes’]
POSIX
: [’Escaped’, ’e’, ’Character’, ’not’, ’in’, ’quotes’]
ORIGINAL : ’Escaped "\\e" Character in double quotes’
non-POSIX: [’Escaped’, ’"\\e"’, ’Character’, ’in’, ’double’, ’quotes’]
POSIX
: [’Escaped’, ’\\e’, ’Character’, ’in’, ’double’, ’quotes’]
ORIGINAL : "Escaped ’\\e’ Character in single quotes"
non-POSIX: [’Escaped’, "’\\e’", ’Character’, ’in’, ’single’, ’quotes’]
POSIX
: [’Escaped’, ’\\e’, ’Character’, ’in’, ’single’, ’quotes’]
ORIGINAL : ’Escaped \’\\\’\’ \\"\\\’\\" single quote’
non-POSIX: error(No closing quotation)
POSIX
: [’Escaped’, ’\\ \\"\\"’, ’single’, ’quote’]
ORIGINAL : ’Escaped "\\"" \\\’\\"\\\’ double quote’
non-POSIX: error(No closing quotation)
POSIX
: [’Escaped’, ’"’, ’\’"\’’, ’double’, ’quote’]
718
Chapter 22. Program Frameworks