mvsim-cli-main.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 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 
15 #include <chrono>
16 #include <iostream>
17 #include <map>
18 
19 #include "mvsim-cli.h"
20 
21 TCLAP::CmdLine cmd("mvsim", ' ', "version", false /* no --help */);
22 
24  "command", "Command to run. Run 'mvsim help' to list commands.", false, "",
25  cmd);
26 
28  "v", "verbose", "Verbosity level", false, "INFO", "ERROR|WARN|INFO|DEBUG",
29  cmd);
30 
32  "", "details", "Shows details in the specified subcommand", cmd);
33 
35  "h", "help", "Shows more detailed help for command", cmd);
36 
37 // ======= Command handlers =======
38 
39 static const std::map<std::string, cmd_t> cliCommands = {
40  {"help", cmd_t(&printListCommands)},
41  {"server", cmd_t(&launchStandAloneServer)},
42  {"launch", cmd_t(&launchSimulation)},
43  {"node", cmd_t(&commandNode)},
44  {"topic", cmd_t(&commandTopic)},
45 };
46 
47 int main(int argc, char** argv)
48 {
49  try
50  {
51  if (!cmd.parse(argc, argv))
52  {
54  return 1;
55  }
56 
57  // Take first unlabeled argument:
58  std::string command;
59  if (const auto& lst = argCmd.getValue(); !lst.empty())
60  command = lst.at(0);
61 
62  // Look up command in table:
63  auto itCmd = cliCommands.find(command);
64 
65  if (!argCmd.isSet() || itCmd == cliCommands.end())
66  {
68  std::cerr << "Error: missing or unknown command.\n";
71  return 1;
72  }
73 
74  // Execute command:
75  return (itCmd->second)();
76  }
77  catch (const std::exception& e)
78  {
79  std::cerr << "ERROR: " << mrpt::exception_to_str(e);
80  return 1;
81  }
82  return 0;
83 }
84 
86 {
87  fprintf(
88  stderr,
89  R"XXX(mvsim: A lightweight multivehicle simulation environment.
90 
91 Available commands:
92  mvsim launch <WORLD.xml> Start a comm. server and simulates a world.
93  mvsim server Start a standalone communication server.
94  mvsim node List connected nodes, etc.
95  mvsim topic Inspect, publish, etc. topics.
96 
97 Or use `mvsim <COMMAND> --help` for further options
98 )XXX");
99  return 0;
100 }
int commandNode()
static const std::map< std::string, cmd_t > cliCommands
void BASE_IMPEXP setConsoleColor(TConsoleColor color, bool changeStdErr=false)
std::function< int(void)> cmd_t
Definition: mvsim-cli.h:23
TCLAP::SwitchArg argHelp("h","help","Shows more detailed help for command", cmd)
int BASE_IMPEXP fprintf(FILE *fil, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(2
int launchStandAloneServer()
int main(int argc, char **argv)
TCLAP::ValueArg< std::string > argVerbosity("v","verbose","Verbosity level", false,"INFO","ERROR|WARN|INFO|DEBUG", cmd)
string command
TCLAP::UnlabeledMultiArg< std::string > argCmd("command","Command to run. Run 'mvsim help' to list commands.", false,"", cmd)
int launchSimulation()
TCLAP::CmdLine cmd("mvsim", ' ',"version", false)
TCLAP::SwitchArg argDetails("","details","Shows details in the specified subcommand", cmd)
bool parse(int argc, char **argv)
int printListCommands()
int commandTopic()


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51