CommandLineParser: add special <none> value to disallow empty strings

some mandatory string keys like paths must not be empty. Add the special
default value `<none>` so the CommandLineParser can enforce this and
generate an according error message for us.
This commit is contained in:
Pavel Rojtberg
2015-09-23 12:03:44 +02:00
parent a388806555
commit 1dd7f546af
3 changed files with 47 additions and 23 deletions

View File

@@ -4,6 +4,20 @@
namespace cv
{
namespace {
static const char* noneValue = "<none>";
static String cat_string(const String& str)
{
int left = 0, right = (int)str.length();
while( left <= right && str[left] == ' ' )
left++;
while( right > left && str[right-1] == ' ' )
right--;
return left >= right ? String("") : str.substr(left, right-left);
}
}
struct CommandLineParserParams
{
public:
@@ -27,7 +41,6 @@ struct CommandLineParser::Impl
std::vector<String> split_range_string(const String& str, char fs, char ss) const;
std::vector<String> split_string(const String& str, char symbol = ' ', bool create_empty_item = false) const;
String cat_string(const String& str) const;
void apply_params(const String& key, const String& value);
void apply_params(int i, String value);
@@ -98,10 +111,10 @@ void CommandLineParser::getByName(const String& name, bool space_delete, int typ
{
String v = impl->data[i].def_value;
if (space_delete)
v = impl->cat_string(v);
v = cat_string(v);
// the key was neither specified nor has it a default value
if(v.empty() && type != Param::STRING) {
if((v.empty() && type != Param::STRING) || v == noneValue) {
impl->error = true;
impl->error_message = impl->error_message + "Missing parameter: '" + name + "'\n";
return;
@@ -133,10 +146,10 @@ void CommandLineParser::getByIndex(int index, bool space_delete, int type, void*
if (impl->data[i].number == index)
{
String v = impl->data[i].def_value;
if (space_delete == true) v = impl->cat_string(v);
if (space_delete == true) v = cat_string(v);
// the key was neither specified nor has it a default value
if(v.empty() && type != Param::STRING) {
if((v.empty() && type != Param::STRING) || v == noneValue) {
impl->error = true;
impl->error_message = impl->error_message + format("Missing parameter #%d\n", index);
return;
@@ -198,7 +211,7 @@ CommandLineParser::CommandLineParser(int argc, const char* const argv[], const S
CommandLineParserParams p;
p.keys = impl->split_string(l[0]);
p.def_value = l[1];
p.help_message = impl->cat_string(l[2]);
p.help_message = cat_string(l[2]);
p.number = -1;
if (p.keys.size() <= 0)
{
@@ -317,16 +330,6 @@ void CommandLineParser::Impl::sort_params()
std::sort (data.begin(), data.end(), cmp_params);
}
String CommandLineParser::Impl::cat_string(const String& str) const
{
int left = 0, right = (int)str.length();
while( left <= right && str[left] == ' ' )
left++;
while( right > left && str[right-1] == ' ' )
right--;
return left >= right ? String("") : str.substr(left, right-left);
}
String CommandLineParser::getPathToApplication() const
{
return impl->path_to_app;
@@ -340,7 +343,8 @@ bool CommandLineParser::has(const String& name) const
{
if (name == impl->data[i].keys[j])
{
return !impl->cat_string(impl->data[i].def_value).empty();
const String v = cat_string(impl->data[i].def_value);
return !v.empty() && v != noneValue;
}
}
}
@@ -404,7 +408,7 @@ void CommandLineParser::printMessage() const
printf(", ");
}
}
String dv = impl->cat_string(impl->data[i].def_value);
String dv = cat_string(impl->data[i].def_value);
if (dv.compare("") != 0)
{
printf(" (value:%s)", dv.c_str());
@@ -424,7 +428,7 @@ void CommandLineParser::printMessage() const
printf("%s", k.c_str());
String dv = impl->cat_string(impl->data[i].def_value);
String dv = cat_string(impl->data[i].def_value);
if (dv.compare("") != 0)
{
printf(" (value:%s)", dv.c_str());