rcdiscover.cc
Go to the documentation of this file.
1 /*
2  * rcdiscover - the network discovery tool for Roboception devices
3  *
4  * Copyright (c) 2018 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Raphael Schaller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
41 
42 #include <iostream>
43 #include <map>
44 #include <functional>
45 
46 #ifdef WIN32
47 #include <winsock2.h>
48 #endif
49 
50 struct Command
51 {
52  std::string description;
53  std::function<int(const std::string &, int, char **)> fun;
54 };
55 
56 static const std::map<std::string, Command> commands =
57  {
58  {"ls", {"List available devices", runDiscover}},
59  {"reconnect", {"Reconnect a device", runReconnect}},
60  {"forceip", {"Temporarily set the IP of a device", runForceIP}},
61  {"reset", {"Reset a device's parameters", runReset}}
62  };
63 
64 class WSA
65 {
66  public:
67  WSA()
68  {
69 #ifdef WIN32
70  WSADATA wsaData;
71  WSAStartup(MAKEWORD(2, 2), &wsaData);
72 #endif
73  }
74 
75  ~WSA()
76  {
77 #ifdef WIN32
78  ::WSACleanup();
79 #endif
80  }
81 };
82 
83 int main(int argc, char *argv[])
84 {
85  WSA wsa;
86 
87  std::string help_string = std::string("Usage: ") +
88  argv[0] + " [-h | --help] [--version] <command> [<args>]\n\n" +
89  "Available commands are:\n";
90 
91  const int max_command_len = getMaxCommandLen(commands);
92  for (const auto &cmd : commands)
93  {
94  const int min_padding = 3;
95  const int padding =
96  max_command_len + min_padding - static_cast<int>(cmd.first.length());
97  help_string += " " + cmd.first +
98  std::string(static_cast<std::size_t>(padding), ' ') +
99  cmd.second.description + "\n";
100  }
101 
102  help_string += std::string("\n") +
103  "If no command is given, 'ls' is assumed.\n" +
104  "See '" + argv[0] + " <command> --help' for command-specific help.";
105 
106  auto command = commands.find("ls");
107 
108  int argc_subcommand = argc - 1;
109  char **argv_subcommand = argv + 1;
110 
111  if (argc > 1)
112  {
113  const std::string argv_1(argv[1]);
114  if (argv_1 == "--help" || argv_1 == "-h")
115  {
116  std::cout << help_string << std::endl;
117  return 0;
118  }
119  if (argv_1 == "--version")
120  {
121  std::cout << PACKAGE_VERSION << std::endl;
122  return 0;
123  }
124 
125  if (!argv_1.empty() && argv_1[0] != '-')
126  {
127  command = commands.find(argv[1]);
128  --argc_subcommand;
129  ++argv_subcommand;
130  }
131  }
132 
133  if (command == commands.end())
134  {
135  std::cerr << "Invalid command\n";
136  std::cerr << help_string << '\n';
137  return 1;
138  }
139 
140  std::string command_str;
141  for (int i = 0; i < (argc - argc_subcommand); ++i)
142  {
143  if (!command_str.empty())
144  { command_str += ' '; }
145  command_str += argv[i];
146  }
147 
148  return command->second.fun(command_str, argc_subcommand, argv_subcommand);
149 }
int runReset(const std::string &command, int argc, char **argv)
std::function< int(const std::string &, int, char **)> fun
Definition: rcdiscover.cc:53
int getMaxCommandLen(const std::map< K, V > &commands)
Definition: cli_utils.h:70
int runForceIP(const std::string &command, int argc, char **argv)
int main(int argc, char *argv[])
Definition: rcdiscover.cc:83
~WSA()
Definition: rcdiscover.cc:75
int runReconnect(const std::string &command, int argc, char **argv)
static const std::map< std::string, Command > commands
Definition: rcdiscover.cc:56
WSA()
Definition: rcdiscover.cc:67
std::string description
Definition: rcdiscover.cc:52
int runDiscover(const std::string &command, int argc, char **argv)


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Sun Apr 18 2021 02:16:32