美文网首页
力卉编程 | 输入参数分类实用函数

力卉编程 | 输入参数分类实用函数

作者: 力卉编程 | 来源:发表于2020-04-04 10:25 被阅读0次

经常出现 -c -p -n 等带参数的命令, Linux用getopt_long可以分解参数,如果是Windows就不能实用。
以下自己写了个参数分析函数,便于大家使用:

struct _optArg {
   char szSec[8];
   int nCount;
   char szArgs[6][64];
   int nType;
};

int args_opt_init(int argc, char * const argv[], struct _optArg *optsOut, int *pnCount)
{
    int i = 0;
    int nSecCount = 1;
    int nCol = 0;
    int mMy = 0;

    if(optsOut == NULL)
    {
        printf("Error\n");
        return 0;
    }
    strcpy(optsOut->szSec, "def");
    for(i = 0; i < argc; i++)
    {
        if(strncmp(argv[i],"-",1) == 0)
        {
            nSecCount++;
            optsOut++;
            strcpy(optsOut->szSec, argv[i]+1);
            //printf("SEC SAVE [%d] [%s]\n", i, optsOut->szSec);
            nCol = 0;
            mMy = 1;
            continue;
        }

        strcpy(optsOut->szArgs[nCol++], argv[i]);
        //printf("VALUE SAVE  [%d][%s][%d]\n", i,argv[i], nCol-1);
        optsOut->nCount++;
    }
    *pnCount = nSecCount;
    return nSecCount;
}
char* args_opt_get(struct _optArg *optsOut, int nOptCnt, const char *psSec, int nIdx)
{
    int i = 0;  
    for(i = 0; i < nOptCnt; i++)
    {
        if(strcmp(optsOut->szSec, psSec) == 0)
        {
            if(nIdx < 0)
            {
                return optsOut->szSec;
            }
            if(optsOut->nCount > nIdx)
            {
                return optsOut->szArgs[nIdx];
            }
            break;
        }
        optsOut++;
    }
    return "";
}
int getopt
int main(int argc, char ** argv)
{
  int nSecCount = 0;
  char *pArg = NULL;
  struct _optArg opts[10] = {0};

  args_opt_init(argc, argv, opts, &nSecCount);  
  
  pArg = args_opt_get(opts, nSecCount, "u", 0);
  if(strcmp(pArg, "") != 0)
  {
    strcpy(stru_ip_info.m_strRemoteIpaddr, pArg);
    pArg = args_opt_get(opts, nSecCount, "u", 1);       
    printf("%s\n", pArg);
    pArg = args_opt_get(opts, nSecCount, "u", 2);
    printf("%s\n", pArg);
    nCmdType = 1;
  }
  return 0;
}

文 | 力卉编程

相关文章

  • 力卉编程 | 输入参数分类实用函数

    经常出现 -c -p -n 等带参数的命令, Linux用getopt_long可以分解参数,如果是Windows...

  • 03-Path

    回顾 • 函数式编程很重要、函数式编程很重要、函数式编程很重要• 要习惯与把函数作为参数输入给另一个函数 以下函数...

  • k-邻近算法

    使用Python实现kNN分类为算法 计算距离函数classify0()如下: 输入参数:用于分类的输入向量inX...

  • python函数式编程

    备注 本篇为慕课网python进阶教程中函数式编程章节的笔记。 简介 函数式编程允许将函数作为输入(参数)输出(返...

  • python笔记 | 简单人脸识别

    整理 | 力卉编程

  • partial函数与total函数区别

    输入 partial函数是部分输入参数; total函数是完全输入参数。

  • 疯狂的Scala缩写和用法

    函数缩写addBy输入一个参数a,输出一个函数Int => Int,这个函数返回了输入参数加上a,由于输入参数只有...

  • iOS开发——bat、头条、爱奇艺iOS面试题总结

    这个栏目将持续更新--请iOS的小伙伴关注 !!! 1.什么是函数式编程? 函数可以接受函数当作输入(参数)和输...

  • C++利用函数指针实现函数式编程的方法

    C++支持函数式编程,虽然使用的较少,下面列出一些用法: 普通函数作为回调普通函数作为输入参数的高阶函数 是最基本...

  • 2018-07-14 函数和参数

    自定义函数 使用def语句自定义函数,其后为函数名(输入参数): 函数体在缩进块中编写。输入参数中,必选参数在前,...

网友评论

      本文标题:力卉编程 | 输入参数分类实用函数

      本文链接:https://www.haomeiwen.com/subject/lmedphtx.html