www.mooseframework.org
Classes | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | List of all members
CommandLine Class Reference

This class wraps provides and tracks access to command line parameters. More...

#include <CommandLine.h>

Classes

struct  Option
 

Public Types

enum  ARGUMENT { NONE, OPTIONAL, REQUIRED }
 Type of argument for a given option. More...
 

Public Member Functions

 CommandLine (int argc, char *argv[])
 
virtual ~CommandLine ()
 
void addArguments (int argc, char *argv[])
 
void addArgument (std::string)
 
const std::vector< std::string > & getArguments ()
 Return the raw argv arguments as a vector. More...
 
void addCommandLineOptionsFromParams (InputParameters &params)
 
void populateInputParams (InputParameters &params)
 
void addOption (const std::string &name, Option cli_opt)
 
bool search (const std::string &option_name)
 This routine searches the command line for the given option "handle" and returns a boolean indicating whether it was found. More...
 
template<typename T >
bool search (const std::string &option_name, T &argument)
 
void printUsage () const
 Print the usage info for this command line. More...
 
void markHitParamUsed (int argi)
 
void markHitParam (int argi)
 
std::set< int > unused (const Parallel::Communicator &comm)
 

Protected Member Functions

template<typename T >
void setArgument (std::stringstream &stream, T &argument)
 Used to set the argument value, allows specialization. More...
 

Protected Attributes

std::map< std::string, Option_cli_options
 Command line options. More...
 

Private Attributes

std::set< int > _used_hiti
 
std::set< int > _hiti
 
std::vector< std::string > _argv
 Storage for the raw argv. More...
 
std::vector< std::string > _args
 

Detailed Description

This class wraps provides and tracks access to command line parameters.

Definition at line 31 of file CommandLine.h.

Member Enumeration Documentation

◆ ARGUMENT

Type of argument for a given option.

Enumerator
NONE 
OPTIONAL 
REQUIRED 

Definition at line 35 of file CommandLine.h.

Constructor & Destructor Documentation

◆ CommandLine()

CommandLine::CommandLine ( int  argc,
char *  argv[] 
)

Definition at line 19 of file CommandLine.C.

19 { addArguments(argc, argv); }
void addArguments(int argc, char *argv[])
Definition: CommandLine.C:22

◆ ~CommandLine()

CommandLine::~CommandLine ( )
virtual

Definition at line 49 of file CommandLine.C.

49 {}

Member Function Documentation

◆ addArgument()

void CommandLine::addArgument ( std::string  arg)

Definition at line 29 of file CommandLine.C.

Referenced by addArguments().

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 }
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:429
std::vector< std::string > _argv
Storage for the raw argv.
Definition: CommandLine.h:122
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
Definition: MooseUtils.C:113
std::vector< std::string > _args
Definition: CommandLine.h:124

◆ addArguments()

void CommandLine::addArguments ( int  argc,
char *  argv[] 
)

Definition at line 22 of file CommandLine.C.

Referenced by CommandLine().

23 {
24  for (int i = 0; i < argc; i++)
25  addArgument(argv[i]);
26 }
void addArgument(std::string)
Definition: CommandLine.C:29

◆ addCommandLineOptionsFromParams()

void CommandLine::addCommandLineOptionsFromParams ( InputParameters params)

Definition at line 52 of file CommandLine.C.

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)
69  cli_opt.argument_type = CommandLine::NONE;
70  else
71  cli_opt.argument_type = CommandLine::REQUIRED;
72 
73  addOption(orig_name, cli_opt);
74  }
75 }
void addOption(const std::string &name, Option cli_opt)
Definition: CommandLine.C:137

◆ addOption()

void CommandLine::addOption ( const std::string &  name,
Option  cli_opt 
)

Definition at line 137 of file CommandLine.C.

Referenced by addCommandLineOptionsFromParams().

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 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:115

◆ getArguments()

const std::vector<std::string>& CommandLine::getArguments ( )
inline

Return the raw argv arguments as a vector.

Definition at line 61 of file CommandLine.h.

Referenced by Moose::PetscSupport::petscSetupOutput().

61 { return _argv; }
std::vector< std::string > _argv
Storage for the raw argv.
Definition: CommandLine.h:122

◆ markHitParam()

void CommandLine::markHitParam ( int  argi)
inline

Definition at line 88 of file CommandLine.h.

88 { _hiti.insert(argi); }
std::set< int > _hiti
Definition: CommandLine.h:119

◆ markHitParamUsed()

