学carla的过程中,也算是看过很多优秀的脚本的写法了,经过不断实践和修改,渐渐形成了自己的风格,特地做了个模板,记录如下:
#! -*- coding:utf-8 -*-
# ======================
# -- Comment -----------
# ======================
"""
# Copyrights: che_lu_man_man
# Description: to do something.
# Author: limanman is a good man.
# Date: 2022-12-12
"""
# ======================
# -- import ------------
# ======================
import glob
import os
import sys
import time
import argparse
# ======================
# -- Functions----------
# ======================
def function_001():
pass
def function_002():
pass
# ======================
# -- game_loop() -------
# ======================
def game_loop(args):
try:
pass
finally:
pass
# ======================
# -- main() ------------
# ======================
def main():
# 打印该脚本的用法
print(__doc__)
# 设置命令行输入参数
my_argparser = argparse.ArgumentParser()
my_argparser.add_argument('-f', '--file', default=None, help='input the file name to get config parameters.')
my_argparser.add_argument('-n', '--name', default="limanman", help='input name to give permission.')
my_args = my_argparser.parse_args()
# 进入主循环
try:
game_loop(my_args)
except KeyboardInterrupt:
print('\nCancelled by user. Bye!')
# ======================
# -- Execution ---------
# ======================
if __name__ == '__main__':
main()
上述脚本简单运行有如下效果:
PS F:\scripts\python_exam_ca> python .\template.py -h
# Copyrights: che_lu_man_man
# Description: to do something.
# Author: limanman is a good man.
# Date: 2022-12-12
usage: template.py [-h] [-f FILE] [-n NAME]
optional arguments:
--help show this help message and exit
FILE, --file FILE input the file name to get config parameters.
NAME, --name NAME input name to give permission.
PS F:\scripts\python_exam_ca>