c++命令行参数解析实现

结果展示:
在这里插入图片描述
在这里插入图片描述

/**
 *
 * 程序功能:解析命令行参数
 *      获取命令行参数,并执行对应功能。
 * 该程序使用方法:
 * 1、编译该代码,会生成一个main可执行程序
 * 2、在终端执行:
 *      ./main -h 可获得帮助信息
 *      ./main -v 获取版本信息
 * 3、带其他参数会出现使用提示信息
 * 
 *  * switch语句使用方法:
 *      switch(整型/枚举类型)
 *      {
 *          case 常量 :
 *              语句
 *              break;//可选
 *          default :
 *              语句
 *      }
 * 
 * **/

#include<iostream>
using namespace std;

void myprint(string str);
enum msg{help,version,error};

int main(int argc, char** argv)
{
    //cout<<"argc = "<<argc<<endl;
    for(int i =0 ; i<argc;i++)
    {
       // cout<<"argv"<<i+1<<"= "<<argv[i]<<endl;
        if(i!=0) myprint(argv[i]);
    }
}

void myprint(string str)
{
    msg temp=error;
    if(str=="-h"||str=="-H") temp=help;
    if(str=="-v"||str=="-V") temp=version;
    switch(temp)
    {
        case help :
            cout<<"usage:\n -h   : use help \n -v   : print the software version"<<endl;//这里可以写成一个函数
            break;
        case version :
            cout<<"version : 1.0.1"<<endl;
            break;
        default:
            cout<<"command "<<str<<" not found, you can use -h or -H for hlep."<<endl;

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值