21static bool IntFlagExists(
const char *flag_name, int32_t *
value) {
22 std::string full_flag_name(
"FLAGS_");
23 full_flag_name += flag_name;
24 std::vector<IntParam *> empty;
26 ParamUtils::FindParam<IntParam>(full_flag_name.c_str(),
GlobalParams()->int_params, empty);
34static bool DoubleFlagExists(
const char *flag_name,
double *
value) {
35 std::string full_flag_name(
"FLAGS_");
36 full_flag_name += flag_name;
37 std::vector<DoubleParam *> empty;
38 auto *
p = ParamUtils::FindParam<DoubleParam>(full_flag_name.c_str(),
43 *
value =
static_cast<double>(*p);
47static bool BoolFlagExists(
const char *flag_name,
bool *
value) {
48 std::string full_flag_name(
"FLAGS_");
49 full_flag_name += flag_name;
50 std::vector<BoolParam *> empty;
52 ParamUtils::FindParam<BoolParam>(full_flag_name.c_str(),
GlobalParams()->bool_params, empty);
60static bool StringFlagExists(
const char *flag_name,
const char **
value) {
61 std::string full_flag_name(
"FLAGS_");
62 full_flag_name += flag_name;
63 std::vector<StringParam *> empty;
64 auto *
p = ParamUtils::FindParam<StringParam>(full_flag_name.c_str(),
66 *
value = (
p !=
nullptr) ?
p->c_str() :
nullptr;
70static void SetIntFlagValue(
const char *flag_name,
const int32_t new_val) {
71 std::string full_flag_name(
"FLAGS_");
72 full_flag_name += flag_name;
73 std::vector<IntParam *> empty;
75 ParamUtils::FindParam<IntParam>(full_flag_name.c_str(),
GlobalParams()->int_params, empty);
77 p->set_value(new_val);
80static void SetDoubleFlagValue(
const char *flag_name,
const double new_val) {
81 std::string full_flag_name(
"FLAGS_");
82 full_flag_name += flag_name;
83 std::vector<DoubleParam *> empty;
84 auto *
p = ParamUtils::FindParam<DoubleParam>(full_flag_name.c_str(),
87 p->set_value(new_val);
90static void SetBoolFlagValue(
const char *flag_name,
const bool new_val) {
91 std::string full_flag_name(
"FLAGS_");
92 full_flag_name += flag_name;
93 std::vector<BoolParam *> empty;
95 ParamUtils::FindParam<BoolParam>(full_flag_name.c_str(),
GlobalParams()->bool_params, empty);
97 p->set_value(new_val);
100static void SetStringFlagValue(
const char *flag_name,
const char *new_val) {
101 std::string full_flag_name(
"FLAGS_");
102 full_flag_name += flag_name;
103 std::vector<StringParam *> empty;
104 auto *
p = ParamUtils::FindParam<StringParam>(full_flag_name.c_str(),
107 p->set_value(std::string(new_val));
110static bool SafeAtoi(
const char *str,
int *val) {
111 char *endptr =
nullptr;
112 *val = strtol(str, &endptr, 10);
113 return endptr !=
nullptr && *endptr ==
'\0';
116static bool SafeAtod(
const char *str,
double *val) {
118 std::stringstream stream(str);
120 stream.imbue(std::locale::classic());
123 bool success = !std::isnan(d);
130static void PrintCommandLineFlags() {
131 const char *kFlagNamePrefix =
"FLAGS_";
132 const int kFlagNamePrefixLen = strlen(kFlagNamePrefix);
134 if (!strncmp(param->name_str(), kFlagNamePrefix, kFlagNamePrefixLen)) {
135 printf(
" --%s %s (type:int default:%d)\n",
136 param->name_str() + kFlagNamePrefixLen,
137 param->info_str(), int32_t(*param));
141 if (!strncmp(param->name_str(), kFlagNamePrefix,
142 kFlagNamePrefixLen)) {
143 printf(
" --%s %s (type:double default:%g)\n",
144 param->name_str() + kFlagNamePrefixLen,
146 static_cast<double>(*param));
150 if (!strncmp(param->name_str(), kFlagNamePrefix, kFlagNamePrefixLen)) {
151 printf(
" --%s %s (type:bool default:%s)\n",
152 param->name_str() + kFlagNamePrefixLen,
154 bool(*param) ?
"true" :
"false");
158 if (!strncmp(param->name_str(), kFlagNamePrefix,
159 kFlagNamePrefixLen)) {
160 printf(
" --%s %s (type:string default:%s)\n",
161 param->name_str() + kFlagNamePrefixLen,
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", rhs, (*argv)[
i]);
234 if (
i + 1 >= *argc) {
235 tprintf(
"ERROR: Could not find value argument for flag %s\n", lhs.c_str());
239 if (!SafeAtoi((*argv)[
i], &int_val)) {
240 tprintf(
"ERROR: Could not parse int32_t from %s\n", (*argv)[
i]);
245 SetIntFlagValue(lhs.c_str(), int_val);
251 if (DoubleFlagExists(lhs.c_str(), &double_val)) {
252 if (rhs !=
nullptr) {
255 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[
i]);
258 if (!SafeAtod(rhs, &double_val)) {
259 tprintf(
"ERROR: Could not parse double from %s in flag %s\n", rhs, (*argv)[
i]);
264 if (
i + 1 >= *argc) {
265 tprintf(
"ERROR: Could not find value argument for flag %s\n", lhs.c_str());
269 if (!SafeAtod((*argv)[
i], &double_val)) {
270 tprintf(
"ERROR: Could not parse double from %s\n", (*argv)[
i]);
275 SetDoubleFlagValue(lhs.c_str(), double_val);
282 if (BoolFlagExists(lhs.c_str(), &bool_val)) {
283 if (rhs ==
nullptr) {
289 tprintf(
"ERROR: Bad argument: %s\n", (*argv)[
i]);
292 if (!strcmp(rhs,
"false") || !strcmp(rhs,
"0")) {
294 }
else if (!strcmp(rhs,
"true") || !strcmp(rhs,
"1")) {
297 tprintf(
"ERROR: Could not parse bool from flag %s\n", (*argv)[
i]);
301 SetBoolFlagValue(lhs.c_str(), bool_val);
306 const char *string_val;
307 if (StringFlagExists(lhs.c_str(), &string_val)) {
308 if (rhs !=
nullptr) {
312 if (
i + 1 >= *argc) {
313 tprintf(
"ERROR: Could not find string value for flag %s\n", lhs.c_str());
316 string_val = (*argv)[++
i];
319 SetStringFlagValue(lhs.c_str(), string_val);
324 tprintf(
"ERROR: Non-existent flag %s\n", (*argv)[
i]);
328 (*argv)[
i - 1] = (*argv)[0];
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, const bool remove_flags)
void tprintf(const char *format,...)
tesseract::ParamsVectors * GlobalParams()
static const char * Version()