15 #ifndef GOOGLE_TESSERACT 18 static bool IntFlagExists(
const char* flag_name, int32_t* value) {
19 STRING full_flag_name(
"FLAGS_");
20 full_flag_name += flag_name;
22 IntParam *p = ParamUtils::FindParam<IntParam>(
24 if (p ==
nullptr)
return false;
25 *value = (int32_t)(*p);
29 static bool DoubleFlagExists(
const char* flag_name,
double* value) {
30 STRING full_flag_name(
"FLAGS_");
31 full_flag_name += flag_name;
33 DoubleParam *p = ParamUtils::FindParam<DoubleParam>(
35 if (p ==
nullptr)
return false;
36 *value =
static_cast<double>(*p);
40 static bool BoolFlagExists(
const char* flag_name,
bool* value) {
41 STRING full_flag_name(
"FLAGS_");
42 full_flag_name += flag_name;
44 BoolParam *p = ParamUtils::FindParam<BoolParam>(
46 if (p ==
nullptr)
return false;
51 static bool StringFlagExists(
const char* flag_name,
const char** value) {
52 STRING full_flag_name(
"FLAGS_");
53 full_flag_name += flag_name;
55 StringParam *p = ParamUtils::FindParam<StringParam>(
57 *value = (p !=
nullptr) ? p->string() :
nullptr;
61 static void SetIntFlagValue(
const char* flag_name,
const int32_t new_val) {
62 STRING full_flag_name(
"FLAGS_");
63 full_flag_name += flag_name;
65 IntParam *p = ParamUtils::FindParam<IntParam>(
68 p->set_value(new_val);
71 static void SetDoubleFlagValue(
const char* flag_name,
const double new_val) {
72 STRING full_flag_name(
"FLAGS_");
73 full_flag_name += flag_name;
75 DoubleParam *p = ParamUtils::FindParam<DoubleParam>(
78 p->set_value(new_val);
81 static void SetBoolFlagValue(
const char* flag_name,
const bool new_val) {
82 STRING full_flag_name(
"FLAGS_");
83 full_flag_name += flag_name;
85 BoolParam *p = ParamUtils::FindParam<BoolParam>(
88 p->set_value(new_val);
91 static void SetStringFlagValue(
const char* flag_name,
const char* new_val) {
92 STRING full_flag_name(
"FLAGS_");
93 full_flag_name += flag_name;
95 StringParam *p = ParamUtils::FindParam<StringParam>(
98 p->set_value(
STRING(new_val));
101 static bool SafeAtoi(
const char* str,
int* val) {
102 char* endptr =
nullptr;
103 *val = strtol(str, &endptr, 10);
104 return endptr !=
nullptr && *endptr ==
'\0';
107 static bool SafeAtod(
const char* str,
double* val) {
108 char* endptr =
nullptr;
109 *val = strtod(str, &endptr);
110 return endptr !=
nullptr && *endptr ==
'\0';
113 static void PrintCommandLineFlags() {
114 const char* kFlagNamePrefix =
"FLAGS_";
115 const int kFlagNamePrefixLen = strlen(kFlagNamePrefix);
118 kFlagNamePrefix, kFlagNamePrefixLen)) {
119 printf(
" --%s %s (type:int default:%d)\n",
120 GlobalParams()->int_params[i]->name_str() + kFlagNamePrefixLen,
126 if (!strncmp(
GlobalParams()->double_params[i]->name_str(),
127 kFlagNamePrefix, kFlagNamePrefixLen)) {
128 printf(
" --%s %s (type:double default:%g)\n",
129 GlobalParams()->double_params[i]->name_str() + kFlagNamePrefixLen,
131 static_cast<double>(*(
GlobalParams()->double_params[i])));
135 if (!strncmp(
GlobalParams()->bool_params[i]->name_str(),
136 kFlagNamePrefix, kFlagNamePrefixLen)) {
137 printf(
" --%s %s (type:bool default:%s)\n",
138 GlobalParams()->bool_params[i]->name_str() + kFlagNamePrefixLen,
144 if (!strncmp(
GlobalParams()->string_params[i]->name_str(),
145 kFlagNamePrefix, kFlagNamePrefixLen)) {
146 printf(
" --%s %s (type:string default:%s)\n",
147 GlobalParams()->string_params[i]->name_str() + kFlagNamePrefixLen,
155 int* argc,
char*** argv,
156 const bool remove_flags) {
158 printf(
"USAGE: %s\n", usage);
159 PrintCommandLineFlags();
163 if (*argc > 1 && (!strcmp((*argv)[1],
"-v") || !strcmp((*argv)[1],
"--version"))) {
169 for (i = 1; i < *argc; ++i) {
170 const char* current_arg = (*argv)[i];
172 if (current_arg[0] !=
'-') {
178 if (current_arg[0] ==
'-') {
182 if (!strcmp(current_arg,
"help")) {
183 printf(
"Usage:\n %s [OPTION ...]\n\n", usage);
184 PrintCommandLineFlags();
189 const char* equals_position = strchr(current_arg,
'=');
190 const char* rhs =
nullptr;
191 if (equals_position !=
nullptr) {
192 rhs = equals_position + 1;
196 if (equals_position ==
nullptr) {
199 lhs.
assign(current_arg, equals_position - current_arg);
202 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
209 if (IntFlagExists(lhs.
string(), &int_val)) {
210 if (rhs !=
nullptr) {
213 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
216 if (!SafeAtoi(rhs, &int_val)) {
217 tprintf(
"ERROR: Could not parse int from %s in flag %s\n",
223 if (i + 1 >= *argc) {
224 tprintf(
"ERROR: Could not find value argument for flag %s\n",
229 if (!SafeAtoi((*argv)[i], &int_val)) {
230 tprintf(
"ERROR: Could not parse int32_t from %s\n", (*argv)[i]);
235 SetIntFlagValue(lhs.
string(), int_val);
241 if (DoubleFlagExists(lhs.
string(), &double_val)) {
242 if (rhs !=
nullptr) {
245 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
248 if (!SafeAtod(rhs, &double_val)) {
249 tprintf(
"ERROR: Could not parse double from %s in flag %s\n",
255 if (i + 1 >= *argc) {
256 tprintf(
"ERROR: Could not find value argument for flag %s\n",
261 if (!SafeAtod((*argv)[i], &double_val)) {
262 tprintf(
"ERROR: Could not parse double from %s\n", (*argv)[i]);
267 SetDoubleFlagValue(lhs.
string(), double_val);
274 if (BoolFlagExists(lhs.
string(), &bool_val)) {
275 if (rhs ==
nullptr) {
281 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[i]);
284 if (!strcmp(rhs,
"false") || !strcmp(rhs,
"0")) {
286 }
else if (!strcmp(rhs,
"true") || !strcmp(rhs,
"1")) {
289 tprintf(
"ERROR: Could not parse bool from flag %s\n", (*argv)[i]);
293 SetBoolFlagValue(lhs.
string(), bool_val);
298 const char* string_val;
299 if (StringFlagExists(lhs.
string(), &string_val)) {
300 if (rhs !=
nullptr) {
304 if (i + 1 >= *argc) {
305 tprintf(
"ERROR: Could not find string value for flag %s\n",
309 string_val = (*argv)[++i];
312 SetStringFlagValue(lhs.
string(), string_val);
317 tprintf(
"ERROR: Non-existent flag %s\n", (*argv)[i]);
321 (*argv)[i - 1] = (*argv)[0];
330 #include "base/init_google.h" 334 int* argc,
char*** argv,
335 const bool remove_flags) {
336 InitGoogle(usage, argc, argv, remove_flags);
GenericVector< IntParam * > int_params
GenericVector< BoolParam * > bool_params
const char * string() const
GenericVector< StringParam * > string_params
tesseract::ParamsVectors * GlobalParams()
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, const bool remove_flags)
DLLSYM void tprintf(const char *format,...)
GenericVector< DoubleParam * > double_params
static const char * Version()
void assign(const char *cstr, int len)