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 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...
 
int argc ()
 
char ** argv ()
 
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
 
int _argc = 0
 
char ** _argv = nullptr
 
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  : _argc(argc), _argv(argv)
20 {
21  for (int i = 0; i < argc; i++)
22  {
23  auto arg_value = std::string(argv[i]);
24 
25  // Handle using a "="
26  if (arg_value.find("=") != std::string::npos)
27  {
28  std::vector<std::string> arg_split;
29 
30  MooseUtils::tokenize(arg_value, arg_split, 1, "=");
31 
32  for (auto & arg_piece : arg_split)
33  _args.push_back(MooseUtils::trim(arg_piece));
34  }
35  else
36  _args.push_back(arg_value);
37  }
38 }
int argc()
Definition: CommandLine.h:76
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:377
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
Definition: MooseUtils.C:112
char ** _argv
Definition: CommandLine.h:116
char ** argv()
Definition: CommandLine.h:77
std::vector< std::string > _args
Definition: CommandLine.h:117

◆ ~CommandLine()

CommandLine::~CommandLine ( )
virtual

Definition at line 40 of file CommandLine.C.

40 {}

Member Function Documentation

◆ addCommandLineOptionsFromParams()

void CommandLine::addCommandLineOptionsFromParams ( InputParameters params)

Definition at line 43 of file CommandLine.C.

44 {
45  for (const auto & it : params)
46  {
47  Option cli_opt;
48  std::vector<std::string> syntax;
49  std::string orig_name = it.first;
50 
51  cli_opt.description = params.getDocString(orig_name);
52  if (!params.isPrivate(orig_name))
53  // If a param is private then it shouldn't have any command line syntax.
54  syntax = params.getSyntax(orig_name);
55  cli_opt.cli_syntax = syntax;
56  cli_opt.required = false;
57  InputParameters::Parameter<bool> * bool_type =
58  dynamic_cast<InputParameters::Parameter<bool> *>(it.second);
59  if (bool_type)
60  cli_opt.argument_type = CommandLine::NONE;
61  else
62  cli_opt.argument_type = CommandLine::REQUIRED;
63 
64  addOption(orig_name, cli_opt);
65  }
66 }
void addOption(const std::string &name, Option cli_opt)
Definition: CommandLine.C:128

◆ addOption()

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

Definition at line 128 of file CommandLine.C.

Referenced by addCommandLineOptionsFromParams().

