6

ヘルプオプションを「実際の」プログラムオプションから分離する簡単な方法はありますか?実際、オプションの階層、つまりBNFを定義することは可能ですか?

options := help_options | program_options
help_options := '-h'
program_options := '-m1' mode1options | '-m2' mode2options
mode1options := number
...

それとも、これを達成するためのより良い方法はありますか?私は精神に戻るべきですか?

4

1 に答える 1

7

ドキュメントでは、[オプショングループ]および[非表示オプション]の見出しの下でオプションを分離する方法について説明しています。options_description複数のオブジェクトを定義しallてから、コマンドラインを解析するためのグループを使用する方法を示しますが、visibleドキュメントを表示するためのグループを使用します。

// Declare an options description instance which will include
// all the options
options_description all("Allowed options");
all.add(general).add(gui).add(backend);

// Declare an options description instance which will be shown
// to the user
options_description visible("Allowed options");
visible.add(general).add(gui);

variables_map vm;
store(parse_command_line(ac, av, all), vm);

if (vm.count("help")) 
{
    cout << visible;
    return 0;
}

Although the Program_options library lets you customize some of the syntax (see Non-conventional Syntax and Custom Validators), it doesn't offer a way of defining a custom grammar. If you want to define the grammar of the command line, use a different tool.

于 2011-11-15T17:02:51.223 に答える