From 5b6aedbef0c2eddea5ff7654a5b2055ec3820d1f Mon Sep 17 00:00:00 2001 From: Alexander Kopachov Date: Fri, 31 Mar 2023 10:52:41 +0200 Subject: [PATCH] Implemented #102 (#104) --- cli/commands/help/help.c | 2 +- cli/commands/move/move.c | 52 +++++++--------------------------------- cli/commands/move/move.h | 2 +- 3 files changed, 11 insertions(+), 45 deletions(-) diff --git a/cli/commands/help/help.c b/cli/commands/help/help.c index 741098ad973..cc47db11f80 100644 --- a/cli/commands/help/help.c +++ b/cli/commands/help/help.c @@ -55,6 +55,7 @@ void totp_cli_command_help_handle() { TOTP_CLI_PRINTF("Arguments:\r\n"); totp_cli_command_add_docopt_arguments(); totp_cli_command_delete_docopt_arguments(); + totp_cli_command_move_docopt_arguments(); totp_cli_command_timezone_docopt_arguments(); totp_cli_command_notification_docopt_arguments(); totp_cli_command_automation_docopt_arguments(); @@ -63,5 +64,4 @@ void totp_cli_command_help_handle() { totp_cli_command_add_docopt_options(); totp_cli_command_update_docopt_options(); totp_cli_command_delete_docopt_options(); - totp_cli_command_move_docopt_options(); } \ No newline at end of file diff --git a/cli/commands/move/move.c b/cli/commands/move/move.c index abb38143ed0..3526fb51398 100644 --- a/cli/commands/move/move.c +++ b/cli/commands/move/move.c @@ -9,8 +9,7 @@ #include "../../../ui/scene_director.h" #include "../../common_command_arguments.h" -#define TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX "index" -#define TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX "-i" +#define TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX "new_index" void totp_cli_command_move_docopt_commands() { TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_MOVE ", " TOTP_CLI_COMMAND_MOVE_ALT @@ -20,16 +19,13 @@ void totp_cli_command_move_docopt_commands() { void totp_cli_command_move_docopt_usage() { TOTP_CLI_PRINTF( " " TOTP_CLI_COMMAND_NAME - " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_MOVE " | " TOTP_CLI_COMMAND_MOVE_ALT) " " DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_ARG_INDEX) " " DOCOPT_OPTIONAL( - DOCOPT_OPTION( - TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX, - DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX))) "\r\n"); + " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_MOVE " | " TOTP_CLI_COMMAND_MOVE_ALT) " " DOCOPT_ARGUMENT( + TOTP_CLI_COMMAND_ARG_INDEX) " " DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX) "\r\n"); } -void totp_cli_command_move_docopt_options() { - TOTP_CLI_PRINTF(" " DOCOPT_OPTION( - TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX, - DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX)) " New token index\r\n"); +void totp_cli_command_move_docopt_arguments() { + TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX + " New token index in the list\r\n"); } void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, Cli* cli) { @@ -44,39 +40,11 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C return; } - FuriString* temp_str = furi_string_alloc(); - int new_token_index = 0; - while(args_read_string_and_trim(args, temp_str)) { - bool parsed = false; - if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX) == 0) { - if(!args_read_int_and_trim(args, &new_token_index)) { - TOTP_CLI_PRINTF_ERROR( - "Missed value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX - "\"\r\n"); - } else if(new_token_index < 1 || new_token_index > plugin_state->tokens_count) { - TOTP_CLI_PRINTF_ERROR( - "\"%" PRId16 - "\" is incorrect value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX - "\"\r\n", - new_token_index); - } else { - parsed = true; - } - } else { - TOTP_CLI_PRINTF_ERROR("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str)); - } - - if(!parsed) { - TOTP_CLI_PRINT_INVALID_ARGUMENTS(); - furi_string_free(temp_str); - return; - } - } - - if(!totp_cli_ensure_authenticated(plugin_state, cli)) { - furi_string_free(temp_str); + if(!args_read_int_and_trim(args, &new_token_index) || new_token_index < 1 || + new_token_index > plugin_state->tokens_count) { + TOTP_CLI_PRINT_INVALID_ARGUMENTS(); return; } @@ -113,6 +81,4 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C if(activate_generate_token_scene) { totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL); } - - furi_string_free(temp_str); } \ No newline at end of file diff --git a/cli/commands/move/move.h b/cli/commands/move/move.h index 9eaad5319b8..da84f2a620a 100644 --- a/cli/commands/move/move.h +++ b/cli/commands/move/move.h @@ -9,4 +9,4 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, Cli* cli); void totp_cli_command_move_docopt_commands(); void totp_cli_command_move_docopt_usage(); -void totp_cli_command_move_docopt_options(); \ No newline at end of file +void totp_cli_command_move_docopt_arguments(); \ No newline at end of file