1# 2# Copyright (c) 2022 Nordic Semiconductor ASA 3# 4# SPDX-License-Identifier: Apache-2.0 5# 6 7from subprocess import Popen, PIPE 8from os import linesep 9from re import sub, S 10from pathlib import Path 11from sys import argv 12 13p_root = Path(__file__).absolute().parents[1] 14p_README = Path(p_root, 'README.md') 15 16pattern = r""" 17Command line documentation 18========================== 19 20Added via `add_helptext.py` 21""" 22 23if __name__ == "__main__": 24 commands = [ 25 ["zcbor", "--help"], 26 ["zcbor", "code", "--help"], 27 ["zcbor", "validate", "--help"], 28 ["zcbor", "convert", "--help"], 29 ] 30 31 output = pattern 32 33 for cmd in commands: 34 stdout, _ = Popen(cmd, stdout=PIPE).communicate() 35 assert b"options:" in stdout, f"Seems like something went wrong: {stdout.decode('utf-8')}" 36 output += f""" 37{" ".join(cmd)} 38{"-" * len(" ".join(cmd))} 39 40``` 41{stdout.decode('utf-8')} 42``` 43""" 44 45 with open(p_README, 'r', encoding="utf-8") as f: 46 readme_contents = f.read() 47 new_readme_contents = sub(pattern + r'.*', output, readme_contents, flags=S) 48 if len(argv) > 1 and argv[1] == "--check": 49 if new_readme_contents != readme_contents: 50 print("Check failed") 51 exit(9) 52 else: 53 with open(p_README, 'w', encoding="utf-8") as f: 54 f.write(new_readme_contents) 55