1

关于情况:

问候,我正在用 C 语言开发一个项目“osfi”(使用 cmake,但目标是仅在基于 Linux 的操作系统上运行),我需要一个函数来解析从命令行传递的参数,所以我使用 GNU C argp 编写了一个。H。我最终创建了一个包含该函数的库 osfi_arg_parser.c。

这是我的程序和库片段的样子:

osfi.c

#include <stdbool.h>
#include "project_config.h"
#include "arg_parser.h"
// other includes...

int main(int argc, char **argv)
{
  struct arg_parser_arguments *arguments_p = arg_parser_parse(argc, argv);
  // other code...
  return(0);
}

arg_parser.c

#include <stdbool.h>
#include <argp.h>
#include "project_config.h"
#include "arg_parser.h"

// version string is passed by cmake through project_config.h.in and project_config.h
// need to add if a define passed by cmake is NULL, then disable program_version program_bug_address
const char *argp_program_version = "osfi " PROJECT_VERSION;
const char *argp_program_bug_address = PROJECT_BUG_ADDRESS;
#define argp_program_version_hook
#define argp_err_exit_status  0
static char doc[] = "Tool that aims automating and speeding up installation "\
                    "and configuration process of Linux-based OSes.";
static char args_doc[] = "[OPTION]...";
static struct argp_option option[] =
{
  {
    "output-dir",      'o',                    "[FILE_TYPE:]OUTPUT_DIR",
    0,
    "Path to directory where to output files of type FILE_TYPE: FILE_TYPE can be \"none\" (default if omitted), \"i\", \"p\", \"l\", \"install\", \"progress\", or \"log\"",
    0
  },
  { 0, 0, 0, 0, 0, 0 }
};
/*
//add another parser for plugin options
static error_t parse_plug_in_opt()
{
}
*/
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
  struct arg_parser_arguments *arguments_p = state->input;
  switch (key)
  {
    case 'o':
      arguments_p->option_output_dir = true;
      arguments_p->argument_output_dir = arg;
      break;
    case ARGP_KEY_NO_ARGS:
      break;

    default:
      return ARGP_ERR_UNKNOWN;
  }
  return 0;
}

void argp_initialize_arg_parser_arguments(struct arg_parser_arguments
                                          *arguments_p)
{
  arguments_p->option_output_dir = false;
  arguments_p->argument_output_dir = "-";
}

//static struct argp_child *argp_child = {  };
static struct argp argp = { option, parse_opt, args_doc, doc, 0, 0, 0};

struct arg_parser_arguments *arg_parser_parse(int argc, char **argv)
{
  struct arg_parser_arguments arguments, *arguments_p = &arguments;
  argp_initialize_arg_parser_arguments(arguments_p);
  argp_parse(&argp, argc, argv, 0, 0, arguments_p);
  return(arguments_p);
}

arg_parser.h

#ifndef ARG_PARSER_H_INCLUDED
#define ARG_PARSER_H_INCLUDED

struct arg_parser_arguments
{
  bool option_output_dir;
  char *argument_output_dir;
};

struct arg_parser_arguments *arg_parser_parse(int argc, char **argv);

#endif

注意:我知道代码没有被重构为 79 个字符的长度,并且可能使用的概念可能是旧的、不实用的或未针对速度或不同架构用例进行优化的概念(我只是在学习并希望进一步阅读和堆栈上的其他 问题我会在很多方面改进我的代码)

我的代码的行为:

[someincognitomachine@someanonyomususer osfi]$ ./build/bin/osfi  --help
Usage: osfi [OPTION...] [OPTION]...
Tool that aims automating and speeding up installation and configuration
process of Linux-based OSes.

  -o,                              --output-dir=[FILE_TYPE:]OUTPUT_DIR
Path to directory where to output files of type
                             FILE_TYPE: FILE_TYPE can be "none" (default if
                             omitted), "i", "p", "l", "install", 

  -?, --help                 Give this help list
      --usage                Give a short usage message
  -V, --version              Print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Report bugs to .

问题:

  1. 如何让 argp 知道我希望我的--output-dir=[FILE_TYPE:]OUTPUT_DIR<documertation-of-the-option>生成的字符串放置在哪里?
  2. 是否有解决此文本行为问题的永久解决方案?(我尝试使用 OPTION_DOC 并在 'o' 选项下添加一个选项,仅在选项上添加文档字符串部分,字符串中的各种转义序列。)
-o,                              --output-dir=[FILE_TYPE:]OUTPUT_DIR
Path to directory where to output files of type
                             FILE_TYPE: FILE_TYPE can be "none" (default if
                             omitted), "i", "p", "l", "install", "progress", or
                             "log"

编辑 1:用片段替换实际代码

4

0 回答 0