mirror of
https://github.com/boostorg/program_options.git
synced 2026-01-19 04:22:15 +00:00
Revive 'additional parser'.
[SVN r28415]
This commit is contained in:
@@ -87,9 +87,6 @@ namespace boost { namespace program_options { namespace detail {
|
||||
std::vector<option> parse_terminator(
|
||||
std::vector<std::string>& args);
|
||||
|
||||
|
||||
#if 0
|
||||
|
||||
/** Set additional parser. This will be called for each token
|
||||
of command line. If first string in pair is not empty,
|
||||
then the token is considered matched by this parser,
|
||||
@@ -100,8 +97,6 @@ namespace boost { namespace program_options { namespace detail {
|
||||
*/
|
||||
void set_additional_parser(additional_parser p);
|
||||
|
||||
#endif
|
||||
|
||||
void check_style(int style) const;
|
||||
|
||||
|
||||
|
||||
@@ -163,6 +163,7 @@ namespace boost { namespace program_options { namespace detail {
|
||||
//
|
||||
// When some 'style parser' consumes some input
|
||||
// we start with the first style parser.
|
||||
assert(m_desc);
|
||||
|
||||
vector<style_parser> style_parsers;
|
||||
if (style & allow_long)
|
||||
@@ -185,6 +186,18 @@ namespace boost { namespace program_options { namespace detail {
|
||||
vector<option> result;
|
||||
while(!args.empty())
|
||||
{
|
||||
if (m_additional_parser) {
|
||||
pair<string, string> r = m_additional_parser(args[0]);
|
||||
if (!r.first.empty()) {
|
||||
option next;
|
||||
next.string_key = r.first;
|
||||
next.value.push_back(r.second);
|
||||
result.push_back(next);
|
||||
args.erase(args.begin());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
bool ok = false;
|
||||
for(unsigned i = 0; i < style_parsers.size(); ++i)
|
||||
{
|
||||
@@ -419,17 +432,11 @@ namespace boost { namespace program_options { namespace detail {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
|
||||
void
|
||||
cmdline::set_additional_parser(additional_parser p)
|
||||
{
|
||||
m_additional_parser = p;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
}}}
|
||||
|
||||
@@ -431,6 +431,41 @@ void test_prefix()
|
||||
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>();
|
||||
}
|
||||
|
||||
|
||||
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");
|
||||
|
||||
detail::cmdline cmd(input, command_line_style::default_style);
|
||||
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");
|
||||
}
|
||||
|
||||
int test_main(int ac, char* av[])
|
||||
{
|
||||
test_long_options();
|
||||
@@ -440,6 +475,7 @@ int test_main(int ac, char* av[])
|
||||
test_guessing();
|
||||
test_arguments();
|
||||
test_prefix();
|
||||
test_additional_parser();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user