www.mooseframework.org
CommandLine.h
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 #ifndef COMMANDLINE_H
16 #define COMMANDLINE_H
17 
18 // Moose Includes
19 #include "MooseError.h"
20 
21 #include "libmesh/parallel.h"
22 
23 // C++ includes
24 #include <vector>
25 #include <string>
26 #include <map>
27 #include <memory>
28 #include <set>
29 
30 // Forward Declaration
31 class InputParameters;
32 
37 {
38 public:
40  enum ARGUMENT
41  {
45  };
46 
47  struct Option
48  {
49  std::string description;
50  std::vector<std::string> cli_syntax;
51  bool required;
54  std::vector<std::string> cli_switch;
55  };
56 
57  CommandLine(int argc, char * argv[]);
58  virtual ~CommandLine();
59 
60  void parseInputParams(const InputParameters & params);
61 
63 
64  void populateInputParams(InputParameters & params);
65 
66  void addOption(const std::string & name, Option cli_opt);
67 
73  bool search(const std::string & option_name);
74 
75  template <typename T>
76  bool search(const std::string & option_name, T & argument);
77 
81  void printUsage() const;
82 
83  int argc() { return _argc; }
84  char ** argv() { return _argv; }
85 
86  // this needs to be tracked here because CommandLine has a global shared instance across all
87  // multiapps/subapps - and we need to track used/unused CLI hit params globally so we know
88  // which ones don't get used - this can't happen at the within-app level.
89  void markHitParamUsed(int argi) { _used_hiti.insert(argi); };
90  void markHitParam(int argi) { _hiti.insert(argi); }
91 
92  // Returns the unused CLI hit parameters. This accounts for different CLI params being used
93  // by different processes in a process-parallel run, so the communicator is needed to rendezvous
94  // which parameters have been used between them all.
95  std::set<int> unused(const Parallel::Communicator & comm)
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  }
108 
109 protected:
111  std::map<std::string, Option> _cli_options;
112 
113 private:
114  std::set<int> _used_hiti;
115  std::set<int> _hiti;
116  int _argc = 0;
117  char ** _argv = nullptr;
118  std::vector<std::string> _args;
119 };
120 
121 template <typename T>
122 bool
123 CommandLine::search(const std::string & option_name, T & argument)
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 }
160 
161 #endif // COMMANDLINE_H
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:111
void markHitParam(int argi)
Definition: CommandLine.h:90
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
ARGUMENT
Type of argument for a given option.
Definition: CommandLine.h:40
std::set< int > unused(const Parallel::Communicator &comm)
Definition: CommandLine.h:95
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
void markHitParamUsed(int argi)
Definition: CommandLine.h:89
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::set< int > _hiti
Definition: CommandLine.h:115
This class wraps provides and tracks access to command line parameters.
Definition: CommandLine.h:36
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
char ** _argv
Definition: CommandLine.h:117
std::set< int > _used_hiti
Definition: CommandLine.h:114
virtual ~CommandLine()
Definition: CommandLine.C:30
char ** argv()
Definition: CommandLine.h:84
void parseInputParams(const InputParameters &params)
MPI_Comm comm
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