getopt 使用 with/without 选项
getopt usage with/without option
我正在编写一个使用 *argv[]
参数的简单代码。我想知道我是否可以使用 getopt()
函数来实现以下目的。
./myprogram -a PATH
./myprogram PATH
该程序可以只采用 PATH
(例如 /usr/tmp
),也可以采用 -a
除了 PATH
选项。 getopt()
可以用于这种状态吗?如果可以,怎么做?
The program can either take merely PATH
(e.g. /usr/tmp
) or take option in addition to PATH
. Can getopt()
be used for this state? If can, how?
当然可以。我不确定您甚至在哪里看到潜在的问题,除非您不理解 POSIX 和 getopt()
在 options 和参数。它们是相关的,但完全不是一回事。
getopt()
适用于实际上没有指定任何选项的情况,它使您可以访问非选项参数,例如 PATH
似乎适合您,无论指定了多少个选项。通常的使用模型是在循环中调用 getopt()
直到它 returns -1
表明命令行中没有更多选项可用。在每一步,全局变量 optind
变量提供下一个要处理的 argv
元素的索引,并且在 getopt()
(第一个)之后 returns -1,optind
提供第一个非选项参数的索引。在你的情况下,那将是你希望找到 PATH
.
的地方
int main(int argc, char *argv[]) {
const char options[] = "a";
_Bool have_a = 0;
char *the_path;
int opt;
do {
switch(opt = getopt(argc, argv, options)) {
case -1:
the_path = argv[optind];
// NOTE: the_path will now be null if no path was specified,
// and you can recognize the presence of additional,
// unexpected arguments by comparing optind to argc
break;
case 'a':
have_a = 1;
break;
case '?':
// handle invalid option ...
break;
default:
// should not happen ...
assert(0);
break;
}
} while (opt >= 0);
}
使用 "a"
的 optstring 允许 -a
的参数充当标志。
optind
有助于检测是否只存在一个附加参数。
程序可以执行为./program -a path
或./program path
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char op = ' ';//default value
int opt;
while ((opt = getopt(argc, argv, "a")) != -1)//optstring allows for -a argument
{
switch (opt)
{
case 'a':
op = 'a';//found option, set op
break;
default:
fprintf(stderr, "%s: unknown option %c\n", argv[0], optopt);
return 1;
}
}
if ( optind + 1 != argc)//one argument allowed besides optstring values
{
fprintf(stderr, "Usage: %s [-a] PATH\n", argv[0]);
return 1;
}
printf("%s %c\n", argv[optind], op);
return 0;
}
我正在编写一个使用 *argv[]
参数的简单代码。我想知道我是否可以使用 getopt()
函数来实现以下目的。
./myprogram -a PATH
./myprogram PATH
该程序可以只采用 PATH
(例如 /usr/tmp
),也可以采用 -a
除了 PATH
选项。 getopt()
可以用于这种状态吗?如果可以,怎么做?
The program can either take merely
PATH
(e.g./usr/tmp
) or take option in addition toPATH
. Cangetopt()
be used for this state? If can, how?
当然可以。我不确定您甚至在哪里看到潜在的问题,除非您不理解 POSIX 和 getopt()
在 options 和参数。它们是相关的,但完全不是一回事。
getopt()
适用于实际上没有指定任何选项的情况,它使您可以访问非选项参数,例如 PATH
似乎适合您,无论指定了多少个选项。通常的使用模型是在循环中调用 getopt()
直到它 returns -1
表明命令行中没有更多选项可用。在每一步,全局变量 optind
变量提供下一个要处理的 argv
元素的索引,并且在 getopt()
(第一个)之后 returns -1,optind
提供第一个非选项参数的索引。在你的情况下,那将是你希望找到 PATH
.
int main(int argc, char *argv[]) {
const char options[] = "a";
_Bool have_a = 0;
char *the_path;
int opt;
do {
switch(opt = getopt(argc, argv, options)) {
case -1:
the_path = argv[optind];
// NOTE: the_path will now be null if no path was specified,
// and you can recognize the presence of additional,
// unexpected arguments by comparing optind to argc
break;
case 'a':
have_a = 1;
break;
case '?':
// handle invalid option ...
break;
default:
// should not happen ...
assert(0);
break;
}
} while (opt >= 0);
}
使用 "a"
的 optstring 允许 -a
的参数充当标志。
optind
有助于检测是否只存在一个附加参数。
程序可以执行为./program -a path
或./program path
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char op = ' ';//default value
int opt;
while ((opt = getopt(argc, argv, "a")) != -1)//optstring allows for -a argument
{
switch (opt)
{
case 'a':
op = 'a';//found option, set op
break;
default:
fprintf(stderr, "%s: unknown option %c\n", argv[0], optopt);
return 1;
}
}
if ( optind + 1 != argc)//one argument allowed besides optstring values
{
fprintf(stderr, "Usage: %s [-a] PATH\n", argv[0]);
return 1;
}
printf("%s %c\n", argv[optind], op);
return 0;
}