python脚本传参main.py设计

 加功能,在opt_info里定义功能信息,p_check加检查,p_set设置参数。

 

执行效果:

 

 

代码:

import sys
import json
import os
# from core.e2j import E2J
# from core.merge import Merge
# from core.e2j_check import E2JCheck

help_info = """
--------------------------航空工具------------------------------

  python main.py e2j                # excel转json,默认配置执行
  python main.py e2j -v hainan-0.1  # excel转json,指定版本信息
  python main.py e2j -v test_1103 -multilingual  # excel转json,指定版本信息,语种版本标志
  python main.py e2j -v test_1103 -multilingual -check  # excel转json,指定版本信息,语种版本标志,简单校验

  # 指定版本为1.2,不要默认配置文件夹的配置,不要默认配置,所有https转http
  python main.py e2j -v 1.2 -no_default_config_files -no_default_config -https_to_http

  # 合并json文件,放在json_merge文件夹下的json文件会被遍历合并,生成到dist目录下
  python main.py merge -v test-merge
  
----------------------------------------------------------------
"""

E2J_CONFIG_FILENAME = "e2j_config.json"
MERGE_CONFIG_FILENAME = "merge_config.json"
E2J_CHECK_CONFIG_FILENAME = "e2j_check_config.json"

E2J_check_run = False

# 测试
class E2J:
  
  def __init__(self,config):
    self.config = config

  def run(self):
    print("E2j run")
    print("E2J config: ", self.config)

class Merge:

  def __init__(self,config):
    self.config = config

  def run(self):
    print("Merge run")
    print("Merge config: ", self.config)

class E2JCheck:

  def __init__(self,config):
    self.config = config

  def run(self):
    print("E2JCheck run")
    print("E2JCheck config: ", self.config)

opt_info = {
  "e2j": {
    # 执行哪个功能
    "func": E2J,
    # 配置路径
    "config_file_name": E2J_CONFIG_FILENAME,
    # 传递哪些配置
    "config": None,
    # key参数,value是字典(num几个参数)
    "-v": {
      # -v 后有几个参数
      "num": 1,
    },
    "-no_default_config_files": {
      "num": 0
    },
    "-no_default_config": {
      "num": 0
    },
    "-https_to_http": {
      "num": 0
    },
    "-multilingual": {
      "num": 0
    },
    "-check": {
      "num": 0
    }
  },
  "merge": {
    # 执行哪个功能
    "func": Merge,
    # 配置路径
    "config_file_name": MERGE_CONFIG_FILENAME,
    # 传递哪些配置
    "config": None,
    # 参数
    "-v": {
      "num": 1,
    },
  },
  "e2j_check": {
    # 执行哪个功能
    "func": E2JCheck,
    # 配置路径
    "config_file_name": E2J_CHECK_CONFIG_FILENAME,
    # 传递哪些配置
    "config": None,
    # 参数
    "-json_file_name": {
      "num": 1,
    },
    "-v": {
      "num": 1,
    },
  }
}


def p_check(opt, p, p_v):
  flag = True
  # -v 目前没什么可检查的

  return flag


def p_set(opt, config, p, p_v=None):
  if opt in ['e2j', 'merge'] and p == "-v":
    config["version"] = p_v
  elif opt == 'e2j' and p == "-no_default_config_files":
    config["default_config_files"] = []
  elif opt == 'e2j' and p == "-no_default_config":
    config["default_config"] = {}
  elif opt == 'e2j' and p == "-https_to_http":
    config["https_to_http"] = True
  elif opt == 'e2j' and p == "-multilingual":
    config["multilingual"] = True
  elif opt == 'e2j' and p == '-check':
    global E2J_check_run
    E2J_check_run = True
    config["e2j_check"] = True
  elif opt == 'e2j_check' and p == "-json_file_name":
    config["json_file_name"] = p_v
  elif opt == 'e2j_check' and p == "-v":
    config["json_file_name"] = '%s_cfg.json' % p_v

def run(script_params=[]):
  if len(script_params) < 1:
    script_params = sys.argv
  # print(script_params) # ['main.py', 'e2j', '-v', '0.2', '-c', '1']

  # 帮助信息
  if len(script_params) < 2:
    print(help_info)
    return

  # 执行参数
  opt = script_params[1]
  # 判断是否存在
  if not opt_info.get(opt, False):
    print("%s功能不存在!请检查传参!" % (opt))
    return

  # 加载配置
  # project_path = os.getcwd()
  # config_path = os.path.join(project_path, "conf", opt_info[opt]["config_file_name"])
  # if not os.path.exists(config_path):
  #   print("%s 配置文件不存在!" % (config_path))
  #   return
  # opt_info[opt]["config"] = json.load(open(config_path, encoding="utf-8"))

  # 加载配置测试
  opt_info[opt]["config"] = {}

  # 遍历除main.py,功能的参数
  for i in range(2, len(script_params)):
    p = script_params[i]
    if not p.startswith('-'):
      continue
    # 判断是否在参数字典中
    if not opt_info[opt].get(p, False):
      print("%s 功能没有 %s 参数!" % (opt, p))
      continue
    # 参数值获取
    p_num = opt_info[opt][p]["num"]
    if p_num < 1:
      # 参数对应设置
      p_set(opt, opt_info[opt]["config"], p)
    # 参数值为1个的
    elif p_num == 1:
      p_v = script_params[i+p_num]
      # 检查参数
      if not p_check(opt, p, p_v): continue
      # 参数对应设置
      p_set(opt, opt_info[opt]["config"], p, p_v)

  # # 执行
  # func = opt_info[opt]["func"]
  # if opt in ['e2j', 'merge']:
  #   func(opt_info[opt]["config"]).run()
  # elif opt == 'e2j_check':
  #   e2j_config_path = os.path.join(project_path, "conf", E2J_CONFIG_FILENAME)
  #   e2j_config = json.load(open(e2j_config_path, encoding="utf-8"))
  #   func(e2j_config, opt_info[opt]["config"]).run()

  # # 调用检查功能
  # if opt == 'e2j' and E2J_check_run:
  #   # 重新调用
  #   run(['main.py', 'e2j_check', '-v', opt_info[opt]['config']['version']])

  # 执行测试
  func = opt_info[opt]["func"]
  func(opt_info[opt]["config"]).run()

if __name__ == '__main__':
  run()

 

posted @ 2021-12-01 10:25  zezhou222  阅读(220)  评论(0编辑  收藏  举报