提升1.55.0程序选项问题

时间:2014-05-17 12:04:38

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

在过去几个月内未能找到合理的ini文件读取方法后,决定最终咬住子弹并下载,构建和链接Boost。

在创建一个测试项目以查看该过程是如何工作之后,我坚持使用该程序在其中一个选项上抛出一个未知的选项异常。以下是测试程序的输出以及捕获的错误消息(在try / catch中添加以进行正确的错误处理)以及ini文件内容和完整代码:

  

测试ini阅读......

     

按任意键继续。 。

     

无法识别的选项' PLAYERDATA.ID'

     

加载值:

     

名称:

     

ID:8639072

     

GDIFF:

     

游戏难度值:

     

遇到随机性:5.96783e-039

     

玩家伤害修正:1.20858e-038

     

计算机伤害修改器:127232

     

项目范围低:-2

     

物品范围高:8623500

     

按任意键继续。 。

的Settings.ini:

[PLAYERDATA]
NAME = Game1A_g
ID = 12231
GDIFF = Medium
#Difficulty Values

[EASY]
ERAND = 0.25f
PDMOD = 2.12f
CDMOD = 0.65f
IRL = 1
IRH = 10

[MEDIUM]
ERAND = 1.00f
PDMOD = 1.00f
CDMOD = 1.00f
IRL = 1
IRH = 25

[HARD]
ERAND = 1.35f
PDMOD = 0.75f
CDMOD = 1.50f
IRL = 1
IRH = 30

代码:

#include <iostream>
#include <Windows.h>
#include <string>
#include <fstream>
#include "boost/program_options.hpp"

using std::cout;
using std::string;
using std::endl;
using std::ifstream;

namespace gOp = boost::program_options;

struct prgVars
{
    string name;
    int id;
    string gDiff;

    float encRand;
    float pDMod;
    float cDMod;
    int iRLow;
    int iRHigh;
};

void loadGData(prgVars& pgv);

int main()
{
    prgVars pgv;

    cout << "Testing ini reading...." << endl;

    system("Pause");

    loadGData(pgv);

    cout << "Loaded values:" << endl;
    cout << "Name: " << pgv.name << endl;
    cout << "ID: " << pgv.id << endl;
    cout << "gDiff: " << pgv.gDiff << endl << endl;

    cout << "Game Difficulty Values: " << endl;
    cout << "Encounter Randomness: " << pgv.encRand << endl;
    cout << "Player Damage Modifier: " << pgv.pDMod << endl;
    cout << "Computer Damage Modifier: " << pgv.cDMod << endl;
    cout << "Item Range Low: " << pgv.iRLow << endl;
    cout << "Item Range High: " << pgv.iRHigh << endl;

    system("Pause");
}

void loadGData(prgVars& pgv)
{
    gOp::options_description desc("Settings");
    desc.add_options()
        ("PLAYERDATA.NAME",gOp::value<string>(&pgv.name),"player_name"),
        ("PLAYERDATA.ID",gOp::value<int>(&pgv.id),"player_id"),
        ("PLAYERDATA.GDIFF",gOp::value<string>(&pgv.gDiff),"game_difficulty");

    gOp::variables_map vm;

    try
    {
        ifstream settings_file("settings.ini",ifstream::in);
        gOp::store(gOp::parse_config_file(settings_file, desc), vm);
        settings_file.close();
        gOp::notify(vm);
    }

    catch (const gOp::error& e )
    {
        std::cerr << e.what() << std::endl;
    }

    if (pgv.gDiff == "Easy")
    {

        gOp::options_description desc("DVars");
        desc.add_options()
            ("EASY.ERAND",gOp::value<float>(&pgv.encRand),"encounter_rate"),
            ("EASY.PDMOD",gOp::value<float>(&pgv.pDMod),"player_damage_bonus"),
            ("EASY.CDMOD",gOp::value<float>(&pgv.cDMod),"computer_damage_bonus"),
            ("EASY.IRL",gOp::value<int>(&pgv.iRLow),"item_rng_low"),
            ("EASY.IRH",gOp::value<int>(&pgv.iRHigh),"item_rng_high");

        gOp::variables_map vm;

        ifstream settings_file("settings.ini",ifstream::in);
        gOp::store(gOp::parse_config_file(settings_file, desc), vm);
        settings_file.close();
        gOp::notify(vm);
    }

    else if (pgv.gDiff == "Medium")
    {
        gOp::options_description desc("DVars");
        desc.add_options()
            ("MEDIUM.ERAND",gOp::value<float>(&pgv.encRand),"encounter_rate"),
        ("MEDIUM.PDMOD",gOp::value<float>(&pgv.pDMod),"player_damage_bonus"),
            ("MEDIUM.CDMOD",gOp::value<float>(&pgv.cDMod),"computer_damage_bonus"),
            ("MEDIUM.IRL",gOp::value<int>(&pgv.iRLow),"item_rng_low"),
            ("MEDIUM.IRH",gOp::value<int>(&pgv.iRHigh),"item_rng_high");

        gOp::variables_map vm;

        ifstream settings_file("settings.ini",ifstream::in);
        gOp::store(gOp::parse_config_file(settings_file, desc), vm);
        settings_file.close();
        gOp::notify(vm);
    }

    else if (pgv.gDiff == "Hard")
    {
        gOp::options_description desc("DVars");
        desc.add_options()
            ("HARD.ERAND",gOp::value<float>(&pgv.encRand),"encounter_rate"),
            ("HARD.PDMOD",gOp::value<float>(&pgv.pDMod),"player_damage_bonus"),
        ("HARD.CDMOD",gOp::value<float>(&pgv.cDMod),"computer_damage_bonus"),
            ("HARD.IRL",gOp::value<int>(&pgv.iRLow),"item_rng_low"),
            ("HARD.IRH",gOp::value<int>(&pgv.iRHigh),"item_rng_high");

        gOp::variables_map vm;

        ifstream settings_file("settings.ini",ifstream::in);
        gOp::store(gOp::parse_config_file(settings_file, desc), vm);
        settings_file.close();
        gOp::notify(vm);
    }
}

2 个答案:

答案 0 :(得分:2)

你的逗号太多了:

desc.add_options()
    ("PLAYERDATA.NAME" , gOp::value<string>(&pgv.name) , "player_name") /*HERE*/
    ("PLAYERDATA.ID"   , gOp::value<int>(&pgv.id)      , "player_id")  /*HERE*/
    ("PLAYERDATA.GDIFF", gOp::value<string>(&pgv.gDiff), "game_difficulty");

请注意,如果您在足够高的警告级别进行编译,编译器会告诉您这一点(以非常神秘的方式......)。

以下是固定程序: Live On Coliru

输出:

Testing ini reading....
unrecognised option 'EASY.ERAND'
Loaded values:            
Name:                     
ID:                       4240909
gDiff:                    

Game Difficulty Values:   
Encounter Randomness:     5.94265e-39
Player Damage Modifier:   0
Computer Damage Modifier: 0
Item Range Low:           0
Item Range High:          4215760

请注意,您可能希望查看Boost PropertyTree以解析INI文件。

答案 1 :(得分:0)

所以我设法通过sehe的帖子和T.C。的评论得到了一些帮助。虽然添加的逗号是一个问题,但主要问题是我并不真正理解解析器如何读取文件。它根据add_options()的第一个参数中给出的对标识符,一次读取整个事物,而不仅仅是它的特定部分。

所以我经历了为ini中的每个值创建了一个元素,即使我最终不会使用它并更新第一个选项列表看起来像这样:

gOp::options_description desc("Settings");
desc.add_options()
    ("PLAYERDATA.NAME",gOp::value<string>(&cName),"player_name")
    ("PLAYERDATA.ID",gOp::value<int>(&pgv.id),"player_id")
    ("PLAYERDATA.GDIFF",gOp::value<string>(&pgv.gDiff),"game_difficulty")
    ("EASY.ERAND",gOp::value<float>(&pgv.encRand_e),"encounter_rate")
    ("EASY.PDMOD",gOp::value<float>(&pgv.pDMod_e),"player_damage_bonus")
    ("EASY.CDMOD",gOp::value<float>(&pgv.cDMod_e),"computer_damage_bonus")
    ("EASY.IRL",gOp::value<int>(&pgv.iRLow_e),"item_rng_low")
    ("EASY.IRH",gOp::value<int>(&pgv.iRHigh_e),"item_rng_high")
    ("MEDIUM.ERAND",gOp::value<float>(&pgv.encRand_m),"encounter_rate")
    ("MEDIUM.PDMOD",gOp::value<float>(&pgv.pDMod_m),"player_damage_bonus")
    ("MEDIUM.CDMOD",gOp::value<float>(&pgv.cDMod_m),"computer_damage_bonus")
    ("MEDIUM.IRL",gOp::value<int>(&pgv.iRLow_m),"item_rng_low")
    ("MEDIUM.IRH",gOp::value<int>(&pgv.iRHigh_m),"item_rng_high")
    ("HARD.ERAND",gOp::value<float>(&pgv.encRand_h),"encounter_rate")
    ("HARD.PDMOD",gOp::value<float>(&pgv.pDMod_h),"player_damage_bonus")
    ("HARD.CDMOD",gOp::value<float>(&pgv.cDMod_h),"computer_damage_bonus")
    ("HARD.IRL",gOp::value<int>(&pgv.iRLow_h),"item_rng_low")
    ("HARD.IRH",gOp::value<int>(&pgv.iRHigh_h),"item_rng_high");

然后在try / catch之后从函数中删除所有其他代码。相反,现在主要用于在每个难度之间进行分支,然后cout相关变量的值。

输出现在正确显示:

  

测试ini阅读......

     

按任意键继续。 。

     

Game1A_g

     

加载值:

     

名称:Game1A_g

     

ID:12231

     

gDiff:中等

     

游戏难度值:

     

遇到随机性:1

     

玩家伤害修正:1

     

计算机伤害修改:1

     

项目范围低:1

     

项目范围高:25

     

按任意键继续。 。

因此,总之,您必须在第一个此类列表中的add_options()列表中包含每个ini选项,否则您将遇到与程序抛出未知选项错误并且无法从文件中获取数据时遇到的相同问题或者使用parse_config_file的bool参数(使用true忽略未知选项)。