Fixposition SDK 0.0.0-heads/main-0-g90a51ff
Collection of c++ libraries and apps for use with Fixposition products
Loading...
Searching...
No Matches
fpltool_opts.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 * \endverbatim
9 *
10 * @file
11 * @brief Fixposition SDK: fpltool args (command line arguments)
12 */
13#ifndef __FPSDK_APPS_FPLTOOL_FPLTOOL_ARGS_HPP__
14#define __FPSDK_APPS_FPLTOOL_FPLTOOL_ARGS_HPP__
15
16/* LIBC/STL */
17#include <cstdint>
18#include <string>
19#include <vector>
20
21/* EXTERNAL */
22#include <unistd.h>
23
24/* Fixposition SDK */
25#include <fpsdk_common/app.hpp>
27
28/* PACKAGE */
29
30namespace fpsdk {
31namespace apps {
32namespace fpltool {
33/* ****************************************************************************************************************** */
34
35/**
36 * @brief Program options
37 */
39{
40 public:
41 FplToolOptions() // clang-format off
42 : ProgramOptions("fpltool", {
43 { 'f', false }, { 'o', true }, { 'x', false }, { 'p', false },
44 { 'P', false }, { 'c', false }, { 'S', true }, { 'D', true } }) {}; // clang-format on
45
46 /**
47 * @brief Commands, modes of operation
48 */
49 enum class Command
50 {
51 UNSPECIFIED, //!< Bad command
52 DUMP, //!< Dump a log (for debugging)
53 META, //!< Get logfile meta data
54 ROSBAG, //!< Create ROS bag from log
55 TRIM, //!< Trim .fpl file
56 RECORD, //!< Record a log
57 EXTRACT, //!< Extract .fpl file to various (non-ROS) files
58 };
59
60 // clang-format off
61 Command command_ = Command::UNSPECIFIED; //!< Command to execute
62 std::string command_str_; //!< String of command_, for debugging
63 std::vector<std::string> inputs_; //!< List of input (files), or other positional arguments
64 std::string output_; //!< Output (file or directory, depending on command)
65 bool overwrite_ = false; //!< Overwrite existing output files
66 int extra_ = 0; //!< Enable extra output, such as hexdumps
67 int progress_ = 0; //!< Do progress reports
68 int compress_ = 0; //!< Compress output
69 uint32_t skip_ = 0; //!< Skip start [sec]
70 uint32_t duration_ = 0; //!< Duration [sec]
71 // clang-format on
72
73 void PrintHelp() final
74 {
75 // clang-format off
76 std::fputs(
77 "\n"
78 "Tool for .fpl logfiles\n"
79 "\n"
80 "Usage:\n"
81 "\n"
82 " parsertool [flags] <input> [<input> ...]\n"
83 "\n"
84 "Usage:\n"
85 "\n"
86 " fpltool [<flags>] <command> [...]\n"
87 "\n"
88 "Where (availability of flags depends on <command>, see below):\n"
89 "\n" ,stdout);
90 std::fputs(COMMON_FLAGS_HELP, stdout);
91 std::fputs(
92 " -p / -P -- Show / don't show progress (default: automatic)\n"
93 " -f -- Force overwrite output (default: refuse to overwrite existing output files)\n"
94 " -c -- Compress output (e.g. ROS bags), -c -c to compress more\n"
95 " -x -- Add extra output, multiple -x can be given\n"
96 " -o <out> -- Output to <out>\n"
97 " -S <sec> -- Skip <sec> seconds from start of log (approximate!) (default: 0, i.e. no skip)\n"
98 " -D <sec> -- Process <sec> seconds of log (approximate!) (default: everything)\n"
99 " \n"
100 "Print information about the data in a .fpl logfile, along with status and meta data\n"
101 "\n"
102 " fpltool [-vqpPx] dump <fpl-file>\n"
103 "\n"
104 "Print the meta data from a .fpl logfile (as YAML)\n"
105 "\n"
106 " fpltool [-vqpP] meta <fpl-file>\n"
107#ifdef FP_USE_ROS1
108 "\n"
109 "Generate a ROS bag from a .fpl logfile\n"
110 "\n"
111 " fpltool [-vqpPfoSD] <fpl-file>\n"
112#endif
113 "\n"
114 "Trim start and/or end of .fpl logfile, i.e. extract a portion of the file. Note that this process is\n"
115 "inaccurate and the effective start time and duration of the resulting file may be off by 30 to 60 seconds.\n"
116 "Therefore, both the start time (-S) and the duration (-D) must be at least 60 seconds.\n"
117 "\n"
118 " fpltool [-vqpPfo] -S <sec> -D <sec> <fpl-file>\n"
119 "\n"
120 "Examples:\n"
121#ifdef FP_USE_ROS1
122 "\n"
123 " Create a some.bag from a some.fpl logfile:\n"
124 "\n"
125 " fpltool rosbag some.fpl\n"
126 "\n"
127 " Create a compressed another.bag with 2 minutes of data starting 60 seconds into some.fpl logfile:\n"
128 "\n"
129 " fpltool rosbag some.fpl -c -c -o another.bag -S 60 -D 120\n"
130#endif
131 "\n"
132 " Show meta data of some.fpl:\n"
133 "\n"
134 " fpltool meta some.fpl\n"
135 "\n"
136 " Trim a verylong.fpl into a shorter one:\n"
137 "\n"
138 " fpltool trim some.fpl -S 3600 -D 1800\n"
139 "\n"
140 " Extract data from some.fpl:\n"
141 "\n"
142 " fpltool output some.fpl\n"
143 "\n"
144 " This results in various data files suitable for further processing. For example, the input/output\n"
145 " messages received/sent by the sensor can be processed by the parsertool:\n"
146 "\n"
147 " parsertool some_userio.raw\n"
148 "\n"
149 "\n", stdout);
150 // clang-format on
151 }
152
153 bool HandleOption(const Option& option, const std::string& argument) final
154 {
155 bool ok = true;
156 switch (option.flag) {
157 case 'f':
158 overwrite_ = true;
159 break;
160 case 'o':
161 output_ = argument;
162 break;
163 case 'x':
164 extra_++;
165 break;
166 case 'p':
167 progress_++;
168 progress_set_ = true;
169 break;
170 case 'P':
171 progress_ = 0;
172 progress_set_ = true;
173 break;
174 case 'c':
175 compress_++;
176 break;
177 case 'S':
179 ok = false;
180 }
181 break;
182 case 'D':
183 if (!fpsdk::common::string::StrToValue(argument, duration_) || (duration_ < 1)) {
184 ok = false;
185 }
186 break;
187 default:
188 ok = false;
189 break;
190 }
191 return ok;
192 }
193
194 bool CheckOptions(const std::vector<std::string>& args) final
195 {
196 bool ok = true;
197
198 // There must a remaining argument, which is the command
199 if (args.size() > 0) {
200 command_str_ = args[0];
201 // clang-format off
202 if (command_str_ == "meta") { command_ = Command::META; }
203 else if (command_str_ == "dump") { command_ = Command::DUMP; }
204 else if (command_str_ == "rosbag") { command_ = Command::ROSBAG; }
205 else if (command_str_ == "trim") { command_ = Command::TRIM; }
206 else if (command_str_ == "record") { command_ = Command::RECORD; }
207 else if (command_str_ == "extract") { command_ = Command::EXTRACT; }
208 // clang-format on
209 else {
210 WARNING("Unknown command '%s'", command_str_.c_str());
211 ok = false;
212 }
213 } else {
214 WARNING("Missing command or wrong arguments");
215 ok = false;
216 }
217
218 // Any further positional arguments
219 for (std::size_t ix = 1; ix < args.size(); ix++) {
220 inputs_.push_back(args[ix]);
221 }
222
223 // Default enable progress output and colours if run interactively
224 if (!progress_set_ && (isatty(fileno(stdin)) == 1)) {
225 progress_ = 1;
226 }
227
228 // Debug
229 DEBUG("command_ = '%s'", command_str_.c_str());
230 for (std::size_t ix = 0; ix < inputs_.size(); ix++) {
231 DEBUG("inputs_[%" PRIuMAX "] = '%s'", ix, inputs_[ix].c_str());
232 }
233 DEBUG("output = '%s'", output_.c_str());
234 DEBUG("overwrite = %s", overwrite_ ? "true" : "false");
235 DEBUG("extra = %d", extra_);
236 DEBUG("progress = %d", progress_);
237 DEBUG("compress = %d", compress_);
238 DEBUG("skip = %d", skip_);
239 DEBUG("duration = %d", duration_);
240
241 return ok;
242 }
243
244 private:
245 bool progress_set_ = false;
246};
247
248/* ****************************************************************************************************************** */
249} // namespace fpltool
250} // namespace apps
251} // namespace fpsdk
252#endif // __FPSDK_APPS_FPLTOOL_FPLTOOL_ARGS_HPP__
Fixposition SDK: Utilities for apps.
uint32_t duration_
Duration [sec].
void PrintHelp() final
Print the help screen and exit(0)
std::string output_
Output (file or directory, depending on command)
Command command_
Command to execute.
int progress_
Do progress reports.
Command
Commands, modes of operation.
@ EXTRACT
Extract .fpl file to various (non-ROS) files.
bool overwrite_
Overwrite existing output files.
std::string command_str_
String of command_, for debugging.
std::vector< std::string > inputs_
List of input (files), or other positional arguments.
bool HandleOption(const Option &option, const std::string &argument) final
Handle a command-line flag argument.
int extra_
Enable extra output, such as hexdumps.
uint32_t skip_
Skip start [sec].
bool CheckOptions(const std::vector< std::string > &args) final
Check options, and handle non-flag arguments.
ProgramOptions(const std::string &app_name, const std::vector< Option > &options)
Constructor.
static constexpr const char * COMMON_FLAGS_HELP
Help screen for common options.
Definition app.hpp:213
Fixposition SDK: Logging.
#define DEBUG(fmt,...)
Print a debug message.
Definition logging.hpp:72
#define WARNING(fmt,...)
Print a warning message.
Definition logging.hpp:60
bool StrToValue(const std::string &str, int8_t &value)
Convert string to value (int8_t)
Fixposition SDK.