Boostは引数を効果的に処理するユーティリティを提供しています。
他の答えが指摘しているように、この答えに示された関数本体全体を新しい関数に移動し、main()から "argc"と "argv"あなたの新しい機能に。
int
main(int argc, char *argv[])
{
namespace po = boost::program_options;
po::options_description desc("Usage: describe usage here [OPTIONS]");
desc.add_options()
("help", "Print usage")
("list,l", po::value<std::string>(), "dummy command taking a value")
;
po::variables_map vm;
try
{
po::store(po::parse_command_line(argc, argv, desc), vm);
if (vm.count("help"))
{
std::cout << desc << std::endl;
return 0;
}
if (vm.count("list"))
{
std::cout << "Dummy command entered" << std::endl;
return 0;
}
po::notify(vm);
}
catch (po::error& e)
{
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << desc << std::endl;
return 1;
}
}
コンパイル&サンプルOP:
g++ -std=c++11 -Iboost_root/boost -Lboost_root/boost/stage/lib/ Options.cpp -l boost_program_options
./a.out --list dummy_value
Dummy command entered
あなたがメソッドを書きたい、しかし、あなたも引数を渡す方法を理解していない場合は...多分、あなたは教育少数のより多くの時間を費やしたはずです自分の好きな言語の初心者の基礎について語りましょう。これは侮辱ではありませんが、次のステップを実行する前に、最も重要な概念を実際に理解する必要があります。 – GhostCat
@Jägermeisterご意見ありがとうございます。 :) – AwaitedOne
[C++のargc argvのmain()の外側にある可能性のある複製](http://stackoverflow.com/questions/12862798/c-process-argc-argv-outside-of-main) – gdlmx