Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 1 | #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" |
| 6 | #include "gflags/gflags.h" |
| 7 | |
| 8 | namespace aos { |
| 9 | |
| 10 | // The information needed by the main function of a CLI tool. |
| 11 | struct CliUtilInfo { |
| 12 | // If this returns true, main should return immediately with 0. |
| 13 | // If this returns false, the other fields will be filled out appropriately. |
| 14 | // event_loop will be filled out before channel_filter is called. |
| 15 | bool Initialize(int *argc, char ***argv, |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 16 | std::function<bool(const aos::Channel *)> channel_filter, |
Austin Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame^] | 17 | std::string_view channel_filter_description, |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 18 | bool expect_args); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 19 | |
| 20 | std::optional<aos::FlatbufferDetachedBuffer<aos::Configuration>> config; |
| 21 | std::optional<aos::ShmEventLoop> event_loop; |
| 22 | std::vector<const aos::Channel *> found_channels; |
| 23 | |
| 24 | private: |
| 25 | // Generate eval command to populate autocomplete responses. Eval escapes |
| 26 | // spaces so channels are paired with their types. If a complete channel name |
| 27 | // is found, only autocompletes the type to avoid repeating arguments. Returns |
| 28 | // no autocomplete suggestions if a channel and type is found with the current |
| 29 | // arguments. |
| 30 | void Autocomplete(std::string_view channel_name, |
| 31 | std::string_view message_type, |
| 32 | std::function<bool(const aos::Channel *)> channel_filter); |
| 33 | |
| 34 | void ShiftArgs(int *argc, char ***argv) { |
| 35 | for (int i = 1; i + 1 < *argc; ++i) { |
| 36 | (*argv)[i] = (*argv)[i + 1]; |
| 37 | } |
| 38 | --*argc; |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | } // namespace aos |
| 43 | |
| 44 | #endif // AOS_AOS_CLI_UTILS_H_ |