如何为应用程序设置默认选项

How to set a default option for an application?

本文关键字:默认 选项 设置 应用程序      更新时间:2023-10-16

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