1 | #include <common/argsToConfig.h> |
---|---|
2 | |
3 | #include <Poco/Util/Application.h> |
4 | #include <Poco/Util/LayeredConfiguration.h> |
5 | #include <Poco/Util/MapConfiguration.h> |
6 | |
7 | |
8 | void argsToConfig(const Poco::Util::Application::ArgVec & argv, Poco::Util::LayeredConfiguration & config, int priority) |
9 | { |
10 | /// Parsing all args and converting to config layer |
11 | /// 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 |
12 | Poco::AutoPtr<Poco::Util::MapConfiguration> map_config = new Poco::Util::MapConfiguration; |
13 | std::string key; |
14 | for (auto & arg : argv) |
15 | { |
16 | auto key_start = arg.find_first_not_of('-'); |
17 | auto pos_minus = arg.find('-'); |
18 | auto pos_eq = arg.find('='); |
19 | |
20 | // old saved '--key', will set to some true value "1" |
21 | if (!key.empty() && pos_minus != std::string::npos && pos_minus < key_start) |
22 | { |
23 | map_config->setString(key, "1"); |
24 | key = ""; |
25 | } |
26 | |
27 | if (pos_eq == std::string::npos) |
28 | { |
29 | if (!key.empty()) |
30 | { |
31 | if (pos_minus == std::string::npos || pos_minus > key_start) |
32 | { |
33 | map_config->setString(key, arg); |
34 | } |
35 | key = ""; |
36 | } |
37 | if (pos_minus != std::string::npos && key_start != std::string::npos && pos_minus < key_start) |
38 | key = arg.substr(key_start); |
39 | continue; |
40 | } |
41 | else |
42 | { |
43 | key = ""; |
44 | } |
45 | |
46 | if (key_start == std::string::npos) |
47 | continue; |
48 | |
49 | if (pos_minus > key_start) |
50 | continue; |
51 | |
52 | key = arg.substr(key_start, pos_eq - key_start); |
53 | if (key.empty()) |
54 | continue; |
55 | std::string value; |
56 | if (arg.size() > pos_eq) |
57 | value = arg.substr(pos_eq + 1); |
58 | |
59 | map_config->setString(key, value); |
60 | key = ""; |
61 | } |
62 | |
63 | Poco::Util::MapConfiguration::Keys keys; |
64 | map_config->keys(keys); |
65 | |
66 | config.add(map_config, priority); |
67 | } |
68 |