
#include <WFX/util/CommandParams.h>

using namespace WFX;
using namespace WFX::util;

CommandParams::CommandParams(Int argc, Char *argv[]) {
    init(argc, argv);
}

CommandParams::CommandParams() {
}

CommandParams::~CommandParams() {
}

void CommandParams::init(Int argc, Char *argv[]) {
    std::string currentOption = "";
    for (Int i=1; i<argc; i++) {
        std::string val = argv[i];
        if (argv[i][0] == '-' && argv[i][1]) {
            Options[val] = true; 
            if (argv[i][1] == '-')
                currentOption = val;
            else
                currentOption = "";
        } else {
            if (currentOption != "") {
                Values[currentOption] = val;
            } else {
                Files.push_back(val);
            }
            currentOption = "";
        }
    }
}

UInt CommandParams::getFileCount() {
    return Files.size();
}

std::string CommandParams::getFile(UInt id) {
    if (id >= getFileCount())
        THROW("File id " << id << " > " << getFileCount());

    return Files[id];
}

bool CommandParams::hasOption(const std::string& o) {
    return (Options.find(o) != Options.end());
}

bool CommandParams::hasValue(std::string o) {
    return (Values.find(o) != Values.end());
}

std::string CommandParams::getValue(const std::string& o) {
    if (Values.find(o) == Values.end())
        THROW("Unknown Value requested " << o);

    return Values[o];
}


void CommandParams::printDebug() {
    LOG("CommandParams:");
    //options
    std::map<std::string, bool>::iterator oIt;
    for (oIt = Options.begin(); oIt != Options.end(); oIt++) {
        LOG(" -> Option: " << (*oIt).first);
    }
    //values
    std::map<std::string, std::string>::iterator vIt;
    for (vIt = Values.begin(); vIt != Values.end(); vIt++) {
        LOG(" -> Values: " << (*vIt).first << " => " << (*vIt).second);
    }
    //files
    std::vector<std::string>::iterator fIt;
    for (fIt = Files.begin(); fIt != Files.end(); fIt++) {
        LOG(" -> File: " << (*fIt));
    }
}


