cli.c 2.3 KB

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