0

それぞれIPアドレスとポートを表す2つのスイッチ「i」と「p」があります。

コマンドラインの形式は何ですか?

私が試してみました:

app -i192.168.1.1 -p12345
app -i 192.168.1.1 -p 12345
app -i=192.168.1.1 -p=12345
app -i='192.168.1.1' -p='12345'
app --IPAddress 192.168.1.1 --Port12345

私のアプリケーションはIPアドレスに問題があり、仮想マシンの場合、DDDを使用したトラブルシューティングは明らかになりません。

また、アプリはデーモンとして実行されているため、IPアドレスとポートのcoutステートメントは無視され、値の出力がconst char *ではないため、syslogへの出力が妨げられます。

私は他のことにもプログラムオプションを使うつもりですが、私はこれに少し頭を悩ませています。

po::options_description config("Configuration");
        config.add_options()
            ("IPAddress,i","IP Address")
            ("Port,p","Port")
             ;
po::variables_map vm;
        po::store(po::parse_command_line(ac, av, config),
                       vm);

        po::notify(vm);
//...and this is how the values are used

int retval = getaddrinfo((vm["IPAddress"].as< string >()).c_str(),(vm["Port"].as<string>()).c_str(), &hint, &list);

これが完全なプログラムです...「値」の後にコンソールに何も出力されません:

#include <sstream>
#include <algorithm>
#include <stdlib.h>
#include <iterator>
#include <string>

//Using boost program options to read command line and config file data
#include <boost/program_options.hpp>
using namespace std;
using namespace boost;
namespace po = boost::program_options;

int main (int argc, char *argv[])
{
    po::options_description config("Configuration");
    config.add_options()
                ("IPAddress,i","IP Address")
                ("Port,p","Port")
                 ;

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, config),vm);
    po::notify(vm);

    cout << "Values\n";

    cout << (vm["IPAddress"].as< string >()).c_str();
    cout << " " << (vm["Port"].as<string>()).c_str();

    return 0;

}

入力した値はどういうわけか印刷できませんか?


これがgdbの出力で、キャストの問題のようです。

28              string address = (vm["IPAddress"].as< string >()).c_str();
(gdb) n
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_any_cast> >'
  what():  boost::bad_any_cast: failed conversion using boost::any_cast

Program received signal SIGABRT, Aborted.
0x0000003afd835935 in raise () from /lib64/libc.so.6
4

1 に答える 1

1

BOOSTプログラムオプションは、Unixシステムで知られている一般的なコマンドラインフレーバーをサポートします。したがって、これら2つは機能するはずです(私のために機能しています)

app -i 192.168.1.1 -p 12345
app --IPAddress=192.168.1.1 --Port=12345

備考:

  • 基本的なチュートリアルのドキュメントはboost.orgにあります(おそらくこれはすでに知っています)
  • このためのスタンドアロンの単体テストを作成することは、確かに良いアドバイスです。boostは、C++用の使いやすいテストフレームワークも提供します
于 2013-03-20T19:21:28.673 に答える