File: parse_command_line.hpp

package info (click to toggle)
analizo 1.25.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,916 kB
  • sloc: perl: 3,325; cpp: 1,780; ansic: 663; cs: 378; java: 265; sh: 104; makefile: 89
file content (229 lines) | stat: -rw-r--r-- 7,439 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*

**ANALIZO NOTE**

This file was copied here from mlpack project as is just to be used as input on
analizo testsuite in order to fix the issue #120.

- https://github.com/analizo/analizo/issues/120

mlpack repository:

- https://github.com/mlpack/mlpack

Original file was copied from the tag mlpack-3.0.0 from mlpack git repository
and it is located on the path below.

- src/mlpack/bindings/cli/parse_command_line.hpp

Link to the original file on GitHub:

- https://github.com/mlpack/mlpack/blob/mlpack-3.0.0/src/mlpack/bindings/cli/parse_command_line.hpp

See full copyrigth info at:

- https://github.com/mlpack/mlpack/blob/mlpack-3.0.0/COPYRIGHT.txt

*/

/**
 * @file parse_command_line.hpp
 * @author Ryan Curtin
 * @author Matthew Amidon
 *
 * Parse the command line options.
 *
 * mlpack is free software; you may redistribute it and/or modify it under the
 * terms of the 3-clause BSD license.  You should have received a copy of the
 * 3-clause BSD license along with mlpack.  If not, see
 * http://www.opensource.org/licenses/BSD-3-Clause for more information.
 */
#ifndef MLPACK_BINDINGS_CLI_PARSE_COMMAND_LINE_HPP
#define MLPACK_BINDINGS_CLI_PARSE_COMMAND_LINE_HPP

#include <mlpack/core.hpp>
#include <boost/program_options.hpp>
#include "print_help.hpp"

namespace mlpack {
namespace bindings {
namespace cli {

// Add default parameters that are included in every program.
PARAM_FLAG("help", "Default help info.", "h");
PARAM_STRING_IN("info", "Get help on a specific module or option.", "", "");
PARAM_FLAG("verbose", "Display informational messages and the full list of "
    "parameters and timers at the end of execution.", "v");
PARAM_FLAG("version", "Display the version of mlpack.", "V");

/**
 * Parse the command line, setting all of the options inside of the CLI object
 * to their appropriate given values.
 */
void ParseCommandLine(int argc, char** argv)
{
  // First, we need to build the boost::program_options variables for parsing.
  using namespace boost::program_options;
  options_description desc;
  variables_map vmap;

  // Go through list of options in order to add them.
  std::map<std::string, util::ParamData>& parameters = CLI::Parameters();
  typedef std::map<std::string, util::ParamData>::const_iterator IteratorType;
  std::map<std::string, std::string> boostNameMap;
  for (IteratorType it = parameters.begin(); it != parameters.end(); ++it)
  {
    // Add the parameter to desc.
    const util::ParamData& d = it->second;
    CLI::GetSingleton().functionMap[d.tname]["AddToPO"](d, NULL,
        (void*) &desc);

    // Generate the name the user passes on the command line.
    std::string boostName;
    CLI::GetSingleton().functionMap[d.tname]["MapParameterName"](d, NULL,
        (void*) &boostName);
    boostNameMap[boostName] = d.name;
  }

  // Mark that we did parsing.
  CLI::GetSingleton().didParse = true;

  // Parse the command line, then place the values in the right place.
  try
  {
    basic_parsed_options<char> bpo(parse_command_line(argc, argv, desc));

    // Iterate over all the options, looking for duplicate parameters.  If we
    // find any, remove the duplicates.  Note that vector options can have
    // duplicates, so we check for those with max_tokens().
    for (size_t i = 0; i < bpo.options.size(); ++i)
    {
      for (size_t j = i + 1; j < bpo.options.size(); ++j)
      {
        if ((bpo.options[i].string_key == bpo.options[j].string_key) &&
            (desc.find(bpo.options[i].string_key,
                       false).semantic()->max_tokens() <= 1))
        {
          // If a duplicate is found, check to see if either one has a value.
          if (bpo.options[i].value.size() == 0 &&
              bpo.options[j].value.size() == 0)
          {
            // If neither has a value, we'll consider it a duplicate flag and
            // remove the duplicate.  It's important to not break out of this
            // loop because there might be another duplicate later on in the
            // vector.
            bpo.options.erase(bpo.options.begin() + j);
            --j; // Fix the index.
          }
          else
          {
            // If one or both has a value, produce an error and politely
            // terminate.  We pull the name from the original_tokens, rather
            // than from the string_key, because the string_key is the parameter
            // after aliases have been expanded.
            Log::Fatal << "\"" << bpo.options[j].original_tokens[0] << "\" is "
                << "defined multiple times." << std::endl;
          }
        }
      }
    }

    store(bpo, vmap);
  }
  catch (std::exception& ex)
  {
    Log::Fatal << "Caught exception from parsing command line: " << ex.what()
        << std::endl;
  }

  // Now iterate through the filled vmap, and overwrite default values with
  // anything that's found on the command line.
  for (variables_map::iterator i = vmap.begin(); i != vmap.end(); ++i)
  {
    // There is not a possibility of an unknown option, since
    // boost::program_options would have already thrown an exception.  Because
    // some names may be mapped, we have to look through each ParamData object
    // and get its boost name.
    const std::string identifier = boostNameMap[i->first];
    util::ParamData& param = parameters[identifier];
    param.wasPassed = true;
    CLI::GetSingleton().functionMap[param.tname]["SetParam"](param,
        (void*) &vmap[i->first].value(), NULL);
  }

  // Flush the buffer, make sure changes are propagated to vmap.
  notify(vmap);

  // If the user specified any of the default options (--help, --version, or
  // --info), handle those.

  // --version is prioritized over --help.
  if (CLI::HasParam("version"))
  {
    std::cout << CLI::GetSingleton().ProgramName() << ": part of "
        << util::GetVersion() << "." << std::endl;
    exit(0); // Don't do anything else.
  }

  // Default help message.
  if (CLI::HasParam("help"))
  {
    Log::Info.ignoreInput = false;
    PrintHelp();
    exit(0); // The user doesn't want to run the program, he wants help.
  }

  // Info on a specific parameter.
  if (CLI::HasParam("info"))
  {
    Log::Info.ignoreInput = false;
    std::string str = CLI::GetParam<std::string>("info");

    // The info node should always be there, but the user may not have specified
    // anything.
    if (str != "")
    {
      PrintHelp(str);
      exit(0);
    }

    // Otherwise just print the generalized help.
    PrintHelp();
    exit(0);
  }

  // Print whether or not we have debugging symbols.  This won't show anything
  // if we have not compiled in debugging mode.
  Log::Debug << "Compiled with debugging symbols." << std::endl;

  if (CLI::HasParam("verbose"))
  {
    // Give [INFO ] output.
    Log::Info.ignoreInput = false;
  }

  // Now, issue an error if we forgot any required options.
  for (std::map<std::string, util::ParamData>::const_iterator iter =
       parameters.begin(); iter != parameters.end(); ++iter)
  {
    const util::ParamData d = iter->second;
    if (d.required)
    {
      const std::string boostName;
      CLI::GetSingleton().functionMap[d.tname]["MapParameterName"](d, NULL,
          (void*) &boostName);

      if (!vmap.count(boostName))
      {
        Log::Fatal << "Required option --" << boostName << " is undefined."
            << std::endl;
      }
    }
  }
}

} // namespace cli
} // namespace bindings
} // namespace mlpack

#endif