このトピックは死に至ったものの、私が探しているものはまだ見つかりませんでした。 C++でコマンドライン引数を解析する必要があります。正しいgetopt/long_getoptの使用方法
私は単にループに期待が、変数に割り当てられた値が何とか動作していないとして、それは動作しますが、引数を印刷するときの問題は、鋳物であるlong_getopt
を使用してブーストを使用してすることはできません。
完全なコンパイル可能なプログラムです。
#include <iostream>
#include <getopt.h>
using namespace std;
int main(int argc, char *argv[])
{
int c;
int iterations = 0;
float decay = 0.0f;
int option_index = 0;
static struct option long_options[] =
{
{"decay", required_argument, 0, 'd'},
{"iteration_num", required_argument, 0, 'i'},
{0, 0, 0, 0}
};
while ((c = getopt_long (argc, argv, "d:i:",
long_options, &option_index) ) !=-1)
{
/* getopt_long stores the option index here. */
switch (c)
{
case 'i':
//I think issue is here, but how do I typecast properly?
// especially when my other argument will be a float
iterations = static_cast<int>(*optarg);
cout<<endl<<"option -i value "<< optarg;
break;
case 'd':
decay = static_cast<float>(*optarg);
cout<<endl<<"option -d with value "<<optarg;
break;
}
}//end while
cout << endl<<"Value from variables, which is different/not-expected";
cout << endl<< decay << endl << iterations << endl;
return(0);
}
私がコメントで述べたように、問題は型キャストにあると思いますが、正しく行うにはどうしたらいいですか?それ以外の方法がある場合は、私に知らせてください。
あなたは./program-name -d 0.8 -i ---などのプログラムを実行することができます100
はあなたの助けをいただき、ありがとうございます。私はUnixとC++の新機能ですが、それを学ぶのが非常に難しいです:)
getoptとfriendsは(かなり)廃止され、argp_parse()に置き換えられました。 –