mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-26 18:11:29 +02:00
GH #48: Need getArgs() accessor
GH #48: Need getArgs() accessor to Util::Application to retrieve start-up arguments
This commit is contained in:
parent
73a3a5e288
commit
7e8797fb5a
@ -105,6 +105,8 @@ class Util_API Application: public Subsystem
|
|||||||
/// Unicode command line arguments.
|
/// Unicode command line arguments.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
typedef std::vector<std::string> ArgVec;
|
||||||
|
|
||||||
enum ExitCode
|
enum ExitCode
|
||||||
/// Commonly used exit status codes.
|
/// Commonly used exit status codes.
|
||||||
/// Based on the definitions in the 4.3BSD <sysexits.h> header file.
|
/// Based on the definitions in the 4.3BSD <sysexits.h> header file.
|
||||||
@ -168,7 +170,7 @@ public:
|
|||||||
/// Unicode command line arguments from wmain().
|
/// Unicode command line arguments from wmain().
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void init(const std::vector<std::string>& args);
|
void init(const ArgVec& args);
|
||||||
/// Processes the application's command line arguments
|
/// Processes the application's command line arguments
|
||||||
/// and sets the application's properties (e.g.,
|
/// and sets the application's properties (e.g.,
|
||||||
/// "application.path", "application.name", etc.).
|
/// "application.path", "application.name", etc.).
|
||||||
@ -265,6 +267,15 @@ public:
|
|||||||
/// "application.logger" configuration property. If that property
|
/// "application.logger" configuration property. If that property
|
||||||
/// is not specified, the logger is "Application".
|
/// 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;
|
const OptionSet& options() const;
|
||||||
/// Returns the application's option set.
|
/// Returns the application's option set.
|
||||||
|
|
||||||
@ -367,7 +378,7 @@ protected:
|
|||||||
private:
|
private:
|
||||||
void setup();
|
void setup();
|
||||||
void setArgs(int argc, char* argv[]);
|
void setArgs(int argc, char* argv[]);
|
||||||
void setArgs(const std::vector<std::string>& args);
|
void setArgs(const ArgVec& args);
|
||||||
void getApplicationPath(Poco::Path& path) const;
|
void getApplicationPath(Poco::Path& path) const;
|
||||||
void processOptions();
|
void processOptions();
|
||||||
bool findAppConfigFile(const std::string& appName, const std::string& extension, Poco::Path& path) const;
|
bool findAppConfigFile(const std::string& appName, const std::string& extension, Poco::Path& path) const;
|
||||||
@ -375,13 +386,13 @@ private:
|
|||||||
typedef Poco::AutoPtr<Subsystem> SubsystemPtr;
|
typedef Poco::AutoPtr<Subsystem> SubsystemPtr;
|
||||||
typedef std::vector<SubsystemPtr> SubsystemVec;
|
typedef std::vector<SubsystemPtr> SubsystemVec;
|
||||||
typedef Poco::AutoPtr<LayeredConfiguration> ConfigPtr;
|
typedef Poco::AutoPtr<LayeredConfiguration> ConfigPtr;
|
||||||
typedef std::vector<std::string> ArgVec;
|
|
||||||
|
|
||||||
ConfigPtr _pConfig;
|
ConfigPtr _pConfig;
|
||||||
SubsystemVec _subsystems;
|
SubsystemVec _subsystems;
|
||||||
bool _initialized;
|
bool _initialized;
|
||||||
std::string _command;
|
std::string _command;
|
||||||
ArgVec _args;
|
ArgVec _argv;
|
||||||
|
ArgVec _unprocessedArgs;
|
||||||
OptionSet _options;
|
OptionSet _options;
|
||||||
bool _unixOptions;
|
bool _unixOptions;
|
||||||
Poco::Logger* _pLogger;
|
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
|
inline const OptionSet& Application::options() const
|
||||||
{
|
{
|
||||||
return _options;
|
return _options;
|
||||||
|
@ -39,6 +39,7 @@
|
|||||||
#include "Poco/Util/AbstractConfiguration.h"
|
#include "Poco/Util/AbstractConfiguration.h"
|
||||||
#include "Poco/AutoPtr.h"
|
#include "Poco/AutoPtr.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
using Poco::Util::Application;
|
using Poco::Util::Application;
|
||||||
@ -154,12 +155,20 @@ protected:
|
|||||||
config().setString(name, value);
|
config().setString(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(const std::vector<std::string>& args)
|
int main(const ArgVec& args)
|
||||||
{
|
{
|
||||||
if (!_helpRequested)
|
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():");
|
logger().information("Arguments to main():");
|
||||||
for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
|
for (ArgVec::const_iterator it = args.begin(); it != args.end(); ++it)
|
||||||
{
|
{
|
||||||
logger().information(*it);
|
logger().information(*it);
|
||||||
}
|
}
|
||||||
|
@ -123,7 +123,7 @@ protected:
|
|||||||
helpFormatter.format(std::cout);
|
helpFormatter.format(std::cout);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(const std::vector<std::string>& args)
|
int main(const ArgVec& args)
|
||||||
{
|
{
|
||||||
if (!_helpRequested)
|
if (!_helpRequested)
|
||||||
{
|
{
|
||||||
|
@ -167,7 +167,7 @@ void Application::init(int argc, wchar_t* argv[])
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void Application::init(const std::vector<std::string>& args)
|
void Application::init(const ArgVec& args)
|
||||||
{
|
{
|
||||||
setArgs(args);
|
setArgs(args);
|
||||||
init();
|
init();
|
||||||
@ -316,7 +316,7 @@ int Application::run()
|
|||||||
{
|
{
|
||||||
initialize(*this);
|
initialize(*this);
|
||||||
rc = EXIT_SOFTWARE;
|
rc = EXIT_SOFTWARE;
|
||||||
rc = main(_args);
|
rc = main(_unprocessedArgs);
|
||||||
uninitialize();
|
uninitialize();
|
||||||
}
|
}
|
||||||
catch (Poco::Exception& exc)
|
catch (Poco::Exception& exc)
|
||||||
@ -335,7 +335,7 @@ int Application::run()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Application::main(const std::vector<std::string>& args)
|
int Application::main(const ArgVec& args)
|
||||||
{
|
{
|
||||||
return EXIT_OK;
|
return EXIT_OK;
|
||||||
}
|
}
|
||||||
@ -345,24 +345,24 @@ void Application::setArgs(int argc, char* argv[])
|
|||||||
{
|
{
|
||||||
_command = argv[0];
|
_command = argv[0];
|
||||||
_pConfig->setInt("application.argc", argc);
|
_pConfig->setInt("application.argc", argc);
|
||||||
_args.reserve(argc);
|
_unprocessedArgs.reserve(argc);
|
||||||
std::string argvKey = "application.argv[";
|
std::string argvKey = "application.argv[";
|
||||||
for (int i = 0; i < argc; ++i)
|
for (int i = 0; i < argc; ++i)
|
||||||
{
|
{
|
||||||
std::string arg(argv[i]);
|
std::string arg(argv[i]);
|
||||||
_pConfig->setString(argvKey + NumberFormatter::format(i) + "]", arg);
|
_pConfig->setString(argvKey + NumberFormatter::format(i) + "]", arg);
|
||||||
_args.push_back(arg);
|
_unprocessedArgs.push_back(arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Application::setArgs(const std::vector<std::string>& args)
|
void Application::setArgs(const ArgVec& args)
|
||||||
{
|
{
|
||||||
poco_assert (!args.empty());
|
poco_assert (!args.empty());
|
||||||
|
|
||||||
_command = args[0];
|
_command = args[0];
|
||||||
_pConfig->setInt("application.argc", (int) args.size());
|
_pConfig->setInt("application.argc", (int) args.size());
|
||||||
_args = args;
|
_unprocessedArgs = args;
|
||||||
std::string argvKey = "application.argv[";
|
std::string argvKey = "application.argv[";
|
||||||
for (int i = 0; i < args.size(); ++i)
|
for (int i = 0; i < args.size(); ++i)
|
||||||
{
|
{
|
||||||
@ -376,9 +376,10 @@ void Application::processOptions()
|
|||||||
defineOptions(_options);
|
defineOptions(_options);
|
||||||
OptionProcessor processor(_options);
|
OptionProcessor processor(_options);
|
||||||
processor.setUnixStyle(_unixOptions);
|
processor.setUnixStyle(_unixOptions);
|
||||||
_args.erase(_args.begin());
|
_argv = _unprocessedArgs;
|
||||||
ArgVec::iterator it = _args.begin();
|
_unprocessedArgs.erase(_unprocessedArgs.begin());
|
||||||
while (it != _args.end() && !_stopOptionsProcessing)
|
ArgVec::iterator it = _unprocessedArgs.begin();
|
||||||
|
while (it != _unprocessedArgs.end() && !_stopOptionsProcessing)
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string value;
|
std::string value;
|
||||||
@ -388,7 +389,7 @@ void Application::processOptions()
|
|||||||
{
|
{
|
||||||
handleOption(name, value);
|
handleOption(name, value);
|
||||||
}
|
}
|
||||||
it = _args.erase(it);
|
it = _unprocessedArgs.erase(it);
|
||||||
}
|
}
|
||||||
else ++it;
|
else ++it;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user