blob: 84a490cf29eb3e9855e44a5730afb2c514527dc3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#!/usr/bin/env python3
# Copyright 2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
import itertools
import shlex
import sys
def quote(s):
""" Surround a value with quotes, escape embedded quotes.
>>> quote("foo'bar")
"'foo\\\\'bar'"
"""
return "'" + s.replace("\\", "\\\\").replace("'", "\\'") + "'"
def format_array(args):
""" Format shell-compatible expressions as a meson array.
>>> format_array(['-O2 -pipe -DFOO="bar baz"'])
"['-O2', '-pipe', '-DFOO=bar baz']"
"""
# Split each argument according to shell rules
args = (shlex.split(x) for x in args)
# Flatten the resulting list of lists
args = itertools.chain.from_iterable(args)
# Add quotes and escape embedded quotes
args = (quote(x) for x in args)
# Format the result
return "[" + ", ".join(args) + "]"
def main(args):
print(format_array(args))
if __name__ == "__main__":
main(sys.argv[1:])
|