你怎么能使用boost :: posix_time :: ptime来使用boost :: program_options?

时间:2015-10-30 03:04:22

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

我做了几次尝试让这个工作无济于事。程序编译但我能想到的每个时间戳格式都无效。

#include <boost/program_options.hpp>
#include <boost/optional.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <string>
using namespace boost::program_options;
using namespace std;

int main(int argc, const char** argv) {
    boost::posix_time::ptime forceDate;
    boost::optional<boost::posix_time::ptime> forceDateOptional;

    options_description descr("Options");
    descr.add_options()
    ("force-date,a", value(&forceDate)->value_name("<date>"), "...");

    try {
        variables_map args;
        store(command_line_parser(argc, argv).options(descr).run(), args);
        notify(args);
        cout << "Done." << endl;
        return 0;
    } catch (std::exception& e) {
        cerr << e.what() << endl;
        return 1;
    }
}

使用g++ -I /usr/local/include -L /usr/local/lib -lboost_program_options -lboost_system x.cpp编译并使用./a.out --force-date 2012-01-03运行。

以下是抛出异常的错误:

the argument ('2012-01-03') for option '--force-date' is invalid

2 个答案:

答案 0 :(得分:3)

让我们首先尝试通过删除program_options并简单地尝试从流中解析posix_time::ptime来简化您的示例。

boost::posix_time::ptime forceDate;
std::istringstream ss("2012-01-03");
if(ss >> forceDate) {
    std::cout << forceDate << "\n";
}

上面的示例没有打印任何内容,因此解析时出现了问题。如果您查找operator>> boost::posix_time::ptime forceDate; std::istringstream ss("2012-Jan-03"); if(ss >> forceDate) { std::cout << forceDate << "\n"; } ,那么很明显该月份的预期格式是缩写名称而不是数字值。

如果您将上述代码更改为

boost::posix_time::time_input_facet

它产生所需的输出。

通过documentation进行更多挖掘显示控制输入格式的方法是使用set_iso_extended_formatboost::posix_time::ptime forceDate; std::istringstream ss("2012-01-03"); auto facet = new boost::posix_time::time_input_facet(); facet->set_iso_extended_format(); ss.imbue(std::locale(ss.getloc(), facet)); if(ss >> forceDate) { std::cout << forceDate << "\n"; } 将格式设置为您要使用的数字格式。

program_options

documentation

现在回到auto facet = new boost::posix_time::time_input_facet(); facet->set_iso_extended_format(); std::locale::global(std::locale(std::locale(), facet)); 。它看起来不像库直接提供语言环境支持,因此我能够使上述解决方案工作的唯一方法是弄乱全局语言环境。如果您将以下行添加到示例中,则其行为与您希望的一样。

video_master

Live demo

答案 1 :(得分:0)

我能够复制你的代码并在Ubuntu 14.04上编译它 - 我得到了你所做的相同结果。所以我将日期传递给std::string,然后尝试从中构建boost::posix_time::ptime

#include <boost/program_options.hpp>
#include <boost/optional.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <string>
using namespace boost::program_options;
using namespace std;

int main (int argc, const char ** argv) {
    auto dateStr = string {};
    auto descr = options_description { "Options" };

    descr.add_options ()
        ("help,h", "produce help message")
        ("date,a", value (&dateStr)->value_name ("<date>"), "...");

    try {
        auto args = variables_map {};
        store (command_line_parser (argc, argv).options (descr).run (), args);
        notify (args);

        auto forceDate = boost::posix_time::ptime { dateStr };

        cout << "Done." << endl;
        return 0;
    } catch (std::exception & e) {
        cout << e.what () << endl;
        return 1;
    }
}

在发现according to this SO answer你必须链接boost_date_time之前,我不得不稍微挖掘一下。我使用以下脚本同时编译和重新运行:

rerun.sh

#!/bin/bash
g++ -o main.x64 -std=c++11 -I . -L/usr/lib/x86_64-linux-gnu main.cpp -lboost_program_options -lboost_system -lboost_date_time
./main.x64 --date "2012-01-01"

这是我用来运行它的最终代码:

main.cpp

#include <boost/program_options.hpp>
#include <boost/optional.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <string>
using namespace boost::program_options;
using namespace boost::posix_time;
using namespace std;

int main (int argc, const char ** argv) {
        auto dateStr = string {};
        auto descr = options_description { "Options" };

        descr.add_options ()
                ("date,a", value (&dateStr)->value_name ("<date>"), "...");

        try {
                auto args = variables_map {};
                store (command_line_parser (argc, argv).options (descr).run (), args);
                notify (args);

                // this is the important part.
                auto d = boost::gregorian::date {
                        boost::gregorian::from_simple_string (dateStr)
                };

                auto forceDate = boost::posix_time::ptime { d };

                cout << "Done." << endl;
                return 0;

        } catch (std::exception & e) {
                cout << e.what () << endl;
                return 1;
        }
}