cli.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Original idea: https://github.com/br0ziliy
  2. #include "cli.h"
  3. #include <lib/toolbox/args.h>
  4. #include "cli_common_helpers.h"
  5. #include "commands/list/list.h"
  6. #include "commands/add/add.h"
  7. #include "commands/delete/delete.h"
  8. #include "commands/timezone/timezone.h"
  9. #define TOTP_CLI_COMMAND_NAME "totp"
  10. #define TOTP_CLI_COMMAND_HELP "help"
  11. static void totp_cli_print_unknown_command(FuriString* unknown_command) {
  12. TOTP_CLI_PRINTF(
  13. "Command \"%s\" is unknown. Use \"help\" command to get list of available commands.",
  14. furi_string_get_cstr(unknown_command));
  15. }
  16. static void totp_cli_print_help() {
  17. TOTP_CLI_PRINTF("Usage:\r\n");
  18. TOTP_CLI_PRINTF(TOTP_CLI_COMMAND_NAME " <command> <arguments>\r\n");
  19. TOTP_CLI_PRINTF("Command list:\r\n");
  20. TOTP_CLI_PRINTF("\t" TOTP_CLI_COMMAND_HELP " - print command usage help\r\n\r\n");
  21. totp_cli_command_list_print_help();
  22. totp_cli_command_delete_print_help();
  23. totp_cli_command_add_print_help();
  24. totp_cli_command_timezone_print_help();
  25. }
  26. static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
  27. PluginState* plugin_state = (PluginState*)context;
  28. FuriString* cmd = furi_string_alloc();
  29. args_read_string_and_trim(args, cmd);
  30. if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP) == 0 || furi_string_empty(cmd)) {
  31. totp_cli_print_help();
  32. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD) == 0) {
  33. totp_cli_command_add_handle(plugin_state, args, cli);
  34. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST) == 0) {
  35. totp_cli_command_list_handle(plugin_state, cli);
  36. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE) == 0) {
  37. totp_cli_command_delete_handle(plugin_state, args, cli);
  38. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE) == 0) {
  39. totp_cli_command_timezone_handle(plugin_state, args, cli);
  40. } else {
  41. totp_cli_print_unknown_command(cmd);
  42. }
  43. furi_string_free(cmd);
  44. }
  45. void totp_cli_register_command_handler(PluginState* plugin_state) {
  46. Cli* cli = furi_record_open(RECORD_CLI);
  47. cli_add_command(
  48. cli, TOTP_CLI_COMMAND_NAME, CliCommandFlagParallelSafe, totp_cli_handler, plugin_state);
  49. furi_record_close(RECORD_CLI);
  50. }
  51. void totp_cli_unregister_command_handler() {
  52. Cli* cli = furi_record_open(RECORD_CLI);
  53. cli_delete_command(cli, TOTP_CLI_COMMAND_NAME);
  54. furi_record_close(RECORD_CLI);
  55. }