Fixposition SDK 0.0.0-heads/main-0-g7b59b93
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", { { 'x', false }, { 's', false } }) {}; // clang-format on
43
44 /**
45 * @brief Commands, modes of operation
46 */
47 enum class Command
48 {
49 UNSPECIFIED, //!< Bad command
50 DUMP, //!< Dump a log (for debugging)
51 META, //!< Get logfile meta data
52 ROSBAG, //!< Create ROS bag from log
53 TRIM, //!< Trim .fpl file
54 RECORD, //!< Record a log
55 };
56
57 // clang-format off
58 Command command_ = Command::UNSPECIFIED; //!< Command to execute
59 std::string command_str_; //!< String of command_, for debugging
60 std::vector<std::string> inputs_; //!< List of input (files), or other positional arguments
61 std::string output_; //!< Output (file or directory, depending on command)
62 bool overwrite_ = false; //!< Overwrite existing output files
63 int extra_ = 0; //!< Enable extra output, such as hexdumps
64 int progress_ = 0; //!< Do progress reports
65 int compress_ = 0; //!< Compress output
66 uint32_t skip_ = 0; //!< Skip start [sec]
67 uint32_t duration_ = 0; //!< Duration [sec]
68 // clang-format on
69
70 void PrintHelp() final
71 {
72 // clang-format off
73 std::fputs(
74 "\n"
75 "Tool for .fpl logfiles\n"
76 "\n"
77 "Usage:\n"
78 "\n"
79 " parsertool [flags] <input> [<input> ...]\n"
80 "\n"
81 "Usage:\n"
82 "\n"
83 " fpltool [<flags>] <command> [...]\n"
84 "\n"
85 "Where (availability of flags depends on <command>, see below):\n"
86 "\n" ,stdout);
87 std::fputs(COMMON_FLAGS_HELP, stdout);
88 std::fputs(
89 " -p / -P -- Show / don't show progress (default: automatic)\n"
90 " -f -- Force overwrite output (default: refuse to overwrite existing output files)\n"
91 " -c -- Compress output (e.g. ROS bags), -c -c to compress more\n"
92 " -x -- Add extra output, multiple -x can be given\n"
93 " -o <out> -- Output to <out>\n"
94 " -S <sec> -- Skip <sec> seconds from start of log (approximate!) (default: 0, i.e. no skip)\n"
95 " -D <sec> -- Process <sec> seconds of log (approximate!) (default: everything)\n"
96 " \n"
97 "Print information about the data in a .fpl logfile, along with status and meta data\n"
98 "\n"
99 " fpltool [-vqpPx] dump <fpl-file>\n"
100 "\n"
101 "Print the meta data from a .fpl logfile (as YAML)\n"
102 "\n"
103 " fpltool [-vqpP] meta <fpl-file>\n"
104#ifdef FP_USE_ROS1
105 "\n"
106 "Generate a ROS bag from a .fpl logfile\n"
107 "\n"
108 " fpltool [-vqpPfoSD] <fpl-file>\n"
109#endif
110 "\n"
111 "Trim start and/or end of .fpl logfile, i.e. extract a portion of the file. Note that this process is\n"
112 "inaccurate and the effective start time and duration of the resulting file may be off by 30 to 60 seconds.\n"
113 "Therefore, both the start time (-S) and the duration (-D) must be at least 60 seconds.\n"
114 "\n"
115 " fpltool [-vqpPfo] -S <sec> -D <sec> <fpl-file>\n"
116 "\n"
117 "Examples:\n"
118#ifdef FP_USE_ROS1
119 "\n"
120 " Create a some.bag from a some.fpl logfile:\n"
121 "\n"
122 " fpltool rosbag some.fpl\n"
123 "\n"
124 " Create a compressed another.bag with 2 minutes of data starting 60 seconds into some.fpl logfile:\n"
125 "\n"
126 " fpltool rosbag some.fpl -c -c -o another.bag -S 60 -D 120\n"
127#endif
128 "\n"
129 " Show meta data of some.fpl:\n"
130 "\n"
131 " fpltool meta some.fpl\n"
132 "\n"
133 " Trim a verylong.fpl into a shorter one:\n"
134 "\n"
135 " fpltool trim some.fpl -S 3600 -D 1800\n"
136 "\n", stdout);
137 // clang-format on
138 }
139
140 bool HandleOption(const Option& option, const std::string& argument) final
141 {
142 bool ok = true;
143 switch (option.flag) {
144 case 'f':
145 overwrite_ = true;
146 break;
147 case 'o':
148 output_ = argument;
149 break;
150 case 'x':
151 extra_++;
152 break;
153 case 'p':
154 progress_++;
155 progress_set_ = true;
156 break;
157 case 'P':
158 progress_ = 0;
159 progress_set_ = true;
160 break;
161 case 'c':
162 compress_++;
163 break;
164 case 'S':
166 ok = false;
167 }
168 break;
169 case 'D':
170 if (!fpsdk::common::string::StrToValue(argument, duration_) || (duration_ < 1)) {
171 ok = false;
172 }
173 break;
174 default:
175 ok = false;
176 break;
177 }
178 return ok;
179 }
180
181 bool CheckOptions(const std::vector<std::string>& args) final
182 {
183 bool ok = true;
184
185 // There must a remaining argument, which is the command
186 if (args.size() > 0) {
187 command_str_ = args[0];
188 // clang-format off
189 if (command_str_ == "meta") { command_ = Command::META; }
190 else if (command_str_ == "dump") { command_ = Command::DUMP; }
191 else if (command_str_ == "rosbag") { command_ = Command::ROSBAG; }
192 else if (command_str_ == "trim") { command_ = Command::TRIM; }
193 else if (command_str_ == "record") { command_ = Command::RECORD; }
194 // clang-format on
195 else {
196 WARNING("Unknown command '%s'", command_str_.c_str());
197 ok = false;
198 }
199 } else {
200 WARNING("Missing command or wrong arguments. Try 'fpltool -h'...");
201 ok = false;
202 }
203
204 // Any further positional arguments
205 for (std::size_t ix = 1; ix < args.size(); ix++) {
206 inputs_.push_back(args[ix]);
207 }
208
209 // Default enable progress output and colours if run interactively
210 if (!progress_set_ && (isatty(fileno(stdin)) == 1)) {
211 progress_ = 1;
212 }
213
214 // Debug
215 DEBUG("command_ = '%s'", command_str_.c_str());
216 for (std::size_t ix = 0; ix < inputs_.size(); ix++) {
217 DEBUG("inputs_[%" PRIuMAX "] = '%s'", ix, inputs_[ix].c_str());
218 }
219 DEBUG("output = '%s'", output_.c_str());
220 DEBUG("overwrite = %s", overwrite_ ? "true" : "false");
221 DEBUG("extra = %d", extra_);
222 DEBUG("progress = %d", progress_);
223 DEBUG("compress = %d", compress_);
224 DEBUG("skip = %d", skip_);
225 DEBUG("duration = %d", duration_);
226
227 return ok;
228 }
229
230 private:
231 bool progress_set_ = false;
232};
233
234/* ****************************************************************************************************************** */
235} // namespace fpltool
236} // namespace apps
237} // namespace fpsdk
238#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.
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:174
Fixposition SDK: Logging.
#define DEBUG(fmt,...)
Print a debug message.
Definition logging.hpp:70
#define WARNING(fmt,...)
Print a warning message.
Definition logging.hpp:58
bool StrToValue(const std::string &str, int8_t &value)
Convert string to value (int8_t)
Fixposition SDK.
Definition fpsdk_doc.hpp:20