- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我提出这个问题是因为没有太多关于如何为每个循环分配这个 optind。
Man page说:
The variable optind is the index of the next element to be processed in argv. The system initializes this value to 1.
下面,我有一个从 Head First C 获得的简单代码,在代码中我们从“减去“optind” >argc”,我们得到剩余参数的数量,然后我们将使用这些数量将剩余参数打印为“成分”。
#include <unistd.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char* delivery = "";
int thick = 0 ;
int count = 0;
char ch;,
for(int i = 0; i < argc;i++){
//This is , to show the whole array and their indexes.
printf("Argv[%i] = %s\n", i, argv[i]);
}
while((ch = getopt(argc, argv, "d:t")) != -1 ){
switch(ch) {
case 'd':
printf("Optind in case 'd' : %i\n",optind);
delivery = optarg;
break;
case 't':
printf("Optind in case 't' : %i\n",optind);
thick = 1;
break;
default:
fprintf(stderr,"Unknown option: '%s'\n", optarg); // optional argument.
return 1;
}
}
argc -= optind;
argv += optind;
printf("Optind : %i and Argc after the subctraction : %i\n",optind,argc);
if(thick)
puts("Thick crust");
if(delivery[0]){
printf("To be delivered %s\n", delivery);
}
puts("Ingredients:");
for(count = 0; count < argc ; count ++){
puts(argv[count]);
}
return 0;
}
所以在代码的开头,for 循环写入所有数组及其索引以查看差异。
然后我运行代码:
./pizzaCode -d now Anchovies Pineapple -t //-t is intentionally at the end
有人告诉我,如果标志在末尾,它就不会出现在“t”的情况下,但不知何故它在我的 ubuntu 上工作。这是我想知道的另一件事,但不是主要问题。
所以输出如下:
Argv[0] = ./pizzaCode
Argv[1] = -d
Argv[2] = now
Argv[3] = Anchovies
Argv[4] = Pineapple
Argv[5] = -t
Optind in case 'd' : 3
Optind in case 't' : 6
Optind : 4 and Argc after the subctraction : 2
Thick crust
To be delivered now
Ingredients:
Anchovies
Pineapple
1- 到目前为止一切都很好,问题是 argv[0] 和 argv 1 是怎么来的变成了凤尾鱼和菠萝?
2- 另一个问题是在“d”的情况下 optind 是如何变成 3 的?因为'd的索引是1,下一个索引是2。
3-循环后 optind 是如何变成 4 的?在“t”的情况下是 6。
我希望你们都清楚我的问题,我只是想理解逻辑而不是必须记住它。提前致谢!
最佳答案
manpage for Gnu getopt
记录这个非标准的实现:
By default,
getopt()
permutes the contents ofargv
as it scans, so that eventually all the nonoptions are at the end.
这实际上并不完全正确;如您在示例中所见,排列发生在扫描最后一个选项之后。但是效果是一样的; argv
被置换,以便非选项位于末尾,并且 optind
被修改为索引第一个非选项。
如果您想避免排列,以便 getopt
的行为与 Posix 一致:
If the first character of optstring is '+' or the environment variable
POSIXLY_CORRECT
is set, then option processing stops as soon as a nonoption argument is encountered.
在这种情况下,不会进行任何排列,optind
的值将被保留。
设置 POSIXLY_CORRECT
有其他后果,在各种 Gnu 实用程序的联机帮助页中随处记录。我的习惯是使用 +
作为选项字符串的第一个字符(除非我真的想要非 Posix 行为),但可以说设置环境变量更便携。
针对您的具体问题:
为什么非选项参数在 argv[0]
和 argv[1]
处?
因为你修改了argv:argv += optind;
为什么在循环处理选项-d
中是optind
3?
因为该选项需要一个参数。所以 next 参数是 now
参数之后的参数,它已经被处理(通过将指向它的指针放入 optarg
)。
optind
是如何变成 4 的?
如上所述,它在 argv
vector 被排列后被修改,以便 optind
成为第一个“未处理的”非选项参数的索引。
关于c - 如何在 C 中分配 "optind"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46636641/
这个问题已经有答案了: what's the difference between the printf and vprintf function families, and when should
我提出这个问题是因为没有太多关于如何为每个循环分配这个 optind。 Man page说: The variable optind is the index of the next element
我一直在学习命令行参数解析。关于这个已经有很长的线索了,我不想在这里挑起一个: Using getopts in bash shell script to get long and short com
根据我将选项传递给该程序的格式,变量 optind 将正确递增以反射(reflect)下一个选项值,否则不会递增。 如果我使用 ./cfind -aru,我会得到以下(奇怪的)输出。 optind:
我正在编写一个使用 getopt 函数的简单程序: #include #include #include int main(int argc, char **argv) { ch
我的shell脚本很简单,如下: while getopts "abc:" flag; do echo "$flag" $OPTIND $OPTARG done 我做了如下测
我意识到 shift 将 cli args 数组向左移动 n 个空间,n 的默认值为 1。这意味着我可以在 while 循环内使用 $1 shift 将数组的值分配给现有变量。我不太明白的是为什么在下
$OPTIND 在POSIX shell 中的定义,bash 是相当一致和直观的——它是下一个要读取的arg 的索引。然而,它在 zsh 中的行为非常令人费解,我找不到它的文档。 例子: # ./te
我是一名优秀的程序员,十分优秀!