Python3.0も無事にリリースされ、盛り上がっているところですが、2.x系との下位互換性が失われていることもあり、なかなかもどかしいところです。
それを繋ぐために、2.6系があって、3.x系への移行ツールも用意されているようですが、今まで2.5系でいろいろやっていると、2.6系へもすぐに乗り換えて良い物かどうか迷ったり。 というわけで、いろいろなバージョンをインストールしたいところです。 Mac(おそらくUnix系OSも)では、結構簡単にできます。 メインになるバージョンを一つ決めて、サブになるバージョンは、python*.*というように、バージョン番号をつけて起動するようにします。make altinstallを使えばよいようです。 makeする必要がありますが、それほど時間もかかりませんので。 しばらく経って、メインのバージョンを切り替えたければ、/usr/local/binあたりのリンクを張り替えれば良いと思います。 ~/Documents/download/Python-2.6.1$ あたりで、 ./configure --enable-framework make sudo make altinstall スポンサーサイト
|
C++でコマンドラインから起動するプログラムを作っていると、やっぱり引数をパースしてくれるライブラリが欲しくなります。少し探したら、さすがboost。ありました。boost.program_optionsを使えばよいようです。
#include <boost/program_options.hpp> namespace po = boost::program_options; int main (int argc, char **argv) { po::options_description opt("options"); opt.add_options() ("help,h","display this help") ("data,d",po::value<string>(),"data file") ("cls,c",po::value<string>(),"class file") ("trees,t",po::value<int>()->default_value(100000), "number of trees (default=100,000)") ("M,m", po::value<int>(), "m (default=sqrt(number of attributes)") ("prefix,p", po::value<string>(), "prefix of output files") ("attr_list,al", po::value<string>(), "list of attributes (file with no header)") ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, opt), vm); po::notify(vm); string data_file; string cls_file; if( vm.count("help") || !vm.count("data") ){ cout << opt << endl; // ヘルプ表示 return 0; }else{ //プログラムの実行のための準備 data_file = vm["data"].as<string>(); cls_file = vm["cls"].as<string>(); } なんとなく、見ればわかると思います。add_optionsで追加するとき、最初に書くのが正式名称で、カンマで区切って省略形です。実際に使うときは、--data=で指定するか、-d で指定するかですね。 プログラムの中でアクセスするときは、正式名称で。省略形は、1文字のみです。省略形では、スペースや=などを使わず続けて書くことが出来ます。 ただ、boost_optionsを使おうと思うと、boostをビルドする必要があります。 Unix系OSとCygwinでは簡単です。Windowsではいろいろ面倒がありそうですが・・・ boostを展開したディレクトリで、 ./configure make make install です。/usr/localなどにboostのヘッダファイルをインストールしている場合など、書き込み権限があるか確認してください。また、configureのオプションでインストール先を変更することも可能です。私は、MacOSでsudoつけただけですが。 ~$ g++ -I /usr/local/include/boost-1_34_1/ ***.cpp /usr/local/lib/libboost_program_options.a でコンパイル出来ます。コマンドライン引数のヘルプを半ば自動で生成してくれるので楽です。 options: -h [ --help ] display this help -d [ --data ] arg data file -c [ --cls ] arg class file -t [ --trees ] arg (=100000) number of trees (default=100,000) -m [ --M ] arg m (default=sqrt(number of attributes) -p [ --prefix ] arg prefix of output files -a [ --attr_list ] arg list of attributes (file with no header) ちなみに、Pythonで似たようなことを実現してくれるモジュールに、getoptとoptparseモジュールがありますが、optparseの方が現代的(オブジェクト指向由来の発想)なので、後者のほうがいいでしょう。 |
| ホーム |
|