Tag: getopt

是否可以重复getopt

我正在尝试使用内置命令创建一个基本shell,我遇到了一些getopt问题。 这是输出(使用valgrind): $ mkdir -p foo/bar mkdir -p foo/bar FLAGON $ mkdir -p foo/test mkdir -p foo/test ==15377== Invalid read of size 1 ==15377== at 0x5201BBE: _getopt_internal_r (in /usr/lib/libc-2.17.so) ==15377== by 0x5202CEA: _getopt_internal (in /usr/lib/libc-2.17.so) ==15377== by 0x5202D37: getopt (in /usr/lib/libc-2.17.so) ==15377== by 0x40351A: shell_ns_cmd_mkdir (shell.c:542) ==15377== by 0x403AB4: normal_shell_cb (shell.c:610) ==15377== by 0x402E8E: shell_mainloop […]

使用getopt和gdb

刚刚将getopt合并到我的main()函数中 getopt为每个调用设置全局变量optarg 使用gdb步执行main() ,在getopt()调用optarg始终为NULL (例如(gdb) p optarg )而printf(“%s\n”, optarg)按预期输出cmd行arg 这是怎么回事? 为什么这两个不一样? 这是gdb的问题,以及它如何检查全局或其他事情?

在C中使用带有非选项参数的getopt

我正在C中创建一个处理大量命令行参数的小程序,所以我决定使用getopt为我排序。 但是,我希望两个非选项参数(源文件和目标文件)是必需的,因此在调用程序时必须将它们作为参数,即使没有标志或其他参数。 这是我用标志处理参数的简化版本: while ((c = getopt(argc, argv, “i:d:btw:h:s:”)) != -1) { switch (c) { case ‘i’: { i = (int)atol(optarg); } case ‘d’: { d = (int)atol(optarg); } case ‘b’: buf = 1; break; case ‘t’: time = 1; break; case ‘w’: w = (int)atol(optarg); break; case ‘h’: h = (int)atol(optarg); break; case ‘s’: […]

如何从optarg中获取值

嗨,我正在写一个简单的客户端 – 服务器程序。 在这个程序中,我必须使用getopt()来获取端口号和ip地址,如下所示: server -i 127.0.0.1 -p 10001 我不知道如何从optarg中获取值,以便稍后在程序中使用。

Getopt-传递参数的字符串参数

我有一个程序,它接受多个命令行参数,所以我使用getopt。 我的一个参数将字符串作为参数。 无论如何通过getopt函数获取该字符串还是我必须通过argv []数组获取它? 也可以像-file一样读取args吗? 我到目前为止看到的所有论据只有一个字符,如-a 编辑 从下面的答案中我编写了一个使用getopt_long()的程序,但是当我使用character参数而不是long参数时,switch语句只识别参数。 我不确定为什么会这样。 在传递参数-mf -file sample我没有看到print语句。 编辑 我尝试输入命令参数作为–file然后它工作。 仅使用-file是不可能的? static struct option long_options[] = { {“mf”, required_argument, NULL, ‘a’}, {“md”, required_argument, NULL, ‘b’}, {“mn”, required_argument, NULL, ‘c’}, {“mw”, required_argument, NULL, ‘d’}, {“lf”, required_argument, NULL, ‘e’}, {“ld”, required_argument, NULL, ‘f’}, {“ln”, required_argument, NULL, ‘g’}, {“lw”, required_argument, NULL, ‘h’}, {“rf”, required_argument, […]

如何将整数作为命令行参数?

我已经阅读了一个getopt()示例,但它没有显示如何接受整数作为参数选项,例如cvalue将在示例的代码中: #include #include #include #include int main (int argc, char **argv) { int aflag = 0; int bflag = 0; char *cvalue = NULL; int index; int c; opterr = 0; while ((c = getopt (argc, argv, “abc:”)) != -1) switch (c) { case ‘a’: aflag = 1; break; case ‘b’: bflag = 1; break; […]

getopt无法检测选项的缺失参数

我有一个程序,它采用各种命令行参数。 为了简化起见,我们将说它需要3个标志, -a , -b和-c ,并使用以下代码来解析我的参数: int c; while((c = getopt(argc, argv, “:a:b:c”)) != EOF) { switch (c) { case ‘a’: cout << optarg << endl; break; case 'b': cout << optarg << endl; break; case ':': cerr << "Missing option." << endl; exit(1); break; } } 注意:a,和b后面的参数标志。 但是,如果我调用我的程序,我会遇到一个问题 ./myprog -a -b parameterForB 我忘记了parameterForA,parameterForA(由optarg表示)返回为-b ,parameterForB被认为是没有参数的选项,optind被设置为argv中parameterForB的索引。 […]

C getopt多值

我的论点是这样的 ./a.out -i file1 file2 file3 如何利用getopt()获取3个(或更多)输入文件? 我正在做这样的事情: while ((opt = getopt(argc, argv, “i:xyz..”))!= -1){ case ‘i’: input = optarg; break; … } 我只得到file1 ; 如何获取file2 , file3 ?

getopt.h:在Windows中编译Linux C代码

我试图在Windows下编译一组九个* .c文件(以及九个相关的* .h文件)。 该代码最初是在Linux中设计的,用于使用标准GNU-Linux / C库“getopt.h”获取命令行参数。 该库不适用于在Windows中构建C代码。 我想忽略我的代码现在所做的事情并提出以下问题。 对于那些熟悉这个C库“getopt.h”的人:如果它依赖于POSIX风格的命令行参数,是否可以在Windows中构建和运行我的代码? 或者我是否必须重新编写适用于Windows的代码,以不同方式传递输入文件(并放弃“getopt.h”依赖关系)?