argos_command_line_arg_parser.cpp
Go to the documentation of this file.
1 
8 #include <argos3/core/config.h>
9 
10 namespace argos {
11 
12  /****************************************/
13  /****************************************/
14 
16  m_eAction(ACTION_UNKNOWN),
17  m_pcInitLogStream(nullptr),
18  m_pcInitLogErrStream(nullptr) {
19  AddFlag(
20  'h',
21  "help",
22  "display this usage information",
23  m_bHelpWanted
24  );
25  AddFlag(
26  'v',
27  "version",
28  "display the current version and release",
29  m_bVersionWanted
30  );
31  AddFlag(
32  'n',
33  "no-color",
34  "do not use colored output [OPTIONAL]",
35  m_bNonColoredLog
36  );
37  AddArgument<std::string>(
38  'c',
39  "config-file",
40  "the experiment XML configuration file",
41  m_strExperimentConfigFile
42  );
43  AddArgument<std::string>(
44  'q',
45  "query",
46  "query the available plugins",
47  m_strQuery
48  );
49  AddArgument<std::string>(
50  'l',
51  "log-file",
52  "output log to file [OPTIONAL]",
53  m_strLogFileName
54  );
55  AddArgument<std::string>(
56  'e',
57  "logerr-file",
58  "output logerr to file [OPTIONAL]",
59  m_strLogErrFileName
60  );
61  }
62 
63  /****************************************/
64  /****************************************/
65 
67  if(m_cLogFile.is_open()) {
68  LOG.GetStream().rdbuf(m_pcInitLogStream);
69  m_cLogFile.close();
70  }
71  if(m_cLogErrFile.is_open()) {
72  LOGERR.GetStream().rdbuf(m_pcInitLogErrStream);
73  m_cLogErrFile.close();
74  }
75  }
76 
77  /****************************************/
78  /****************************************/
79 
81  char** ppch_argv) {
82  CCommandLineArgParser::Parse(n_argc, ppch_argv);
83  /* Configure LOG/LOGERR coloring */
84  if(m_bNonColoredLog) {
87  }
88 
89  /* Check whether LOG and LOGERR should go to files */
90  if(m_strLogFileName != "") {
92  m_cLogFile.open(m_strLogFileName.c_str(), std::ios::trunc | std::ios::out);
93  if(m_cLogFile.fail()) {
94  THROW_ARGOSEXCEPTION("Error opening file \"" << m_strLogFileName << "\"");
95  }
96  m_pcInitLogStream = LOG.GetStream().rdbuf();
97  LOG.GetStream().rdbuf(m_cLogFile.rdbuf());
98  }
99  if(m_strLogErrFileName != "") {
101  m_cLogErrFile.open(m_strLogErrFileName.c_str(), std::ios::trunc | std::ios::out);
102  if(m_cLogErrFile.fail()) {
103  THROW_ARGOSEXCEPTION("Error opening file \"" << m_strLogErrFileName << "\"");
104  }
105  m_pcInitLogErrStream = LOGERR.GetStream().rdbuf();
106  LOGERR.GetStream().rdbuf(m_cLogErrFile.rdbuf());
107  }
108 
109  /* Check that either -h, -v, -c or -q was passed (strictly one of them) */
110  UInt32 nOptionsOn = 0;
111  if(m_strExperimentConfigFile != "") ++nOptionsOn;
112  if(m_strQuery != "") ++nOptionsOn;
113  if(m_bHelpWanted) ++nOptionsOn;
114  if(m_bVersionWanted) ++nOptionsOn;
115  if(nOptionsOn == 0) {
116  THROW_ARGOSEXCEPTION("No --help, --version, --config-file or --query options specified.");
117  }
118  if(nOptionsOn > 1) {
119  THROW_ARGOSEXCEPTION("Options --help, --version, --config-file and --query are mutually exclusive.");
120  }
121 
122  if(m_strExperimentConfigFile != "") {
123  m_eAction = ACTION_RUN_EXPERIMENT;
124  }
125 
126  if(m_strQuery != "") {
127  m_eAction = ACTION_QUERY;
128  }
129 
130  if(m_bHelpWanted) {
131  m_eAction = ACTION_SHOW_HELP;
132  }
133 
134  if(m_bVersionWanted) {
135  m_eAction = ACTION_SHOW_VERSION;
136  }
137 
138  }
139 
140  /****************************************/
141  /****************************************/
142 
144  c_log << "Autonomous Robots Go Swarming (ARGoS) v" << ARGOS_VERSION << "-" << ARGOS_RELEASE << std::endl;
145  c_log << "Released under the terms of the MIT license." << std::endl;
146  c_log << std::endl;
147  c_log << "Usage: argos3 [OPTIONS]" << std::endl;
148  c_log << std::endl;
149  c_log << " -h | --help display this usage information" << std::endl;
150  c_log << " -v | --version display ARGoS version and release" << std::endl;
151  c_log << " -c FILE | --config-file FILE the experiment XML configuration file" << std::endl;
152  c_log << " -q QUERY | --query QUERY query the available plugins." << std::endl;
153  c_log << " -n | --no-color do not use colored output [OPTIONAL]" << std::endl;
154  c_log << " -l | --log-file FILE redirect LOG to FILE [OPTIONAL]" << std::endl;
155  c_log << " -e | --logerr-file FILE redirect LOGERR to FILE [OPTIONAL]" << std::endl << std::endl;
156  c_log << "The options --config-file and --query are mutually exclusive. Either you use" << std::endl;
157  c_log << "the first, and thus you run an experiment, or you use the second to query the" << std::endl;
158  c_log << "plugins." << std::endl << std::endl;
159  c_log << "EXAMPLES" << std::endl << std::endl;
160  c_log << "To run an experiment, type:" << std::endl << std::endl;
161  c_log << " argos3 -c /path/to/myconfig.argos" << std::endl << std::endl;
162  c_log << "To query the plugins, type:" << std::endl << std::endl;
163  c_log << " argos3 -q QUERY" << std::endl << std::endl;
164  c_log << "where QUERY can have the following values:" << std::endl << std::endl;
165  c_log << " all print a list of all the available plugins" << std::endl;
166  c_log << " actuators print a list of all the available actuators" << std::endl;
167  c_log << " sensors print a list of all the available sensors" << std::endl;
168  c_log << " physics_engines print a list of all the available physics engines" << std::endl;
169  c_log << " media print a list of all the available media" << std::endl;
170  c_log << " visualizations print a list of all the available visualizations" << std::endl;
171  c_log << " entities print a list of all the available entities" << std::endl << std::endl;
172  c_log << "Alternatively, QUERY can be the name of a specific plugin as returned by the" << std::endl;
173  c_log << "above commands. In this case, you get a complete description of the matching" << std::endl;
174  c_log << "plugins." << std::endl << std::endl;
175  }
176 
177  /****************************************/
178  /****************************************/
179 
181  LOG << "ARGOS_VERSION=" ARGOS_VERSION "-" ARGOS_RELEASE << std::endl;
182  LOG << "ARGOS_INSTALL_PREFIX=" ARGOS_INSTALL_PREFIX << std::endl;
183 #ifdef ARGOS_USE_DOUBLE
184  LOG << "ARGOS_USE_DOUBLE=ON" << std::endl;
185 #else
186  LOG << "ARGOS_USE_DOUBLE=OFF" << std::endl;
187 #endif
188 #ifdef ARGOS_WITH_LUA
189  LOG << "ARGOS_WITH_LUA=ON" << std::endl;
190 #else
191  LOG << "ARGOS_WITH_LUA=OFF" << std::endl;
192 #endif
193  LOG << "ARGOS_BUILD_FLAGS=" ARGOS_BUILD_FLAGS << std::endl;
194  }
195 
196  /****************************************/
197  /****************************************/
198 
199 }
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
signed int SInt32
32-bit signed integer.
Definition: datatypes.h:93
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
CARGoSLog LOGERR(std::cerr, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_RED))
Definition: argos_log.h:180
CARGoSLog LOG(std::cout, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_GREEN))
Definition: argos_log.h:179
virtual void Parse(SInt32 n_argc, char **ppch_argv)
Parses the command line.
virtual ~CARGoSCommandLineArgParser()
Class destructor.
virtual void PrintVersion()
Prints the current ARGoS version and release.
virtual void PrintUsage(CARGoSLog &c_log)
Prints usage information to the wanted log.
void AddFlag(char ch_short_option, const std::string &str_long_option, const std::string &str_description, bool &b_flag)
Adds a flag to the parser.
virtual void Parse(SInt32 n_argc, char **ppch_argv)
Parses the arguments on the command line.
std::ostream & GetStream()
Definition: argos_log.h:122
void DisableColoredOutput()
Definition: argos_log.h:114