使用 boost::program_options

发布于 2024-10-09 02:07:38 字数 345 浏览 0 评论 0 原文

在我的程序中,我有一个对的列表 - 名称和大小。

我想使用 boost::program_options 从命令行界面构建此列表。

它应该看起来像这样:

myProg --value("约翰",10) --value("史蒂夫",14) --value("玛吉",28)

我还需要按顺序排列 - Steve 将在 John 之后和 Marge 之前列表。这可以通过 boost::program_options 实现吗?

此 CLI 语法只是获取列表的一个想法。如果你有更好的,请告诉。

In my program I have a list of pairs - name and size.

I want to build this list from the command line interface using boost::program_options.

It should look something like this:

myProg --value("John",10) --value("Steve",14) --value("Marge",28)

I also need this to be in order - Steve will be after John and before Marge on the list. Is that possible with boost::program_options?

This CLI syntax is just an idea to get the list. If you have a better one, do tell.

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

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

发布评论

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

评论(2

醉态萌生 2024-10-16 02:07:38

您只需定义您的选项

("value", value<vector<YourPairType>>()->composing(), "description")

和适当的选项

istream& operator >> (istream& in, YourPairType& pr) { /* ... */ }

,以您的 ("John",10) 格式从 in 读取单个 YourPairType 。解析的选项将按照它们在命令行中出现的顺序存储。

如果您使用自定义验证器<,您可以获得更大的灵活性/a> 而不是运算符>>

You just define your option

("value", value<vector<YourPairType>>()->composing(), "description")

and an appropriate

istream& operator >> (istream& in, YourPairType& pr) { /* ... */ }

that reads a single YourPairType from in in your ("John",10) format. Parsed options will be stored in the order they appear in the command line.

You can achieve greater flexibility if you use custom validators instead of operator >>.

茶色山野 2024-10-16 02:07:38

每行具有一对值的文件可以是一个选项。该文件可以是纯 ascii 文本文件,也可以使用 xml 文件 - 请参阅 增强序列化

A file with each line having one pair of values can be one option. The file could be a plain ascii text file or you can go for xml files too - refer to boost serialization.

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