FST  openfst-1.7.1
OpenFst Library
flags.cc
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 //
13 // Google-style flag handling definitions.
14 
15 #include <cstring>
16 
17 #include <fst/compat.h>
18 #include <fst/flags.h>
19 
20 static const char *private_tmpdir = getenv("TMPDIR");
21 
22 DEFINE_int32(v, 0, "verbosity level");
23 DEFINE_bool(help, false, "show usage information");
24 DEFINE_bool(helpshort, false, "show brief usage information");
25 DEFINE_string(tmpdir, private_tmpdir ? private_tmpdir : "/tmp",
26  "temporary directory");
27 
28 using namespace std;
29 
30 static string flag_usage;
31 static string prog_src;
32 
33 // Sets prog_src to src.
34 static void SetProgSrc(const char *src) {
35  prog_src = src;
36  // Remove "-main" in src filename. Flags are defined in fstx.cc but SetFlags()
37  // is called in fstx-main.cc, which results in a filename mismatch in
38  // ShowUsageRestrict() below.
39  static constexpr char kMainSuffix[] = "-main.cc";
40  const int prefix_length = prog_src.size() - strlen(kMainSuffix);
41  if (prefix_length > 0 && prog_src.substr(prefix_length) == kMainSuffix) {
42  prog_src.erase(prefix_length, strlen("-main"));
43  }
44 }
45 
46 void SetFlags(const char *usage, int *argc, char ***argv,
47  bool remove_flags, const char *src) {
48  flag_usage = usage;
49  SetProgSrc(src);
50 
51  int index = 1;
52  for (; index < *argc; ++index) {
53  string argval = (*argv)[index];
54  if (argval[0] != '-' || argval == "-") break;
55  while (argval[0] == '-') argval = argval.substr(1); // Removes initial '-'.
56  string arg = argval;
57  string val = "";
58  // Splits argval (arg=val) into arg and val.
59  auto pos = argval.find("=");
60  if (pos != string::npos) {
61  arg = argval.substr(0, pos);
62  val = argval.substr(pos + 1);
63  }
64  auto bool_register = FlagRegister<bool>::GetRegister();
65  if (bool_register->SetFlag(arg, val))
66  continue;
67  auto string_register = FlagRegister<string>::GetRegister();
68  if (string_register->SetFlag(arg, val))
69  continue;
70  auto int32_register = FlagRegister<int32>::GetRegister();
71  if (int32_register->SetFlag(arg, val))
72  continue;
73  auto int64_register = FlagRegister<int64>::GetRegister();
74  if (int64_register->SetFlag(arg, val))
75  continue;
76  auto double_register = FlagRegister<double>::GetRegister();
77  if (double_register->SetFlag(arg, val))
78  continue;
79  LOG(FATAL) << "SetFlags: Bad option: " << (*argv)[index];
80  }
81  if (remove_flags) {
82  for (auto i = 0; i < *argc - index; ++i) {
83  (*argv)[i + 1] = (*argv)[i + index];
84  }
85  *argc -= index - 1;
86  }
87  if (FLAGS_help) {
88  ShowUsage(true);
89  exit(1);
90  }
91  if (FLAGS_helpshort) {
92  ShowUsage(false);
93  exit(1);
94  }
95 }
96 
97 // If flag is defined in file 'src' and 'in_src' true or is not
98 // defined in file 'src' and 'in_src' is false, then print usage.
99 static void
100 ShowUsageRestrict(const std::set<pair<string, string>> &usage_set,
101  const string &src, bool in_src, bool show_file) {
102  string old_file;
103  bool file_out = false;
104  bool usage_out = false;
105  for (const auto &pair : usage_set) {
106  const auto &file = pair.first;
107  const auto &usage = pair.second;
108  bool match = file == src;
109  if ((match && !in_src) || (!match && in_src)) continue;
110  if (file != old_file) {
111  if (show_file) {
112  if (file_out) cout << "\n";
113  cout << "Flags from: " << file << "\n";
114  file_out = true;
115  }
116  old_file = file;
117  }
118  cout << usage << "\n";
119  usage_out = true;
120  }
121  if (usage_out) cout << "\n";
122 }
123 
124 void ShowUsage(bool long_usage) {
125  std::set<pair<string, string>> usage_set;
126  cout << flag_usage << "\n";
127  auto bool_register = FlagRegister<bool>::GetRegister();
128  bool_register->GetUsage(&usage_set);
129  auto string_register = FlagRegister<string>::GetRegister();
130  string_register->GetUsage(&usage_set);
131  auto int32_register = FlagRegister<int32>::GetRegister();
132  int32_register->GetUsage(&usage_set);
133  auto int64_register = FlagRegister<int64>::GetRegister();
134  int64_register->GetUsage(&usage_set);
135  auto double_register = FlagRegister<double>::GetRegister();
136  double_register->GetUsage(&usage_set);
137  if (!prog_src.empty()) {
138  cout << "PROGRAM FLAGS:\n\n";
139  ShowUsageRestrict(usage_set, prog_src, true, false);
140  }
141  if (!long_usage) return;
142  if (!prog_src.empty()) cout << "LIBRARY FLAGS:\n\n";
143  ShowUsageRestrict(usage_set, prog_src, false, true);
144 }
#define LOG(type)
Definition: log.h:48
static FlagRegister< T > * GetRegister()
Definition: flags.h:77
DEFINE_bool(help, false,"show usage information")
void SetFlags(const char *usage, int *argc, char ***argv, bool remove_flags, const char *src)
Definition: flags.cc:46
DEFINE_int32(v, 0,"verbosity level")
DEFINE_string(tmpdir, private_tmpdir?private_tmpdir:"/tmp","temporary directory")
void ShowUsage(bool long_usage)
Definition: flags.cc:124