www.mooseframework.org
CommandLine.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 // MOOSE includes
16 #include "CommandLine.h"
17 #include "MooseInit.h"
18 #include "MooseUtils.h"
19 #include "InputParameters.h"
20 
21 // C++ includes
22 #include <iomanip>
23 
24 CommandLine::CommandLine(int argc, char * argv[]) : _argc(argc), _argv(argv)
25 {
26  for (int i = 0; i < argc; i++)
27  _args.push_back(std::string(argv[i]));
28 }
29 
31 
32 void
34 {
35  for (const auto & it : params)
36  {
37  Option cli_opt;
38  std::vector<std::string> syntax;
39  std::string orig_name = it.first;
40 
41  cli_opt.description = params.getDocString(orig_name);
42  if (!params.isPrivate(orig_name))
43  // If a param is private then it shouldn't have any command line syntax.
44  syntax = params.getSyntax(orig_name);
45  cli_opt.cli_syntax = syntax;
46  cli_opt.required = false;
47  InputParameters::Parameter<bool> * bool_type =
48  dynamic_cast<InputParameters::Parameter<bool> *>(it.second);
49  if (bool_type)
51  else
53 
54  addOption(orig_name, cli_opt);
55  }
56 }
57 
58 void
60 {
61  for (const auto & it : params)
62  {
63  std::string orig_name = it.first;
64  if (search(orig_name))
65  {
66  {
67  InputParameters::Parameter<std::string> * string_type =
68  dynamic_cast<InputParameters::Parameter<std::string> *>(it.second);
69  if (string_type)
70  {
71  search(orig_name, params.set<std::string>(orig_name));
72  continue;
73  }
74 
75  InputParameters::Parameter<Real> * real_type =
76  dynamic_cast<InputParameters::Parameter<Real> *>(it.second);
77  if (real_type)
78  {
79  search(orig_name, params.set<Real>(orig_name));
80  continue;
81  }
82 
83  InputParameters::Parameter<unsigned int> * uint_type =
84  dynamic_cast<InputParameters::Parameter<unsigned int> *>(it.second);
85  if (uint_type)
86  {
87  search(orig_name, params.set<unsigned int>(orig_name));
88  continue;
89  }
90 
91  InputParameters::Parameter<int> * int_type =
92  dynamic_cast<InputParameters::Parameter<int> *>(it.second);
93  if (int_type)
94  {
95  search(orig_name, params.set<int>(orig_name));
96  continue;
97  }
98 
99  InputParameters::Parameter<bool> * bool_type =
100  dynamic_cast<InputParameters::Parameter<bool> *>(it.second);
101  if (bool_type)
102  {
103  search(orig_name, params.set<bool>(orig_name));
104  continue;
105  }
106  }
107  }
108  else if (params.isParamRequired(orig_name))
109  mooseError("Missing required command-line parameter: ",
110  orig_name,
111  "\nDoc String: ",
112  params.getDocString(orig_name));
113  }
114 }
115 
116 void
117 CommandLine::addOption(const std::string & name, Option cli_opt)
118 {
119  for (const auto & stx : cli_opt.cli_syntax)
120  cli_opt.cli_switch.push_back(stx.substr(0, stx.find_first_of(" =")));
121 
122  _cli_options[name] = cli_opt;
123 }
124 
125 bool
126 CommandLine::search(const std::string & option_name)
127 {
128  std::map<std::string, Option>::iterator pos = _cli_options.find(option_name);
129  if (pos != _cli_options.end())
130  {
131  for (const auto & search_string : pos->second.cli_switch)
132  {
133  for (auto & arg : _args)
134  {
135  if (arg == search_string)
136  return true;
137  }
138  }
139 
140  if (pos->second.required)
141  {
142  printUsage();
143  mooseError("Required parameter: ", option_name, " missing");
144  }
145  return false;
146  }
147  mooseError("Unrecognized option name: ", option_name);
148 }
149 
150 void
152 {
153  // Grab the first item out of argv
154  std::string command(_args[0]);
155  command.substr(command.find_last_of("/\\") + 1);
156 
157  Moose::out << "Usage: " << command << " [<options>]\n\n"
158  << "Options:\n"
159  << std::left;
160 
161  for (const auto & i : _cli_options)
162  {
163  if (i.second.cli_syntax.empty())
164  continue;
165 
166  std::stringstream oss;
167  for (unsigned int j = 0; j < i.second.cli_syntax.size(); ++j)
168  {
169  if (j)
170  oss << " ";
171  oss << i.second.cli_syntax[j];
172  }
173  Moose::out << " " << std::setw(50) << oss.str() << i.second.description << "\n";
174  }
175 
176  Moose::out << "\nSolver Options:\n"
177  << " See solver manual for details (Petsc or Trilinos)\n";
178 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:111
CommandLine(int argc, char *argv[])
Definition: CommandLine.C:24
std::vector< std::string > cli_switch
This gets filled in automagicaly when calling addOption()
Definition: CommandLine.h:54
int argc()
Definition: CommandLine.h:83
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
std::string description
Definition: CommandLine.h:49
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void populateInputParams(InputParameters &params)
Definition: CommandLine.C:59
void printUsage() const
Print the usage info for this command line.
Definition: CommandLine.C:151
void addCommandLineOptionsFromParams(InputParameters &params)
Definition: CommandLine.C:33
void addOption(const std::string &name, Option cli_opt)
Definition: CommandLine.C:117
ARGUMENT argument_type
Definition: CommandLine.h:52
virtual ~CommandLine()
Definition: CommandLine.C:30
bool search(const std::string &option_name)
This routine searches the command line for the given option "handle" and returns a boolean indicating...
Definition: CommandLine.C:126
std::vector< std::string > cli_syntax
Definition: CommandLine.h:50
std::vector< std::string > _args
Definition: CommandLine.h:118