#include "KMOption.h"

#include <iostream>

#include "common/KMUtil.h"

KMOption::KMOption()
{
    m_commit = "active";
    m_runtime_commit = "active";
}

KMOption::~KMOption() { }

void KMOption::parseOptions(int argc, char **argv)
{
    m_command = "";
    for (int i = 0; i < argc; i++)
    {
        if (argv[i][0] == '-')
        {
            matchOption(argv[i]);
        }
    }
    m_arch = m_arch == "" ? KMUtil::kmGetArch() : m_arch;
    m_branch = m_branch == "" ? KMUtil::kmGetDefaultChannel() : m_branch;
}

void KMOption::matchOption(char *option)
{
    string key;
    string value;
    char *tmpOpt = option;
    while (*tmpOpt == '-')
    {
        tmpOpt++;
    }
    string strOpt = tmpOpt;
    size_t pos = strOpt.find('=');
    if (pos != std::string::npos)
    {
        key = strOpt.substr(0, pos);
        value = strOpt.substr(pos + 1);
    }
    else
    {
        key = strOpt;
    }

    // 参数判断
    if (key == "branch")
    {
        m_branch = value;
    }
    else if (key == "arch")
    {
        m_arch = value;
    }
    else if (key == "commit")
    {
        m_commit = value;
    }
    else if (key == "runtime-commit")
    {
        m_runtime_commit = value;
    }
    else if (key == "command")
    {
        m_command = value;
    }
}
