| 1 | // |
| 2 | // SampleApp.cpp |
| 3 | // |
| 4 | // This sample demonstrates the Application class. |
| 5 | // |
| 6 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 7 | // and Contributors. |
| 8 | // |
| 9 | // SPDX-License-Identifier: BSL-1.0 |
| 10 | // |
| 11 | |
| 12 | |
| 13 | #include "Poco/Util/Application.h" |
| 14 | #include "Poco/Util/Option.h" |
| 15 | #include "Poco/Util/OptionSet.h" |
| 16 | #include "Poco/Util/HelpFormatter.h" |
| 17 | #include "Poco/Util/AbstractConfiguration.h" |
| 18 | #include "Poco/AutoPtr.h" |
| 19 | #include <iostream> |
| 20 | #include <sstream> |
| 21 | |
| 22 | |
| 23 | using Poco::Util::Application; |
| 24 | using Poco::Util::Option; |
| 25 | using Poco::Util::OptionSet; |
| 26 | using Poco::Util::HelpFormatter; |
| 27 | using Poco::Util::AbstractConfiguration; |
| 28 | using Poco::Util::OptionCallback; |
| 29 | using Poco::AutoPtr; |
| 30 | |
| 31 | |
| 32 | class SampleApp: public Application |
| 33 | /// This sample demonstrates some of the features of the Util::Application class, |
| 34 | /// such as configuration file handling and command line arguments processing. |
| 35 | /// |
| 36 | /// Try SampleApp --help (on Unix platforms) or SampleApp /help (elsewhere) for |
| 37 | /// more information. |
| 38 | { |
| 39 | public: |
| 40 | SampleApp(): _helpRequested(false) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | protected: |
| 45 | void initialize(Application& self) |
| 46 | { |
| 47 | loadConfiguration(); // load default configuration files, if present |
| 48 | Application::initialize(self); |
| 49 | // add your own initialization code here |
| 50 | } |
| 51 | |
| 52 | void uninitialize() |
| 53 | { |
| 54 | // add your own uninitialization code here |
| 55 | Application::uninitialize(); |
| 56 | } |
| 57 | |
| 58 | void reinitialize(Application& self) |
| 59 | { |
| 60 | Application::reinitialize(self); |
| 61 | // add your own reinitialization code here |
| 62 | } |
| 63 | |
| 64 | void defineOptions(OptionSet& options) |
| 65 | { |
| 66 | Application::defineOptions(options); |
| 67 | |
| 68 | options.addOption( |
| 69 | Option("help" , "h" , "display help information on command line arguments" ) |
| 70 | .required(false) |
| 71 | .repeatable(false) |
| 72 | .callback(OptionCallback<SampleApp>(this, &SampleApp::handleHelp))); |
| 73 | |
| 74 | options.addOption( |
| 75 | Option("define" , "D" , "define a configuration property" ) |
| 76 | .required(false) |
| 77 | .repeatable(true) |
| 78 | .argument("name=value" ) |
| 79 | .callback(OptionCallback<SampleApp>(this, &SampleApp::handleDefine))); |
| 80 | |
| 81 | options.addOption( |
| 82 | Option("config-file" , "f" , "load configuration data from a file" ) |
| 83 | .required(false) |
| 84 | .repeatable(true) |
| 85 | .argument("file" ) |
| 86 | .callback(OptionCallback<SampleApp>(this, &SampleApp::handleConfig))); |
| 87 | |
| 88 | options.addOption( |
| 89 | Option("bind" , "b" , "bind option value to test.property" ) |
| 90 | .required(false) |
| 91 | .repeatable(false) |
| 92 | .argument("value" ) |
| 93 | .binding("test.property" )); |
| 94 | } |
| 95 | |
| 96 | void handleHelp(const std::string& name, const std::string& value) |
| 97 | { |
| 98 | _helpRequested = true; |
| 99 | displayHelp(); |
| 100 | stopOptionsProcessing(); |
| 101 | } |
| 102 | |
| 103 | void handleDefine(const std::string& name, const std::string& value) |
| 104 | { |
| 105 | defineProperty(value); |
| 106 | } |
| 107 | |
| 108 | void handleConfig(const std::string& name, const std::string& value) |
| 109 | { |
| 110 | loadConfiguration(value); |
| 111 | } |
| 112 | |
| 113 | void displayHelp() |
| 114 | { |
| 115 | HelpFormatter helpFormatter(options()); |
| 116 | helpFormatter.setCommand(commandName()); |
| 117 | helpFormatter.setUsage("OPTIONS" ); |
| 118 | helpFormatter.setHeader("A sample application that demonstrates some of the features of the Poco::Util::Application class." ); |
| 119 | helpFormatter.format(std::cout); |
| 120 | } |
| 121 | |
| 122 | void defineProperty(const std::string& def) |
| 123 | { |
| 124 | std::string name; |
| 125 | std::string value; |
| 126 | std::string::size_type pos = def.find('='); |
| 127 | if (pos != std::string::npos) |
| 128 | { |
| 129 | name.assign(def, 0, pos); |
| 130 | value.assign(def, pos + 1, def.length() - pos); |
| 131 | } |
| 132 | else name = def; |
| 133 | config().setString(name, value); |
| 134 | } |
| 135 | |
| 136 | int main(const ArgVec& args) |
| 137 | { |
| 138 | if (!_helpRequested) |
| 139 | { |
| 140 | logger().information("Command line:" ); |
| 141 | std::ostringstream ostr; |
| 142 | for (ArgVec::const_iterator it = argv().begin(); it != argv().end(); ++it) |
| 143 | { |
| 144 | ostr << *it << ' '; |
| 145 | } |
| 146 | logger().information(ostr.str()); |
| 147 | logger().information("Arguments to main():" ); |
| 148 | for (ArgVec::const_iterator it = args.begin(); it != args.end(); ++it) |
| 149 | { |
| 150 | logger().information(*it); |
| 151 | } |
| 152 | logger().information("Application properties:" ); |
| 153 | printProperties("" ); |
| 154 | } |
| 155 | return Application::EXIT_OK; |
| 156 | } |
| 157 | |
| 158 | void printProperties(const std::string& base) |
| 159 | { |
| 160 | AbstractConfiguration::Keys keys; |
| 161 | config().keys(base, keys); |
| 162 | if (keys.empty()) |
| 163 | { |
| 164 | if (config().hasProperty(base)) |
| 165 | { |
| 166 | std::string msg; |
| 167 | msg.append(base); |
| 168 | msg.append(" = " ); |
| 169 | msg.append(config().getString(base)); |
| 170 | logger().information(msg); |
| 171 | } |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | for (AbstractConfiguration::Keys::const_iterator it = keys.begin(); it != keys.end(); ++it) |
| 176 | { |
| 177 | std::string fullKey = base; |
| 178 | if (!fullKey.empty()) fullKey += '.'; |
| 179 | fullKey.append(*it); |
| 180 | printProperties(fullKey); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | bool _helpRequested; |
| 187 | }; |
| 188 | |
| 189 | |
| 190 | POCO_APP_MAIN(SampleApp) |
| 191 | |