Poco Application framework learning (2) increase command line parameters

Then learn the Application framework. Our general application will have several parameters provided. -h, –help, -v –version, –daemon. Below we introduce the realization of these functions. Application provides a defineOptions( OptionSet & option ) method. A derived class that wants to support command line arguments must override this method and call this method of the base class. Misfortune: OptionSet is just a vector of Option class. Poco::Util::Option This class helps us add some startup parameters when starting the application. And OptionSet is a vector that stores multiple Option. Here is a simple example code to introduce its functions. For details, please refer to the header file Option.h or refer to the implementation of the Poco::Util::ServerApplication framework.
/*
 * main.cpp
 *
 *  Created on: 2015 2 8 
 *      Author: yuhaiyang
 */

#include "Poco/Poco.h"
#include "Poco/Util/Application.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include 
#include 
#include 
#include 


using namespace Poco;

class MyApplication : public Util::Application
{
public:
    MyApplication( )
    {
        m_helpRequested =  false ;
    }
    void initialize( Application& self );
    void uninitialize( );

    void defineOptions( Util::OptionSet& options);
    void handleHelp(const std::string& name, const std::string& value );
    void handleDaemon(const std::string& name, const std::string& value);
    int main(const std::vector<std::string>& args);
private:
    bool m_helpRequested;//  true  main()  。
    void beDaemon( );
};

void MyApplication::initialize( Application& self )
{
    Util::Application::initialize( self );// , 。
    std::cout << "this is initialize
"
; } void MyApplication::uninitialize( ) { Util::Application::uninitialize( );// , 。 std::cout << "this is uninitialize
"
; } void MyApplication::defineOptions( Util::OptionSet& options) { Util::Application::defineOptions( options );// std::cout << "defineOptions " << std::endl; options.addOption( Util::Option("help", "h", "display help information on command line arguments") .required(false) .repeatable(false) .callback(Util::OptionCallback < MyApplication >(this, &MyApplication::handleHelp)) ); options.addOption( Util::Option("daemon", "", "Run application as a daemon.") .required(false) .repeatable(false) .callback(Util::OptionCallback(this, &MyApplication::handleDaemon)) ); } void MyApplication::handleHelp(const std::string& name, const std::string& value ) { m_helpRequested = true; Poco::Util::HelpFormatter helpFormatter( options() ); helpFormatter.format(std::cout); } void MyApplication::handleDaemon(const std::string& name, const std::string& value) { beDaemon(); } void MyApplication::beDaemon() { pid_t pid; if ((pid = fork()) < 0) throw SystemException("cannot fork daemon process"); else if (pid != 0) exit(0); setsid(); umask(0); FILE* fin = freopen("/dev/null", "r+", stdin); if (!fin) throw Poco::OpenFileException("Cannot attach stdin to /dev/null"); FILE* fout = freopen("/dev/null", "r+", stdout); if (!fout) throw Poco::OpenFileException("Cannot attach stdout to /dev/null"); FILE* ferr = freopen("/dev/null", "r+", stderr); if (!ferr) throw Poco::OpenFileException("Cannot attach stderr to /dev/null"); } int MyApplication::main(const std::vector<std::string>& args) { if( !m_helpRequested ) { std::cout << "this is main no help
"
; while(1) { std::cout << "hello world" << std::endl; sleep( 3 ); } }else { std::cout <<"this is main call help
"
; } return Application::EXIT_OK; } /// // int main( int argc , char **argv ) { try { MyApplication app; app.init( argc, argv );// 。 app.run( ); }catch( Poco::Exception &e ) { std::cerr << "some error: " << e.what() << std::endl; } }
Execution result: Execute directly without parameters ./a.out Result: defineOptions is called this is initialize this is main no help hello world Execute with parameters, print help information. ./PocoOption -h Result: defineOptions is called usage: -h, --help display help information on command line arguments --daemon Run application as a daemon. this is initialize this is main call help this is uninitialize Execute with parameters, go to the background./PocoOption –daemon Result: defineOptions is called ps aux | grep The Poco process has gone to the background to become a daemon. yuhaiya+ 6442 0.0 0.0 30468 1032 ? Ss 17:19 0:00 ./PocoOption –daemon
Summary: Obviously Application calls the defineOptions() method. Remember to add parameters when executing, then Application will help us call defineOptions( Util::OptionSet& options ) The corresponding function registered when adding the instruction.

좋은 웹페이지 즐겨찾기