⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmdline_test.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    test_case test_cases1[] = {        {"/d d -bar", s_success, "-d: d -bar"},        {"--foo", s_success, "--foo"},        {"/d13", s_extra_parameter, ""},        {"/f14", s_success, "-f:14"},        {"/f", s_missing_parameter, ""},        {0}    };    test_cmdline(",d ,f=", style, test_cases1);    style = cmdline::style_t(        allow_short         | allow_slash_for_short | short_allow_next        | short_allow_adjacent | allow_sticky);    test_case test_cases2[] = {        {"/de", s_extra_parameter, ""},        {"/fe", s_success, "-f:e"},        {0}    };    test_cmdline(",d ,f= ,e", style, test_cases2);}void test_disguised_long(){    using namespace command_line_style;    cmdline::style_t style;    style = cmdline::style_t(        allow_short | short_allow_adjacent        | allow_dash_for_short        | short_allow_next | allow_long_disguise        | long_allow_adjacent);    test_case test_cases1[] = {        {"-foo -f", s_success, "foo: foo:"},        {"-goo=x -gy", s_success, "goo:x goo:y"},        {"-bee=x -by", s_success, "bee:x bee:y"},        {0}    };    test_cmdline("foo,f goo,g= bee,b?", style, test_cases1);    style = cmdline::style_t(style | allow_slash_for_short);    test_case test_cases2[] = {        {"/foo -f", s_success, "foo: foo:"},        {"/goo=x", s_success, "goo:x"},        {0}    };    test_cmdline("foo,f goo,g= bee,b?", style, test_cases2);}void test_guessing(){    using namespace command_line_style;    cmdline::style_t style;    style = cmdline::style_t(        allow_short | short_allow_adjacent        | allow_dash_for_short                | allow_long | long_allow_adjacent        | allow_guessing | allow_long_disguise);    test_case test_cases1[] = {        {"--opt1", s_success, "opt123:"},        {"--opt", s_ambiguous_option, ""},        {"--f=1", s_success, "foo:1"},        {"-far", s_success, "foo:ar"},        {0}    };    test_cmdline("opt123 opt56 foo,f=", style, test_cases1);}void test_arguments(){    using namespace command_line_style;    cmdline::style_t style;    style = cmdline::style_t(        allow_short | allow_long        | allow_dash_for_short        | short_allow_adjacent | long_allow_adjacent);    test_case test_cases1[] = {        {"-f file -gx file2", s_success, "-f: file -g:x file2"},        {"-f - -gx - -- -e", s_success, "-f: - -g:x - -e"},        {0}    };    test_cmdline(",f ,g= ,e", style, test_cases1);    // "--" should stop options regardless of whether long options are    // allowed or not.    style = cmdline::style_t(        allow_short | short_allow_adjacent        | allow_dash_for_short);    test_case test_cases2[] = {        {"-f - -gx - -- -e", s_success, "-f: - -g:x - -e"},        {0}    };    test_cmdline(",f ,g= ,e", style, test_cases2);}void test_prefix(){    using namespace command_line_style;    cmdline::style_t style;    style = cmdline::style_t(        allow_short | allow_long        | allow_dash_for_short        | short_allow_adjacent | long_allow_adjacent        );    test_case test_cases1[] = {        {"--foo.bar=12", s_success, "foo.bar:12"},        {0}    };    test_cmdline("foo*=", style, test_cases1);}pair<string, string> at_option_parser(string const&s){    if ('@' == s[0])        return std::make_pair(string("response-file"), s.substr(1));    else        return pair<string, string>();}pair<string, string> at_option_parser_broken(string const&s){    if ('@' == s[0])        return std::make_pair(string("some garbage"), s.substr(1));    else        return pair<string, string>();}void test_additional_parser(){    options_description desc;    desc.add_options()        ("response-file", value<string>(), "response file")        ("foo", value<int>(), "foo")        ;    vector<string> input;    input.push_back("@config");    input.push_back("--foo=1");    cmdline cmd(input);    cmd.set_options_description(desc);    cmd.set_additional_parser(at_option_parser);    vector<option> result = cmd.run();    BOOST_REQUIRE(result.size() == 2);    BOOST_CHECK_EQUAL(result[0].string_key, "response-file");    BOOST_CHECK_EQUAL(result[0].value[0], "config");    BOOST_CHECK_EQUAL(result[1].string_key, "foo");    BOOST_CHECK_EQUAL(result[1].value[0], "1");        // Test that invalid options returned by additional style    // parser are detected.    cmdline cmd2(input);    cmd2.set_options_description(desc);    cmd2.set_additional_parser(at_option_parser_broken);    BOOST_CHECK_THROW(cmd2.run(), unknown_option);}vector<option> at_option_parser2(vector<string>& args){    vector<option> result;    if ('@' == args[0][0]) {        // Simulate reading the response file.        result.push_back(option("foo", vector<string>(1, "1")));        result.push_back(option("bar", vector<string>(1, "1")));        args.erase(args.begin());    }    return result;}void test_style_parser(){    options_description desc;    desc.add_options()        ("foo", value<int>(), "foo")        ("bar", value<int>(), "bar")        ;    vector<string> input;    input.push_back("@config");    cmdline cmd(input);    cmd.set_options_description(desc);    cmd.extra_style_parser(at_option_parser2);    vector<option> result = cmd.run();    BOOST_REQUIRE(result.size() == 2);    BOOST_CHECK_EQUAL(result[0].string_key, "foo");    BOOST_CHECK_EQUAL(result[0].value[0], "1");        BOOST_CHECK_EQUAL(result[1].string_key, "bar");    BOOST_CHECK_EQUAL(result[1].value[0], "1");    }void test_unregistered(){    // Check unregisted option when no options are registed at all.    options_description desc;    vector<string> input;    input.push_back("--foo=1");    input.push_back("--bar");    input.push_back("1");    input.push_back("-b");    input.push_back("-biz");    cmdline cmd(input);    cmd.set_options_description(desc);    cmd.allow_unregistered();        vector<option> result = cmd.run();    BOOST_REQUIRE(result.size() == 5);    // --foo=1    BOOST_CHECK_EQUAL(result[0].string_key, "foo");    BOOST_CHECK_EQUAL(result[0].unregistered, true);    BOOST_CHECK_EQUAL(result[0].value[0], "1");    // --bar    BOOST_CHECK_EQUAL(result[1].string_key, "bar");    BOOST_CHECK_EQUAL(result[1].unregistered, true);    BOOST_CHECK(result[1].value.empty());    // '1' is considered a positional option, not a value to    // --bar    BOOST_CHECK(result[2].string_key.empty());    BOOST_CHECK(result[2].position_key == 0);    BOOST_CHECK_EQUAL(result[2].unregistered, false);    BOOST_CHECK_EQUAL(result[2].value[0], "1");    // -b    BOOST_CHECK_EQUAL(result[3].string_key, "-b");    BOOST_CHECK_EQUAL(result[3].unregistered, true);    BOOST_CHECK(result[3].value.empty());    // -biz    BOOST_CHECK_EQUAL(result[4].string_key, "-b");    BOOST_CHECK_EQUAL(result[4].unregistered, true);    BOOST_CHECK_EQUAL(result[4].value[0], "iz");    // Check sticky short options together with unregisted options.        desc.add_options()        ("help,h", "")        ("magic,m", value<string>(), "")        ;    input.clear();    input.push_back("-hc");    input.push_back("-mc");    cmdline cmd2(input);    cmd2.set_options_description(desc);    cmd2.allow_unregistered();        result = cmd2.run();    BOOST_REQUIRE(result.size() == 3);    BOOST_CHECK_EQUAL(result[0].string_key, "help");    BOOST_CHECK_EQUAL(result[0].unregistered, false);    BOOST_CHECK(result[0].value.empty());    BOOST_CHECK_EQUAL(result[1].string_key, "-c");    BOOST_CHECK_EQUAL(result[1].unregistered, true);    BOOST_CHECK(result[1].value.empty());    BOOST_CHECK_EQUAL(result[2].string_key, "magic");    BOOST_CHECK_EQUAL(result[2].unregistered, false);    BOOST_CHECK_EQUAL(result[2].value[0], "c");    // CONSIDER:    // There's a corner case:    //   -foo    // when 'allow_long_disguise' is set. Should this be considered    // disguised long option 'foo' or short option '-f' with value 'oo'?    // It's not clear yet, so I'm leaving the decision till later.}int main(int ac, char* av[]){    test_long_options();    test_short_options();    test_dos_options();    test_disguised_long();    test_guessing();    test_arguments();    test_prefix();    test_additional_parser();    test_style_parser();    test_unregistered();    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -