CLI11解析命令行

cli11解析命令行,可以直接使用 CLI11_INLINE void CLI::App::parse ( std::vector< std::string > & args )接口, 接受的字符串数组需要逆序,同时去掉第一个bin路径参数。

也可以使用CLI11_INLINE void App::parse(std::string commandline, bool program_name_included) 接口,直接解析命令行参数。

从字符串中解析tokens

vector<string_view> GetTokens(string_view input) {
  vector<string_view> tokens;
  const regex arg_regex(R"((?:[^\s"]+|"[^"]*")+)");
  cmatch matches;

  while (regex_search(input.data(), input.data() + input.size(), matches, arg_regex)) {
    string_view match(matches[0].first, matches[0].length());
    if (match.front() == '"' && match.back() == '"') {
      match.remove_prefix(1);
      match.remove_suffix(1);
    }
    tokens.push_back(match);
    input.remove_prefix(matches.position() + matches.length());
  }
  return tokens;
}

posted @ 2025-11-07 14:13  Xdesigner  阅读(0)  评论(0)    收藏  举报