www.mooseframework.org
CommandLine.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 // MOOSE includes
11 #include "CommandLine.h"
12 #include "MooseInit.h"
13 #include "MooseUtils.h"
14 #include "InputParameters.h"
15 
16 // C++ includes
17 #include <iomanip>
18 
19 CommandLine::CommandLine(int argc, char * argv[]) { addArguments(argc, argv); }
20 
21 void
22 CommandLine::addArguments(int argc, char * argv[])
23 {
24  for (int i = 0; i < argc; i++)
25  addArgument(argv[i]);
26 }
27 
28 void
29 CommandLine::addArgument(std::string arg)
30 {
31  _argv.push_back(arg);
32 
33  auto arg_value = std::string(arg);
34 
35  // Handle using a "="
36  if (arg_value.find("=") != std::string::npos)
37  {
38  std::vector<std::string> arg_split;
39 
40  MooseUtils::tokenize(arg_value, arg_split, 1, "=");
41 
42  for (auto & arg_piece : arg_split)
43  _args.push_back(MooseUtils::trim(arg_piece));
44  }
45  else
46  _args.push_back(arg_value);
47 }
48 
50 
51 void
53 {
54  for (const auto & it : params)
55  {
56  Option cli_opt;
57  std::vector<std::string> syntax;
58  std::string orig_name = it.first;
59 
60  cli_opt.description = params.getDocString(orig_name);
61  if (!params.isPrivate(orig_name))
62  // If a param is private then it shouldn't have any command line syntax.
63  syntax = params.getSyntax(orig_name);
64  cli_opt.cli_syntax = syntax;
65  cli_opt.required = false;
66  InputParameters::Parameter<bool> * bool_type =
67  dynamic_cast<InputParameters::Parameter<bool> *>(it.second);
68  if (bool_type)
70  else
72 
73  addOption(orig_name, cli_opt);
74  }
75 }
76 
77 void
79 {
80  for (const auto & it : params)
81  {
82  std::string orig_name = it.first;
83 
84  if (search(orig_name))
85  {
86  {
87  InputParameters::Parameter<std::string> * string_type =
88  dynamic_cast<InputParameters::Parameter<std::string> *>(it.second);
89  if (string_type)
90  {
91  search(orig_name, params.set<std::string>(orig_name));
92  continue;
93  }
94 
95  InputParameters::Parameter<Real> * real_type =
96  dynamic_cast<InputParameters::Parameter<Real> *>(it.second);
97  if (real_type)
98  {
99  search(orig_name, params.set<Real>(orig_name));
100  continue;
101  }
102 
103  InputParameters::Parameter<unsigned int> * uint_type =
104  dynamic_cast<InputParameters::Parameter<unsigned int> *>(it.second);
105  if (uint_type)
106  {
107  search(orig_name, params.set<unsigned int>(orig_name));
108  continue;
109  }
110 
111  InputParameters::Parameter<int> * int_type =
112  dynamic_cast<InputParameters::Parameter<int> *>(it.second);
113  if (int_type)
114  {
115  search(orig_name, params.set<int>(orig_name));
116  continue;
117  }
118 
119  InputParameters::Parameter<bool> * bool_type =
120  dynamic_cast<InputParameters::Parameter<bool> *>(it.second);
121  if (bool_type)
122  {
123  search(orig_name, params.set<bool>(orig_name));
124  continue;
125  }
126  }
127  }
128  else if (params.isParamRequired(orig_name))
129  mooseError("Missing required command-line parameter: ",
130  orig_name,
131  "\nDoc String: ",
132  params.getDocString(orig_name));
133  }
134 }
135 
136 void
137 CommandLine::addOption(const std::string & name, Option cli_opt)
138 {
139  for (const auto & stx : cli_opt.cli_syntax)
140  cli_opt.cli_switch.push_back(stx.substr(0, stx.find_first_of(" =")));
141 
142  _cli_options[name] = cli_opt;
143 }
144 
145 bool
146 CommandLine::search(const std::string & option_name)
147 {
148  std::map<std::string, Option>::iterator pos = _cli_options.find(option_name);
149  if (pos != _cli_options.end())
150  {
151  for (const auto & search_string : pos->second.cli_switch)
152  for (auto & arg : _args)
153  if (arg == search_string)
154  return true;
155 
156  if (pos->second.required)
157  {
158  printUsage();
159  mooseError("Required parameter: ", option_name, " missing");
160  }
161  return false;
162  }
163  mooseError("Unrecognized option name: ", option_name);
164 }
165 
166 void
168 {
169  // Grab the first item out of argv
170  std::string command(_args[0]);
171  command.substr(command.find_last_of("/\\") + 1);
172 
173  Moose::out << "Usage: " << command << " [<options>]\n\n"
174  << "Options:\n"
175  << std::left;
176 
177  for (const auto & i : _cli_options)
178  {
179  if (i.second.cli_syntax.empty())
180  continue;
181 
182  std::stringstream oss;
183  for (unsigned int j = 0; j < i.second.cli_syntax.size(); ++j)
184  {
185  if (j)
186  oss << " ";
187  oss << i.second.cli_syntax[j];
188  }
189  Moose::out << " " << std::setw(50) << oss.str() << i.second.description << "\n";
190  }
191 
192  Moose::out << "\nSolver Options:\n"
193  << " See solver manual for details (Petsc or Trilinos)\n";
194 }
195 
196 template <>
197 void
198 CommandLine::setArgument<std::string>(std::stringstream & stream, std::string & argument)
199 {
200  argument = stream.str();
201 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:114
CommandLine(int argc, char *argv[])
Definition: CommandLine.C:19
std::vector< std::string > cli_switch
This gets filled in automagicaly when calling addOption()
Definition: CommandLine.h:48
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
Definition: MooseUtils.h:532
void addArgument(std::string)
Definition: CommandLine.C:29
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
std::string description
Definition: CommandLine.h:43
void printUsage() const
Print the usage info for this command line.
Definition: CommandLine.C:167
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void populateInputParams(InputParameters &params)
Definition: CommandLine.C:78
std::vector< std::string > _argv
Storage for the raw argv.
Definition: CommandLine.h:121
void addCommandLineOptionsFromParams(InputParameters &params)
Definition: CommandLine.C:52
void addOption(const std::string &name, Option cli_opt)
Definition: CommandLine.C:137
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
Definition: MooseUtils.C:113
ARGUMENT argument_type
Definition: CommandLine.h:46
virtual ~CommandLine()
Definition: CommandLine.C:49
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:146
std::vector< std::string > cli_syntax
Definition: CommandLine.h:44
std::vector< std::string > _args
Definition: CommandLine.h:123
void addArguments(int argc, char *argv[])
Definition: CommandLine.C:22