Cプログラムで2つのオプションを解析しようとしています。
プログラムは次のように呼び出されます。
./a.out [OPTIONS] [DIRECTORY 1] [DIRECTORY 2]
プログラムは2つのディレクトリを同期し、2つのオプションがあります。(-r)
再帰的な同期(フォルダ内のフォルダ)、および(-n)
リモートにファイルが存在しない場合にローカルからリモートにファイルをコピーするため。
Options are:
-r : recursive
-n : copy file if it doesn't exist in remote folder
だから呼び出す:
./a.out -r D1 D2
D1
からまでのすべてのファイルとディレクトリを再帰的に同期しますD2
。D1
に存在するファイルと存在しないファイルD2
は無視されます。
そして呼び出し:
./a.cout -rn D1 D2
同じことを行いますが、に存在するファイルとに存在しD1
ないファイルD2
はにコピーされD2
ます。
問題は、呼び出しは呼び出しと./a.out -rn
同じではなく、ではないために機能しないことです。./a.out -nr
./a.out -r -n
(-n)
D1
これが私がメインを実装する方法です。
int main(int argc, char** argv) {
int next_option = 0;
const char* const short_options = "hrn:";
const struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "recursive", 1, NULL, 'r' },
{ "new", 1, NULL, 'n' },
{ NULL, 0, NULL, 0 }
};
int recursive = 0;
int new = 0;
do {
next_option = getopt_long(argc, argv, short_options, long_options, NULL);
switch(next_option) {
case 'r':
recursive = 1;
break;
case 'n':
new = 1;
break;
case 'h':
print_help();
return 0;
case -1:
break;
default:
print_help();
return -1;
}
} while(next_option != -1);
sync(argv[2], argv[3], recursive, new);
return EXIT_SUCCESS;
}