【问题标题】:Passing arguments into functions using getopt_long in C在 C 中使用 getopt_long 将参数传递给函数
【发布时间】:2012-07-18 21:24:22
【问题描述】:

我知道这个话题可能已经被写死了,但是我找不到任何让我理解它的东西。我需要在命令行中输入一个值(例如 IP 地址)并将其传递给函数。

下面是我的 getopt_long 函数。

while (1)
{
    static struct option long_options[] =
    {
        /* Options */
    {"send",       no_argument,       0, 's'}, /* args s and r have no function yet */
    {"recieve",    no_argument,       0, 'r'},
    {"file",       required_argument, 0, 'f'}, 
    {"destip",     required_argument, 0, 'i'},
    {"destport",   required_argument, 0, 'p'},
    {"sourceip",   required_argument, 0, 'o'},
    {"sourceport", required_argument, 0, 't'},
    {0, 0, 0, 0}
    };

   int option_index = 0;

   c = getopt_long (argc, argv, "srf:d:i:p:o:t:",
                long_options, &option_index);

              /* Detect the end of the options. */
   if (c == -1)
     break;

   switch (c)
     {
     case 0:
       /* If this option set a flag, do nothing else now. */
       if (long_options[option_index].flag != 0)
         break;
       printf ("option %s", long_options[option_index].name);
       if (optarg)
         printf (" with arg %s", optarg);
       printf ("\n");
       break;

     case 's':
       puts ("option -s\n");
       break;

     case 'r':
       puts ("option -r\n");
       break;

     case 'f':
       printf ("option -f with value `%s'\n", optarg);
       break;

     case 'i':
       printf ("option -i with value `%s'\n", optarg);
       break;

     case 'p':
       printf ("option -p with value `%s'\n", optarg);
       break;

     case 'o': 
       printf ("option -o with value `%s'\n", optarg);
       break;

     case 't': 
       printf ("option -t with value `%s'\n", optarg);
       break;

     case '?':
       /* Error message printed */
       break;

     default:
       abort ();
     }
}

/* Print any remaining command line arguments (not options). */
if (optind < argc)
{
    printf ("non-option ARGV-elements: ");
    while (optind < argc)
    printf ("%s ", argv[optind++]);
    putchar ('\n');
}

这是我需要值的地方(非常标准的 tcp 结构的一部分)

ip->iph_sourceip = inet_addr(arg);

如何正确执行此操作?我进行了相当多的研究,虽然很多都涵盖了类似的主题,但它们似乎并不能很好地解释我的问题。

【问题讨论】:

  • 您似乎拥有所需的一切?在switch 的情况下i 参数ip-&gt;iph_sourceip = inet_addr(optarg);,或者将optarg 存储在另一个变量中以便稍后传递给您的数据包设置?
  • 我明白了……那我真傻,哈哈。但是我必须在 send_tcp 函数中删除ip-&gt;iph_sourceip = inet_addr(optarg);,还是应该是 i 开关盒的副本? @pb2q

标签: c parsing getopt getopt-long


【解决方案1】:

使用getopt 时,您通常会声明与各种开关匹配的变量,以便在参数解析完成后对它们进行操作;一些参数你可以在参数处理过程中立即采取行动。

例如,您可能有一个 address 变量用于存储来自 -i 命令的地址,与 -p 参数类似:

in_addr_t address;
int port;

// ... later in your switch statement:
switch (c)
{
    // ...

   case 'i':
       printf("option -i with value `%s'\n", optarg);
       address = inet_addr(optarg);
       break;
   case 'p':
       printf("option -p with value `%s'\n", optarg);
       // be sure to add handling of bad (non-number) input here
       port = atoi(optarg);
       break;
    // ...
}

// later in your code, e.g. after arg parsing, something like:
send_tcp(address, port);

【讨论】:

  • 好的,这似乎很好地回答了我的问题。谢谢:) 还有一件事。这是我第一次看到in_addr_t。那有什么作用? @pb2q
  • 表示ip地址的抽象类型,由inet_addr函数返回,该函数采用点分字符串表示并将其转换为in_addr_t类型。
  • 通过包含inet.h,您将获得in_addr_t 以及inet_addr 函数。您示例中的iph_sourceip 的类型为in_addr_t
  • 所以说我需要使用 htons 函数更改端口。我需要类似的东西吗? (很抱歉,如果我遇到了一些问题,但是对于一个相当新的业余程序员来说,这已经是相当多的了。)@pb2q 编辑:我已经包含了 。这些能解决问题吗?
  • 是的,对于您需要稍后采取行动的任何参数,请在您通过参数移动时创建一个变量,除非您可以立即对该参数采取行动。因此,在您的情况下,您可能还会有一个端口变量,我将编辑答案。抱歉,不确定您对使用 htons 是什么意思。当您走得更远并需要针对特定​​问题的其他帮助时发布新问题,而不是使用 cmets 在此处提出更多问题。
猜你喜欢
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多