boost 程序选项中的布尔选项
我正在使用 boost 程序选项从命令行参数获取布尔值。我希望将我的参数指定为“Y”、“是”、“N”、“否”。
实际上,我的代码使用临时字符串来完成此操作,该字符串
- 将由
boost program options
- 检查进行解析 “Y”、“是”、“N”或“否”
- 分配给布尔变量成员的
最重要的是,我还使用另一个临时字符串来获取默认值,
因为我尝试了下面的代码。没用
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
- will be parsed by
boost program options
- checked against "Y", "Yes", "N" or "No"
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将以一种或另一种方式解析字符串。有几个选项,主要取决于您查询该值的频率。这是与我最近使用的类似的示例; CopyConstructable 和Assignable 因此它可以很好地与STL 配合使用。我想我需要做一些额外的事情才能让它与program_options一起工作,但你明白了要点:
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: