Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shell): fix the validation for the command while there is not any positional argument #2166

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading