Search code examples
c++boost-program-options

program_options variable map change parameters


in this code

    po::options_description desc(prog + " Allowed options");
    desc.add_options()
        ("help,h", "produce help message")
        ("version,v", "outputs Version of program")
        ("day,d", po::value<date>()->default_value(day_clock::universal_day()- single_day), "specific day format YYYY-MM-DD (default not today but today-singleday=yesterday)")
        ("startdate,s", po::value<date>()->default_value(day_clock::universal_day()), "start date format YYYY-MM-DD (default today)")
        ("enddate,e", po::value<date>()->default_value(day_clock::universal_day()), "end date format YYYY-MM-DD (default today)")
        ("thread,t", po::value<int>()->default_value(20), "number of threads (default 20)")
        ("folder,f", po::value<fs::path>()->default_value(path), "destination folder (default .)")
        ("candle,c", po::value<timeframe_enum>()->default_value(timeframe_enum::TICK, "TICK"), "use candles instead of ticks. Accepted values TICK M1 M2 M5 M10 M15 M30 H1 H4")
        ("header", po::value<bool>()->default_value(false), "include CSV header (default false)")
        ("symbols", po::value<std::vector<symbols_enum>>()->multitoken()->composing()->default_value(symbols_default, "GBPJPY"), "symbol list using format EURUSD EURGBP")
        ;
    po::positional_options_description pd;
    pd.add("symbols", -1);

    command_line_parser parser{ argc, argv };
    parser.options(desc).positional(pd);
    parsed_options parsed_options = parser.run();

    po::variables_map vm;
    po::store(parsed_options, vm);
    po::notify(vm);

    if (!vm.count("startdate"))
    {

        vm["startdate"] = vm["day"].as<date>();
    }

in the last if statement when i try to change variable map by subscripting it gives error.
is it possible to change variable map after store()?


Solution

  • variables_map extends the standard library map, and it override the subscript operator as following:

    const variable_value & operator[](const std::string & name) const;
    

    This mean you can't change the value as it is a const reference. Looking at the code of store, I see the following lines:

        // We need to access map's operator[], not the overriden version
        // variables_map. Ehmm.. messy.
        std::map<std::string, variable_value>& m = xm;
    

    This probably mean that you can try to do something similar yourself. But take into consideration that from the API, it is apparent that the writer of this class tried to prevent you from doing such a thing, so it might be unsafe.