129 {
130  for (const auto & stx : cli_opt.cli_syntax)
131  cli_opt.cli_switch.push_back(stx.substr(0, stx.find_first_of(" =")));
132 
133  _cli_options[name] = cli_opt;
134 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:110

◆ argc()

int CommandLine::argc ( )
inline

Definition at line 76 of file CommandLine.h.

Referenced by CommandLine(), and Moose::PetscSupport::petscSetupOutput().

76 { return _argc; }

◆ argv()

char** CommandLine::argv ( )
inline

Definition at line 77 of file CommandLine.h.

Referenced by CommandLine(), and Moose::PetscSupport::petscSetupOutput().

77 { return _argv; }
char ** _argv
Definition: CommandLine.h:116

◆ markHitParam()

void CommandLine::markHitParam ( int  argi)
inline

Definition at line 83 of file CommandLine.h.

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

◆ markHitParamUsed()

void CommandLine::markHitParamUsed ( int  argi)
inline

Definition at line 82 of file CommandLine.h.

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

◆ populateInputParams()

void CommandLine::populateInputParams ( InputParameters params)

Definition at line 69 of file CommandLine.C.

70 {
71  for (const auto & it : params)
72  {
73  std::string orig_name = it.first;
74 
75  if (search(orig_name))
76  {
77  {
78  InputParameters::Parameter<std::string> * string_type =
79  dynamic_cast<InputParameters::Parameter<std::string> *>(it.second);
80  if (string_type)
81  {
82  search(orig_name, params.set<std::string>(orig_name));
83  continue;
84  }
85 
86  InputParameters::Parameter<Real> * real_type =
87  dynamic_cast<InputParameters::Parameter<Real> *>(it.second);
88  if (real_type)
89  {
90  search(orig_name, params.set<Real>(orig_name));
91  continue;
92  }
93 
94  InputParameters::Parameter<unsigned int> * uint_type =
95  dynamic_cast<InputParameters::Parameter<unsigned int> *>(it.second);
96  if (uint_type)
97  {
98  search(orig_name, params.set<unsigned int>(orig_name));
99  continue;
100  }
101 
102  InputParameters::Parameter<int> * int_type =
103  dynamic_cast<InputParameters::Parameter<int> *>(it.second);
104  if (int_type)
105  {
106  search(orig_name, params.set<int>(orig_name));
107  continue;
108  }
109 
110  InputParameters::Parameter<bool> * bool_type =
111  dynamic_cast<InputParameters::Parameter<bool> *>(it.second);
112  if (bool_type)
113  {
114  search(orig_name, params.set<bool>(orig_name));
115  continue;
116  }
117  }
118  }
119  else if (params.isParamRequired(orig_name))
120  mooseError("Missing required command-line parameter: ",
121  orig_name,
122  "\nDoc String: ",
123  params.getDocString(orig_name));
124  }
125 }
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:137

◆ printUsage()

void CommandLine::printUsage ( ) const

Print the usage info for this command line.

Definition at line 158 of file CommandLine.C.

Referenced by search().

159 {
160  // Grab the first item out of argv
161  std::string command(_args[0]);
162  command.substr(command.find_last_of("/\\") + 1);
163 
164  Moose::out << "Usage: " << command << " [<options>]\n\n"
165  << "Options:\n"
166  << std::left;
167 
168  for (const auto & i : _cli_options)
169  {
170  if (i.second.cli_syntax.empty())
171  continue;
172 
173  std::stringstream oss;
174  for (unsigned int j = 0; j < i.second.cli_syntax.size(); ++j)
175  {
176  if (j)
177  oss << " ";
178  oss << i.second.cli_syntax[j];
179  }
180  Moose::out << " " << std::setw(50) << oss.str() << i.second.description << "\n";
181  }
182 
183  Moose::out << "\nSolver Options:\n"
184  << " See solver manual for details (Petsc or Trilinos)\n";
185 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:110
std::vector< std::string > _args
Definition: CommandLine.h:117

◆ 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 137 of file CommandLine.C.

Referenced by populateInputParams().

138 {
139  std::map<std::string, Option>::iterator pos = _cli_options.find(option_name);
140  if (pos != _cli_options.end())
141  {
142  for (const auto & search_string : pos->second.cli_switch)
143  for (auto & arg : _args)
144  if (arg == search_string)
145  return true;
146 
147  if (pos->second.required)
148  {
149  printUsage();
150  mooseError("Required parameter: ", option_name, " missing");
151  }
152  return false;
153  }
154  mooseError("Unrecognized option name: ", option_name);
155 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:110
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:158
std::vector< std::string > _args
Definition: CommandLine.h:117

◆ search() [2/2]

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

Definition at line 133 of file CommandLine.h.

134 {
135  std::map<std::string, Option>::iterator pos = _cli_options.find(option_name);
136  if (pos != _cli_options.end())
137  {
138  for (unsigned int i = 0; i < pos->second.cli_switch.size(); ++i)
139  {
140 
141  for (size_t j = 0; j < _args.size(); j++)
142  {
143  auto arg = _args[j];
144 
145  if (arg == pos->second.cli_switch[i])
146  {
147  // "Flag" CLI options are added as Boolean types, when we see them
148  // we set the Boolean argument to true
149  if (pos->second.argument_type == NONE)
150  argument = true;
151  else if (j + 1 < _args.size())
152  {
153  std::stringstream ss;
154  ss << _args[j + 1];
155 
156  setArgument(ss, argument);
157  }
158  return true;
159  }
160  }
161  }
162 
163  if (pos->second.required)
164  {
165  Moose::err << "Required parameter: " << option_name << " missing\n";
166  printUsage();
167  }
168  return false;
169  }
170  mooseError("Unrecognized option name");
171 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:110
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:158
void setArgument(std::stringstream &stream, T &argument)
Used to set the argument value, allows specialization.
Definition: CommandLine.h:122
std::vector< std::string > _args
Definition: CommandLine.h:117

◆ setArgument()

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

Used to set the argument value, allows specialization.

Definition at line 122 of file CommandLine.h.

Referenced by search().

123 {
124  stream >> argument;
125 }

◆ unused()

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

Definition at line 88 of file CommandLine.h.

89  {
90  comm.set_union(_hiti);
91  comm.set_union(_used_hiti);
92 
93  std::set<int> unused;
94  for (int i : _hiti)
95  {
96  if (_used_hiti.count(i) == 0)
97  unused.insert(i);
98  }
99  return unused;
100  }
std::set< int > unused(const Parallel::Communicator &comm)
Definition: CommandLine.h:88
std::set< int > _hiti
Definition: CommandLine.h:114
std::set< int > _used_hiti
Definition: CommandLine.h:113
MPI_Comm comm

Member Data Documentation

◆ _argc

int CommandLine::_argc = 0
private

Definition at line 115 of file CommandLine.h.

Referenced by argc().

◆ _args

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

Definition at line 117 of file CommandLine.h.

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

◆ _argv

char** CommandLine::_argv = nullptr
private

Definition at line 116 of file CommandLine.h.

Referenced by argv().

◆ _cli_options

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

Command line options.

Definition at line 110 of file CommandLine.h.

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

◆ _hiti

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

Definition at line 114 of file CommandLine.h.

Referenced by markHitParam(), and unused().

◆ _used_hiti

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

Definition at line 113 of file CommandLine.h.

Referenced by markHitParamUsed(), and unused().


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