Commit 225907d3 authored by Dominik Charousset's avatar Dominik Charousset

Support unquoted atoms on the CLI

parent cb4f6977
......@@ -144,8 +144,20 @@ auto config_option_set::parse(config_map& config, argument_iterator first,
auto val = config_value::parse(slice);
if (!val)
return pec::illegal_argument;
if (opt.check(*val) != none)
return pec::type_mismatch;
if (opt.check(*val) != none) {
// The parser defaults to unescaped strings. For example, --foo=bar
// will interpret `bar` as a string. Hence, we'll get a type mismatch
// if `foo` expects an atom. We check this special case here to avoid
// the clumsy --foo="'bar'" notation on the command line.
if (holds_alternative<std::string>(*val)
&& opt.type_name() == "atom"
&& slice.substr(0, 1) != "\""
&& slice.size() <= 10) {
*val = atom_from_string(std::string{slice.begin(), slice.end()});
} else {
return pec::type_mismatch;
}
}
opt.store(*val);
submap[opt_name] = std::move(*val);
}
......
......@@ -114,4 +114,25 @@ CAF_TEST(implicit global) {
CAF_CHECK_EQUAL(get_or(cfg, "global.help", false), true);
}
CAF_TEST(atom parameters) {
opts.add<atom_value>("global", "value,v");
CAF_MESSAGE("test atom option without quotes");
auto parse_args = [&](std::vector<std::string> args) -> expected<atom_value> {
dictionary<config_value::dictionary> cfg;
auto res = opts.parse(cfg, std::move(args));
if (res.first != pec::success)
return res.first;
auto atm = get_if<atom_value>(&cfg, "global.value");
if (atm == none)
return sec::invalid_argument;
return *atm;
};
CAF_CHECK_EQUAL(parse_args({"-v", "'foobar'"}), atom("foobar"));
CAF_CHECK_EQUAL(parse_args({"-v'foobar'"}), atom("foobar"));
CAF_CHECK_EQUAL(parse_args({"--value='foobar'"}), atom("foobar"));
CAF_CHECK_EQUAL(parse_args({"-v", "foobar"}), atom("foobar"));
CAF_CHECK_EQUAL(parse_args({"-vfoobar"}), atom("foobar"));
CAF_CHECK_EQUAL(parse_args({"--value=foobar"}), atom("foobar"));
}
CAF_TEST_FIXTURE_SCOPE_END()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment