From 2c121aa88dafecf782fabcc2956225cb55f5c09b Mon Sep 17 00:00:00 2001 From: Dan Wang Date: Fri, 13 Dec 2024 14:21:01 +0800 Subject: [PATCH] fix(shell): fix the validation for the command while there is not any positional argument (#2166) Fix https://github.com/apache/incubator-pegasus/issues/2168. --- src/replica/replica_backup.cpp | 1 - src/shell/command_utils.h | 20 +++++++++++++++----- src/shell/commands/detect_hotkey.cpp | 2 +- src/shell/commands/duplication.cpp | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/replica/replica_backup.cpp b/src/replica/replica_backup.cpp index 1be16e30c0..3e8856bee1 100644 --- a/src/replica/replica_backup.cpp +++ b/src/replica/replica_backup.cpp @@ -28,7 +28,6 @@ #include #include #include -#include #include #include diff --git a/src/shell/command_utils.h b/src/shell/command_utils.h index c5c7f32044..3c4ffacac1 100644 --- a/src/shell/command_utils.h +++ b/src/shell/command_utils.h @@ -38,12 +38,13 @@ class host_port; struct shell_context; -// Check if positional arguments are empty, and they should also be empty, which means only +// Check if there is not any other positional argument except the command, which means only // parameters and flags are needed. -inline dsn::error_s empty_pos_args(const argh::parser &cmd) +inline dsn::error_s no_pos_arg(const argh::parser &cmd) { - if (cmd.size() > 0) { - return FMT_ERR(dsn::ERR_INVALID_PARAMETERS, "there shouldn't be any positional arguments"); + // 1 means there is not any other positional argument except the command. + if (cmd.size() > 1) { + return FMT_ERR(dsn::ERR_INVALID_PARAMETERS, "there shouldn't be any positional argument"); } return dsn::error_s::ok(); @@ -74,13 +75,22 @@ validate_cmd(const argh::parser &cmd, } if (flags.find(flag) == flags.end()) { - return FMT_ERR(dsn::ERR_INVALID_PARAMETERS, "unknown flag\n", flag); + return FMT_ERR(dsn::ERR_INVALID_PARAMETERS, "unknown flag", flag); } } return dsn::error_s::ok(); } +// Check if the parameters and flags are in the given set while there is not any positional +// argument. +inline dsn::error_s validate_cmd(const argh::parser &cmd, + const std::set ¶ms, + const std::set &flags) +{ + return validate_cmd(cmd, params, flags, no_pos_arg); +} + bool validate_ip(shell_context *sc, const std::string &host_port_str, /*out*/ dsn::host_port &target_hp, diff --git a/src/shell/commands/detect_hotkey.cpp b/src/shell/commands/detect_hotkey.cpp index 1efa686aa6..dcdf2c5888 100644 --- a/src/shell/commands/detect_hotkey.cpp +++ b/src/shell/commands/detect_hotkey.cpp @@ -87,7 +87,7 @@ bool detect_hotkey(command_executor *e, shell_context *sc, arguments args) argh::parser cmd(args.argc, args.argv, argh::parser::PREFER_PARAM_FOR_UNREG_OPTION); - const auto &check = validate_cmd(cmd, params, flags, empty_pos_args); + const auto &check = validate_cmd(cmd, params, flags); if (!check) { // TODO(wangdan): use SHELL_PRINT* macros instead. fmt::print(stderr, "{}\n", check.description()); diff --git a/src/shell/commands/duplication.cpp b/src/shell/commands/duplication.cpp index 1f37a6a423..55c770922e 100644 --- a/src/shell/commands/duplication.cpp +++ b/src/shell/commands/duplication.cpp @@ -637,7 +637,7 @@ bool ls_dups(command_executor *e, shell_context *sc, arguments args) argh::parser cmd(args.argc, args.argv, argh::parser::PREFER_PARAM_FOR_UNREG_OPTION); // Check if input parameters and flags are valid. - const auto &check = validate_cmd(cmd, params, flags, empty_pos_args); + const auto &check = validate_cmd(cmd, params, flags); if (!check) { SHELL_PRINTLN_ERROR("{}", check.description()); return false;