blob: 7e143ddbd9692ee2375ba6b3b1c970ac518ff3d7 [file] [log] [blame]
Brian Silvermanea2c95f2021-02-10 18:10:26 -08001#ifndef AOS_AOS_CLI_UTILS_H_
2#define AOS_AOS_CLI_UTILS_H_
3
4#include "aos/configuration.h"
5#include "aos/events/shm_event_loop.h"
Austin Schuh893d7f42022-09-16 15:01:35 -07006#include "aos/events/simulated_event_loop.h"
Brian Silvermanea2c95f2021-02-10 18:10:26 -08007#include "gflags/gflags.h"
8
9namespace aos {
10
Austin Schuh893d7f42022-09-16 15:01:35 -070011struct PrintOptions {
12 // Format the JSON with the pretty option.
13 bool pretty;
14 // Max vector size to skip expanding.
15 size_t max_vector_size;
16 // Put everything on a separate line instead of keeping small messages
17 // together.
18 bool pretty_max;
19 // Print the timestamps.
20 bool print_timestamps;
21 // Make everything JSON compliant.
22 bool json;
23 // Print the distributed clock.
24 bool distributed_clock;
25 // Print numbers out in hex.
26 bool use_hex;
27};
28
29// Print the flatbuffer out to stdout, both to remove the unnecessary cruft from
30// glog and to allow the user to readily redirect just the logged output
31// independent of any debugging information on stderr.
32void PrintMessage(const std::string_view node_name,
33 aos::NodeEventLoopFactory *node_factory,
34 const aos::Channel *channel, const aos::Context &context,
35 aos::FastStringBuilder *builder, PrintOptions options);
36
37void PrintMessage(const aos::Channel *channel, const aos::Context &context,
38 aos::FastStringBuilder *builder, PrintOptions options);
39
Brian Silvermanea2c95f2021-02-10 18:10:26 -080040// The information needed by the main function of a CLI tool.
41struct CliUtilInfo {
42 // If this returns true, main should return immediately with 0.
43 // If this returns false, the other fields will be filled out appropriately.
44 // event_loop will be filled out before channel_filter is called.
45 bool Initialize(int *argc, char ***argv,
Austin Schuh59f3b0f2021-07-31 20:50:40 -070046 std::function<bool(const aos::Channel *)> channel_filter,
Austin Schuhba2c8652022-08-17 14:56:08 -070047 std::string_view channel_filter_description,
Austin Schuh59f3b0f2021-07-31 20:50:40 -070048 bool expect_args);
Brian Silvermanea2c95f2021-02-10 18:10:26 -080049
50 std::optional<aos::FlatbufferDetachedBuffer<aos::Configuration>> config;
51 std::optional<aos::ShmEventLoop> event_loop;
52 std::vector<const aos::Channel *> found_channels;
53
54 private:
55 // Generate eval command to populate autocomplete responses. Eval escapes
56 // spaces so channels are paired with their types. If a complete channel name
57 // is found, only autocompletes the type to avoid repeating arguments. Returns
58 // no autocomplete suggestions if a channel and type is found with the current
59 // arguments.
60 void Autocomplete(std::string_view channel_name,
61 std::string_view message_type,
62 std::function<bool(const aos::Channel *)> channel_filter);
63
64 void ShiftArgs(int *argc, char ***argv) {
65 for (int i = 1; i + 1 < *argc; ++i) {
66 (*argv)[i] = (*argv)[i + 1];
67 }
68 --*argc;
69 }
70};
71
72} // namespace aos
73
74#endif // AOS_AOS_CLI_UTILS_H_