# -*- coding: utf-8 -*-
# 命令行基础类
# 作者: 陈磊
# 时间: 2019-10-22


import sys
import getopt


from WorkUtils.UtilsLog import UtilsLog


class UtilsCmd:
    def __init__(self):
        self.log = UtilsLog()
        self.log.info("调用命令行基础类")
        self.log.info(self.__class__)

    def usage(self):
        self.log.debug("帮助信息:")
        self.log.debug(" *  -h : 打印当前信息")
        self.log.debug(" *  -v [val] : 版本号")
        self.log.debug(" *  -p [val] : 测试路径")

    def unittest_cmd(self):
        self.log.debug("运行用例执行参数模式......")
        version = ""
        description = ""
        try:
            opts, args = getopt.getopt(sys.argv[1:], "hd:v:")
            for op, value in opts:
                if op == "-d":
                    description = value
                elif op == "-v":
                    version = value
                elif op == "-h":
                    # 帮助信息
                    self.usage()
                    sys.exit()
                else:
                    sys.exit()
        except getopt.GetoptError as e:
            self.log.error("出现ERROR:")
            self.log.error(e)
        return {
            "description": description,
            "version": version
        }

    def pytest_cmd(self):
        """
        pytest框架
        :return:
        """
        self.log.debug("运行用例执行参数模式......")
        version = None
        environment = None
        path = None
        try:
            opts, args = getopt.getopt(sys.argv[1:], "hv:e:p:")
            for op, value in opts:
                if op == "-v":
                    version = value
                elif op == "-e":
                    environment = value
                elif op == "-p":
                    path = value.replace("/", "\\")
                    # path = value
                elif op == "-h":
                    # 帮助信息
                    self.usage()
                    sys.exit()
                else:
                    sys.exit()
        except getopt.GetoptError as e:
            self.log.error("出现ERROR:")
            self.log.error(e)

        _json = {
            "version": version,
            "environment": environment,
            "path": path
        }
        self.log.info(_json)
        return _json
