15

ブーストの長い対応物なしで短いオプションを指定するにはどうすればよいでしょうか?

(",w", po::value<int>(), "Perfrom write with N frames")

これを生成します

-w [ -- ] arg : Perfrom write with N frames

短いオプションのみを指定する方法はありますか?

4

1 に答える 1

15

コマンドラインパーサーを使用している場合は、さまざまなスタイルを設定する方法があります。したがって、解決策は、長いオプションのみを使用し、allow_long_disguiseスタイルを有効にして、長いオプションを1つのダッシュで指定できるようにすることです(つまり、「-long_option」)。次に例を示します。

#include <iostream>
#include <boost/program_options.hpp>

namespace options = boost::program_options;
using namespace std;

int
main (int argc, char *argv[])
{
        options::options_description desc (string (argv[0]).append(" options"));
        desc.add_options()
            ("h", "Display this message")
        ;
        options::variables_map args;
        options::store (options::command_line_parser (argc, argv).options (desc)
                        .style (options::command_line_style::default_style |
                                options::command_line_style::allow_long_disguise)
                        .run (), args);
        options::notify (args);
        if (args.count ("h"))
        {
            cout << desc << endl;
            return 0;
        }
}

ただし、説明の出力には少し問題があります。

$ ./test --h
./test options:
  --h                   Display this message

そして、これがこの出力を形成するために使用されているものであるため、これを修正するのは困難です。

std::string
option_description::format_name() const
{
    if (!m_short_name.empty())
        return string(m_short_name).append(" [ --").
        append(m_long_name).append(" ]");
    else
        return string("--").append(m_long_name);
}

頭に浮かぶこれに対する唯一の修正は、結果の文字列で「-」を「-」に置き換えることです。例えば:

#include <iostream>
#include <sstream>
#include <boost/program_options.hpp>
#include <boost/algorithm/string/replace.hpp>

namespace options = boost::program_options;
using namespace std;

int
main (int argc, char *argv[])
{
        options::options_description desc (string (argv[0]).append(" options"));
        desc.add_options()
            ("h", "Display this message")
        ;
        options::variables_map args;
        options::store (options::command_line_parser (argc, argv).options (desc)
                        .style (options::command_line_style::default_style |
                                options::command_line_style::allow_long_disguise)
                        .run (), args);
        options::notify (args);
        if (args.count ("h"))
        {
            std::stringstream stream;
            stream << desc;
            string helpMsg = stream.str ();
            boost::algorithm::replace_all (helpMsg, "--", "-");
            cout << helpMsg << endl;
            return 0;
        }
}

あなたができる最善のことは、空の長いオプションの説明を出力するコードを修正し、ライブラリの作成者にパッチを送信することです。

于 2010-09-01T20:32:49.740 に答える