我需要在 c 中使用 getopt 将目录作为命令行参数获取
I need to get the directory as a command-line argument using getopt in c
我是 getopt 的新手,我需要使用 getopt 获取目录名称作为参数。这是行不通的。
该程序需要确定哪个 argv 是目录,以便我可以将路径传递给函数。如果有 dirname 参数,我将最后一个命令行参数作为路径传递,或者将当前工作目录传递给该函数。
请通过提供正确的代码片段来帮助我:
dt [-h] [-I n] [-L -d -g -i -p -s -t -u | -l] [dirname]
我试过使用 optopt 但没有用。
int c;
while( (c = getopt(argc, argv, "hI:Ldgipstul")) != -1){
switch(c){
case 'h':
printf("This is the help message, please read README file for further information");
exit(1);
printf("In the help page\n");
break;
case 'I':
printf("Setting indentation\n");
indentation = atoi(optarg);
printf("Indentation is: %d\n", indentation);
break;
case 'L':
printf("Following symbolic links\n");
break;
case 'd':
//printf("Time of last modification\n");
break;
case 'g':
//printf("Print group id\n");
groupid = groupId(path);
printf("Group Id is: %d\n",groupid);
break;
case 'i':
printf("Print number of links in inode table\n");
int numberlink = numberLinks(path);
printf("number of links: %d\n",numberlink);
break;
case 'p':
printf("Permissions\n");
break;
case 's':
printf("Sizes\n");
break;
case 't':
printf("Information of file\n");
break;
case 'u':
//printf("Print user id\n");
userid = userId(path);
printf("User Id is: %d\n",userid);
break;
case 'l':
printf("Optional one\n");
break;
default:
perror("Not a valid command-line argument");
break;
}
}
当getopt()
循环结束时,变量optind
包含第一个非选项参数的索引。那将是 dirname
参数。所以你可以这样写:
char *directory;
if (optind < argc) {
directory = argv[optind];
} else {
directory = "."; // default to current directory
}
我是 getopt 的新手,我需要使用 getopt 获取目录名称作为参数。这是行不通的。
该程序需要确定哪个 argv 是目录,以便我可以将路径传递给函数。如果有 dirname 参数,我将最后一个命令行参数作为路径传递,或者将当前工作目录传递给该函数。
请通过提供正确的代码片段来帮助我:
dt [-h] [-I n] [-L -d -g -i -p -s -t -u | -l] [dirname]
我试过使用 optopt 但没有用。
int c;
while( (c = getopt(argc, argv, "hI:Ldgipstul")) != -1){
switch(c){
case 'h':
printf("This is the help message, please read README file for further information");
exit(1);
printf("In the help page\n");
break;
case 'I':
printf("Setting indentation\n");
indentation = atoi(optarg);
printf("Indentation is: %d\n", indentation);
break;
case 'L':
printf("Following symbolic links\n");
break;
case 'd':
//printf("Time of last modification\n");
break;
case 'g':
//printf("Print group id\n");
groupid = groupId(path);
printf("Group Id is: %d\n",groupid);
break;
case 'i':
printf("Print number of links in inode table\n");
int numberlink = numberLinks(path);
printf("number of links: %d\n",numberlink);
break;
case 'p':
printf("Permissions\n");
break;
case 's':
printf("Sizes\n");
break;
case 't':
printf("Information of file\n");
break;
case 'u':
//printf("Print user id\n");
userid = userId(path);
printf("User Id is: %d\n",userid);
break;
case 'l':
printf("Optional one\n");
break;
default:
perror("Not a valid command-line argument");
break;
}
}
当getopt()
循环结束时,变量optind
包含第一个非选项参数的索引。那将是 dirname
参数。所以你可以这样写:
char *directory;
if (optind < argc) {
directory = argv[optind];
} else {
directory = "."; // default to current directory
}