0
私はMacOS 10.12でgetopt
という奇妙な経験をしています。次の例:getopt on MacOS 10.12
#include <sys/types.h>
#include <unistd.h>
#include <sys/event.h>
#include <sys/time.h>
#include <cstdlib>
#include <string>
#include <iostream>
#include <cstdio>
void print_usage_and_exit()
{
std::cout << “usage: execute-command -p <pid> -c <command>\n”;
exit(EXIT_FAILURE);
}
int main(int argc, char** argv)
{
int option;
pid_t parent = getppid();
std::string command;
opterr = 0;
while ((option = getopt(argc, argv, “p:c:“)) != 1)
{
printf(“processing argument %d\n”, optind);
switch (option)
{
case ‘p’:
std::cout << “pid: ” << parent << “\n”;
break;
case ‘c’:
command = optarg;
std::cout << “command: ” << command << “\n”;
break;
default:
printf(“%c, err: %d, optopt: %c\n”, option, opterr, optopt);
print_usage_and_exit();
break;
}
}
return EXIT_SUCCESS;
}
は、私はclang++
でそれをコンパイルしてから./test -c a -p 12
ようですが、使用量の結果が印刷されていることを実行しています。問題は、getopt
が、-1の代わりにすべての引数を解析するときに、?
を返すことです(マンページから期待されています)。私は何か間違っているのですか?
は、そのような愚かなタイプミスをいただきありがとうございます:)私はそれだけで手遅れだと思いますD:もし私ができるなら、私はこれを1000のアップボントにしたいと思います:D –
また、私は追加しようとしていました。 'getopt'はあなたが述べたように'? 'を返していないので、正当に' -1'を返しています。あなたのprintステートメントは '-1'を文字として出力する方法を知らないので、代わりに'? 'を表示するだけです。 – selbie
公正なポイント、ありがとう。しかし、再び-1を1とミキシングすると、私はとても気分が悪くなります。しかし、私たちは皆、それらの瞬間を持っていると思います。 –