mirror of
https://github.com/gnif/LookingGlass.git
synced 2024-11-25 14:57:20 +00:00
[common] seperate validator and clean up output
This commit is contained in:
parent
9886316e07
commit
86c7286aad
@ -52,7 +52,7 @@ struct Option
|
|||||||
const char * description;
|
const char * description;
|
||||||
struct OptionValue value;
|
struct OptionValue value;
|
||||||
|
|
||||||
bool (*validator)(struct OptionValue * value);
|
bool (*validator)(struct OptionValue * value, const char ** error);
|
||||||
void (*printHelp)();
|
void (*printHelp)();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -68,6 +68,9 @@ bool option_get_bool (const char * module, const char * name);
|
|||||||
// called by the main application to parse the command line arguments
|
// called by the main application to parse the command line arguments
|
||||||
bool option_parse(int argc, char * argv[]);
|
bool option_parse(int argc, char * argv[]);
|
||||||
|
|
||||||
|
// called by the main application to validate the option values
|
||||||
|
bool option_validate();
|
||||||
|
|
||||||
// print out the options, help, and their current values
|
// print out the options, help, and their current values
|
||||||
void option_print();
|
void option_print();
|
||||||
|
|
||||||
|
@ -199,23 +199,38 @@ bool option_parse(int argc, char * argv[])
|
|||||||
free(arg);
|
free(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool option_validate()
|
||||||
|
{
|
||||||
// validate the option values
|
// validate the option values
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
for(int i = 0; i < state.oCount; ++i)
|
for(int i = 0; i < state.oCount; ++i)
|
||||||
{
|
{
|
||||||
struct Option * o = &state.options[i];
|
struct Option * o = &state.options[i];
|
||||||
|
const char * error = NULL;
|
||||||
if (o->validator)
|
if (o->validator)
|
||||||
if (!o->validator(&o->value))
|
if (!o->validator(&o->value, &error))
|
||||||
{
|
{
|
||||||
DEBUG_ERROR("Invalid value provided to option %s:%s", o->module, o->name);
|
printf("\nInvalid value provided to the option: %s:%s\n", o->module, o->name);
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
printf("\n Error: %s\n", error);
|
||||||
|
|
||||||
if (o->printHelp)
|
if (o->printHelp)
|
||||||
|
{
|
||||||
|
printf("\n");
|
||||||
o->printHelp();
|
o->printHelp();
|
||||||
|
}
|
||||||
|
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!ok)
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user