网站效果演示,wordpress全局密码,注册城乡规划师考试时间2024,林州网站建设服务C/C实现无序入参的命令解析工具 1 实现思路2 主要功能3 效果展示3.1 直接运行代码图3.2help命令执行效果图3.3命令行执行命令解析效果图 4 代码实现5 代码下载 1 实现思路
基本介绍#xff1a; 思路来源于atlas,atc(模型转换工具)#xff0c;该工具对命令支持众多#xff0… C/C实现无序入参的命令解析工具 1 实现思路2 主要功能3 效果展示3.1 直接运行代码图3.2help命令执行效果图3.3命令行执行命令解析效果图 4 代码实现5 代码下载 1 实现思路
基本介绍 思路来源于atlas,atc(模型转换工具)该工具对命令支持众多且命令支持盲录入支持基本的–help命令查看所有命令参数的解释因此自己仿照实现已解决工作当中的通用性工具使用不方便、难以产品化的问题。 atc 工具的应用命令如下
atc --framework5 --model./yolov5l_onnx --input_formatNCHW --input_shapeimages:1,3,960,960 --output_typeFP32 --output./yolov5l_onnx --logerror --precision_modeallow_fp32_to_fp16 --soc_versionAscend310P3下图为atc工具help命令效果
2 主要功能
支持跨平台代码简单轻松实现移植到linux支持命令的无序输入支持 –help 命令查看 ./cmdParser --help支持错误命令检查告警支持直接编辑完成命令扩展
3 效果展示
3.1 直接运行代码图 3.2help命令执行效果图 3.3命令行执行命令解析效果图 4 代码实现
#include iostream
#include map
#include vector
#include string
#include sstream
#include stdio.h//命令解析方法
std::mapstd::string, std::string parseCommand(const std::string command) {std::mapstd::string, std::string parameters;std::istringstream iss(command);std::string token;while (std::getline(iss, token, )) {size_t pos token.find();if (pos ! std::string::npos) {std::string key token.substr(0, pos);std::string value token.substr(pos 1);parameters[key] value;}}return parameters;
}//命令库直接可以复制编辑
std::mapstd::string, std::string cmdlib {{--framework,number Specify the framework version},{--model,path Specify the model file path},{--input_format,format Specify the input format},{--input_shape,shape Specify the input shape},{--output_type,type Specify the output type},{--output,path Specify the output file path},{--log,level Specify the log level},{--precision_mode,mode Specify the precision mode},{--soc_version,version Specify the SoC version},{--help,Display this help and exit}
};//help展示所有命令
void displayHelp()
{std::cout Usage: command [options]\n Options:\n;for (const auto p : cmdlib){//命令对齐排版printf(%-30s%-50s\n, p.first.c_str(), p.second.c_str());}
}int main(int argc, char* argv[]) {//解析help命令if (argc 2 std::string(argv[1]).find(--help) ! std::string::npos) {displayHelp();return 0;}//将所有输入的命令都连成一个字符串std::string command;for (int i 1; i argc; i) { // Start from 1 to skip the program namecommand argv[i];if (i argc - 1) { // Add a space between arguments, but not after the last onecommand ;}}std::cout The combined command is: command std::endl;//错误 命令测试 command --input_shape\images:1,3,960,960\ --output_typeFP32 --output./yolov5l_onnx --logerror --precision_modeallow_fp32_to_fp16 --soc_versionAscend310P3 --framework5 --model./yolov5l --input_formatNCHW;//正确 测试命令 command --input_shape\images:1,3,960,960\ --output_typeFP32 --output./yolov5l_onnx --logerror --precision_modeallow_fp32_to_fp16 --soc_versionAscend310P3 --framework5 --model./yolov5l --input_formatNCHW;//解析输入的字符串命令auto parameters parseCommand(command);if (!parameters.empty()){for (const auto p : parameters){//检查输入的命令是否存在非法命令auto itor cmdlib.find(p.first);if (itor cmdlib.end()){std::cerr Error: Please Check Invalid command: p.first std::endl;return 0;}}//打印解析输入命令得到的数据结构for (const auto p : parameters){std::cout p.first p.second std::endl;}}return 0;
}5 代码下载
cmdParser