如何知道c中是否传递了特定的optarg选项

时间:2017-04-21 14:48:01

标签: c arguments command-line-arguments

以下C代码工作正常,但我怎么知道用户是否已通过选项-d?

从下面的代码我可以知道,只有当用户使用optarg -d选项

Eg : ./application -d 

只有这个代码有效!

如果用户输入

       ./application -a 

如果选项-d被传递,我就无法知道。

同样选项-a应该采用多个值,但是下面的代码仅适用于单个值

    eg : ./application -a knn , lr , ln

如何让这段代码接受同一选项的多个值?

以下代码适用于单值

   eg :  ./application  -a knn




       int main(int argc, char *argv[]) {
        int opt= 0;
        int start = -1, end = -1;
        char *alg,*dir,*graph;
       //Specifying the expected options
       //The two options s and e expect numbers as argument
        static struct option long_options[] = {
        {"start",no_argument,0,'s' },
        {"end",no_argument,0,'e' },
        {"algorithm",no_argument, 0,'a' },
        {"directory",required_argument, 0,'d' },
        {"graph",required_argument,0,'g' },
        {0,0,0,0}
       };

        int long_index =0;
        int i=0,j=0;
        size_t size = 1;
        while ((opt = getopt_long(argc, argv,"s:e:a:d:h:g:",
               long_options, &long_index )) != -1) {
             switch (opt) {
             case 'd' :
                    dir = optarg;

                      if (optarg == NULL)
                         printf("d option is must");
                      else
                         {
                         printf("option -d value is must\n");
                         usage();
                         exit(EXIT_FAILURE);
                         }
                      break;
             case '?':
                     if (optopt == ('d' || 'a' || 'g' || 's' || 'e'))
                       fprintf (stderr, "Option -%c  reqd", optopt);
                       usage();
                       exit(EXIT_FAILURE);
             case 'a' : 
                       alg = optarg;
                        if(alg == "lr" || alg == "knn" || alg == "cart")
                          {
                        printf("you entered option -a  \"%s\"\n",optarg);
                          }
                        else
                          {
                         printf("Wrong option -a value is passed\n");
                          :
                          :
                          :

1 个答案:

答案 0 :(得分:0)

请记住main()的参数是:

int main( int argc, char *argv[] )

因此检查argc大于1表示已经传递了一些参数。

argv[]是一个指向char字符串的指针列表,因此可以遍历该列表

#include <stdio.h>   // fprintf()
#include <stdlib.h>  // exit(), EXIT_FAILURE

int main( int argc, char *argv[] )
{
    if( 1 >= argc )
    {
        fprintf( stderr, "USAGE: %s <-d>\n", argv[0] );
        exit( EXIT_FAILURE ); 
    }

    int found = 0;
    for( int i = 1; i<=argc; i++ )
    {
        if( 0 == strnlen( argv[i], "-d", 2 ) )
        {
            printf( "-d parameter entered\n" );
            found = 1;
            break;
        }
    }

    if( 0 == found )
    {
        fprintf( stderr, "some parameters entered but none are [-d\]n" );
        exit( EXIT_FAILURE );
    }

    // implied else, parameter -d entered by user

    ....
} // end function: main