2011-06-16 117 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

相關問題