2011-06-16 124 views
1

这是我编码的一个小应用程序。现在我想用/ 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) 
+0

你的意思是说,你想用默认调用成员函数'handleHelp'?我们大多数人可能不熟悉您使用的图书馆。所以,如果你能够更多地解释程序实际执行的内容并且可能是执行顺序,那将会很有帮助。例如,在类定义中编写'main'有点像我以前从未见过的。 – Mahesh 2011-06-16 14:43:32

回答

2

OT:<rant>我爱POCO,以前使用过它。现在我更倾向于Boost,因为编译器支持变得无处不在。 这个问题是我对侵入性/限制性框架的定义:简单的事情变得很难做到。缺乏有效的程序员的价值</rant>

我建议保持一个标志,像这样:(看福尔_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) 
+0

谢谢你的建议。 – surendran 2011-06-16 16:54:04

相关问题