// Copyright Vladimir Prus 2002-2004. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) #include #include #include using namespace boost::program_options; // We'll use po::value everywhere to workaround vc6 bug. namespace po = boost::program_options; #include using namespace boost; #define BOOST_INCLUDE_MAIN // for testing, include rather than link #include #include using namespace std; #include // for putenv #define TEST_CHECK_THROW(expression, exception, description) \ try \ { \ expression; \ BOOST_ERROR(description);\ throw 10; \ } \ catch(exception &) \ { \ } pair > > msp(const string& s1) { return std::make_pair(s1, vector< vector >()); } pair > > msp(const string& s1, const string& s2) { vector< vector > v(1); v[0].push_back(s2); return std::make_pair(s1, v); } void check_value(const option& option, const char* name, const char* value) { BOOST_CHECK(option.string_key == name); BOOST_REQUIRE(option.value.size() == 1); BOOST_CHECK(option.value.front() == value); } vector sv(char* array[], unsigned size) { vector r; for (unsigned i = 0; i < size; ++i) r.push_back(array[i]); return r; } pair additional_parser(const std::string&) { return pair(); } void test_command_line() { // The following commented out blocks used to test parsing // command line without syntax specification behaviour. // It is disabled now and probably will never be enabled again: // it is not possible to figure out what command line means without // user's help. #if 0 char* cmdline1[] = { "--a", "--b=12", "-f", "-g4", "-", "file" }; options_and_arguments a1 = parse_command_line(cmdline1, cmdline1 + sizeof(cmdline1)/sizeof(cmdline1[0])); BOOST_REQUIRE(a1.options().size() == 4); BOOST_CHECK(a1.options()[0] == msp("a", "")); BOOST_CHECK(a1.options()[1] == msp("b", "12")); BOOST_CHECK(a1.options()[2] == msp("-f", "")); BOOST_CHECK(a1.options()[3] == msp("-g", "4")); BOOST_REQUIRE(a1.arguments().size() == 2); BOOST_CHECK(a1.arguments()[0] == "-"); BOOST_CHECK(a1.arguments()[1] == "file"); char* cmdline2[] = { "--a", "--", "file" }; options_and_arguments a2 = parse_command_line(cmdline2, cmdline2 + sizeof(cmdline2)/sizeof(cmdline2[0])); BOOST_REQUIRE(a2.options().size() == 1); BOOST_CHECK(a2.options()[0] == msp("a", "")); BOOST_CHECK(a2.arguments().size() == 1); BOOST_CHECK(a2.arguments()[0] == "file"); #endif options_description desc; desc.add_options() ("foo,f", new untyped_value(), "") // Explicit qualification is a workaround for vc6 ("bar,b", po::value(), "") ("baz", new untyped_value()) ("plug*", new untyped_value()) ; char* cmdline3_[] = { "--foo=12", "-f4", "--bar=11", "-b4", "--plug3=10"}; vector cmdline3 = sv(cmdline3_, sizeof(cmdline3_)/sizeof(cmdline3_[0])); vector