我可以使用popt库一次读取两个值的选项吗?

时间:2013-04-26 11:30:17

标签: c++ linux

我正在编写一个使用popt库读取参数的示例。我的代码如下。

enum
{
   INPUT_NAME=1,
   SYMBOL
};
int main(int argc, char **argv)
{
    char filename[ 128 ], symbol[32];
    memset(filename, 0x0, 128);
    memset(symbol, 0x0, 32);

    struct poptOption opttable[] =
    {
        { "file", 'f', POPT_ARG_STRING, filename, INPUT_NAME, "filenames to read", "list of files we need to read" },
        { "symbol", 'r', POPT_ARG_STRING, symbol, SYMBOL, "symbol to view", NULL },
        POPT_AUTOHELP
        POPT_TABLEEND
    };
    poptContext options_socket = poptGetContext( NULL, argc, ( const char **)argv, opttable, 0 );
    int optionvalue(0);
    while( optionvalue > -1 )
    {
        optionvalue = poptGetNextOpt( options_socket );
        if(optionvalue == INPUT_NAME)
        {
           strcpy(filename, poptGetOptArg( options_socket ));
           printf("filename you are giving as input is :%s\n", filename);
        }
        else if( optionvalue == SYMBOL)
        {
           strcpy(symbol, poptGetOptArg( options_socket ));
           printf("symbol you are giving as input is :%s\n", symbol);
        }
    }
    return 0;
}

通过使用此代码,我可以读取每个选项的单个值。有没有办法使用单一选项获取值列表?

我目前的计划是这样的。

sample run to my program: ./popt -f file1.txt file2.txt -r symbol1
OUTPUT:
filename you are giving as input is :file1.txt
symbol you are giving as input is :symbol1
DESIRED OUTPUT:
filename you are giving as input is :file1.txt
filename you are giving as input is :file2.txt
symbol you are giving as input is :symbol1

是否可以使用popt库?

1 个答案:

答案 0 :(得分:0)

我认为它可以通过使用poptGetArgs函数来实现。