mvsim-cli-main.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #include <mrpt/3rdparty/tclap/CmdLine.h>
11 #include <mrpt/core/exceptions.h>
12 #include <mrpt/system/CTicTac.h>
13 #include <mrpt/system/os.h> // kbhit()
14 #include <mrpt/version.h>
15 #include <mvsim/mvsim_version.h>
16 
17 #include <chrono>
18 #include <iostream>
19 #include <map>
20 
21 #include "mvsim-cli.h"
22 
23 // ======= Command handlers =======
24 std::unique_ptr<cli_flags> cli;
25 
26 const std::map<std::string, cmd_t> cliCommands = {
27  {"help", cmd_t(&printListCommands)},
28  {"server", cmd_t(&launchStandAloneServer)},
29  {"launch", cmd_t(&launchSimulation)},
30  {"node", cmd_t(&commandNode)},
31  {"topic", cmd_t(&commandTopic)},
32 };
33 
35 {
36 #if MRPT_VERSION >= 0x233
37  mrpt::system::consoleColorAndStyle(
38  mrpt::system::ConsoleForegroundColor::RED);
39 #else
40  mrpt::system::setConsoleColor(mrpt::system::CONCOL_RED);
41 #endif
42 }
43 
45 {
46 #if MRPT_VERSION >= 0x233
47  mrpt::system::consoleColorAndStyle(
48  mrpt::system::ConsoleForegroundColor::DEFAULT);
49 #else
50  mrpt::system::setConsoleColor(mrpt::system::CONCOL_NORMAL);
51 #endif
52 }
53 
54 int main(int argc, char** argv)
55 {
56  try
57  {
58  cli = std::make_unique<cli_flags>();
59 
60  if (!cli->cmd.parse(argc, argv))
61  {
63  return 1;
64  }
65 
66  if (cli->argVersion.isSet())
67  {
68  printVersion();
69  return 0;
70  }
71 
72  // Take first unlabeled argument:
73  std::string command;
74  if (const auto& lst = cli->argCmd.getValue(); !lst.empty())
75  command = lst.at(0);
76 
77  // Look up command in table:
78  auto itCmd = cliCommands.find(command);
79 
80  if (!cli->argCmd.isSet() || itCmd == cliCommands.end())
81  {
82  if (!cli->argHelp.isSet())
83  {
85  std::cerr << "Error: missing or unknown command.\n";
87  }
89  return 1;
90  }
91 
92  // Execute command:
93  return (itCmd->second)();
94  }
95  catch (const std::exception& e)
96  {
97  std::cerr << "ERROR: " << mrpt::exception_to_str(e);
98  return 1;
99  }
100  return 0;
101 }
102 
104 {
105  fprintf(
106  stderr,
107  R"XXX(mvsim v%s: A lightweight multivehicle simulation environment.
108 
109 Available commands:
110  mvsim launch <WORLD.xml> Start a comm. server and simulates a world.
111  mvsim server Start a standalone communication server.
112  mvsim node List connected nodes, etc.
113  mvsim topic Inspect, publish, etc. topics.
114  mvsim --version Shows program version.
115  mvsim --help Shows this information.
116 
117 Or use `mvsim <COMMAND> --help` for further options
118 )XXX",
119  MVSIM_VERSION);
120  return 0;
121 }
122 
123 void printVersion() { std::cout << "mvsim v" << MVSIM_VERSION << std::endl; }
int commandNode()
const std::map< std::string, cmd_t > cliCommands
std::function< int(void)> cmd_t
Definition: mvsim-cli.h:74
void setConsoleErrorColor()
int launchStandAloneServer()
int main(int argc, char **argv)
#define MVSIM_VERSION
Definition: mvsim_version.h:19
void printVersion()
ROSLIB_DECL std::string command(const std::string &cmd)
int launchSimulation()
std::unique_ptr< cli_flags > cli
int printListCommands()
void setConsoleNormalColor()
int commandTopic()


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21