我有一个需要接受多个命令行参数的程序。我已经到了需要将其设置为接受参数 n 的阶段,该参数指定最终将打印的字符串的最大和最小长度。基本上输入可能如下所示:
-a -n7,7 -i // with -a and -i being other arguments
我可以自己挑选参数,但我也不确定如何提取那些最大值和最小值。我已经尝试过(见下文),但每当我尝试使用变量最小值和最大值时,我都会遇到运行时错误。干杯,伙计们。
int c;
while ((c = getopt(argc, argv, ":wpsaevin")) != -1) {
switch (c) {
case 'w': // pattern matches whole word
mode = WHOLE;
break;
case 'p': // pattern matches prefix
mode = PREFIX;
break;
case 'a': // pattern matches anywhere
mode = ANYWHERE;
break;
case 's': // pattern matches suffix
mode = SUFFIX;
break;
case 'e': // pattern matches anywhere
mode = EMBEDDED;
break;
case 'v': // reverse sense of match
reverse_match = true;
break;
case 'i': // ignore case of pattern
ignore_case = true;
break;
case 'n': //Specifies word length
length_spec = true;
cin >> minimum >> maximum;
if (minimum == 0 && maximum == 0) { //no word limit
length_spec = false;
} else if (maximum == 0) {
maximum = 100;
}
break;
}
}
argc -= optind;
argv += optind;