www.mooseframework.org
Classes | Public Types | Public 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 parseInputParams (const InputParameters &params)
 
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 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 36 of file CommandLine.h.

Member Enumeration Documentation

Type of argument for a given option.

Enumerator
NONE 
OPTIONAL 
REQUIRED 

Definition at line 40 of file CommandLine.h.

Constructor & Destructor Documentation

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

Definition at line 24 of file CommandLine.C.

24  : _argc(argc), _argv(argv)
25 {
26  for (int i = 0; i < argc; i++)
27  _args.push_back(std::string(argv[i]));
28 }
int argc()
Definition: CommandLine.h:83
char ** _argv
Definition: CommandLine.h:117
char ** argv()
Definition: CommandLine.h:84
std::vector< std::string > _args
Definition: CommandLine.h:118
CommandLine::~CommandLine ( )
virtual

Definition at line 30 of file CommandLine.C.

30 {}

Member Function Documentation

void CommandLine::addCommandLineOptionsFromParams ( InputParameters params)

Definition at line 33 of file CommandLine.C.

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)
50  cli_opt.argument_type = CommandLine::NONE;
51  else
52  cli_opt.argument_type = CommandLine::REQUIRED;
53 
54  addOption(orig_name, cli_opt);
55  }
56 }
void addOption(const std::string &name, Option cli_opt)
Definition: CommandLine.C:117
void CommandLine::addOption ( const std::string &  name,
Option  cli_opt 
)

Definition at line 117 of file CommandLine.C.

Referenced by addCommandLineOptionsFromParams().

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 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:111
int CommandLine::argc ( )
inline

Definition at line 83 of file CommandLine.h.

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

83 { return _argc; }
char** CommandLine::argv ( )
inline

Definition at line 84 of file CommandLine.h.

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

84 { return _argv; }
char ** _argv
Definition: CommandLine.h:117
void CommandLine::markHitParam ( int  argi)
inline

Definition at line 90 of file CommandLine.h.

90 { _hiti.insert(argi); }
std::set< int > _hiti
Definition: CommandLine.h:115
void CommandLine::markHitParamUsed ( int  argi)
inline

Definition at line 89 of file CommandLine.h.

89 { _used_hiti.insert(argi); };
std::set< int > _used_hiti
Definition: CommandLine.h:114
void CommandLine::parseInputParams ( const InputParameters params)
void CommandLine::populateInputParams ( InputParameters params)

Definition at line 59 of file CommandLine.C.

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

Print the usage info for this command line.

Definition at line 151 of file CommandLine.C.

Referenced by search().

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
std::vector< std::string > _args
Definition: CommandLine.h:118
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 126 of file CommandLine.C.

Referenced by populateInputParams().

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 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:111
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
void printUsage() const
Print the usage info for this command line.
Definition: CommandLine.C:151
std::vector< std::string > _args
Definition: CommandLine.h:118
template<typename T >
bool CommandLine::search ( const std::string &  option_name,
T &  argument 
)

Definition at line 123 of file CommandLine.h.

124 {
125  std::map<std::string, Option>::iterator pos = _cli_options.find(option_name);
126  if (pos != _cli_options.end())
127  {
128  for (unsigned int i = 0; i < pos->second.cli_switch.size(); ++i)
129  {
130 
131  for (size_t j = 0; j < _args.size(); j++)
132  {
133  auto arg = _args[j];
134  if (arg == pos->second.cli_switch[i])
135  {
136  // "Flag" CLI options are added as Boolean types, when we see them
137  // we set the Boolean argument to true
138  if (pos->second.argument_type == NONE)
139  argument = true;
140  else if (j + 1 < _args.size())
141  {
142  std::stringstream ss;
143  ss << _args[j + 1];
144  ss >> argument;
145  }
146  return true;
147  }
148  }
149  }
150 
151  if (pos->second.required)
152  {
153  Moose::err << "Required parameter: " << option_name << " missing\n";
154  printUsage();
155  }
156  return false;
157  }
158  mooseError("Unrecognized option name");
159 }
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:111
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
void printUsage() const
Print the usage info for this command line.
Definition: CommandLine.C:151
std::vector< std::string > _args
Definition: CommandLine.h:118
std::set<int> CommandLine::unused ( const Parallel::Communicator &  comm)
inline

Definition at line 95 of file CommandLine.h.

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

Member Data Documentation

int CommandLine::_argc = 0
private

Definition at line 116 of file CommandLine.h.

Referenced by argc().

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

Definition at line 118 of file CommandLine.h.

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

char** CommandLine::_argv = nullptr
private

Definition at line 117 of file CommandLine.h.

Referenced by argv().

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

Command line options.

Definition at line 111 of file CommandLine.h.

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

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

Definition at line 115 of file CommandLine.h.

Referenced by markHitParam(), and unused().

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

Definition at line 114 of file CommandLine.h.

Referenced by markHitParamUsed(), and unused().


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