关于C#:如何将getopt()与非选项参数一起使用

How do you use getopt() with non option arguments

我正在尝试将getopt()用于需要" e"或" d"选项来选择加密或解密,然后选择要用于其中任一密钥的程序。 我的问题是不确定如何使用getopt()处理密钥。 在这一点上,我已经阅读了很多有关getopt()的内容以及其他许多文章。 我目前收到浮点错误和核心转储,并收到警告消息:

cypher.c:在" main"函数中:
cypher.c:14:3:警告:从不兼容的指针类型传递" getopt"的参数2 [默认启用]
/usr/include/getopt.h:152:12:注意:预期为'char * const *',但参数的类型为'char *'
cypher.c:28:13:警告:赋值使指针从整数变为整数而不进行强制转换[默认启用]

以下是任何帮助的实际代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

  int e,x;
  int i=0;      
  int c=fgetc(stdin);
  //  int n=strlen(key);
  int encrypt;

  while((x = getopt (argc, argv,"ed")) != -1){
    switch (x){
    case 'e':
      encrypt=1;
      break;
    case 'd':
      encrypt=0;
      break;
    default:
      fputs("you broke it\
"
,stderr);
      exit(1);
    }
  }
    char key[100];
    key[100]= argv[optind++];
    int n = strlen(key);

    if(encrypt == 1){
      while(c != EOF){
         c= fgetc(stdin);
         e=(c - 32 + key[i % n]) % 95 +32;
         fputc( e, stdout);
         i++;
      }
    }
    else{
      while( e != EOF){
         c = fgetc(stdin);
         c=(e - 32 -key[i % n] +3 * 95) % 95 +32;
         fputc(c, stdout);
         i++;
      }
    }
  exit (0);
 }


通常,您要将选项处理分为两个步骤:

  • 收集所有选项并对其进行任何预处理(例如检查其有效性),并根据需要将其存储在变量或结构中。
  • 实际上会根据遇到的所有选项采取措施。
  • 因此,基本上,您可能需要设置一个全局变量(例如opt_mode = ENCRYPTopt_mode = DECRYPT或类似的东西),并根据需要存储密钥。然后,在完成所有选项处理之后,实际上基于opt_mode变量进行加密或解密。


    getopt(3)有一个很好的例子:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>

    int
    main(int argc, char *argv[])
    {
        int flags, opt;
        int nsecs, tfnd;

       nsecs = 0;
        tfnd = 0;
        flags = 0;
        while ((opt = getopt(argc, argv,"nt:")) != -1) {
            switch (opt) {
            case 'n':
                flags = 1;
                break;
            case 't':
                nsecs = atoi(optarg);
                tfnd = 1;
                break;
            default: /* '?' */
                fprintf(stderr,"Usage: %s [-t nsecs] [-n] name\
    "
    ,
                        argv[0]);
                exit(EXIT_FAILURE);
            }
        }

       printf("flags=%d; tfnd=%d; optind=%d\
    "
    , flags, tfnd, optind);

       if (optind >= argc) {
            fprintf(stderr,"Expected argument after options\
    "
    );
            exit(EXIT_FAILURE);
        }

       printf("name argument = %s\
    "
    , argv[optind]);

       /* Other code omitted */

       exit(EXIT_SUCCESS);
    }

    大多数新手Linux甚至都不了解man,Windows中没有man
    另外,该软件包可能未安装在Sysadmin中。如果可以在计算机上安装软件包,请安装软件包:

    1
    sudo apt get install manpages-dev # on debian based systems

    这是一个有用的资源,您可以找到所有可能的手册页的列表:

    1
     dpkg -L manpages-dev

    这是您要寻找的地方:

    1
    2
    3
    4
    $ dpkg -L manpages-dev| grep getop
    /usr/share/man/man3/getopt.3.gz
    /usr/share/man/man3/getopt_long_only.3.gz
    /usr/share/man/man3/getopt_long.3.gz

    这是一个很好的示例,除了手册页(通常很简洁)之外,这些文本还带有清晰的文字...

    http://linuxprograms.wordpress.com/2012/06/22/c-getopt-example/