Parsing command line options with multiple arguments [getopt?]

前端 未结 3 2031
轮回少年
轮回少年 2021-01-31 11:36

I need my program to get several arguments from command line, the syntax is as follows:

getpwd -l user1 user2 ... -L -X -S...

So, I need to get

3条回答
  •  -上瘾入骨i
    2021-01-31 11:51

    The following function will generate an argc, argv pair for a single option within an argument list:

    void GetArgsList (int argc, char *argv[], int* o_argc, char*** o_argv)
    {
        char** ret = NULL;
        int i;
        int count = 0;
    
        for (i = optind - 1; i < argc ; ++i)
        {
            if (argv[i][0]=='-')
            {
                break;
            }
            else
            {
                if (NULL == ret)
                {
                    ret = &argv[i];
                }
                count++;
            }
        }
    
        optind = i - 1;
    
        if (o_argc)
        {
            *o_argc = count;
        }
    
        *o_argv = ret;
    }
    

提交回复
热议问题