void CommandLine::markHitParamUsed ( int  argi)
inline

Definition at line 87 of file CommandLine.h.

87 { _used_hiti.insert(argi); };
std::set< int > _used_hiti
Definition: CommandLine.h:118

◆ populateInputParams()

void CommandLine::populateInputParams ( InputParameters params)

Definition at line 78 of file CommandLine.C.

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 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
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

◆ printUsage()

void CommandLine::printUsage ( ) const

Print the usage info for this command line.

Definition at line 167 of file CommandLine.C.

Referenced by search().

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 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:115
std::vector< std::string > _args
Definition: CommandLine.h:124

◆ search() [1/2]

bool CommandLine::search ( const std::string &  option_name)

This routine searches the command line for the given option "handle" and returns a boolean indicating whether it was found.

If the given option has an argument it is also filled in.

Definition at line 146 of file CommandLine.C.

Referenced by populateInputParams().

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 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:115
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
void printUsage() const
Print the usage info for this command line.
Definition: CommandLine.C:167
std::vector< std::string > _args
Definition: CommandLine.h:124

◆ search() [2/2]

template<typename T >
bool CommandLine::search ( const std::string &  option_name,
T &  argument 
)

Definition at line 140 of file CommandLine.h.

141 {
142  std::map<std::string, Option>::iterator pos = _cli_options.find(option_name);
143  if (pos != _cli_options.end())
144  {
145  for (unsigned int i = 0; i < pos->second.cli_switch.size(); ++i)
146  {
147 
148  for (size_t j = 0; j < _args.size(); j++)
149  {
150  auto arg = _args[j];
151 
152  if (arg == pos->second.cli_switch[i])
153  {
154  // "Flag" CLI options are added as Boolean types, when we see them
155  // we set the Boolean argument to true
156  if (pos->second.argument_type == NONE)
157  argument = true;
158  else if (j + 1 < _args.size())
159  {
160  std::stringstream ss;
161  ss << _args[j + 1];
162 
163  setArgument(ss, argument);
164  }
165  return true;
166  }
167  }
168  }
169 
170  if (pos->second.required)
171  {
172  Moose::err << "Required parameter: " << option_name << " missing\n";
173  printUsage();
174  }
175  return false;
176  }
177  mooseError("Unrecognized option name");
178 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:115
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
void printUsage() const
Print the usage info for this command line.
Definition: CommandLine.C:167
void setArgument(std::stringstream &stream, T &argument)
Used to set the argument value, allows specialization.
Definition: CommandLine.h:129
std::vector< std::string > _args
Definition: CommandLine.h:124

◆ setArgument()

template<typename T >
void CommandLine::setArgument ( std::stringstream &  stream,
T &  argument 
)
protected

Used to set the argument value, allows specialization.

Definition at line 129 of file CommandLine.h.

Referenced by search().

130 {
131  stream >> argument;
132 }

◆ unused()

std::set<int> CommandLine::unused ( const Parallel::Communicator &  comm)
inline

Definition at line 93 of file CommandLine.h.

94  {
95  comm.set_union(_hiti);
96  comm.set_union(_used_hiti);
97 
98  std::set<int> unused;
99  for (int i : _hiti)
100  {
101  if (_used_hiti.count(i) == 0)
102  unused.insert(i);
103  }
104  return unused;
105  }
std::set< int > unused(const Parallel::Communicator &comm)
Definition: CommandLine.h:93
std::set< int > _hiti
Definition: CommandLine.h:119
std::set< int > _used_hiti
Definition: CommandLine.h:118
MPI_Comm comm

Member Data Documentation

◆ _args

std::vector<std::string> CommandLine::_args
private

Definition at line 124 of file CommandLine.h.

Referenced by addArgument(), printUsage(), and search().

◆ _argv

std::vector<std::string> CommandLine::_argv
private

Storage for the raw argv.

Definition at line 122 of file CommandLine.h.

Referenced by addArgument(), and getArguments().

◆ _cli_options

std::map<std::string, Option> CommandLine::_cli_options
protected

Command line options.

Definition at line 115 of file CommandLine.h.

Referenced by addOption(), printUsage(), and search().

◆ _hiti

std::set<int> CommandLine::_hiti
private

Definition at line 119 of file CommandLine.h.

Referenced by markHitParam(), and unused().

◆ _used_hiti

std::set<int> CommandLine::_used_hiti
private

Definition at line 118 of file CommandLine.h.

Referenced by markHitParamUsed(), and unused().


The documentation for this class was generated from the following files: