【问题标题】:How to accept a char or string as input如何接受字符或字符串作为输入
【发布时间】:2017-05-05 05:14:51
【问题描述】:

在阅读了一个关于 argp 的著名 pdf 之后,我想用它做点什么,但我遇到了一个问题,在这个例子中:

static int parse_opt (int key, char *arg, struct argp_state *state)
{
    switch (key)
    {
        case 'd':
        {
            unsigned int i;
            for (i = 0; i < atoi (arg); i++)
                printf (".");
            printf ("\n");
            break;
        }
    }
    return 0;
}

int main (int argc, char **argv)
{
    struct argp_option options[] = 
    {
        { "dot", 'd', "NUM", 0, "Show some dots on the screen"},
        { 0 }
    };
    struct argp argp = { options, parse_opt, 0, 0 };
    return argp_parse (&argp, argc, argv, 0, 0, 0);
}

-d 接受一个 int 类型的参数,但是如果我想得到一个 char 或 char 数组作为参数呢? pdf 不包括文档。

我开始学习 C,我对它有基本的了解,我对其他语言更熟悉,所以要了解更多关于它的信息,我想存档,但我不明白我怎样才能做到接受一个字符数组。

将 arg 与 char 进行比较时不起作用的代码:

static int parse_opt(int key, char *arg, struct argp_state *state)
{       
    switch(key)
    {
        case 'e':
        {
            //Here I want to check if "TOPIC" has something, in this case, a char array
            //then based on that, do something.
            if (0 == strcmp(arg, 'e'))
            {
                printf("Worked");
            }
        }
    }

    return 0;
}//End of parse_opt

int main(int argc, char **argv)
{
    struct argp_option options[] = 
    {
        {"example", 'e', "TOPIC", 0, "Shows examples about a mathematical topic"},
        {0}
    };

    struct argp argp = {options, parse_opt};

    return argp_parse (&argp, argc, argv, 0, 0, 0); 
}//End of main

提前致谢。

【问题讨论】:

  • 介意澄清The -d accepts an argument of int type, ?
  • " -d 接受 int 类型的参数"。这不是真的。 arg 始终是一个字符串。是您的代码将其更改为int。如果您想将其存储为字符串,则不要调用atoi。
  • @sjsam 当然,有一个名为 i 的无符号整数,它与 -d 的参数“NUM”有关系,“NUM”参数只接受整数,我希望它接受 char 或 char数组,我希望我清楚
  • @kaylum 上面的代码不是我的实际项目,我是从 pdf 中得到的,在我的代码中我试图将 arg 与 char 进行比较,但它没有用,因为它说它需要 const char 但参数是 int 类型,如果我是一个完全的菜鸟,请耐心等待。
  • 好吧,然后显示您尝试但不起作用的代码。看不到代码就无法指出错误。

标签: c argp


【解决方案1】:

https://www.gnu.org/software/libc/manual/html_node/Argp.html

#include <stdio.h>
#include <argp.h>
#include <string.h>

static int parse_opt(int key, char *arg, struct argp_state *state) {
  (void)state; // We don't use state
  switch (key) {
  case 'c': {
    if (strlen(arg) == 1) { // we only want one char
      char c = *arg;        // or arg[0]
      printf("my super char %c !!!\n", c);
    } else {
      return 1;
    }
  }
  }
  return 0;
}

int main(int argc, char **argv) {
  struct argp_option const options[] = {
      {"char", 'c', "c", 0, "a super char", 0}, {0}};
  struct argp const argp = {options, &parse_opt, NULL, NULL, NULL, NULL, NULL};
  argp_parse(&argp, argc, argv, 0, NULL, NULL);
}

【讨论】:

  • 我有点明白了,但是当它说 NULL 时,你能解释一下 struct argp 中的部分吗,文档中的示例没有显示类似的内容,或者至少在我看到的示例中没有显示。
  • 这只是为了避免警告,因为struct argp 有很多字段,doc。要知道NULL 是什么,请阅读this
猜你喜欢
  • 2019-12-14
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-28
相关资源
最近更新 更多