2 # A tool to parse the output of `clang-format --help` and update the
3 # documentation in ../ClangFormat.rst automatically.
11 PARENT_DIR
= os
.path
.join(os
.path
.dirname(__file__
), "..")
12 DOC_FILE
= os
.path
.join(PARENT_DIR
, "ClangFormat.rst")
15 def substitute(text
, tag
, contents
):
16 replacement
= "\n.. START_%s\n\n%s\n\n.. END_%s\n" % (tag
, contents
, tag
)
17 pattern
= r
"\n\.\. START_%s\n.*\n\.\. END_%s\n" % (tag
, tag
)
18 return re
.sub(pattern
, replacement
, text
, flags
=re
.S
)
21 def indent(text
, columns
, indent_first_line
=True):
22 indent_str
= " " * columns
23 s
= re
.sub(r
"\n([^\n])", "\n" + indent_str
+ "\\1", text
, flags
=re
.S
)
24 if not indent_first_line
or s
.startswith("\n"):
29 def get_help_output():
30 args
= [binary
, "--help"]
31 cmd
= subprocess
.Popen(args
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
)
32 out
, _
= cmd
.communicate()
33 out
= out
.decode(sys
.stdout
.encoding
)
38 out
= get_help_output()
39 out
= re
.sub(r
" clang-format\.exe ", " clang-format ", out
)
42 """.. code-block:: console
48 out
= indent(out
, 2, indent_first_line
=False)
52 def validate(text
, columns
):
53 for line
in text
.splitlines():
54 if len(line
) > columns
:
55 print("warning: line too long:\n", line
, file=sys
.stderr
)
58 p
= argparse
.ArgumentParser()
59 p
.add_argument("-d", "--directory", help="directory of clang-format")
60 p
.add_argument("-o", "--output", help="path of output file")
63 binary
= "clang-format"
65 binary
= opts
.directory
+ "/" + binary
67 help_text
= get_help_text()
68 validate(help_text
, 100)
70 with
open(DOC_FILE
, encoding
="utf-8") as f
:
73 contents
= substitute(contents
, "FORMAT_HELP", help_text
)
76 opts
.output
if opts
.output
else DOC_FILE
, "w", newline
="", encoding
="utf-8"