boost :: program_options用于单字节变量

时间:2013-02-02 21:42:12

标签: c++ boost boost-program-options

*强调文本*如何使用Boost程序选项从命令行接受单字节变量?

--id1=1 --id2=1的命令行参数导致id1 = 49(或'1',0x31)和id2 = 1的值。

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

using namespace std;

int main(int argc, char** argv)
{
    (void)argc;
    (void)argv;
    namespace po = boost::program_options;

    const int myargc = 3;
    const char* myargv[] = {"foo","--id1=1","--id2=2" };

    uint8_t  id1;
    uint16_t id2; // works as expected.

    po::variables_map vm;
    po::options_description cmd_options( "Command options" );
    cmd_options.add_options()
    ( "id1", po::value<uint8_t >( &id1 )->default_value( 0 ), "A 1-byte ID" )
    ( "id2", po::value<uint16_t>( &id2 )->default_value( 0 ), "A 2-byte ID" )
    ;

    po::store( po::parse_command_line( myargc, myargv, cmd_options ), vm );
    po::notify( vm );
    // Using command line parameters of --id1=1 --id2=1,    
    // at this point, id1=49 (or '1', 0x31) and id2=1.
    cout << "BPO parsing of " << myargv[1] << " and " << myargv[2] << endl;
    cout << "id1: " <<      id1 << endl;
    cout << "id1: " << (int)id1 << endl;
    cout << "id2: " <<      id2 << endl;

    id1 = boost::lexical_cast<uint8_t>("1");
    id2 = boost::lexical_cast<int>("2");

    cout << "Using boost::lexical_cast" << endl;
    cout << "id1: " <<      id1 << endl;
    cout << "id1: " << (int)id1 << endl;
    cout << "id2: " <<      id2 << endl;

}

输出是:

BPO parsing of --id1=1 and --id2=2
id1: 1
id1: 49
id2: 2
Using boost::lexical_cast
id1: 1
id1: 49
id2: 2

Boost最终调用boost :: lexical_cast(“1”)',它转换为char而不是数值 - “1”变为'1',即49.

有没有办法更改boost :: program_options :: add_options()初始化,将单个值视为int而不是string / char?如果没有,我有什么选择来改变解析或映射?明显(但不利)的选项是:[1]不要使用类似char的值[2]手动解析(绕过Boost)或[3]在Boost解析后执行二次转换。

2 个答案:

答案 0 :(得分:3)

您观察到的行为与boost::program_optionsboost::lexical_cast无关,而是uint8_t类型的流提取工作原理。

以下demonstrates
#include <iostream>
#include <sstream>

int
main()
{
   uint8_t id1;
   uint16_t id2;
   std::istringstream iss( "1 1" );
   iss >> id1 >> id2;
   std::cout << "id1 char:     " << id1 << std::endl;
   std::cout << "id1 uint8_t:  " << (uint8_t)id1 << std::endl;
   std::cout << "id1 int:      " << (int)id1 << std::endl;
   std::cout << "id2 uint16_t: " << (uint16_t)id2 << std::endl;
   std::cout << "id2 int:      " << (int)id2 << std::endl;
}

产生

id1 char:     1
id1 uint8_t:  1
id1 int:      49
id2 uint16_t: 1
id2 int:      1

如果您想将程序选项限制为单个字节,建议您使用boost::numeric_cast

#include <stdint.h>
#include <iostream> 
#include <boost/bind.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/program_options.hpp>

int
main(int argc, char** argv)
{
    (void)argc;
    (void)argv;
    namespace po = boost::program_options;

    const int myargc = 3;
    const char* myargv[] = {"foo","--id1=1","--id2=2" };

    unsigned  id1;
    uint16_t id2; // works as expected.

    po::options_description cmd_options( "Command options" );
    cmd_options.add_options()
        ( "id1", po::value<unsigned>( &id1 )->notifier([](unsigned value){ boost::numeric_cast<uint8_t>(value); } ), "A 1-byte ID" )
        ( "id2", po::value<uint16_t>( &id2 ), "A 2-byte ID" )
        ;

    po::variables_map vm;
    po::store( po::parse_command_line( myargc, myargv, cmd_options ), vm );
    po::notify( vm );
    std::cout << "id1: " << id1 << std::endl;
    std::cout << "id2: " << id2 << std::endl;
}

这是一个sample session

答案 1 :(得分:2)

创建一个字节大小的数字字节类,但是像数字值一样流,而不是像char一样流式传输。

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

using namespace std;

struct NumByte
{
    uint8_t value;

    NumByte() : value() {}
    NumByte( const uint8_t &arg ) : value(arg) {}
    NumByte( const NumByte &arg ) : value(arg.value) {}

    operator uint8_t() const { return value; }

    friend istream& operator>>(istream& in, NumByte& valArg)
    {
        int i;
        in >> i;
        valArg.value = static_cast<uint8_t>(i);
        return in;
    }

    friend ostream& operator<<(ostream& out, NumByte& valArg)
    {
        out << static_cast<int>(valArg.value);
        return out;
    }
};

int main(int argc, char** argv)
{
    (void)argc;
    (void)argv;
    namespace po = boost::program_options;

    const int myargc = 3;
    const char* myargv[] = {"foo","--id1=1","--id2=2" };

    NumByte  id1;
    uint16_t id2; 

    po::variables_map vm;
    po::options_description cmd_options( "Command options" );
    cmd_options.add_options()
      ( "id1", po::value<NumByte >( &id1 )->default_value( 0 ), "A 1-byte ID" )
      ( "id2", po::value<uint16_t>( &id2 )->default_value( 0 ), "A 2-byte ID" )
      ;

    po::store( po::parse_command_line( myargc, myargv, cmd_options ), vm );
    po::notify( vm );

    assert( sizeof(NumByte)==1 ); // insure the size of a numeric byte is the size of a byte.

    cout << "BPO parsing of " << myargv[1] << " and " << myargv[2] << endl;
    cout << "id1: " <<      id1 << endl;
    cout << "id1: " << (int)id1 << endl;
    cout << "id2: " <<      id2 << endl;

    id1 = boost::lexical_cast<NumByte>("1");
    id2 = boost::lexical_cast<int>("2");

    cout << "Using boost::lexical_cast" << endl;
    cout << "id1: " <<      id1 << endl;
    cout << "id1: " << (int)id1 << endl;
    cout << "id2: " <<      id2 << endl;

}

输出是:

BPO parsing of --id1=1 and --id2=2
id1: 1
id1: 1
id2: 2
Using boost::lexical_cast
id1: 1
id1: 1
id2: 2