Skip to content

Commit

Permalink
fix(shell): fix the validation for the command while there is not any…
Browse files Browse the repository at this point in the history
… positional argument (#2166)

Fix #2168.
  • Loading branch information
empiredan authored Dec 13, 2024
1 parent eb88b27 commit 2c121aa
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/replica/replica_backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <map>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

Expand Down
20 changes: 15 additions & 5 deletions src/shell/command_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<std::string> &params,
const std::set<std::string> &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,
Expand Down
2 changes: 1 addition & 1 deletion src/shell/commands/detect_hotkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/shell/commands/duplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2c121aa

Please sign in to comment.