Más contenido relacionado La actualidad más candente (20) Similar a Boost jp9 program_options (20) Boost jp9 program_options7. 複数回挨拶させたい!
int main(int argc, char *argv[]){
int hellon = 1;
if (argc > 2 && strcmp(argv[1], “--count”)==0)
hellon = atoi(argv[2]);
for (int i = 0; i < hellon; ++i)
cout << “Hello, World!” << endl;
}
8. 複数回挨拶させたい!
int main(int argc, char *argv[]){
int hellon = 1;
if (argc > 2 && strcmp(argv[1], “--count”)==0)
hellon = atoi(argv[2]);
for (int i = 0; i < hellon; ++i)
cout << “Hello, World!” << endl;
}
9. --help も付けたくなった
ら?
int main(int argc, char *argv[]){
int hellon = 1;
if (argc > 1 && strcmp(argv[1],”--help”) == 0){
cout << “Usage …” << endl;
exit(0);
}
if (argc > 2 && strcmp(argv[1], “--count”)==0)
hellon = atoi(argv[2]);
…
10. --hello-c++で
別の挨拶を出したくなったら?
int main(int argc, char *argv[]){
int hellon = 1;
char helloMessage []= “Hello, World!”;
if (argc > 1 && strcmp(argv[1],”--help”) == 0){
cout << “Usage …” << endl; ここに追加を忘れてないか?
exit(0);
}
if (argc > 2 && strcmp(argv[1], “--count”)==0)
hellon = atoi(argv[2]);
if (argc > 2 && strcmp(argv[1], “--hello-c++”)==0)
helloMessage = “Hello, C++ !”;
…
11. --hello-c++ と –x の両方に同じ意
味を持たせたくなったら?
if (argc > 2 &&( strcmp(argv[1], “--hello-c++”)==0
|| strcmp(argv[1], “-x”) == 0) )
12. --color, -c でカラー表示機能も
付けたくなったら?
for (int i = 0; i < argc; ++i){
if (strcmp(argv[i], “--count”)==0)
hellon = atoi(argv[i+1]);
if (strcmp(argv[i],”—color”)==0
||strcmp(argv[i],”-c”))
…;
if (strcmp(argv[i], “--hello-c++”)==0
|| strcmp(argv[i], “-x”) == 0)
…;
}
13. -c –x と指定可能なら
-cx と –xc も許可されるべきだよ
ね
for (int i = 0; i < argc; ++i){
if (strcmp(argv[i], “--count”)==0)
hellon = atoi(argv[i+1]);
if (strcmp(argv[i],”—color”)==0
&&strcmp(argv[i],”-c”))
…;
if (strcmp(argv[i], “--hello-c++”)==0
&& strcmp(argv[i], “-x”) == 0)
…;
if (strcmp(argv[i], “-cx”) || strcmp(argv[i],”-xc”)
…….
}
14. --countは省略-n にしよう
-n3 とトークンが連結されてて
も
使えるようにしよう
if (………)
…….
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
…….
15. あ、あと –version, -v オプション
も付けて勿論-cvx, -cxv, -xvc, -xvc,-
vcx,-vxc と全部可能な感じで
if (………)
…….
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
……if (………………………………………………………………………)
…….
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
……
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
……
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
……
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
…….
if (………………………………………………………………………)
……
18. 概要
コマンドラインオプション解析を簡潔に書ける
-xvz と –x –v –z 等を受ける事も自動でしてくれる
カスタマイズ性は高く無いけどそのまま使えるライ
ブラリ
!ここから
using namespace std;
using namespace boost;
using namespace boost::program_options;
前提
19. 先程の例
options_description opt(“オプション”)
opt.add_options()
(“help”, “ヘルプ表示”)
(“count,n”, value<int>(), “挨拶する回数”)
(“hello-c++,x”, “メッセージをHello,C++ にする”)
(“color,c”, “カラー表示”)
(“version,v”, “バージョンを表示”)
21. 先ほどの例
int hellon = 1;
if (vm.count(“count”))
hellon = vm[“count”].as<int>();
for ( int i = 0; i < hellon; ++i )
cout << “Hello, World!” << endl;
23. ヘルプ自動生成
if ( vm.count[“help”] )
cout << opt << endl;
オプション:
--help ヘルプを表示
-n [ --count ] arg 挨拶する回数
-x [ --hellp-c++ ] メッセージをHello,C++ に
-c [ --color ] カラー表示
-v [ --version ] バージョン表示
28. オプションとペアになってない
値も扱う
./foo --hoge XXX /etc/passwd
/etc/passwd がどのオプションとも結びついてない
30. オプションとペアになってない
値も扱う
positional_options_description pd;
pd.add(“input-file", 1);
./foo --hoge XXX /etc/passwd
↓
./foo --hoge XXX --input-file /etc/passwd
解決……?
32. オプションとペアになってない
オプションを2つ用意する
値も扱う
公開ヘルプ用、実際の処理用
options_description opt(“オプション”)
opt.add_options()
(“help”, “ヘルプ表示”)
options_description hidden(“処理用”)
hidden.add_options()
( opt )
(“—input-file”, value<int>(), “隠しオプション”)
33. オプションとペアになってない
値も扱う
variables_map vm;
store(parse_command_line(argc, argv, hidden), vm);
notify(vm);
if (vm.count(“help”) )
cout << opt << endl;
35. •未知のオプションにも対応させ
る
突然の死
./compiler-wrapper --compiler-option –O2 -lSDL
36. •未知のオプションにも対応させ
る
./compiler-wrapper --compiler-option –O2 -lSDL
-O, -l を定義してないので不明なオプション例外
basic_command_line_parser の
allow_unregistered を使う
37. •未知のオプションにも対応させ
る
./compiler-wrapper --compiler-option –O2 -lSDL
-O, -l を定義してないので不明なオプション例外
basic_command_line_parser の
allow_unregistered を使う
38. •未知のオプションにも対応させ
る
command_line_parser(argc, argv)
.options(opt)
.allow_unregistered()
.run();
39. •未知のオプションにも対応させ
る
./compiler-wrapper --compiler-option –O2 -lSDL
./compiler-wrapper --compiler-option=“–O2 -lSDL”
40. 他のオプション形式にも対応す
る
boost::program_options が標準で用意してるのは
二種類のみ
ロングオプション(ハイフン二個、 --hoge=value 等可
能)
ショートオプション(ハイフン一個、一文字限定 –
acxv 可能
ハイフン3つ以上等
41. 他のオプション形式にも対応す
る
-fno-rtti とかやりたい
Windows風に /Z /D とかやりたい
tar xvzf とかやりたい
突然の……
42. 他のオプション形式にも対応す
る
別に確実に死ぬ訳ではない
ただ、オプションとして認識されない
(から、適切なオプション設定が無いと死ぬ)
オプションとして認識させる必要がある
引数解析前にパーサで前処理させれば良し
43. 他のオプション形式にも対応す
る
pair<string,string> reg_f (string const& s){
if ( s.find(“-f”) == 0 )
if ( s.substr(2, 3) == “no-” )
return { s.substr(5), string(“false”) };
else
return { s.substr(3), string(“true”) };
else
return {string(), string()};
}
44. 他のオプション形式にも対応す
る
store(command_line_parser(ac,av)
.options(desc)
.extra_parser(reg_foo)
.run(), vm);
46. 他のオプション形式にも対応す
る
実はこれでは /D や xvzf には対処できない
extra_parser は一つのトークン
⇒ 一つのオプション、値のペアにするのみ
47. 他のオプション形式にも対応す
る
編集した argc argv を渡す
basic_command_line_parser
( int argc, char const* const argv);
basic_command_line_parser
( vector<string> const& args );
Qi ~!Karma ja~!!
54. パーサー
それぞれの parser は basic_parsed_options< charT
>
を返す
variables_map vm;
store(parse_hogehoge(), vm);
notify(vm);
独自定義のパーサが利用出来る!!