【问题标题】:How to know if particular optarg option is passed or not in c如何知道在 c 中是否通过了特定的 optarg 选项
【发布时间】:2017-09-18 15:10:22
【问题描述】:

下面的 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");
                          :
                          :
                          :

【问题讨论】:

    标签: c arguments command-line-arguments


    【解决方案1】:

    记住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
    

    【讨论】:

    • if( 0 == strnlen( argv[i], "-d", 2 ) ) ,你能解释一下这行代码吗?
    • @programmer,strlen() 的手册页有很好的解释,但是,为简单起见,将argv[i] 指向的 char 数组的前 2 个字节与字符串“-d”进行比较" ,如果它们匹配,则输入 if() 的正文
    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 2012-01-10
    • 1970-01-01
    相关资源
    最近更新 更多