c - 使用 argp.h 解析为整数

标签 c arguments parameter-passing

我希望能够在我的 C 程序中接收程序参数和选项。这些选项应被视为 floatint。由于某种原因,我找不到关于 argp.h 的好文章、教程、文档。 。我从 examples 开始实现。在 GNU Libc 手册上,但不幸的是,它并没有让我找到解决方案。

这是我尝试解决问题的方法(可以编译示例,包括每个必要的行):

#include <stdlib.h>
#include <argp.h>

static char doc[] = "Doc";
static char args_doc[] = "ARG1";

static struct argp_option options[] = {
  {"bool", 'b', 0, 0, "Simple boolean flag, works as I expected."},
  {"int", 'i', 0, 0, "Would like to be able to parse options as --int=4 or -i 4."},  // but I can't
  {0}
};

struct arguments {char *args[1]; int xbool, xint;};

static error_t
parse_opt (int key, char *arg, struct argp_state *state) {
    struct arguments *arguments = state->input;
    printf("key = %c, arg = %s\n", key, arg);  // My attempt to understand the problem
    //if (arg == NULL) return 0;  // Prevents segfaults, in order to see how the args and keys change
    switch (key) {
        case 'b': arguments->xbool = 1; break;
        case 'i': arguments->xint = (int) strtol(arg, NULL, 10); break;
        case ARGP_KEY_ARG: if (state->arg_num >= 1) {argp_usage(state);} arguments->args[state->arg_num] = arg; break;
        case ARGP_KEY_END: if (state->arg_num < 1) {argp_usage(state);} break;
        default: return ARGP_ERR_UNKNOWN;
        }
    return 0;
}

static struct argp argp = { options, parse_opt, args_doc, doc };

int main (int argc, char **argv) {
    struct arguments arguments;
    arguments.xbool = 0;
    arguments.xint = 0;
    argp_parse (&argp, argc, argv, 0, 0, &arguments);
    printf("ARG1 = %s\nbool option = %s\nparsed int option = %d",
           arguments.args[0], arguments.xbool ? "true" : "false", arguments.xint);
    exit (0);
}

简单的 bool 标志按预期工作,因此 ./myprogram.out myarg --bool./myprogram.out myarg -b 更改标志的值。

但是,我似乎找不到将参数解析为整数或 float 的方法。

这是我得到的输出 ./a.out -b --int=2 myarg:

key = , arg = (null)
key = b, arg = (null)
./a.out: option '--int' doesn't allow an argument
Try `a.out --help' or `a.out --usage' for more information.

对于./a.out -b --int 2 myarg,当我尝试解析 NULL 时,出现段错误:key = i, arg = (null) 。我添加了 NULL 检查,这样我就可以看到,我想要解析的选项带有 , 键(预计带有 i)。

key = i, arg = (null)
key = , arg = 2

我考虑过使用库,因为程序需要处理各种 float 和 int 选项,回退到默认值,而且我经常看到不建议滚动自己的参数和选项解析器。根据示例,argp.h 看起来很有前途,但我还无法让它工作。

ps。我知道直接解析为整数和 float 不是 argp 的一部分,这就是为什么我(看起来天真地)尝试将其添加到结构和 parse_opt 函数中.

最佳答案

事实证明,options[] 出现错误。如果选项具有与其关联的参数,则必须提供 argp_option 结构中的第三个 const char *arg 参数 [source: GNU C: Argp Option Vectors] .

static struct argp_option options[] = {
  {"bool", 'b', 0, 0, "Simple boolean flag, works as I expected."},
  {"int", 'i', "Numbah", 0, "Now everything works as expected, I get the correct key-value (key-arg) pair in the parse_opt function"},
  {0}
};

关于c - 使用 argp.h 解析为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41946371/

相关文章:

Lua:使用表作为参数

java - 按值传递(StringBuilder 与 String)

c# - 在互操作方法中为 `null` 参数传递 `ref struct` 引用

c - 是否可以估计调用函数在堆栈中需要的大小?

ios - OpenGL ES 纹理质量下降

c - 使用c中多维数组中的行的递归函数中的语法错误

java - Java 真的是按值传递对象吗?

c - 手动从简单的 .csv 文件将数据导入为 uint64_t

c - 如何找出库中缺少的函数,但哪些函数存在于与库一起发布的公开头文件中?

c - 指针作为函数参数接收内存地址(C)