www.mooseframework.org
CommandLine.h
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 #pragma once
11 
12 // Moose Includes
13 #include "MooseError.h"
14 
15 #include "libmesh/parallel.h"
16 
17 // C++ includes
18 #include <vector>
19 #include <string>
20 #include <map>
21 #include <memory>
22 #include <set>
23 
24 // Forward Declaration
25 class InputParameters;
26 
31 {
32 public:
34  enum ARGUMENT
35  {
39  };
40 
41  struct Option
42  {
43  std::string description;
44  std::vector<std::string> cli_syntax;
45  bool required;
48  std::vector<std::string> cli_switch;
49  };
50 
51  CommandLine(int argc, char * argv[]);
52  virtual ~CommandLine();
53 
54  void addArguments(int argc, char * argv[]);
55  void addArgument(std::string);
56 
60  const std::vector<std::string> & getArguments() { return _argv; }
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  // this needs to be tracked here because CommandLine has a global shared instance across all
84  // multiapps/subapps - and we need to track used/unused CLI hit params globally so we know
85  // which ones don't get used - this can't happen at the within-app level.
86  void markHitParamUsed(int argi) { _used_hiti.insert(argi); };
87  void markHitParam(int argi) { _hiti.insert(argi); }
88 
89  // Returns the unused CLI hit parameters. This accounts for different CLI params being used
90  // by different processes in a process-parallel run, so the communicator is needed to rendezvous
91  // which parameters have been used between them all.
92  std::set<int> unused(const Parallel::Communicator & comm)
93  {
94  comm.set_union(_hiti);
95  comm.set_union(_used_hiti);
96 
97  std::set<int> unused;
98  for (int i : _hiti)
99  {
100  if (_used_hiti.count(i) == 0)
101  unused.insert(i);
102  }
103  return unused;
104  }
105 
106 protected:
110  template <typename T>
111  void setArgument(std::stringstream & stream, T & argument);
112 
114  std::map<std::string, Option> _cli_options;
115 
116 private:
117  std::set<int> _used_hiti;
118  std::set<int> _hiti;
119 
121  std::vector<std::string> _argv;
122 
123  std::vector<std::string> _args;
124 };
125 
126 template <typename T>
127 void
128 CommandLine::setArgument(std::stringstream & stream, T & argument)
129 {
130  stream >> argument;
131 }
132 
133 // Specialization for std::string
134 template <>
135 void CommandLine::setArgument<std::string>(std::stringstream & stream, std::string & argument);
136 
137 template <typename T>
138 bool
139 CommandLine::search(const std::string & option_name, T & argument)
140 {
141  std::map<std::string, Option>::iterator pos = _cli_options.find(option_name);
142  if (pos != _cli_options.end())
143  {
144  for (unsigned int i = 0; i < pos->second.cli_switch.size(); ++i)
145  {
146 
147  for (size_t j = 0; j < _args.size(); j++)
148  {
149  auto arg = _args[j];
150 
151  if (arg == pos->second.cli_switch[i])
152  {
153  // "Flag" CLI options are added as Boolean types, when we see them
154  // we set the Boolean argument to true
155  if (pos->second.argument_type == NONE)
156  argument = true;
157  else if (j + 1 < _args.size())
158  {
159  std::stringstream ss;
160  ss << _args[j + 1];
161 
162  setArgument(ss, argument);
163  }
164  return true;
165  }
166  }
167  }
168 
169  if (pos->second.required)
170  {
171  Moose::err << "Required parameter: " << option_name << " missing\n";
172  printUsage();
173  }
174  return false;
175  }
176  mooseError("Unrecognized option name");
177 }
178 
std::map< std::string, Option > _cli_options
Command line options.
Definition: CommandLine.h:114
void markHitParam(int argi)
Definition: CommandLine.h:87
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
ARGUMENT
Type of argument for a given option.
Definition: CommandLine.h:34
std::set< int > unused(const Parallel::Communicator &comm)
Definition: CommandLine.h:92
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 markHitParamUsed(int argi)
Definition: CommandLine.h:86
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...
std::set< int > _hiti
Definition: CommandLine.h:118
This class wraps provides and tracks access to command line parameters.
Definition: CommandLine.h:30
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
ARGUMENT argument_type
Definition: CommandLine.h:46
std::set< int > _used_hiti
Definition: CommandLine.h:117
void setArgument(std::stringstream &stream, T &argument)
Used to set the argument value, allows specialization.
Definition: CommandLine.h:128
virtual ~CommandLine()
Definition: CommandLine.C:49
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:146
std::vector< std::string > cli_syntax
Definition: CommandLine.h:44
std::vector< std::string > _args
Definition: CommandLine.h:123
const std::vector< std::string > & getArguments()
Return the raw argv arguments as a vector.
Definition: CommandLine.h:60
void addArguments(int argc, char *argv[])
Definition: CommandLine.C:22