如何接受字符或字符串作为输入
How to accept a char or string as input
在阅读了关于 argp 的著名 pdf 之后,我想用它做点什么,但是我遇到了问题,在这个例子中:
static int parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'd':
{
unsigned int i;
for (i = 0; i < atoi (arg); i++)
printf (".");
printf ("\n");
break;
}
}
return 0;
}
int main (int argc, char **argv)
{
struct argp_option options[] =
{
{ "dot", 'd', "NUM", 0, "Show some dots on the screen"},
{ 0 }
};
struct argp argp = { options, parse_opt, 0, 0 };
return argp_parse (&argp, argc, argv, 0, 0, 0);
}
-d接受的是int类型的参数,但是如果我想得到一个char或者char数组作为参数呢? pdf 没有涵盖文档。
我正在开始学习 C,我对它有一个基本的了解,我对其他语言比较熟悉,所以要了解更多关于它的信息我想把它存档,但我不明白我怎么能让它接受一个字符数组。
将 arg 与 char 进行比较时无效的代码:
static int parse_opt(int key, char *arg, struct argp_state *state)
{
switch(key)
{
case 'e':
{
//Here I want to check if "TOPIC" has something, in this case, a char array
//then based on that, do something.
if (0 == strcmp(arg, 'e'))
{
printf("Worked");
}
}
}
return 0;
}//End of parse_opt
int main(int argc, char **argv)
{
struct argp_option options[] =
{
{"example", 'e', "TOPIC", 0, "Shows examples about a mathematical topic"},
{0}
};
struct argp argp = {options, parse_opt};
return argp_parse (&argp, argc, argv, 0, 0, 0);
}//End of main
提前致谢。
https://www.gnu.org/software/libc/manual/html_node/Argp.html
#include <stdio.h>
#include <argp.h>
#include <string.h>
static int parse_opt(int key, char *arg, struct argp_state *state) {
(void)state; // We don't use state
switch (key) {
case 'c': {
if (strlen(arg) == 1) { // we only want one char
char c = *arg; // or arg[0]
printf("my super char %c !!!\n", c);
} else {
return 1;
}
}
}
return 0;
}
int main(int argc, char **argv) {
struct argp_option const options[] = {
{"char", 'c', "c", 0, "a super char", 0}, {0}};
struct argp const argp = {options, &parse_opt, NULL, NULL, NULL, NULL, NULL};
argp_parse(&argp, argc, argv, 0, NULL, NULL);
}
在阅读了关于 argp 的著名 pdf 之后,我想用它做点什么,但是我遇到了问题,在这个例子中:
static int parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'd':
{
unsigned int i;
for (i = 0; i < atoi (arg); i++)
printf (".");
printf ("\n");
break;
}
}
return 0;
}
int main (int argc, char **argv)
{
struct argp_option options[] =
{
{ "dot", 'd', "NUM", 0, "Show some dots on the screen"},
{ 0 }
};
struct argp argp = { options, parse_opt, 0, 0 };
return argp_parse (&argp, argc, argv, 0, 0, 0);
}
-d接受的是int类型的参数,但是如果我想得到一个char或者char数组作为参数呢? pdf 没有涵盖文档。
我正在开始学习 C,我对它有一个基本的了解,我对其他语言比较熟悉,所以要了解更多关于它的信息我想把它存档,但我不明白我怎么能让它接受一个字符数组。
将 arg 与 char 进行比较时无效的代码:
static int parse_opt(int key, char *arg, struct argp_state *state)
{
switch(key)
{
case 'e':
{
//Here I want to check if "TOPIC" has something, in this case, a char array
//then based on that, do something.
if (0 == strcmp(arg, 'e'))
{
printf("Worked");
}
}
}
return 0;
}//End of parse_opt
int main(int argc, char **argv)
{
struct argp_option options[] =
{
{"example", 'e', "TOPIC", 0, "Shows examples about a mathematical topic"},
{0}
};
struct argp argp = {options, parse_opt};
return argp_parse (&argp, argc, argv, 0, 0, 0);
}//End of main
提前致谢。
https://www.gnu.org/software/libc/manual/html_node/Argp.html
#include <stdio.h>
#include <argp.h>
#include <string.h>
static int parse_opt(int key, char *arg, struct argp_state *state) {
(void)state; // We don't use state
switch (key) {
case 'c': {
if (strlen(arg) == 1) { // we only want one char
char c = *arg; // or arg[0]
printf("my super char %c !!!\n", c);
} else {
return 1;
}
}
}
return 0;
}
int main(int argc, char **argv) {
struct argp_option const options[] = {
{"char", 'c', "c", 0, "a super char", 0}, {0}};
struct argp const argp = {options, &parse_opt, NULL, NULL, NULL, NULL, NULL};
argp_parse(&argp, argc, argv, 0, NULL, NULL);
}