// 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; #include #include #include using namespace std; #if defined(__sun) #include // for putenv on solaris #else #include // for putenv #endif #include "minitest.hpp" #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(const 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(); } namespace command_line { #if 0 // 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. void test_parsing_without_specifying_options() { 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 void test_many_different_options() { options_description desc; desc.add_options() ("foo,f", new untyped_value(), "") ( // Explicit qualification is a workaround for vc6 "bar,b", po::value(), "") ("car,voiture", new untyped_value()) ("dog,dawg", new untyped_value()) ("baz", new untyped_value()) ("plug*", new untyped_value()); const char* cmdline3_[] = { "--foo=12", "-f4", "--bar=11", "-b4", "--voiture=15", "--dawg=16", "--dog=17", "--plug3=10" }; vector cmdline3 = sv(cmdline3_, sizeof(cmdline3_) / sizeof(const char*)); vector