3 #ifdef USE_STD_NAMESPACE
6 bool IntFlagExists(
const char* flag_name,
inT32* value) {
7 STRING full_flag_name(
"FLAGS_");
8 full_flag_name += flag_name;
10 IntParam *p = ParamUtils::FindParam<IntParam>(
12 if (p ==
NULL)
return false;
17 bool DoubleFlagExists(
const char* flag_name,
double* value) {
18 STRING full_flag_name(
"FLAGS_");
19 full_flag_name += flag_name;
21 DoubleParam *p = ParamUtils::FindParam<DoubleParam>(
23 if (p ==
NULL)
return false;
24 *value =
static_cast<double>(*p);
28 bool BoolFlagExists(
const char* flag_name,
bool* value) {
29 STRING full_flag_name(
"FLAGS_");
30 full_flag_name += flag_name;
32 BoolParam *p = ParamUtils::FindParam<BoolParam>(
34 if (p ==
NULL)
return false;
39 bool StringFlagExists(
const char* flag_name,
const char** value) {
40 STRING full_flag_name(
"FLAGS_");
41 full_flag_name += flag_name;
43 StringParam *p = ParamUtils::FindParam<StringParam>(
45 *value = (p !=
NULL) ? p->string() :
NULL;
50 void SetIntFlagValue(
const char* flag_name,
const inT32 new_val) {
51 STRING full_flag_name(
"FLAGS_");
52 full_flag_name += flag_name;
54 IntParam *p = ParamUtils::FindParam<IntParam>(
57 p->set_value(new_val);
60 void SetDoubleFlagValue(
const char* flag_name,
const double new_val) {
61 STRING full_flag_name(
"FLAGS_");
62 full_flag_name += flag_name;
64 DoubleParam *p = ParamUtils::FindParam<DoubleParam>(
67 p->set_value(new_val);
70 void SetBoolFlagValue(
const char* flag_name,
const bool new_val) {
71 STRING full_flag_name(
"FLAGS_");
72 full_flag_name += flag_name;
74 BoolParam *p = ParamUtils::FindParam<BoolParam>(
77 p->set_value(new_val);
80 void SetStringFlagValue(
const char* flag_name,
const char* new_val) {
81 STRING full_flag_name(
"FLAGS_");
82 full_flag_name += flag_name;
84 StringParam *p = ParamUtils::FindParam<StringParam>(
87 p->set_value(
STRING(new_val));
90 bool SafeAtoi(
const char* str,
int* val) {
92 *val = strtol(str, &endptr, 10);
93 return endptr !=
NULL && *endptr ==
'\0';
96 bool SafeAtod(
const char* str,
double* val) {
98 *val = strtod(str, &endptr);
99 return endptr !=
NULL && *endptr ==
'\0';
102 void PrintCommandLineFlags() {
103 const char* kFlagNamePrefix =
"FLAGS_";
104 const int kFlagNamePrefixLen = strlen(kFlagNamePrefix);
107 kFlagNamePrefix, kFlagNamePrefixLen)) {
108 tprintf(
" --%s %s (type:int default:%d)\n",
109 GlobalParams()->int_params[i]->name_str() + kFlagNamePrefixLen,
115 if (!strncmp(
GlobalParams()->double_params[i]->name_str(),
116 kFlagNamePrefix, kFlagNamePrefixLen)) {
117 tprintf(
" --%s %s (type:double default:%g)\n",
118 GlobalParams()->double_params[i]->name_str() + kFlagNamePrefixLen,
120 static_cast<double>(*(
GlobalParams()->double_params[i])));
124 if (!strncmp(
GlobalParams()->bool_params[i]->name_str(),
125 kFlagNamePrefix, kFlagNamePrefixLen)) {
126 tprintf(
" --%s %s (type:bool default:%s)\n",
127 GlobalParams()->bool_params[i]->name_str() + kFlagNamePrefixLen,
133 if (!strncmp(
GlobalParams()->string_params[i]->name_str(),
134 kFlagNamePrefix, kFlagNamePrefixLen)) {
135 tprintf(
" --%s %s (type:string default:%s)\n",
136 GlobalParams()->string_params[i]->name_str() + kFlagNamePrefixLen,
145 int* argc,
char*** argv,
146 const bool remove_flags) {
148 for (i = 1; i < *argc; ++i) {
149 const char* current_arg = (*argv)[i];
151 if (current_arg[0] !=
'-') {
156 while (*current_arg ==
'-') {
160 if (!strcmp(current_arg,
"help") ||
161 !strcmp(current_arg,
"helpshort")) {
163 PrintCommandLineFlags();
168 const char* equals_position = strchr(current_arg,
'=');
169 const char* rhs =
NULL;
170 if (equals_position !=
NULL) {
171 rhs = equals_position + 1;
175 if (equals_position ==
NULL) {
178 lhs.
assign(current_arg, equals_position - current_arg);
181 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
188 if (IntFlagExists(lhs.
string(), &int_val)) {
192 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
195 if (!SafeAtoi(rhs, &int_val)) {
196 tprintf(
"ERROR: Could not parse int from %s in flag %s\n",
202 if (i + 1 >= *argc) {
203 tprintf(
"ERROR: Could not find value argument for flag %s\n",
208 if (!SafeAtoi((*argv)[i], &int_val)) {
209 tprintf(
"ERROR: Could not parse inT32 from %s\n", (*argv)[i]);
214 SetIntFlagValue(lhs.
string(), int_val);
220 if (DoubleFlagExists(lhs.
string(), &double_val)) {
224 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
227 if (!SafeAtod(rhs, &double_val)) {
228 tprintf(
"ERROR: Could not parse double from %s in flag %s\n",
234 if (i + 1 >= *argc) {
235 tprintf(
"ERROR: Could not find value argument for flag %s\n",
240 if (!SafeAtod((*argv)[i], &double_val)) {
241 tprintf(
"ERROR: Could not parse double from %s\n", (*argv)[i]);
246 SetDoubleFlagValue(lhs.
string(), double_val);
253 if (BoolFlagExists(lhs.
string(), &bool_val)) {
260 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
263 if (!strcmp(rhs,
"false") || !strcmp(rhs,
"0")) {
265 }
else if (!strcmp(rhs,
"true") || !strcmp(rhs,
"1")) {
268 tprintf(
"ERROR: Could not parse bool from flag %s\n", (*argv)[i]);
272 SetBoolFlagValue(lhs.
string(), bool_val);
277 const char* string_val;
278 if (StringFlagExists(lhs.
string(), &string_val)) {
283 if (i + 1 >= *argc) {
284 tprintf(
"ERROR: Could not find string value for flag %s\n",
288 string_val = (*argv)[++i];
291 SetStringFlagValue(lhs.
string(), string_val);
296 tprintf(
"ERROR: Non-existent flag %s\n", (*argv)[i]);
300 (*argv)[i - 1] = (*argv)[0];
309 #include "base/init_google.h"
313 int* argc,
char*** argv,
314 const bool remove_flags) {
315 InitGoogle(usage, argc, argv, remove_flags);
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, const bool remove_flags)
GenericVector< IntParam * > int_params
GenericVector< BoolParam * > bool_params
void assign(const char *cstr, int len)
GenericVector< DoubleParam * > double_params
tesseract::ParamsVectors * GlobalParams()
const char * string() const
GenericVector< StringParam * > string_params