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

发布于 2024-11-15 21:42:40 字数 2347 浏览 1 评论 0原文

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

Here is a small application i coded. Now i wanna make /h as default option, so that when a user runs it he can get a help message. Can anyone help me with this please?

#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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反差帅 2024-11-22 21:42:41

OT:我喜欢 PoCo,以前用过它。这些天我更倾向于 Boost,因为编译器支持变得无处不在。 这个问题是我对侵入性/限制性框架的定义:简单的事情变得很难做。有时违背了优秀程序员的价值

我建议保留一个标志,如下所示:(查看_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)

OT: <rant>I love PoCo, have used it before. These days I gravitate to Boost instead, since compiler support is becoming ubiquitous. This problem is my definition of a intrusive/restrictive framework: simple things become hard to do. Defies the value of a good programmer sometimes.</rant>

I suggest keeping a flag, like so: (look 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文