我一直在编写一个简单的 python 脚本来编译和运行给定的 .cpp 文件。
我已经让它按预期工作,但我希望能够在 .cpp 文件的编译产生错误或警告时停止脚本,而不是继续执行 .exe 文件。
# run_cpp.py
# Script that compiles and executes a .cpp file
# Usage:
# python run_cpp.py -i <filename> (without .cpp extension)
import sys, os, getopt
def main(argv):
cpp_file = ''
exe_file = ''
try:
opts, args = getopt.getopt(argv, "hi:",["help",'ifile='])
except getopt.GetoptError as err:
# print help information and exit
print(err)
usage()
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-i", "--ifile"):
cpp_file …Run Code Online (Sandbox Code Playgroud)