如何为应用程序设置默认选项?
这是我编写的一个小应用程序。现在我想将 /h 设置为默认选项,以便用户运行它时可以获得帮助消息。有人可以帮我解决这个问题吗?
#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()
{
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)
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;
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OT:
我喜欢 PoCo,以前用过它。这些天我更倾向于 Boost,因为编译器支持变得无处不在。 这个问题是我对侵入性/限制性框架的定义:简单的事情变得很难做。有时违背了优秀程序员的价值。我建议保留一个标志,如下所示:(查看
_noop
)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
)