C/C++实现无序入参的命令解析工具

1 实现思路

基本介绍: 思路来源于atlas,atc(模型转换工具),该工具对命令支持众多,且命令支持盲录入,支持基本的–help命令查看所有命令参数的解释,因此自己仿照实现,已解决工作当中的通用性工具使用不方便、难以产品化的问题。
atc 工具的应用命令如下:

atc --framework=5 --model=./yolov5l_onnx --input_format=NCHW --input_shape="images:1,3,960,960" --output_type=FP32 --output=./yolov5l_onnx --log=error --precision_mode=allow_fp32_to_fp16 --soc_version=Ascend310P3

下图为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::map<std::string, std::string> parseCommand(const std::string& command) {
    std::map<std::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::map<std::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 name
        command += argv[i];
        if (i < argc - 1) { // Add a space between arguments, but not after the last one
            command += " ";
        }
    }
    std::cout << "The combined command is: " << command << std::endl;

    //错误 命令测试 command = "--input_shape=\"images:1,3,960,960\" --output_type=FP32 --output=./yolov5l_onnx --log=error --precision_mode=allow_fp32_to_fp16 --soc_version=Ascend310P3  --framework=5 --model=./yolov5l --input_format=NCHW";
    //正确 测试命令 
    command = "--input_shape=\"images:1,3,960,960\" --output_type=FP32 --output=./yolov5l_onnx --log=error --precision_mode=allow_fp32_to_fp16 --soc_version=Ascend310P3  --framework=5 --model=./yolov5l --input_format=NCHW";

	//解析输入的字符串命令
    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

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值