diff --git a/Util/include/Poco/Util/Application.h b/Util/include/Poco/Util/Application.h index 15d2adc6f..8c18cda4f 100644 --- a/Util/include/Poco/Util/Application.h +++ b/Util/include/Poco/Util/Application.h @@ -105,6 +105,8 @@ class Util_API Application: public Subsystem /// Unicode command line arguments. { public: + typedef std::vector ArgVec; + enum ExitCode /// Commonly used exit status codes. /// Based on the definitions in the 4.3BSD header file. @@ -168,7 +170,7 @@ public: /// Unicode command line arguments from wmain(). #endif - void init(const std::vector& args); + void init(const ArgVec& args); /// Processes the application's command line arguments /// and sets the application's properties (e.g., /// "application.path", "application.name", etc.). @@ -265,6 +267,15 @@ public: /// "application.logger" configuration property. If that property /// is not specified, the logger is "Application". + const ArgVec& argv() const; + /// Returns reference to vector of the application's arguments as + /// specified on the command line. If user overrides the + /// Application::main(const ArgVec&) function, it will receive + /// only the command line parameters that were not processed in + /// Application::processOptons(). This function returns the + /// full set of command line parameters as received in + /// main(argc, argv*). + const OptionSet& options() const; /// Returns the application's option set. @@ -367,7 +378,7 @@ protected: private: void setup(); void setArgs(int argc, char* argv[]); - void setArgs(const std::vector& args); + void setArgs(const ArgVec& args); void getApplicationPath(Poco::Path& path) const; void processOptions(); bool findAppConfigFile(const std::string& appName, const std::string& extension, Poco::Path& path) const; @@ -375,13 +386,13 @@ private: typedef Poco::AutoPtr SubsystemPtr; typedef std::vector SubsystemVec; typedef Poco::AutoPtr ConfigPtr; - typedef std::vector ArgVec; ConfigPtr _pConfig; SubsystemVec _subsystems; bool _initialized; std::string _command; - ArgVec _args; + ArgVec _argv; + ArgVec _unprocessedArgs; OptionSet _options; bool _unixOptions; Poco::Logger* _pLogger; @@ -435,6 +446,12 @@ inline Poco::Logger& Application::logger() const } +inline const Application::ArgVec& Application::argv() const +{ + return _argv; +} + + inline const OptionSet& Application::options() const { return _options; diff --git a/Util/samples/SampleApp/src/SampleApp.cpp b/Util/samples/SampleApp/src/SampleApp.cpp index 74bf595ca..99d542033 100644 --- a/Util/samples/SampleApp/src/SampleApp.cpp +++ b/Util/samples/SampleApp/src/SampleApp.cpp @@ -39,6 +39,7 @@ #include "Poco/Util/AbstractConfiguration.h" #include "Poco/AutoPtr.h" #include +#include using Poco::Util::Application; @@ -154,12 +155,20 @@ protected: config().setString(name, value); } - int main(const std::vector& args) + int main(const ArgVec& args) { if (!_helpRequested) { + logger().information("Command line:"); + const ArgVec argVec = argv(); + std::ostringstream ostr; + for (ArgVec::const_iterator it = argVec.begin(); it != argVec.end(); ++it) + { + ostr << *it << ' '; + } + logger().information(ostr.str()); logger().information("Arguments to main():"); - for (std::vector::const_iterator it = args.begin(); it != args.end(); ++it) + for (ArgVec::const_iterator it = args.begin(); it != args.end(); ++it) { logger().information(*it); } diff --git a/Util/samples/SampleServer/src/SampleServer.cpp b/Util/samples/SampleServer/src/SampleServer.cpp index 5bcd4a960..905255184 100644 --- a/Util/samples/SampleServer/src/SampleServer.cpp +++ b/Util/samples/SampleServer/src/SampleServer.cpp @@ -123,7 +123,7 @@ protected: helpFormatter.format(std::cout); } - int main(const std::vector& args) + int main(const ArgVec& args) { if (!_helpRequested) { diff --git a/Util/src/Application.cpp b/Util/src/Application.cpp index 71580b749..04b84cf0d 100644 --- a/Util/src/Application.cpp +++ b/Util/src/Application.cpp @@ -167,7 +167,7 @@ void Application::init(int argc, wchar_t* argv[]) #endif -void Application::init(const std::vector& args) +void Application::init(const ArgVec& args) { setArgs(args); init(); @@ -316,7 +316,7 @@ int Application::run() { initialize(*this); rc = EXIT_SOFTWARE; - rc = main(_args); + rc = main(_unprocessedArgs); uninitialize(); } catch (Poco::Exception& exc) @@ -335,7 +335,7 @@ int Application::run() } -int Application::main(const std::vector& args) +int Application::main(const ArgVec& args) { return EXIT_OK; } @@ -345,24 +345,24 @@ void Application::setArgs(int argc, char* argv[]) { _command = argv[0]; _pConfig->setInt("application.argc", argc); - _args.reserve(argc); + _unprocessedArgs.reserve(argc); std::string argvKey = "application.argv["; for (int i = 0; i < argc; ++i) { std::string arg(argv[i]); _pConfig->setString(argvKey + NumberFormatter::format(i) + "]", arg); - _args.push_back(arg); + _unprocessedArgs.push_back(arg); } } -void Application::setArgs(const std::vector& args) +void Application::setArgs(const ArgVec& args) { poco_assert (!args.empty()); _command = args[0]; _pConfig->setInt("application.argc", (int) args.size()); - _args = args; + _unprocessedArgs = args; std::string argvKey = "application.argv["; for (int i = 0; i < args.size(); ++i) { @@ -376,9 +376,10 @@ void Application::processOptions() defineOptions(_options); OptionProcessor processor(_options); processor.setUnixStyle(_unixOptions); - _args.erase(_args.begin()); - ArgVec::iterator it = _args.begin(); - while (it != _args.end() && !_stopOptionsProcessing) + _argv = _unprocessedArgs; + _unprocessedArgs.erase(_unprocessedArgs.begin()); + ArgVec::iterator it = _unprocessedArgs.begin(); + while (it != _unprocessedArgs.end() && !_stopOptionsProcessing) { std::string name; std::string value; @@ -388,7 +389,7 @@ void Application::processOptions() { handleOption(name, value); } - it = _args.erase(it); + it = _unprocessedArgs.erase(it); } else ++it; }