mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-18 00:15:27 +01: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.
|
||||
{
|
||||
public:
|
||||
typedef std::vector<std::string> ArgVec;
|
||||
|
||||
enum ExitCode
|
||||
/// Commonly used exit status codes.
|
||||
/// Based on the definitions in the 4.3BSD <sysexits.h> header file.
|
||||
@ -168,7 +170,7 @@ public:
|
||||
/// Unicode command line arguments from wmain().
|
||||
#endif
|
||||
|
||||
void init(const std::vector<std::string>& 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<std::string>& 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<Subsystem> SubsystemPtr;
|
||||
typedef std::vector<SubsystemPtr> SubsystemVec;
|
||||
typedef Poco::AutoPtr<LayeredConfiguration> ConfigPtr;
|
||||
typedef std::vector<std::string> 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;
|
||||
|
@ -39,6 +39,7 @@
|
||||
#include "Poco/Util/AbstractConfiguration.h"
|
||||
#include "Poco/AutoPtr.h"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
using Poco::Util::Application;
|
||||
@ -154,12 +155,20 @@ protected:
|
||||
config().setString(name, value);
|
||||
}
|
||||
|
||||
int main(const std::vector<std::string>& 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<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);
|
||||
}
|
||||
|
@ -123,7 +123,7 @@ protected:
|
||||
helpFormatter.format(std::cout);
|
||||
}
|
||||
|
||||
int main(const std::vector<std::string>& args)
|
||||
int main(const ArgVec& args)
|
||||
{
|
||||
if (!_helpRequested)
|
||||
{
|
||||
|
@ -167,7 +167,7 @@ void Application::init(int argc, wchar_t* argv[])
|
||||
#endif
|
||||
|
||||
|
||||
void Application::init(const std::vector<std::string>& 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<std::string>& 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<std::string>& 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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user