Skip to content

Commit

Permalink
refactor: Replace FlagsOfKnownArg with GetArgFlags
Browse files Browse the repository at this point in the history
Rename suggested by João Barbosa <joao.paulo.barbosa@gmail.com>
bitcoin/bitcoin#16545 (comment)

This also gets rid of ArgsManager::NONE constant, which was an implementation
detail not meant to be used by ArgsManager callers.

Finally this reverts a change from 7f40528
bitcoin/bitcoin#15934 adding "-" characters to argument
names. Better for GetArgFlags to require "-" prefixes for consistency with
other ArgsManager methods, and to be more efficient later when GetArg functions
need to call GetArgFlags (bitcoin/bitcoin#16545)

This commit does not change behavior.
  • Loading branch information
ryanofsky authored and backpacker69 committed Jul 1, 2020
1 parent e78d17f commit 7f404e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/util/system.cpp
Expand Up @@ -326,9 +326,9 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
key.erase(0, 1);
std::string section;
util::SettingsValue value = InterpretOption(section, key, val);
const unsigned int flags = FlagsOfKnownArg(key);
Optional<unsigned int> flags = GetArgFlags('-' + key);
if (flags) {
if (!CheckValid(key, value, flags, error)) {
if (!CheckValid(key, value, *flags, error)) {
return false;
}
// Weird behavior preserved for backwards compatibility: command
Expand All @@ -355,16 +355,16 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
return success;
}

unsigned int ArgsManager::FlagsOfKnownArg(const std::string& key) const
Optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const
{
LOCK(cs_args);
for (const auto& arg_map : m_available_args) {
const auto search = arg_map.second.find('-' + key);
const auto search = arg_map.second.find(name);
if (search != arg_map.second.end()) {
return search->second.m_flags;
}
}
return ArgsManager::NONE;
return nullopt;
}

std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
Expand Down Expand Up @@ -735,9 +735,9 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file
std::string section;
std::string key = option.first;
util::SettingsValue value = InterpretOption(section, key, option.second);
const unsigned int flags = FlagsOfKnownArg(key);
Optional<unsigned int> flags = GetArgFlags('-' + key);
if (flags) {
if (!CheckValid(key, value, flags, error)) {
if (!CheckValid(key, value, *flags, error)) {
return false;
}
m_settings.ro_config[section][key].push_back(value);
Expand Down
6 changes: 3 additions & 3 deletions src/util/system.h
Expand Up @@ -19,6 +19,7 @@
#include <compat/assumptions.h>
#include <fs.h>
#include <logging.h>
#include <optional.h>
#include <sync.h>
#include <tinyformat.h>
#include <util/memory.h>
Expand Down Expand Up @@ -133,7 +134,6 @@ class ArgsManager
{
public:
enum Flags {
NONE = 0x00,
// Boolean options can accept negation syntax -noOPTION or -noOPTION=1
ALLOW_BOOL = 0x01,
ALLOW_INT = 0x02,
Expand Down Expand Up @@ -297,9 +297,9 @@ class ArgsManager

/**
* Return Flags for known arg.
* Return ArgsManager::NONE for unknown arg.
* Return nullopt for unknown arg.
*/
unsigned int FlagsOfKnownArg(const std::string& key) const;
Optional<unsigned int> GetArgFlags(const std::string& name) const;
};

extern ArgsManager gArgs;
Expand Down

0 comments on commit 7f404e8

Please sign in to comment.