在C中,如果没有传递选项,我如何告诉argp该做什么?

时间:2016-06-06 21:17:34

标签: c glibc

Mods,请不要拍下来,因为我在过去一周左右的空闲时间里一直在寻找谷歌的每个角落,并且仍然没有找到任何解决方案。

我一直在努力学习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); 
}

我看了无处不在,要么我在我面前丢失了一些东西,要么就没有办法让argp有一个&#34;后退&#34;如果没有指定选项。

任何具有命令行选项的程序都应该能够在没有指定的情况下进行回退。在这种情况下,例如,我希望它能够生成七个三角形和一个徽标(&#34; ./logo-generator -c&#34;或&#34; ./logo-generator -o&#34; ),或一切可能,如果没有指定任何选项(&#34; ./logo-generator&#34;)

我有一个标题为all_imgs()的函数,我希望在没有指定选项的情况下使用该程序。我知道这听起来很简单,起初我觉得愚蠢不知道,但在14个不同的Google查询中大约1.5页,我意识到我不会发疯,并且没有&#34;如果没有指定这样做& #34;示例

通过提供源和场景,我真的希望这对于你们中的一个人来说足够具体了解。如果我遗漏了任何其他信息,请询问,我很乐意将其交给您。另外,如果您完全需要了解我在main.c中使用的函数,可以在options.c中找到它们,可以在上面的GitHub repo中找到它(&lt; 10声誉禁止我把它放在这里,我的意思是没有伤害,发誓)。

我没有要求您重新编写main.c和/或任何其他文件。具体来说,这是main.c中的一个问题,没有其他文件影响它。它编译得很好,但我正在寻找main.c.的最小变化,而不是整个重写。我要求解决此问题的SIMPLEST。

感谢您的时间。

- MGage

编辑:我已根据要求添加了来源。我已检查为&#34;用户选项添加的链接,&#34;但我不想使用论据,我也不明白这是多么困难。只是选择。我需要用户能够指定一个选项,如果他们只想生成一部分图像,如果没有,则生成所有内容。再一次,我不想重写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); 
  }
}

谢谢大家。我知道很多人在读这篇文章时可能会对我的愚蠢感到沮丧。

1 个答案:

答案 0 :(得分:1)

你知道argc是传递的参数数量,对吧?你不需要一个图书馆来计算它们。第一个参数始终是程序的名称,因此您正在查找案例argc == 1.

>>> a.__dict__ == A.__getattribute__(a, '__dict__')
True
>>> A.__dict__ == type.__getattribute__(A, '__dict__')
True