如何解决argp.h
有关情况:
问候,我正在用C语言开发一个“ osfi”项目(带有cmake,但目标是只能在基于Linux的OS上运行),我需要一个函数来解析从命令行传递的参数,因此我使用GNU Cargp.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","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 }
};
/*
//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};
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,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个字符,并可能使用过时,不切实际或未针对速度或不同体系结构用例进行优化的概念(我只是在学习并希望通过进一步阅读& OTHER questions 在堆栈上,我可以在很多方面使我的代码变得更好)
我的代码的行为:
[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 .
问题:
- 如何使argp知道我要将
--output-dir=[FILE_TYPE:]OUTPUT_DIR
和<documertation-of-the-option>
生成的字符串放在哪里? - 是否有解决此文本行为问题的永久性解决方案? (我尝试使用OPTION_DOC并在'o'选项下添加一个选项,只是在选项上包含文档字符串部分,字符串中的所有转义序列。)
-o,"progress",or
"log"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。