argsToConfig.cpp 2.0 KB
Newer Older
I
Ivan 已提交
1
#include "argsToConfig.h"
2 3 4 5 6 7 8 9 10 11 12

#include <Poco/Util/LayeredConfiguration.h>
#include <Poco/Util/MapConfiguration.h>


void argsToConfig(const Poco::Util::Application::ArgVec & argv, Poco::Util::LayeredConfiguration & config, int priority)
{
    /// Parsing all args and converting to config layer
    /// Test: -- --1=1 --1=2 --3 5 7 8 -9 10 -11=12 14= 15== --16==17 --=18 --19= --20 21 22 --23 --24 25 --26 -27 28 ---29=30 -- ----31 32 --33 3-4
    Poco::AutoPtr<Poco::Util::MapConfiguration> map_config = new Poco::Util::MapConfiguration;
    std::string key;
13
    for (const auto & arg : argv)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    {
        auto key_start = arg.find_first_not_of('-');
        auto pos_minus = arg.find('-');
        auto pos_eq = arg.find('=');

        // old saved '--key', will set to some true value "1"
        if (!key.empty() && pos_minus != std::string::npos && pos_minus < key_start)
        {
            map_config->setString(key, "1");
            key = "";
        }

        if (pos_eq == std::string::npos)
        {
            if (!key.empty())
            {
                if (pos_minus == std::string::npos || pos_minus > key_start)
                {
                    map_config->setString(key, arg);
                }
                key = "";
            }
            if (pos_minus != std::string::npos && key_start != std::string::npos && pos_minus < key_start)
                key = arg.substr(key_start);
            continue;
        }
        else
        {
            key = "";
        }

        if (key_start == std::string::npos)
            continue;

        if (pos_minus > key_start)
            continue;

        key = arg.substr(key_start, pos_eq - key_start);
        if (key.empty())
            continue;
        std::string value;
        if (arg.size() > pos_eq)
            value = arg.substr(pos_eq + 1);

        map_config->setString(key, value);
        key = "";
    }

    Poco::Util::MapConfiguration::Keys keys;
    map_config->keys(keys);

    config.add(map_config, priority);
}