mvsim-cli-main.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2024 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)}, {"server", cmd_t(&launchStandAloneServer)},
28  {"launch", cmd_t(&launchSimulation)}, {"node", cmd_t(&commandNode)},
29  {"topic", cmd_t(&commandTopic)},
30 };
31 
33 {
34 #if MRPT_VERSION >= 0x233
35  mrpt::system::consoleColorAndStyle(mrpt::system::ConsoleForegroundColor::RED);
36 #else
37  mrpt::system::setConsoleColor(mrpt::system::CONCOL_RED);
38 #endif
39 }
40 
42 {
43 #if MRPT_VERSION >= 0x233
44  mrpt::system::consoleColorAndStyle(mrpt::system::ConsoleForegroundColor::DEFAULT);
45 #else
46  mrpt::system::setConsoleColor(mrpt::system::CONCOL_NORMAL);
47 #endif
48 }
49 
50 int main(int argc, char** argv)
51 {
52  try
53  {
54  cli = std::make_unique<cli_flags>();
55 
56  if (!cli->cmd.parse(argc, argv))
57  {
59  return 1;
60  }
61 
62  if (cli->argVersion.isSet())
63  {
64  printVersion();
65  return 0;
66  }
67 
68  // Take first unlabeled argument:
69  std::string command;
70  if (const auto& lst = cli->argCmd.getValue(); !lst.empty()) command = lst.at(0);
71 
72  // Look up command in table:
73  auto itCmd = cliCommands.find(command);
74 
75  if (!cli->argCmd.isSet() || itCmd == cliCommands.end())
76  {
77  if (!cli->argHelp.isSet())
78  {
80  std::cerr << "Error: missing or unknown command.\n";
82  }
84  return 1;
85  }
86 
87  // Execute command:
88  return (itCmd->second)();
89  }
90  catch (const std::exception& e)
91  {
92  std::cerr << "ERROR: " << mrpt::exception_to_str(e);
93  return 1;
94  }
95  return 0;
96 }
97 
99 {
100  fprintf(
101  stderr,
102  R"XXX(mvsim v%s: A lightweight multivehicle simulation environment.
103 
104 Available commands:
105  mvsim launch <WORLD.xml> Start a comm. server and simulates a world.
106  mvsim server Start a standalone communication server.
107  mvsim node List connected nodes, etc.
108  mvsim topic Inspect, publish, etc. topics.
109  mvsim --version Shows program version.
110  mvsim --help Shows this information.
111 
112 Or use `mvsim <COMMAND> --help` for further options
113 )XXX",
114  MVSIM_VERSION);
115  return 0;
116 }
117 
118 void printVersion() { std::cout << "mvsim v" << MVSIM_VERSION << std::endl; }
printVersion
void printVersion()
Definition: mvsim-cli-main.cpp:107
setConsoleNormalColor
void setConsoleNormalColor()
Definition: mvsim-cli-main.cpp:41
command
ROSLIB_DECL std::string command(const std::string &cmd)
launchStandAloneServer
int launchStandAloneServer()
Definition: mvsim-cli-server.cpp:40
printListCommands
int printListCommands()
Definition: mvsim-cli-main.cpp:98
launchSimulation
int launchSimulation()
Definition: mvsim-cli-launch.cpp:161
commandNode
int commandNode()
Definition: mvsim-cli-node.cpp:25
cmd_t
std::function< int(void)> cmd_t
Definition: mvsim-cli.h:65
main
int main(int argc, char **argv)
Definition: mvsim-cli-main.cpp:50
commandTopic
int commandTopic()
Definition: mvsim-cli-topic.cpp:34
cliCommands
const std::map< std::string, cmd_t > cliCommands
Definition: mvsim-cli-main.cpp:26
mvsim-cli.h
mvsim_version.h
MVSIM_VERSION
#define MVSIM_VERSION
Definition: mvsim_version.h:19
setConsoleErrorColor
void setConsoleErrorColor()
Definition: mvsim-cli-main.cpp:32
cli
std::unique_ptr< cli_flags > cli
Definition: mvsim-cli-main.cpp:24


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:08