1

我正在编写一个示例来使用 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库可以吗?

4

1 回答 1

0

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

于 2013-04-29T11:34:32.457 回答