如何使用提升解析文件路径

How to parse filepath with boost?

本文关键字:文件 路径 何使用      更新时间:2023-10-16

我使用 boost::p rogram_options 和 boost::filesystem。我需要解析命令行参数。

 int main(int argc, char** argv)
 {
   //command line arguments
     variables_map vm;
     try{
    options_description desc{ "Program usage " };
    desc.add_options()
        ("help,h", "Show help")
        ("input,I", value<std::string>(), "Input directory")
        ("output,O", value<std::string>(), "Output  file ");

    store(parse_command_line(argc, argv, desc), vm);
    notify(vm);

如何将参数用作文件路径?

path file_path = vm.count["input"];
path file_path = vm.count["input"].as<std::string>();

不工作

您可以使用--input分配命令行中提供的路径的值,以便file_path

if (vm.count("input")) { 
   std::string my_path = vm["input"].as<std::string>();
   boost::filesystem::path file_path (my_path);
   std::cout << "The input path is: " << file_path << std::endl;
}

希望这有帮助。