私はのような名前付きプログラムオプションを許可しない、boost_program_options
の位置指定リストプログラムオプションを作りたいと思います。boost :: program_optionsの定位オプションに説明を追加するには?
私は次のコードスニペットがあります
#include <boost/program_options.hpp>
#include <iostream>
#include <string>
#include <vector>
namespace po = boost::program_options;
int main(int argc, const char* argv[]) {
po::options_description desc("Allowed options");
desc.add_options()("help", "produce help message")
("files", po::value<std::vector<std::string>>()->required(), "list of files");
po::positional_options_description pos;
pos.add("files", -1);
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv).options(desc).positional(pos).run(), vm);
po::notify(vm);
} catch(const po::error& e) {
std::cerr << "Couldn't parse command line arguments properly:\n";
std::cerr << e.what() << '\n' << '\n';
std::cerr << desc << '\n';
return 1;
}
if(vm.count("help") || !vm.count("files")) {
std::cout << desc << "\n";
return 1;
}
}
問題は次のように私は位置引数リストとしてファイルのリストを読み込むことができるということです。
./a.out file1 file2 file3
を残念ながら、このようなだけでなく(これは私は無効にしたい)
./a.out --files file1 file2 file3
問題はまたyie LDS:
./a.out
Couldn't parse command line arguments properly:
the option '--files' is required but missing
Allowed options:
--help produce help message
--files arg list of files
だから私の希望のシナリオは、より多くの(似たOS)のようになる:私はdesc.add_option()(...)
からfiles
オプションを削除
./a.out
Couldn't parse command line arguments properly:
[FILES ...] is required but missing
Allowed options:
--help produce help message
--optionx some random option used in future
[FILE ...] list of files
した後、それはので、私は、私はそこにそれを必要と考えている動作を停止します。
名前付きパラメータで入力ファイルを指定する機能を削除する必要があるのはなぜですか?それは何にも有害ではありません、なぜそれを無効にするあなたの外出? –
@DanMašek私はそれが両方の方法が有効な入力方法(私は特に1つしか必要ではなく、それをサポートするのに役立つ)を可能にするので、ユーザーにとってはそれほど明確ではないと信じています。 – Patryk
OK。そこには、私には最も侵略的ではない解決策があります。引数のリスト全体に位置オプションを散らばらせることができますが、より多くのバリデーションを簡単に追加することで、より多くのオプションを制限することができます。 –