如何为应用程序设置默认选项
How to set a default option for an application?
这里是我编码的一个小应用程序。现在我想把/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)
OT:<rant>
我喜欢PoCo,以前用过。这些天我转而使用Boost,因为编译器支持变得无处不在这个问题是我对侵入性/限制性框架的定义:简单的事情变得很难做。有时会挑战一个好程序员的价值。</rant>
我建议保留一个标志,如下所示:(看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)
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 如何使用默认参数等选择模板专业化
- 具有默认模板参数的多态类的模板推导失败
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在提升程序选项中设置矢量<矢量>的默认值<string>
- boost::p rogram_options:是否可以将一个选项设置为另一个选项的默认值
- 如何使用选项真、假、默认值和切换C++实现标志
- C ++提升程序选项始终给出默认值
- 在QT中注册iOS默认用户首选项
- 更改Visual Studio中类的默认选项卡结构
- C++中用于将默认变量类型设置为int的任何选项
- 具有接受参数选项的默认构造函数
- 是否有一个 GNU C 编译器选项可以生成运行速度更快但精度低于默认设置的浮点程序
- 如何使用提升程序选项获取默认参数值
- 使用vm.count()时,始终存在具有默认值的Boost程序选项
- boost程序选项中的默认参数和隐式参数
- 将set选项设置为默认值
- R Makevars 文件覆盖 R CMD 的默认 g++ 选项?
- 如何为应用程序设置默认选项
- 使用默认值提升程序选项