2013-04-07 9 views
5

私はBoost.program_optionsを使用して、POSIXユーティリティの実装にコマンドラインを解析しています。簡単な例として、cmpを取ってください。--help出力でコマンドラインオペランドの説明を表示する方法

ここでは、すべての引数の説明を表示する追加の引数--helpが必要です。これはこの場合重要です。私が持っている:file1file2オプションの説明を表示することができません

po::options_description options("Options"); 
options.add_options()("help", "Show this help output.") 
        (",l", "(Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference.") 
        (",s", "Write nothing for differing files; return exit status only.") 

po::positional_options_description operands; 
operands.add("file1", 1);//, "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.") 
operands.add("file2", 1);//, "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used."); 

po::variables_map vm; 
po::store(po::command_line_parser(argc, argv).options(options).positional(operands).run(), vm); 
po::notify(vm); 

if(vm.count("help")) 
{ 
    std::cout << "cmp: compare two files\nUsage: cmp [ -l | -s ] file1 file2\n" << options; 
    return 0; 
} 

を。もちろん、それらをoptionsに追加することはできますが、これは少なくとも2つの望ましくない引数[-]-file{1,2}を追加します。これは本当に嫌いです。私は(明らかにそれをハードコーディングせず)--helpために、この出力をしたい:

cmp: compare two files 
Usage: cmp [ -l | -s ] file1 file2 
Options: 
    --help    Show this help output. 
    -l     (Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference. 
    -s     Write nothing for differing files; return exit status only. 
Operands: 
    file1     A pathname of the first file to be compared. If file1 is '-', the standard input shall be used. 
    file2     A pathname of the second file to be compared. If file2 is '-', the standard input shall be used. 

は、ライブラリの周りにハッキングすることなく、これを達成する方法はありますか?私はこれがかなり基本的なものだと思うだろうが、私はtutorialsのどれかでそれを見つけることができない。

UPDATE皆様のご協力のために、feature requestを提出しました。

答えて

1

これは理想的ではありませんが、プログラムオプションの「ダミー」セットを作成すると、boost :: program_optionsフォーマッタでヘルプテキストが表示され、すぐに置き換えてダッシュを削除できますか?

次に、optionsヘルプテキストに加えてヘルプテキストを出力します。このような

何か:

po::options_description dummy_options("Operands"); 
dummy_options.add_options() 
    ("file1", po::value<std::string>(), "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.") 
    ("file2", po::value<std::string>(), "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.") 
    ; 

std::stringstream s; 
s << dummy_options; 
std::string dummy_help_text = s.str(); 
boost::replace_all(dummy_help_text, "--", ""); 
boost::replace_all(dummy_help_text, "arg", "  "); 

std::cout << dummy_help_text << std::endl; 

出力は次のようになります。

Operands: 
    file1     A pathname of the first file to be compared. If file1 
         is '-', the standard input shall be used. 
    file2     A pathname of the second file to be compared. If file2 
         is '-', the standard input shall be used. 

とりわけ、列の間の間隔は、あなたからのヘルプ出力と一致しません、ので、それは理想的ではありません他のoptions出力。しかし、何か素早く汚れていれば、それは基本的にうまくいく、それはOKかもしれない。

とにかく考えています。

(これを行うにはBoost.Program_Options APIには何も表示されません。https://stackoverflow.com/a/3621947/368896は、このようなことはサポートされていませんが、現在は3歳です。)

+0

悪いニュース、残念ながら ':(' – rubenvb

関連する問題