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

标签 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");
                          :
                          :
                          :

最佳答案

记住 main() 的参数是:

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

因此检查 argc 是否大于 1 表明某些参数已传递。

argv[] 是指向字符字符串的指针列表,因此可以循环该列表

#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

关于c - 如何知道 c 中是否传递了特定 optarg 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43545689/

相关文章:

c - 用 pthreads 杀死线程 - C

java - 将文件拖放到 .jar 并将文件名传递给 main()

c - 在 C 程序中传递数组作为参数并打印数组项

javascript - 如何将命令行参数传递给 Node.js 程序?

bash - 将 $@ 传递给 shell 脚本中的函数

c - 检查 argv 中的符号时出现段错误

c - 内核 block 设备 - 使用自旋锁陷入死锁

c - C中的结构误解

java - 调用方法时将数组初始化为参数

c - 什么是 char*argv[ ] 以及它与 char **argv 有何相似之处