boost 程序选项中的布尔选项

发布于 2024-10-19 00:43:53 字数 507 浏览 2 评论 0原文

我正在使用 boost 程序选项从命令行参数获取布尔值。我希望将我的参数指定为“Y”、“是”、“N”、“否”。

实际上,我的代码使用临时字符串来完成此操作,该字符串

  1. 将由 boost program options
  2. 检查进行解析 “Y”、“是”、“N”或“否”
  3. 分配给布尔变量成员的

最重要的是,我还使用另一个临时字符串来获取默认值,

因为我尝试了下面的代码。没用

      namespace pod = boost::program_options;

      ("Section.Flag", 
           pod::value<bool>(&myFlag_bool)->default_value( false ), 
           "description")

你知道是否可以使用升压程序选项比我用来实现这一目标更好吗?

I am using boost program options to get boolean values from command line argument. I would like my argument to be specified as "Y", Yes", "N", "No".

Actually my code did it using a temporary string that

  1. will be parsed by boost program options
  2. checked against "Y", "Yes", "N" or "No"
  3. assigned to the boolean variable member.

On top of that I am also using another temp string getting the default value.

I did all that work since I tried thee code below that didn't work

      namespace pod = boost::program_options;

      ("Section.Flag", 
           pod::value<bool>(&myFlag_bool)->default_value( false ), 
           "description")

Do you know whether boost program options can be used some better then the one I use to achieve that?

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

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

发布评论

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

评论(1

诗酒趁年少 2024-10-26 00:43:53

您将以一种或另一种方式解析字符串。有几个选项,主要取决于您查询该值的频率。这是与我最近使用的类似的示例; CopyConstructable 和Assignable 因此它可以很好地与STL 配合使用。我想我需要做一些额外的事情才能让它与program_options一起工作,但你明白了要点:

#include <boost/algorithm/string.hpp>

class BooleanVar
{
public:
    BooleanVar(const string& str)
        : value_(BooleanVar::FromString(str))
    {
    };

    BooleanVar(bool value)
        : value_(value)
    {
    };

    BooleanVar(const BooleanVar& booleanVar)
        : value_(booleanVar)
    {
    };

    operator bool()
    {
        return value_;
    };

    static bool FromString(const string& str)
    {
        if (str.empty()) {
            return false;
        }

        // obviously you could use stricmp or strcasecmp(POSIX) etc if you do not use boost
        // or even a heavier solution using iostreams and std::boolalpha etc
        if (
            str == "1" 
            || boost::iequals(str, "y")
            || boost::iequals(str, "yes")
            || boost::iequals(str, "true")
        )
        {
            return true;
        }

        return false;
    };

protected:
    bool value_;
};

You are going to be parsing a string one way or another. There are a couple options, mostly depending on how often you are going to be querying this value. Here is an example of something similar to what I recently used; CopyConstructable and Assignable so it works well with STL. I think I needed to do a few extra things to get it to work with program_options, but you get the gist:

#include <boost/algorithm/string.hpp>

class BooleanVar
{
public:
    BooleanVar(const string& str)
        : value_(BooleanVar::FromString(str))
    {
    };

    BooleanVar(bool value)
        : value_(value)
    {
    };

    BooleanVar(const BooleanVar& booleanVar)
        : value_(booleanVar)
    {
    };

    operator bool()
    {
        return value_;
    };

    static bool FromString(const string& str)
    {
        if (str.empty()) {
            return false;
        }

        // obviously you could use stricmp or strcasecmp(POSIX) etc if you do not use boost
        // or even a heavier solution using iostreams and std::boolalpha etc
        if (
            str == "1" 
            || boost::iequals(str, "y")
            || boost::iequals(str, "yes")
            || boost::iequals(str, "true")
        )
        {
            return true;
        }

        return false;
    };

protected:
    bool value_;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文