Here is a small application i coded. Now i wanna make /h as default option, so that when a user runs it he can get a help message. Can anyone help me with this please?
#include "Poco/Util/Application.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/AutoPtr.h"
#include "Poco/Process.h"
#include "Poco/PipeStream.h"
#include "Poco/StreamCopier.h"
#include <fstream>
#include <iostream>
using Poco::Util::Application;开发者_JS百科
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;
using Poco::Util::AbstractConfiguration;
using Poco::Util::OptionCallback;
using Poco::AutoPtr;
using Poco::Process;
using Poco::ProcessHandle;
using namespace std;
class SampleApp: public Application
{
protected:
void defineOptions(OptionSet& options)
{
Application::defineOptions(options);
options.addOption(
Option("help", "h", "Displays help details")
.required(false)
.repeatable(false)
.callback(OptionCallback<SampleApp>(this, &SampleApp::handleHelp)));
options.addOption(
Option("Execute", "e", "Executes a c++ code and stores output in processess.txt")
.required(false)
.repeatable(false)
.callback(OptionCallback<SampleApp>(this, &SampleApp::handleExecute)));
}
void handleHelp(const std::string& name, const std::string& value)
{
Help();
}
void handleExecute(const std::string& name, const std::string& value)
{
Execute();
}
void Help()
{
cout << "App.exe /option";
}
void Execute()
{
std::string cmd("D:\\Projects\\sample_cpp\\Debug\\sample_cpp.exe");
std::vector<std::string> path;
path.push_back("");
Poco::Pipe outPipe;
ProcessHandle ph = Process::launch(cmd, path, 0, &outPipe, 0);
Poco::PipeInputStream istr(outPipe);
std::ofstream ostr("processes.txt");
Poco::StreamCopier::copyStream(istr, ostr);
cout << "Chk for processess.txt file" << endl;
}
int main(const std::vector<std::string>& args)
{
return Application::EXIT_OK;
}
};
POCO_APP_MAIN(SampleApp)
OT: <rant>
I love PoCo, have used it before. These days I gravitate to Boost instead, since compiler support is becoming ubiquitous. This problem is my definition of a intrusive/restrictive framework: simple things become hard to do. Defies the value of a good programmer sometimes.</rant>
I suggest keeping a flag, like so: (look foor _noop
)
#include "Poco/Util/Application.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/AutoPtr.h"
#include "Poco/Process.h"
#include "Poco/PipeStream.h"
#include "Poco/StreamCopier.h"
#include <fstream>
#include <iostream>
using Poco::Util::Application;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;
using Poco::Util::AbstractConfiguration;
using Poco::Util::OptionCallback;
using Poco::AutoPtr;
using Poco::Process;
using Poco::ProcessHandle;
using namespace std;
class SampleApp: public Application
{
protected:
void defineOptions(OptionSet& options)
{
Application::defineOptions(options);
options.addOption(
Option("help", "h", "Displays help details")
.required(false)
.repeatable(false)
.callback(OptionCallback<SampleApp>(this, &SampleApp::handleHelp)));
options.addOption(
Option("Execute", "e", "Executes a c++ code and stores output in processess.txt")
.required(false)
.repeatable(false)
.callback(OptionCallback<SampleApp>(this, &SampleApp::handleExecute)));
}
void handleHelp(const std::string& name, const std::string& value)
{
Help();
}
void handleExecute(const std::string& name, const std::string& value)
{
Execute();
}
void Help()
{
_noop = false;
cout << "App.exe /option";
}
void Execute()
{
_noop = false;
std::string cmd("D:\\Projects\\sample_cpp\\Debug\\sample_cpp.exe");
std::vector<std::string> path;
path.push_back("");
Poco::Pipe outPipe;
ProcessHandle ph = Process::launch(cmd, path, 0, &outPipe, 0);
Poco::PipeInputStream istr(outPipe);
std::ofstream ostr("processes.txt");
Poco::StreamCopier::copyStream(istr, ostr);
cout << "Chk for processess.txt file" << endl;
}
SampleApp() : _noop(true) { }
int main(const std::vector<std::string>& args)
{
if (_noop)
Help();
return Application::EXIT_OK;
}
private:
bool _noop;
};
POCO_APP_MAIN(SampleApp)
精彩评论