Cmake,Boost解析命令行

时间:2016-11-08 10:08:42

标签: c++ visual-c++ boost

我有问题。我正在使用Cmake并使用VS 2015进行提升。

我打算使用cgal和assimp开发一个工具。将文件格式转换为我自己的格式。但是,我遇到了一个问题,可能很简单,但看不到它。

当我按F5时,我收到的调试错误就像我附加的两张图片一样。我还附上了我的代码。图片在这里:

enter image description here

enter image description here

#include <boost/asio.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/program_options.hpp>
#include <boost/system/error_code.hpp>
#include <iostream>
#include <sstream>
#include <string>

struct CMDOPTIONS {
    std::string input, output, directory;
    int points;
};

namespace po = boost::program_options;

bool process_command_line(int argc, char **argv, CMDOPTIONS &cmdoptions1) {

    if (argv == nullptr) {
        return false;
    }

    try {
        po::options_description desc("Program Usage");
        po::options_description desc ("Program Usage");
        desc.add_options()
            ("halp, h", "Halp!")
            ("input, i", po::value<std::string>(&cmdoptions1.input), "Specify Input")
            ("output, o", po::value<std::string>(&cmdoptions1.output) ,"Specify Output")
            ("points, p", po::value<int>(&cmdoptions1.points),  "% of reduced points")
            ("directory, d", po::value<std::string>(&cmdoptions1.directory)->required(), "set the directory");

        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, desc), vm);
        po::notify(vm);

        if (vm.count("help")) {
            std::cout << desc << std::endl;
            return false;
        }

        // trolololol XD
        if (vm.count("input")) {
            &cmdoptions1.input;
        }

        if (vm.count("output")) {
            &cmdoptions1.output;
        }

        if (vm.count("points")) {
            &cmdoptions1.points;
        }

        if (vm.count("directory")) {
            &cmdoptions1.directory;
        }

    } catch (std::exception &e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return false;
    } catch (int e) {
        std::cerr << "Unknown error!" << e << std::endl;
        return false;
    }

    return true;
}

int main(int argc, char **argv, CMDOPTIONS &cmdoptions1) {

    bool result = process_command_line(argc, argv, cmdoptions1);
    if (!result)
        return -1;

    std::cout << "input" << &cmdoptions1.input << std::endl;
    std::cout << "output" << &cmdoptions1.output << std::endl;
    std::cout << "points" << &cmdoptions1.points << std::endl;
    std::cout << "directory\t" << &cmdoptions1.directory << std::endl;
}

1 个答案:

答案 0 :(得分:2)

您的cmdoptions1未初始化。

事实上,你的main不应该编译(那是什么类型的签名?)。

此外,您还要打印指向配置值的指针。

第三,当您输入trololol XD时,编译器无法获得您的意图。

<强> Live On Coliru

#include <boost/asio.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/program_options.hpp>
#include <boost/system/error_code.hpp>
#include <iostream>
#include <sstream>
#include <string>

struct CMDOPTIONS {
    std::string input, output, directory;
    int points;
};

namespace po = boost::program_options;

bool process_command_line(int argc, char **argv, CMDOPTIONS &cmdoptions1) {

    if (argv == nullptr) {
        return false;
    }

    try {
        po::options_description desc("Program Usage");
        desc.add_options()
            ("halp, h", "Halp!")
            ("input, i", po::value<std::string>(&cmdoptions1.input), "Specify Input")
            ("output, o", po::value<std::string>(&cmdoptions1.output) ,"Specify Output")
            ("points, p", po::value<int>(&cmdoptions1.points),  "% of reduced points")
            ("directory, d", po::value<std::string>(&cmdoptions1.directory)->required(), "set the directory");

        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, desc), vm);
        po::notify(vm);

        if (vm.count("help")) {
            std::cout << desc << std::endl;
            return false;
        }

    } catch (std::exception &e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return false;
    } catch (int e) {
        std::cerr << "Unknown error!" << e << std::endl;
        return false;
    }

    return true;
}

int main(int argc, char **argv) {

    CMDOPTIONS cmdoptions1;
    bool result = process_command_line(argc, argv, cmdoptions1);
    if (!result)
        return -1;

    std::cout << "input"       << cmdoptions1.input     << std::endl;
    std::cout << "output"      << cmdoptions1.output    << std::endl;
    std::cout << "points"      << cmdoptions1.points    << std::endl;
    std::cout << "directory\t" << cmdoptions1.directory << std::endl;
}