ArgsManager: automate checking for correct command options

This commit is contained in:
Anthony Towns 2023-11-06 18:58:01 +10:00
parent 414f24c5b1
commit d10e294aaa
4 changed files with 35 additions and 16 deletions

View File

@ -357,6 +357,29 @@ std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const
return ret;
}
bool ArgsManager::CheckCommandOptions(const std::string& command, std::vector<std::string>* errors) const
{
LOCK(cs_args);
auto command_options = m_available_args.find(OptionsCategory::COMMAND_OPTIONS);
if (command_options == m_available_args.end()) return true;
const std::set<std::string> dummy;
auto command_args = m_command_args.find(command);
const std::set<std::string>& valid_opts = (command_args == m_command_args.end() ? dummy : command_args->second);
bool ok = true;
for (const auto& opts : command_options->second) {
if (!IsArgSet(opts.first)) continue;
if (valid_opts.count(opts.first)) continue;
if (errors != nullptr) {
errors->emplace_back(strprintf("The %s option cannot be used with the '%s' command.", opts.first, command));
ok = false;
}
}
return ok;
}
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
{
std::vector<std::string> result;

View File

@ -213,6 +213,11 @@ protected:
*/
std::optional<const Command> GetCommand() const;
/**
* Check for invalid command options
*/
bool CheckCommandOptions(const std::string& command, std::vector<std::string>* errors = nullptr) const;
/**
* Get blocks directory path
*

View File

@ -114,21 +114,12 @@ static void WalletShowInfo(CWallet* wallet_instance)
bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
{
if (args.IsArgSet("-format") && command != "createfromdump") {
tfm::format(std::cerr, "The -format option can only be used with the \"createfromdump\" command.\n");
return false;
}
if (args.IsArgSet("-dumpfile") && command != "dump" && command != "createfromdump") {
tfm::format(std::cerr, "The -dumpfile option can only be used with the \"dump\" and \"createfromdump\" commands.\n");
return false;
}
if (args.IsArgSet("-descriptors") && command != "create") {
tfm::format(std::cerr, "The -descriptors option can only be used with the 'create' command.\n");
return false;
}
if (args.IsArgSet("-legacy") && command != "create") {
tfm::format(std::cerr, "The -legacy option can only be used with the 'create' command.\n");
return false;
{
std::vector<std::string> details;
if (!args.CheckCommandOptions(command, &details)) {
tfm::format(std::cerr, "Error: Invalid arguments provided:\n%s\n", MakeUnorderedList(details));
return false;
}
}
if (command == "create" && !args.IsArgSet("-wallet")) {
tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");

View File

@ -344,7 +344,7 @@ class ToolWalletTest(BitcoinTestFramework):
self.assert_raises_tool_error('Dump file {} does not exist.'.format(non_exist_dump), '-wallet=todump', '-dumpfile={}'.format(non_exist_dump), 'createfromdump')
wallet_path = self.nodes[0].wallets_path / "todump2"
self.assert_raises_tool_error('Failed to create database path \'{}\'. Database already exists.'.format(wallet_path), '-wallet=todump2', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
self.assert_raises_tool_error("The -descriptors option can only be used with the 'create' command.", '-descriptors', '-wallet=todump2', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
self.assert_raises_tool_error("Error: Invalid arguments provided:\n- The -descriptors option cannot be used with the 'createfromdump' command.", '-descriptors', '-wallet=todump2', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
self.log.info('Checking createfromdump')
self.do_tool_createfromdump("load", "wallet.dump")