c++ - boost program_options : Read in 3d Vector as Command Line Parameter

标签 c++ boost

我有

boost::program_options::options_description desc("Required options");                                             
desc.add_options()(                                                                                                                
  detail::enu_orig_lat_opt,                                               
  boost::program_options::value<float>(),                                 
  "Origin of enu latitudinal coordinates."                                                                        
)(                                                                                                                
  detail::enu_orig_lng_opt,                                               
  boost::program_options::value<float>(),                                 
  "Origin of enu longitudinal coordinates."                                                                       
)(                                                                                                                
  detail::enu_orig_alt_opt,                                               
  boost::program_options::value<float>(),                                 
  "Origin of enu altitude coordinates."                                                                       
);

我可以使用具有 multitoken 的单个选项值并将其设为 std::vector<float> 类型或者我可以使用具有三个字段的结构并使用该类型的值。到目前为止,我一直在为这两种选择而苦苦挣扎,并且无法让它们发挥作用。上面的选项(见代码)的问题是,如果用户只指定一个值,我必须添加代码来验证其他值的存在。

所以我真的有两个问题。第一,有没有人有读取 3d vector 的示例代码,包括负 float 作为命令行选项?或者两个(如果不是的话)如果设置了任何一个或多个选项,确保三个相关选项全部指定的理想方法是什么?

最佳答案

关于验证选项组的主题,请参阅此相关答案:c++/boost program_options one option disable other

最简单的方法是使您的 vector 类型可流化:

Live On Wandbox

#include <iosfwd>
#include <istream>

template <typename T> struct LLA { 
    T lat, lon, alt;
    friend std::istream& operator>>(std::istream& is, LLA& lla) {
        char ch;
        if (is >> lla.lat >> ch && ch == ';'
         && is >> lla.lon >> ch && ch == ';'
         && is >> lla.alt)
            return is;
        is.setstate(std::ios::failbit);
        return is;
    }
};

using Origin = LLA<float>;

#include <boost/program_options.hpp>
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv) {
    Origin origin;

    po::options_description desc;
    desc.add_options()
        ("origin,o", po::value(&origin), "origin of enu (lat;lon;alt)")
        ;

    auto parsed = po::parse_command_line(argc, argv, desc, po::command_line_style::default_style);
    po::variables_map vm;
    po::store(parsed, vm);
    po::notify(vm);

    std::cout << "Origin: lat:" << origin.lat << " lon:" << origin.lon << " alt:" << origin.alt << "\n";
}

将打印例如

Origin: lat:3 lon:-5 alt:7

当用例如调用时

./test --origin='3;-5;7'
./test --origin '3;-5;7'
./test -0 '3;-5;7'

关于c++ - boost program_options : Read in 3d Vector as Command Line Parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47115845/

相关文章:

c++ - 如何计算和使用 cvMat 平均值

c++ - 朗伯阴影和阴影(光线追踪)的问题

c++ - 无法使用 Microsoft CodeGen 使用 Visual Studio 2015 和 Clang 3.7 构建 Google Test

c++ - C++ 中的 Delphi Format() 模拟

c++ - VC++ 优化打破了与 NaN 的比较?

c++ - 将 kde 应用程序移植到 windows

c++ - boost multi_index - 如果元素类型为仅移动,如何将元素从一个容器添加到另一个容器?

c++ - Boost线程 - 通过引用传递参数

c++ - 如何在托管共享内存段中创建同步机制?

c++ - 使用boost factory按需生产产品C++