在 C 中,如果没有传递任何选项,我如何告诉 argp 要做什么?
In C, how would I tell argp what to do if no option is passed?
版主,请不要拍下来,因为在过去一周左右的空闲时间里,我一直在 Google 的每个角落和缝隙中寻找解决方案,但仍然一无所获.
我一直致力于学习 C。您可以在此处找到与此问题相关的整个项目,尽管许多其他程序可能也希望能够获得答案:
https://github.com/Christoffen-Corporation/logo-generator
但这个问题主要涉及文件 main.c。这是来源:
#include <stdio.h>
#include <argp.h>
#include <cairo.h>
#include "include.h"
const char *argp_program_version = "The Christoffen Corporation Logo Generator v2.0.1";
const char *argp_program_bug_address = "M. Gage Morgan <gage@christoffen.com>";
static char doc[] = "Generates all of the logo, or just some.";
static int parse_opt (int key, char *arg, struct argp_state *state) {
switch (key) {
case 'C':
colored_nologo();
break;
case 'c':
colored_all();
break;
case 'O':
outlined_nologo();
break;
case 'o':
outlined_all();
break;
case 'F':
frankenlogos();
break;
case 'a':
all_imgs();
}
return 0;
}
int main (int argc, char **argv) {
struct argp_option options[] =
{
{ "colored-no-logo", 'C', 0, 0, "Generate all colored triangles, except for the logo\n"},
{ "colored-all", 'c', 0, 0, "Generate all colored triangles, and the logo\n"},
{ "outlined-no-logo", 'O', 0, 0, "Generate all outlined triangles, except for the logo\n"},
{ "outlined-all", 'o', 0, 0, "Generate all outlined triangles, and the logo\n"},
{ "frankenlogos", 'F', 0, 0, "Generate the Frankenlogos (don't ask; just do)\n"},
{ "all-images", 'a', 0, 0, "Generate all images: Outlines, Colors, and Logos.\n"},
{ 0 }
};
struct argp argp = { options, parse_opt, 0, 0 };
return argp_parse (&argp, argc, argv, 0, 0, 0);
}
我已经查看了 EVERYWHERE,要么我错过了我面前的东西,要么没有办法让 argp 有一个 "fallback" 如果没有指定选项。
如果指定了 none,任何具有命令行选项的程序都应该能够进行回退。例如,在这种情况下,我希望它能够只生成七个三角形和一个徽标(“./logo-generator -c
”或“./logo-generator -o
”),或者如果没有指定任何选项( "./logo-generator
")
我有一个名为 all_imgs()
的函数,我希望程序在未指定任何选项时回退。我知道这听起来很简单,起初我因为不知道而感到愚蠢,但是大约 1.5 页到 14 个不同的 Google 查询,我意识到我并没有发疯并且没有 "if nothing specified do this" 示例。
通过提供可用的源和场景,我真的希望这足够具体,让 SO 的你们中的一个人能够弄清楚。如果我遗漏了任何其他信息,请询问,我很乐意提供给您。此外,如果您绝对需要了解我在 main.c 中使用的函数,它们可以在 options.c 中找到,可以在上面的 GitHub 存储库中找到(<10 名誉禁用我不会把它放在这里,我的意思是没有伤害,发誓)。
我不是要你重写 main.c and/or 任何其他文件。具体来说,这是 main.c 中的一个问题,没有其他文件影响它。它编译得很好,但我正在寻找 main.c 的最小变化,而不是整个重写。我正在寻求解决此问题的最简单方法。
非常感谢你的宝贵时间。
--MGage
编辑: 我已按要求添加了源代码。我已经检查了为 "User Options," 添加的 link,但我不想使用参数,而且我不明白这有什么难的。只是选项。如果用户只想生成一部分图像,我需要用户能够指定一个选项,如果不想,则生成所有图像。同样,我不想重写 main.
最终编辑: 我不知道为什么我没有早点意识到这一点,但我标记为正确的答案很有帮助。对于让这件事变得困难,我深表歉意。结果就是在函数开始前加上条件IF,把想要的函数交给argc,然后用ELSE把我想要的argp的想要的部分粘到程序里。为了演示,这是最终结果(并且有效!):
#include <stdio.h>
#include <argp.h>
#include <cairo.h>
#include "include.h"
const char *argp_program_version = "The Christoffen Corporation Logo Generator v2.0.1";
const char *argp_program_bug_address = "M. Gage Morgan <gage@christoffen.com>";
static char doc[] = "Generates all of the logo, or just some.";
static int parse_opt (int key, char *arg, struct argp_state *state) {
switch (key) {
case 'C':
colored_nologo();
break;
case 'c':
colored_all();
break;
case 'O':
outlined_nologo();
break;
case 'o':
outlined_all();
break;
case 'F':
frankenlogos();
break;
}
return 0;
}
int main (int argc, char **argv) {
if (argc == 1) {
all_imgs();
} else {
struct argp_option options[] =
{
{ "colored-no-logo", 'C', 0, 0, "Generate all colored triangles, except for the logo\n"},
{ "colored-all", 'c', 0, 0, "Generate all colored triangles, and the logo\n"},
{ "outlined-no-logo", 'O', 0, 0, "Generate all outlined triangles, except for the logo\n"},
{ "outlined-all", 'o', 0, 0, "Generate all outlined triangles, and the logo\n"},
{ "frankenlogos", 'F', 0, 0, "Generate the Frankenlogos (don't ask; just do)\n"},
{ 0 }
};
struct argp argp = { options, parse_opt, 0, 0 };
return argp_parse (&argp, argc, argv, 0, 0, 0);
}
}
谢谢大家。我知道很多阅读本文的人可能对我的愚蠢感到沮丧。
你知道argc是传递的参数个数吧?你不需要图书馆来计算它们。第一个参数始终是程序的名称,因此您正在寻找 argc == 1.
的情况
int main(int argc, char **argv)
{
if (argc == 1) {
/* there are no extra arguments, handle this case */
} else {
/* there are arguments, proceed to parse and handle them */
}
}
版主,请不要拍下来,因为在过去一周左右的空闲时间里,我一直在 Google 的每个角落和缝隙中寻找解决方案,但仍然一无所获.
我一直致力于学习 C。您可以在此处找到与此问题相关的整个项目,尽管许多其他程序可能也希望能够获得答案:
https://github.com/Christoffen-Corporation/logo-generator
但这个问题主要涉及文件 main.c。这是来源:
#include <stdio.h>
#include <argp.h>
#include <cairo.h>
#include "include.h"
const char *argp_program_version = "The Christoffen Corporation Logo Generator v2.0.1";
const char *argp_program_bug_address = "M. Gage Morgan <gage@christoffen.com>";
static char doc[] = "Generates all of the logo, or just some.";
static int parse_opt (int key, char *arg, struct argp_state *state) {
switch (key) {
case 'C':
colored_nologo();
break;
case 'c':
colored_all();
break;
case 'O':
outlined_nologo();
break;
case 'o':
outlined_all();
break;
case 'F':
frankenlogos();
break;
case 'a':
all_imgs();
}
return 0;
}
int main (int argc, char **argv) {
struct argp_option options[] =
{
{ "colored-no-logo", 'C', 0, 0, "Generate all colored triangles, except for the logo\n"},
{ "colored-all", 'c', 0, 0, "Generate all colored triangles, and the logo\n"},
{ "outlined-no-logo", 'O', 0, 0, "Generate all outlined triangles, except for the logo\n"},
{ "outlined-all", 'o', 0, 0, "Generate all outlined triangles, and the logo\n"},
{ "frankenlogos", 'F', 0, 0, "Generate the Frankenlogos (don't ask; just do)\n"},
{ "all-images", 'a', 0, 0, "Generate all images: Outlines, Colors, and Logos.\n"},
{ 0 }
};
struct argp argp = { options, parse_opt, 0, 0 };
return argp_parse (&argp, argc, argv, 0, 0, 0);
}
我已经查看了 EVERYWHERE,要么我错过了我面前的东西,要么没有办法让 argp 有一个 "fallback" 如果没有指定选项。
如果指定了 none,任何具有命令行选项的程序都应该能够进行回退。例如,在这种情况下,我希望它能够只生成七个三角形和一个徽标(“./logo-generator -c
”或“./logo-generator -o
”),或者如果没有指定任何选项( "./logo-generator
")
我有一个名为 all_imgs()
的函数,我希望程序在未指定任何选项时回退。我知道这听起来很简单,起初我因为不知道而感到愚蠢,但是大约 1.5 页到 14 个不同的 Google 查询,我意识到我并没有发疯并且没有 "if nothing specified do this" 示例。
通过提供可用的源和场景,我真的希望这足够具体,让 SO 的你们中的一个人能够弄清楚。如果我遗漏了任何其他信息,请询问,我很乐意提供给您。此外,如果您绝对需要了解我在 main.c 中使用的函数,它们可以在 options.c 中找到,可以在上面的 GitHub 存储库中找到(<10 名誉禁用我不会把它放在这里,我的意思是没有伤害,发誓)。
我不是要你重写 main.c and/or 任何其他文件。具体来说,这是 main.c 中的一个问题,没有其他文件影响它。它编译得很好,但我正在寻找 main.c 的最小变化,而不是整个重写。我正在寻求解决此问题的最简单方法。
非常感谢你的宝贵时间。
--MGage
编辑: 我已按要求添加了源代码。我已经检查了为 "User Options," 添加的 link,但我不想使用参数,而且我不明白这有什么难的。只是选项。如果用户只想生成一部分图像,我需要用户能够指定一个选项,如果不想,则生成所有图像。同样,我不想重写 main.
最终编辑: 我不知道为什么我没有早点意识到这一点,但我标记为正确的答案很有帮助。对于让这件事变得困难,我深表歉意。结果就是在函数开始前加上条件IF,把想要的函数交给argc,然后用ELSE把我想要的argp的想要的部分粘到程序里。为了演示,这是最终结果(并且有效!):
#include <stdio.h>
#include <argp.h>
#include <cairo.h>
#include "include.h"
const char *argp_program_version = "The Christoffen Corporation Logo Generator v2.0.1";
const char *argp_program_bug_address = "M. Gage Morgan <gage@christoffen.com>";
static char doc[] = "Generates all of the logo, or just some.";
static int parse_opt (int key, char *arg, struct argp_state *state) {
switch (key) {
case 'C':
colored_nologo();
break;
case 'c':
colored_all();
break;
case 'O':
outlined_nologo();
break;
case 'o':
outlined_all();
break;
case 'F':
frankenlogos();
break;
}
return 0;
}
int main (int argc, char **argv) {
if (argc == 1) {
all_imgs();
} else {
struct argp_option options[] =
{
{ "colored-no-logo", 'C', 0, 0, "Generate all colored triangles, except for the logo\n"},
{ "colored-all", 'c', 0, 0, "Generate all colored triangles, and the logo\n"},
{ "outlined-no-logo", 'O', 0, 0, "Generate all outlined triangles, except for the logo\n"},
{ "outlined-all", 'o', 0, 0, "Generate all outlined triangles, and the logo\n"},
{ "frankenlogos", 'F', 0, 0, "Generate the Frankenlogos (don't ask; just do)\n"},
{ 0 }
};
struct argp argp = { options, parse_opt, 0, 0 };
return argp_parse (&argp, argc, argv, 0, 0, 0);
}
}
谢谢大家。我知道很多阅读本文的人可能对我的愚蠢感到沮丧。
你知道argc是传递的参数个数吧?你不需要图书馆来计算它们。第一个参数始终是程序的名称,因此您正在寻找 argc == 1.
的情况int main(int argc, char **argv)
{
if (argc == 1) {
/* there are no extra arguments, handle this case */
} else {
/* there are arguments, proceed to parse and handle them */
}
}