12 #ifdef USE_STD_NAMESPACE 15 bool IntFlagExists(
const char* flag_name,
inT32* value) {
16 STRING full_flag_name(
"FLAGS_");
17 full_flag_name += flag_name;
19 IntParam *p = ParamUtils::FindParam<IntParam>(
21 if (p == NULL)
return false;
26 bool DoubleFlagExists(
const char* flag_name,
double* value) {
27 STRING full_flag_name(
"FLAGS_");
28 full_flag_name += flag_name;
30 DoubleParam *p = ParamUtils::FindParam<DoubleParam>(
32 if (p == NULL)
return false;
33 *value =
static_cast<double>(*p);
37 bool BoolFlagExists(
const char* flag_name,
bool* value) {
38 STRING full_flag_name(
"FLAGS_");
39 full_flag_name += flag_name;
41 BoolParam *p = ParamUtils::FindParam<BoolParam>(
43 if (p == NULL)
return false;
48 bool StringFlagExists(
const char* flag_name,
const char** value) {
49 STRING full_flag_name(
"FLAGS_");
50 full_flag_name += flag_name;
52 StringParam *p = ParamUtils::FindParam<StringParam>(
54 *value = (p != NULL) ? p->string() : NULL;
59 void SetIntFlagValue(
const char* flag_name,
const inT32 new_val) {
60 STRING full_flag_name(
"FLAGS_");
61 full_flag_name += flag_name;
63 IntParam *p = ParamUtils::FindParam<IntParam>(
66 p->set_value(new_val);
69 void SetDoubleFlagValue(
const char* flag_name,
const double new_val) {
70 STRING full_flag_name(
"FLAGS_");
71 full_flag_name += flag_name;
73 DoubleParam *p = ParamUtils::FindParam<DoubleParam>(
76 p->set_value(new_val);
79 void SetBoolFlagValue(
const char* flag_name,
const bool new_val) {
80 STRING full_flag_name(
"FLAGS_");
81 full_flag_name += flag_name;
83 BoolParam *p = ParamUtils::FindParam<BoolParam>(
86 p->set_value(new_val);
89 void SetStringFlagValue(
const char* flag_name,
const char* new_val) {
90 STRING full_flag_name(
"FLAGS_");
91 full_flag_name += flag_name;
93 StringParam *p = ParamUtils::FindParam<StringParam>(
96 p->set_value(
STRING(new_val));
99 bool SafeAtoi(
const char* str,
int* val) {
101 *val = strtol(str, &endptr, 10);
102 return endptr != NULL && *endptr ==
'\0';
105 bool SafeAtod(
const char* str,
double* val) {
107 *val = strtod(str, &endptr);
108 return endptr != NULL && *endptr ==
'\0';
111 void PrintCommandLineFlags() {
112 const char* kFlagNamePrefix =
"FLAGS_";
113 const int kFlagNamePrefixLen = strlen(kFlagNamePrefix);
116 kFlagNamePrefix, kFlagNamePrefixLen)) {
117 printf(
" --%s %s (type:int default:%d)\n",
118 GlobalParams()->int_params[i]->name_str() + kFlagNamePrefixLen,
124 if (!strncmp(
GlobalParams()->double_params[i]->name_str(),
125 kFlagNamePrefix, kFlagNamePrefixLen)) {
126 printf(
" --%s %s (type:double default:%g)\n",
127 GlobalParams()->double_params[i]->name_str() + kFlagNamePrefixLen,
129 static_cast<double>(*(
GlobalParams()->double_params[i])));
133 if (!strncmp(
GlobalParams()->bool_params[i]->name_str(),
134 kFlagNamePrefix, kFlagNamePrefixLen)) {
135 printf(
" --%s %s (type:bool default:%s)\n",
136 GlobalParams()->bool_params[i]->name_str() + kFlagNamePrefixLen,
142 if (!strncmp(
GlobalParams()->string_params[i]->name_str(),
143 kFlagNamePrefix, kFlagNamePrefixLen)) {
144 printf(
" --%s %s (type:string default:%s)\n",
145 GlobalParams()->string_params[i]->name_str() + kFlagNamePrefixLen,
154 int* argc,
char*** argv,
155 const bool remove_flags) {
157 printf(
"USAGE: %s\n", usage);
158 PrintCommandLineFlags();
163 for (i = 1; i < *argc; ++i) {
164 const char* current_arg = (*argv)[i];
166 if (current_arg[0] !=
'-') {
171 while (*current_arg ==
'-') {
175 if (!strcmp(current_arg,
"help") ||
176 !strcmp(current_arg,
"helpshort")) {
178 PrintCommandLineFlags();
183 const char* equals_position = strchr(current_arg,
'=');
184 const char* rhs = NULL;
185 if (equals_position != NULL) {
186 rhs = equals_position + 1;
190 if (equals_position == NULL) {
193 lhs.
assign(current_arg, equals_position - current_arg);
196 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
203 if (IntFlagExists(lhs.
string(), &int_val)) {
207 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
210 if (!SafeAtoi(rhs, &int_val)) {
211 tprintf(
"ERROR: Could not parse int from %s in flag %s\n",
217 if (i + 1 >= *argc) {
218 tprintf(
"ERROR: Could not find value argument for flag %s\n",
223 if (!SafeAtoi((*argv)[i], &int_val)) {
224 tprintf(
"ERROR: Could not parse inT32 from %s\n", (*argv)[i]);
229 SetIntFlagValue(lhs.
string(), int_val);
235 if (DoubleFlagExists(lhs.
string(), &double_val)) {
239 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
242 if (!SafeAtod(rhs, &double_val)) {
243 tprintf(
"ERROR: Could not parse double from %s in flag %s\n",
249 if (i + 1 >= *argc) {
250 tprintf(
"ERROR: Could not find value argument for flag %s\n",
255 if (!SafeAtod((*argv)[i], &double_val)) {
256 tprintf(
"ERROR: Could not parse double from %s\n", (*argv)[i]);
261 SetDoubleFlagValue(lhs.
string(), double_val);
268 if (BoolFlagExists(lhs.
string(), &bool_val)) {
275 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
278 if (!strcmp(rhs,
"false") || !strcmp(rhs,
"0")) {
280 }
else if (!strcmp(rhs,
"true") || !strcmp(rhs,
"1")) {
283 tprintf(
"ERROR: Could not parse bool from flag %s\n", (*argv)[i]);
287 SetBoolFlagValue(lhs.
string(), bool_val);
292 const char* string_val;
293 if (StringFlagExists(lhs.
string(), &string_val)) {
298 if (i + 1 >= *argc) {
299 tprintf(
"ERROR: Could not find string value for flag %s\n",
303 string_val = (*argv)[++i];
306 SetStringFlagValue(lhs.
string(), string_val);
311 tprintf(
"ERROR: Non-existent flag %s\n", (*argv)[i]);
315 (*argv)[i - 1] = (*argv)[0];
324 #include "base/init_google.h" 328 int* argc,
char*** argv,
329 const bool remove_flags) {
330 InitGoogle(usage, argc, argv, remove_flags);
GenericVector< BoolParam * > bool_params
tesseract::ParamsVectors * GlobalParams()
GenericVector< DoubleParam * > double_params
GenericVector< StringParam * > string_params
GenericVector< IntParam * > int_params
const char * string() const
void assign(const char *cstr, int len)
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, const bool remove_flags)