Getopt - 文件名作为参数 [英] Getopt - filename as argument
问题描述
假设我编写了一个如下调用的 C 程序:
Let's say I made a C program that is called like this:
./something -d dopt filename
所以 -d
是一个命令,dopt
是 -d
的可选参数,filename
是一个./something
的参数,因为我也可以调用 ./something filename
.
So -d
is a command, dopt
is an optional argument to -d
and filename
is an argument to ./something
, because I can also call ./something filename
.
表示获取文件名的getopt
形式是什么?
What is the getopt
form to represent get the filename?
推荐答案
使用 optstring "d:"
Use optstring "d:"
以通常的方式使用 optarg
捕获 -d dopt
.然后查看 optind
(与 argc
比较),它会告诉您是否还有任何非选项参数.如果是这样,您的文件名是其中的第一个.
Capture -d dopt
with optarg
in the usual way. Then look at optind
(compare it with argc
), which tells you whether there are any non-option arguments left. If so, your filename is the first of these.
getopt
没有具体告诉您非选项参数是什么或检查数字.它只是告诉你它们从哪里开始(首先将它们移动到参数数组的末尾,如果你处于 GNU 的非严格 POSIX 模式)
getopt
doesn't specifically tell you what the non-option arguments are or check the number. It just tells you where they start (having first moved them to the end of the argument array, if you're in GNU's non-strict-POSIX mode)
这篇关于Getopt - 文件名作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!