Skip to content

Commit

Permalink
fix(shell): Add PARSE_OPT_STRS marco for shell command execute flag i…
Browse files Browse the repository at this point in the history
…nput mode (#2040)

The marco `PARSE_STRS` execute strs with `param_index`,and it only
execute the number of params_index of input strs.

The marco `PARSE_OPT_STRS` can execute input strs with flag.

The historical flag input mode should be continued.
  • Loading branch information
Samunroyu authored Jun 5, 2024
1 parent 6c96b5a commit 8fd8902
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/shell/command_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,12 @@ class aggregate_stats_calcs
} \
} while (false)

#define PARSE_OPT_STRS(container, def_val, ...) \
do { \
const auto param = cmd(__VA_ARGS__, (def_val)).str(); \
::dsn::utils::split_args(param.c_str(), container, ','); \
} while (false)

// A helper macro to parse command argument, the result is filled in an uint32_t variable named
// 'value'.
#define PARSE_UINT(value) \
Expand Down
23 changes: 19 additions & 4 deletions src/shell/commands/cold_backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>

Expand Down Expand Up @@ -157,15 +159,28 @@ bool ls_backup_policy(command_executor *e, shell_context *sc, arguments args)

bool query_backup_policy(command_executor *e, shell_context *sc, arguments args)
{
const std::string query_backup_policy_help = "<policy_name> [-b|--backup_info_cnt] [-j|--json]";
const std::string query_backup_policy_help =
"<-p|--policy_name> [-b|--backup_info_cnt] [-j|--json]";
argh::parser cmd(args.argc, args.argv, argh::parser::PREFER_PARAM_FOR_UNREG_OPTION);
RETURN_FALSE_IF_NOT(cmd.pos_args().size() > 1,
RETURN_FALSE_IF_NOT(cmd.params().size() >= 1,
"invalid command, should be in the form of '{}'",
query_backup_policy_help);

int param_index = 1;
std::vector<std::string> policy_names;
PARSE_STRS(policy_names);
PARSE_OPT_STRS(policy_names, "", {"-p", "--policy_name"});

if (policy_names.empty()) {
SHELL_PRINTLN_ERROR(
"invalid command, policy_name should be in the form of 'val1,val2,val3' and "
"should not be empty");
return false;
}

std::set<std::string> str_set(policy_names.begin(), policy_names.end());
if (str_set.size() != policy_names.size()) {
SHELL_PRINTLN_ERROR("invalid command, policy_name has duplicate values");
return false;
}

uint32_t backup_info_cnt;
PARSE_OPT_UINT(backup_info_cnt, 3, {"-b", "--backup_info_cnt"});
Expand Down

0 comments on commit 8fd8902

Please sign in to comment.