Python Glossary Русский

all Python 3shlexterm

shlex.shlex

command-line parsing

A shell-style lexical analyser: a stream of tokens with configurable quotes, comments and punctuation.

Syntax

lexer = shlex.shlex(instream=None, infile=None, posix=False, punctuation_chars=False)

posix defaults to False, a compatibility mode in which quotes stay inside the token instead of being stripped as shlex.split() does. Without whitespace_split=True the string is also cut at punctuation: 'a=b' becomes three tokens. punctuation_chars=True turns ;, && and | into separate tokens, which is how pipelines get parsed.

Examples

import shlex
print(list(shlex.shlex('a="b c"')))  # → ['a', '=', '"b c"']
lex = shlex.shlex('a="b c"', posix=True)
lex.whitespace_split = True
print(list(lex))  # → ['a=b c']
print(list(shlex.shlex('ls|wc -l && echo ok', punctuation_chars=True)))  # → ['ls', '|', 'wc', '-l', '&&', 'echo', 'ok']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy