[英]Using getopt in C with non-option arguments
我正在 C 中制作一個小程序,處理大量命令行 arguments,所以我決定使用 getopt 為我排序。
但是,我希望兩個非選項 arguments(源文件和目標文件)是強制性的,因此在調用程序時必須將它們作為 arguments,即使沒有標志或其他 arguments。
這是我必須處理帶有標志的 arguments 的簡化版本:
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':
s = (int)atol(optarg);
break;
default:
break;
}
}
如何編輯它以便也處理非選項 arguments?
我還希望能夠在選項之前或之后擁有非選項,那么如何處理呢?
getopt
設置optind
變量以指示下一個參數的位置。
在選項循環后添加類似於此的代碼:
if (argv[optind] == NULL || argv[optind + 1] == NULL) {
printf("Mandatory argument(s) missing\n");
exit(1);
}
編輯:
如果您想在常規參數之后允許選項,您可以執行類似以下操作:
while (optind < argc) {
if ((c = getopt(argc, argv, "i:d:btw:h:s:")) != -1) {
// Option argument
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':
s = (int)atol(optarg);
break;
default:
break;
}
else {
// Regular argument
<code to handle the argument>
optind++; // Skip to the next argument
}
}
可以在這里找到非常好的示例: GNU Libc代碼:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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;
case 'c':
cvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
printf ("aflag = %d, bflag = %d, cvalue = %s\n",
aflag, bflag, cvalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
它允許在參數之前和之后有選項。 我確實編譯並運行了測試示例:
$ ./a.out aa ff bb -a -ctestparam hello
aflag = 1, bflag = 0, cvalue = testparam
Non-option argument aa
Non-option argument ff
Non-option argument bb
Non-option argument hello
根據https://www.man7.org/linux/man-pages/man3/getopt.3.html
默認情況下,getopt() 在掃描時置換 argv 的內容,以便最終所有非選項都在最后。 還實現了另外兩種掃描模式。 如果 optstring 的第一個字符是 '+' 或設置了環境變量 POSIXLY_CORRECT,則一旦遇到非選項參數,選項處理就會停止。 如果 optstring 的第一個字符是“-”,那么每個非選項 argv 元素都被處理,就好像它是具有字符代碼 1 的選項的參數一樣。(這被編寫為期望選項和其他 argv 元素的程序使用以任何順序並且關心兩者的順序。)特殊參數“--”強制結束選項掃描,而不管掃描模式如何。
int main(int argc, char** argv) {
char* inputfile;
char* outputfile;
char* output_file_type;
char* color_red;
char* color_blue;
char* color_green;
int opt;
if (argv[optind] == NULL || argv[optind + 1] == NULL) {
printf("Mandatory argument(s) missing\n");
exit(1);
}
while((opt = getopt(argc, argv, ":i:o:r:g:b:t:")) != -1){
switch(opt){
case 'i':
inputfile = optarg;
printf("Input file : %s\n",inputfile);
break;
case 'o':
outputfile = optarg;
printf("Output File: %s\n",outputfile);
break;
case 't':
output_file_type = optarg;
printf("Output File type: %s\n", output_file_type);
break;
case 'r':
color_red = optarg;
printf("Color Red: %s\n",color_red);
break;
case 'g':
color_green = optarg;
printf("Color Green: %s\n",color_green);
break;
case 'b':
color_blue = optarg;
printf("Color Blue: %s\n",color_blue);
break;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknown option: %c\n", optopt);
break;
}
}
for (; optind < argc; optind++){
printf("Given extra arguments: %s\n", argv[optind]);
}
return (EXIT_SUCCESS);
}
運行命令:
gcc main.c -o image
./image -i ./resource/input_file.bmp -o ./resource/output_file.bmp -t BPM -r 10 -g 24 -b 40
輸出:
Input file : ./resource/input_file.bmp
Output File: ./resource/output_file.bmp
Output File type: BPM
Color Red: 10
Color Green: 24
GNU Libc示例也不適用於MinGW-W64 7.1.0。 非選項參數不會移到最后,以便解析在第一個非選項參數之后停止。
所以默認的排列選項似乎不起作用。
如果您希望 getopt 解析並在 while 循環中返回非選項參數(按指定的順序),您必須通過在 optstring 前面放置一個減號 (-) 來指示它這樣做。
提供的示例是“-:a:b:X”,其中減號 (-)“禁止 getopt 將所有非選項參數移動到命令行的末尾”和冒號 (:)“禁止 getopt 顯示錯誤消息”。
如果找到非選項參數,則 getopt 將返回整數值 1。
我將提前 go 並提出我的答案。我已經對其進行了測試,它符合Linux 上所述的getopt
。 請注意,此答案僅允許首先使用選項,然后是之后的非選項。 但這對於很多 CLI 工具來說是正常的。
我現在添加這個答案是因為我發現 Klas 的答案中的 EDIT 不起作用。
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <ctype.h>
int main (int argc, char * const argv[])
{
int c;
// Process option arguments
while (-1 != (c = getopt(argc, argv, "abc:"))) {
switch (c) {
case 'a': printf("option: a\n"); break;
case 'b': printf("option: b\n"); break;
case 'c': printf("option: c with arg: \"%s\"\n", optarg); break;
default:
printf("unknown arg: %2X\n", optopt);
}
}
// Process remaining arguments
for (int i = optind; i < argc; ++i) {
printf("non-option: %s\n", argv[i]);
}
return EXIT_SUCCESS;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.