19 #ifndef GOOGLE_TESSERACT
22 static bool IntFlagExists(
const char* flag_name, int32_t* value) {
23 STRING full_flag_name(
"FLAGS_");
24 full_flag_name += flag_name;
26 IntParam *p = ParamUtils::FindParam<IntParam>(
28 if (p ==
nullptr)
return false;
29 *value = (int32_t)(*p);
33 static bool DoubleFlagExists(
const char* flag_name,
double* value) {
34 STRING full_flag_name(
"FLAGS_");
35 full_flag_name += flag_name;
37 DoubleParam *p = ParamUtils::FindParam<DoubleParam>(
39 if (p ==
nullptr)
return false;
40 *value = static_cast<double>(*p);
44 static bool BoolFlagExists(
const char* flag_name,
bool* value) {
45 STRING full_flag_name(
"FLAGS_");
46 full_flag_name += flag_name;
48 BoolParam *p = ParamUtils::FindParam<BoolParam>(
50 if (p ==
nullptr)
return false;
55 static bool StringFlagExists(
const char* flag_name,
const char** value) {
56 STRING full_flag_name(
"FLAGS_");
57 full_flag_name += flag_name;
59 StringParam *p = ParamUtils::FindParam<StringParam>(
61 *value = (p !=
nullptr) ? p->c_str() :
nullptr;
65 static void SetIntFlagValue(
const char* flag_name,
const int32_t new_val) {
66 STRING full_flag_name(
"FLAGS_");
67 full_flag_name += flag_name;
69 IntParam *p = ParamUtils::FindParam<IntParam>(
72 p->set_value(new_val);
75 static void SetDoubleFlagValue(
const char* flag_name,
const double new_val) {
76 STRING full_flag_name(
"FLAGS_");
77 full_flag_name += flag_name;
79 DoubleParam *p = ParamUtils::FindParam<DoubleParam>(
82 p->set_value(new_val);
85 static void SetBoolFlagValue(
const char* flag_name,
const bool new_val) {
86 STRING full_flag_name(
"FLAGS_");
87 full_flag_name += flag_name;
89 BoolParam *p = ParamUtils::FindParam<BoolParam>(
92 p->set_value(new_val);
95 static void SetStringFlagValue(
const char* flag_name,
const char* new_val) {
96 STRING full_flag_name(
"FLAGS_");
97 full_flag_name += flag_name;
99 StringParam *p = ParamUtils::FindParam<StringParam>(
102 p->set_value(
STRING(new_val));
105 static bool SafeAtoi(
const char* str,
int* val) {
106 char* endptr =
nullptr;
107 *val = strtol(str, &endptr, 10);
108 return endptr !=
nullptr && *endptr ==
'\0';
111 static bool SafeAtod(
const char* str,
double* val) {
113 std::stringstream stream(str);
115 stream.imbue(std::locale::classic());
118 bool success = !std::isnan(d);
125 static void PrintCommandLineFlags() {
126 const char* kFlagNamePrefix =
"FLAGS_";
127 const int kFlagNamePrefixLen = strlen(kFlagNamePrefix);
130 kFlagNamePrefix, kFlagNamePrefixLen)) {
131 printf(
" --%s %s (type:int default:%d)\n",
132 GlobalParams()->int_params[i]->name_str() + kFlagNamePrefixLen,
138 if (!strncmp(
GlobalParams()->double_params[i]->name_str(),
139 kFlagNamePrefix, kFlagNamePrefixLen)) {
140 printf(
" --%s %s (type:double default:%g)\n",
141 GlobalParams()->double_params[i]->name_str() + kFlagNamePrefixLen,
143 static_cast<double>(*(
GlobalParams()->double_params[i])));
147 if (!strncmp(
GlobalParams()->bool_params[i]->name_str(),
148 kFlagNamePrefix, kFlagNamePrefixLen)) {
149 printf(
" --%s %s (type:bool default:%s)\n",
150 GlobalParams()->bool_params[i]->name_str() + kFlagNamePrefixLen,
152 bool(*(
GlobalParams()->bool_params[i])) ?
"true" :
"false");
156 if (!strncmp(
GlobalParams()->string_params[i]->name_str(),
157 kFlagNamePrefix, kFlagNamePrefixLen)) {
158 printf(
" --%s %s (type:string default:%s)\n",
159 GlobalParams()->string_params[i]->name_str() + kFlagNamePrefixLen,
167 int* argc,
char*** argv,
168 const bool remove_flags) {
170 printf(
"USAGE: %s\n", usage);
171 PrintCommandLineFlags();
175 if (*argc > 1 && (!strcmp((*argv)[1],
"-v") || !strcmp((*argv)[1],
"--version"))) {
181 for (i = 1; i < *argc; ++i) {
182 const char* current_arg = (*argv)[i];
184 if (current_arg[0] !=
'-') {
190 if (current_arg[0] ==
'-') {
194 if (!strcmp(current_arg,
"help")) {
195 printf(
"Usage:\n %s [OPTION ...]\n\n", usage);
196 PrintCommandLineFlags();
201 const char* equals_position = strchr(current_arg,
'=');
202 const char* rhs =
nullptr;
203 if (equals_position !=
nullptr) {
204 rhs = equals_position + 1;
208 if (equals_position ==
nullptr) {
211 lhs.
assign(current_arg, equals_position - current_arg);
214 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
221 if (IntFlagExists(lhs.
c_str(), &int_val)) {
222 if (rhs !=
nullptr) {
225 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
228 if (!SafeAtoi(rhs, &int_val)) {
229 tprintf(
"ERROR: Could not parse int from %s in flag %s\n",
235 if (i + 1 >= *argc) {
236 tprintf(
"ERROR: Could not find value argument for flag %s\n",
241 if (!SafeAtoi((*argv)[i], &int_val)) {
242 tprintf(
"ERROR: Could not parse int32_t from %s\n", (*argv)[i]);
247 SetIntFlagValue(lhs.
c_str(), int_val);
253 if (DoubleFlagExists(lhs.
c_str(), &double_val)) {
254 if (rhs !=
nullptr) {
257 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
260 if (!SafeAtod(rhs, &double_val)) {
261 tprintf(
"ERROR: Could not parse double from %s in flag %s\n",
267 if (i + 1 >= *argc) {
268 tprintf(
"ERROR: Could not find value argument for flag %s\n",
273 if (!SafeAtod((*argv)[i], &double_val)) {
274 tprintf(
"ERROR: Could not parse double from %s\n", (*argv)[i]);
279 SetDoubleFlagValue(lhs.
c_str(), double_val);
286 if (BoolFlagExists(lhs.
c_str(), &bool_val)) {
287 if (rhs ==
nullptr) {
293 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
296 if (!strcmp(rhs,
"false") || !strcmp(rhs,
"0")) {
298 }
else if (!strcmp(rhs,
"true") || !strcmp(rhs,
"1")) {
301 tprintf(
"ERROR: Could not parse bool from flag %s\n", (*argv)[i]);
305 SetBoolFlagValue(lhs.
c_str(), bool_val);
310 const char* string_val;
311 if (StringFlagExists(lhs.
c_str(), &string_val)) {
312 if (rhs !=
nullptr) {
316 if (i + 1 >= *argc) {
317 tprintf(
"ERROR: Could not find string value for flag %s\n",
321 string_val = (*argv)[++i];
324 SetStringFlagValue(lhs.
c_str(), string_val);
329 tprintf(
"ERROR: Non-existent flag %s\n", (*argv)[i]);
333 (*argv)[i - 1] = (*argv)[0];
342 #include "base/init_google.h"
346 int* argc,
char*** argv,
347 const bool remove_flags) {
348 InitGoogle(usage, argc, argv, remove_flags);