61 return (LocaleIndependentAtoi<int>(strValue) != 0);
66 return arg.size() > 0 && arg[0] ==
'-' ? arg.substr(1) : arg;
81 size_t option_index = key.find(
'.');
82 if (option_index != std::string::npos) {
83 result.
section = key.substr(0, option_index);
84 key.erase(0, option_index + 1);
86 if (key.starts_with(
"no")) {
106 unsigned int flags, std::string& error)
111 error =
strprintf(
"Negating of -%s is meaningless and therefore forbidden", key.
name);
116 LogWarning(
"Parsed potentially confusing double-negative -%s=%s", key.
name, *value);
122 error =
strprintf(
"Can not set -%s with no value. Please specify value with -%s=value.", key.
name, key.
name);
125 return value ? *value :
"";
136 std::set<std::string> unsuitables;
141 if (m_network.empty())
return std::set<std::string> {};
146 for (
const auto& arg : m_network_only_args) {
148 unsuitables.insert(arg);
157 static const std::set<std::string> available_sections{
166 std::list<SectionInfo> unrecognized = m_config_sections;
167 unrecognized.remove_if([](
const SectionInfo& appeared){
return available_sections.contains(appeared.
m_name); });
180 m_settings.command_line_options.clear();
182 for (
int i = 1; i < argc; i++) {
183 std::string key(argv[i]);
190 if (key.starts_with(
"-psn_"))
continue;
193 if (key ==
"-")
break;
194 std::optional<std::string> val;
195 size_t is_index = key.find(
'=');
196 if (is_index != std::string::npos) {
197 val = key.substr(is_index + 1);
207 if (!m_accept_any_command &&
m_command.empty()) {
211 error =
strprintf(
"Invalid command '%s'", argv[i]);
224 if (key.length() > 1 && key[1] ==
'-')
236 error =
strprintf(
"Invalid parameter %s", argv[i]);
240 std::optional<common::SettingsValue> value =
InterpretValue(keyinfo, val ? &*val :
nullptr, *
flags, error);
241 if (!value)
return false;
243 m_settings.command_line_options[keyinfo.
name].push_back(*value);
247 if (
auto* includes =
common::FindKey(m_settings.command_line_options,
"includeconf")) {
251 error =
"-includeconf cannot be used from commandline; -includeconf=" +
values.begin()->write();
261 for (
const auto&
arg_map : m_available_args) {
263 if (search !=
arg_map.second.end()) {
264 return search->second.m_flags;
267 return m_default_flags;
279 m_default_flags =
flags;
286 if (value.isFalse())
return {};
288 if (path_str.empty())
return default_value;
291 return result.has_filename() ? result : result.parent_path();
303 fs::path& path = m_cached_blocks_path;
307 if (!path.empty())
return path;
311 if (!fs::is_directory(path)) {
321 fs::create_directories(path);
338 fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
341 if (!path.empty())
return path;
344 if (!datadir.empty()) {
346 if (!fs::is_directory(path)) {
354 if (net_specific && !
BaseParams().DataDir().empty()) {
365 m_cached_datadir_path = fs::path();
366 m_cached_network_datadir_path = fs::path();
367 m_cached_blocks_path = fs::path();
379 if (!m_accept_any_command) {
381 ret.command = *(it++);
385 ret.args.push_back(*(it++));
395 if (command_options == m_available_args.end()) {
400 const auto command_args = m_command_args.find(
command);
402 if (command_args == m_command_args.end()) {
408 return command_args->second.contains(opt);
413 for (
const auto& [arg,
_] : command_options->second) {
416 if (errors !=
nullptr) {
417 errors->emplace_back(
strprintf(
"The %s option cannot be used with the '%s' command.", arg,
command));
426 std::vector<std::string> result;
428 result.push_back(value.isFalse() ?
"0" : value.isTrue() ?
"1" : value.get_str());
441 if (settings.empty()) {
453static void SaveErrors(
const std::vector<std::string> errors, std::vector<std::string>* error_out)
455 for (
const auto& error : errors) {
457 error_out->emplace_back(error);
472 m_settings.rw_settings.clear();
473 std::vector<std::string> read_errors;
478 for (
const auto& setting : m_settings.rw_settings) {
481 LogWarning(
"Ignoring unknown rw_settings value %s", setting.first);
489 fs::path path, path_tmp;
491 throw std::logic_error(
"Attempt to write settings file when dynamic settings are disabled.");
495 std::vector<std::string> write_errors;
521 return GetArg(strArg).value_or(strDefault);
532 if (value.
isNull())
return std::nullopt;
533 if (value.
isFalse())
return "0";
534 if (value.
isTrue())
return "1";
544template <std::
integral Int>
547 return GetArg<Int>(strArg).value_or(nDefault);
550template <std::
integral Int>
554 return SettingTo<Int>(value);
557template <std::
integral Int>
560 if (value.
isNull())
return std::nullopt;
562 if (value.
isTrue())
return 1;
564 return LocaleIndependentAtoi<Int>(value.
get_str());
567template <std::
integral Int>
570 return SettingTo<Int>(value).value_or(nDefault);
586 if (value.
isNull())
return std::nullopt;
596#define INSTANTIATE_INT_TYPE(Type) \
597 template Type ArgsManager::GetArg<Type>(const std::string&, Type) const; \
598 template std::optional<Type> ArgsManager::GetArg<Type>(const std::string&) const; \
599 template Type SettingTo<Type>(const common::SettingsValue&, Type); \
600 template std::optional<Type> SettingTo<Type>(const common::SettingsValue&)
611#undef INSTANTIATE_INT_TYPE
617 m_settings.forced_settings[
SettingName(strArg)] = strValue;
632 m_settings.forced_settings[
SettingName(strArg)] = strValue;
637 Assert(
cmd.find(
'=') == std::string::npos);
641 m_accept_any_command =
false;
644 if (!options.empty()) {
646 bool command_has_all_options_defined =
true;
647 for (
const auto& opt : options) {
648 if (!cmdopts.contains(opt)) {
649 command_has_all_options_defined =
false;
652 Assert(command_has_all_options_defined);
654 m_command_args.try_emplace(
cmd, std::move(options));
664 size_t eq_index =
name.find(
'=');
665 if (eq_index == std::string::npos) {
666 eq_index =
name.size();
668 std::string arg_name =
name.substr(0, eq_index);
671 std::map<std::string, Arg>&
arg_map = m_available_args[cat];
676 m_network_only_args.emplace(arg_name);
682 for (
const std::string&
name : names) {
691 m_available_args.clear();
692 m_command_args.clear();
693 m_network_only_args.clear();
694 m_config_sections.clear();
700 std::vector<std::string> found{};
702 if (cmds != m_available_args.end()) {
703 for (
const auto& [
cmd, argspec] : cmds->second) {
705 found.push_back(
cmd);
708 if (found.size() > 1) {
709 throw std::runtime_error(
strprintf(
"Only one of %s may be specified.",
util::Join(found,
", ")));
716 const bool show_debug =
GetBoolArg(
"-help-debug",
false);
723 if (select.empty())
return;
724 if (command_options == m_available_args.end())
return;
725 for (
const auto& [
name, info] : command_options->second) {
727 if (!select.contains(
name))
continue;
732 for (
const auto& [category, category_args] : m_available_args) {
762 if (show_debug) usage +=
HelpMessageGroup(
"Wallet debugging/testing options:");
789 for (
const auto& [arg_name, arg_info] : category_args) {
791 usage +=
HelpMessageOpt(arg_name, arg_info.m_help_param, arg_info.m_help_text);
794 const auto cmd_args = m_command_args.find(arg_name);
795 if (cmd_args == m_command_args.end())
continue;
796 for_matching_cmd_opts(cmd_args->second, [&](
const auto& cmdopt_name,
const auto& cmdopt_info) {
797 usage += HelpMessageOpt(cmdopt_name, cmdopt_info.m_help_param, cmdopt_info.m_help_text, true);
818 return std::string(message) + std::string(
"\n\n");
821std::string
HelpMessageOpt(std::string_view option, std::string_view help_param, std::string_view message,
bool subopt)
823 constexpr int screen_width = 79;
828 int bump = msg_indent - opt_indent;
832 int msg_width = screen_width - msg_indent;
835 opt_indent,
"", option, help_param,
840 "addrman (use deterministic addrman)",
841 "reindex_after_failure_noninteractive_yes (When asked for a reindex after failure interactively, simulate as-if answered with 'yes')",
842 "bip94 (enforce BIP94 consensus rules)",
848 return std::any_of(options.begin(), options.end(), [test_option](
const auto& option) {
849 return option == test_option;
863 fs::path legacy_path = GetSpecialFolderPath(CSIDL_APPDATA) /
"Bitcoin";
864 if (
fs::exists(legacy_path))
return legacy_path;
867 return GetSpecialFolderPath(CSIDL_LOCAL_APPDATA) /
"Bitcoin";
870 char* pszHome = getenv(
"HOME");
871 if (pszHome ==
nullptr || strlen(pszHome) == 0)
872 pathRet = fs::path(
"/");
874 pathRet = fs::path(pszHome);
877 return pathRet /
"Library/Application Support/Bitcoin";
880 return pathRet /
".bitcoin";
888 return datadir.empty() || fs::is_directory(
fs::absolute(datadir));
894 return *
Assert(m_config_path);
901 m_config_path = path;
906 std::variant<ChainType, std::string> arg =
GetChainArg();
907 if (
auto* parsed = std::get_if<ChainType>(&arg))
return *parsed;
908 throw std::runtime_error(
strprintf(
"Unknown chain %s.", std::get<std::string>(arg)));
914 if (
auto* parsed = std::get_if<ChainType>(&arg))
return ChainTypeToString(*parsed);
915 return std::get<std::string>(arg);
920 auto get_net = [&](
const std::string& arg) {
929 const bool fRegTest = get_net(
"-regtest");
930 const bool fSigNet = get_net(
"-signet");
931 const bool fTestNet = get_net(
"-testnet");
932 const bool fTestNet4 = get_net(
"-testnet4");
933 const auto chain_arg =
GetArg(
"-chain");
935 if ((
int)chain_arg.has_value() + (
int)fRegTest + (
int)fSigNet + (
int)fTestNet + (
int)fTestNet4 > 1) {
936 throw std::runtime_error(
"Invalid combination of -regtest, -signet, -testnet, -testnet4 and -chain. Can use at most one.");
977 const std::string&
prefix,
978 const std::string& section,
979 const std::map<std::string, std::vector<common::SettingsValue>>&
args)
const
982 std::string section_str = section.empty() ?
"" :
"[" + section +
"] ";
983 for (
const auto& arg :
args) {
984 for (
const auto& value : arg.second) {
987 std::string value_str = (*
flags &
SENSITIVE) ?
"****" : value.write();
988 LogInfo(
"%s %s%s=%s\n",
prefix, section_str, arg.first, value_str);
997 for (
const auto& section : m_settings.ro_config) {
998 logArgsPrefix(
"Config file arg:", section.first, section.second);
1000 for (
const auto& setting : m_settings.rw_settings) {
1001 LogInfo(
"Setting file arg: %s = %s\n", setting.first, setting.second.write());
1003 logArgsPrefix(
"Command-line arg:",
"", m_settings.command_line_options);
const std::vector< std::string > TEST_OPTIONS_DOC
#define INSTANTIATE_INT_TYPE(Type)
bool HelpRequested(const ArgsManager &args)
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
fs::path GetDefaultDataDir()
static void SaveErrors(const std::vector< std::string > errors, std::vector< std::string > *error_out)
std::optional< common::SettingsValue > InterpretValue(const KeyInfo &key, const std::string *value, unsigned int flags, std::string &error)
Interpret settings value based on registered flags.
const char *const BITCOIN_SETTINGS_FILENAME
bool CheckDataDirOption(const ArgsManager &args)
std::optional< bool > SettingToBool(const common::SettingsValue &value)
std::optional< std::string > SettingToString(const common::SettingsValue &value)
std::optional< Int > SettingTo(const common::SettingsValue &value)
bool HasTestOption(const ArgsManager &args, const std::string &test_option)
Checks if a particular test option is present in -test command-line arg options.
static std::string SettingName(const std::string &arg)
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
KeyInfo InterpretKey(std::string key)
Parse "name", "section.name", "noname", "section.noname" settings keys.
const char *const BITCOIN_CONF_FILENAME
std::string HelpMessageOpt(std::string_view option, std::string_view help_param, std::string_view message, bool subopt)
Format a string to be used as option description in help messages.
static bool InterpretBool(const std::string &strValue)
Interpret a string argument as a boolean.
const CBaseChainParams & BaseParams()
Return the currently selected parameters.
std::optional< ChainType > ChainTypeFromString(std::string_view chain)
std::string ChainTypeToString(ChainType chain)
#define Assert(val)
Identity function.
bool ParseParameters(int argc, const char *const argv[], std::string &error) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
std::vector< common::SettingsValue > GetSettingsList(const std::string &arg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get list of setting values.
std::vector< std::string > GetArgs(const std::string &strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return a vector of strings of the given argument.
ChainType GetChainType() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Returns the appropriate chain type from the program arguments.
void CheckMultipleCLIArgs() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Check CLI command args.
@ ALLOW_ANY
disable validation
@ DISALLOW_NEGATION
disallow -nofoo syntax
@ DISALLOW_ELISION
disallow -foo syntax that doesn't assign any value
std::list< SectionInfo > GetUnrecognizedSections() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Log warnings for unrecognized section names in the config file.
common::SettingsValue GetSetting(const std::string &arg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get setting value.
bool GetSettingsPath(fs::path *filepath=nullptr, bool temp=false, bool backup=false) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get settings file path, or return false if read-write settings were disabled with -nosettings.
bool CheckCommandOptions(const std::string &command, std::vector< std::string > *errors=nullptr) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Check that any command-specific options the user specified are valid for the given command.
fs::path GetBlocksDirPath() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get blocks directory path.
fs::path GetDataDirBase() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path.
fs::path GetConfigFilePath() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return config file path (read-only)
void SetDefaultFlags(std::optional< unsigned int >) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Set default flags to return for an unknown arg.
void AddCommand(const std::string &cmd, const std::string &help, std::set< std::string > options={}) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Add command.
bool SoftSetArg(const std::string &strArg, const std::string &strValue) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Set an argument if it doesn't already have a value.
std::set< std::string > GetUnsuitableSectionOnlyArgs() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Log warnings for options in m_section_only_args when they are specified in the default section but no...
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Add argument.
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Write settings file or backup settings file.
fs::path GetPathArg_(std::string arg, const fs::path &default_value={}) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
void ClearPathCache() EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Clear cached directory paths.
fs::path GetDataDir(bool net_specific) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
Get data directory path.
void SetConfigFilePath(fs::path) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
common::SettingsValue GetPersistentSetting(const std::string &name) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get current setting from config file or read/write settings file, ignoring nonpersistent command line...
void ForceSetArg(const std::string &strArg, const std::string &strValue) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return path argument or default value.
void LogArgs() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Log the config file options and the command line arguments, useful for troubleshooting.
std::string GetArg(const std::string &strArg, const std::string &strDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return string argument or default value.
void AddHiddenArgs(const std::vector< std::string > &args) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Add many hidden arguments.
std::variant< ChainType, std::string > GetChainArg() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return -regtest/-signet/-testnet/-testnet4/-chain= setting as a ChainType enum if a recognized chain ...
void logArgsPrefix(const std::string &prefix, const std::string §ion, const std::map< std::string, std::vector< common::SettingsValue > > &args) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
std::string GetChainTypeString() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Returns the appropriate chain type string from the program arguments.
bool ReadSettingsFile(std::vector< std::string > *errors=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Read settings file.
void ClearArgs() EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Clear available arguments.
bool IsArgSet(const std::string &strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return true if the given argument has been manually set.
std::optional< const Command > GetCommand() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get the command and command args (returns std::nullopt if no command provided)
bool SoftSetBoolArg(const std::string &strArg, bool fValue) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Set a boolean argument if it doesn't already have a value.
fs::path GetDataDirNet() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path with appended network identifier.
bool IsArgNegated(const std::string &strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return true if the argument was originally passed as a negated option, i.e.
common::SettingsValue GetSetting_(const std::string &arg) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
std::optional< unsigned int > GetArgFlags(const std::string &name) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return Flags for known arg.
std::optional< unsigned int > GetArgFlags_(const std::string &name) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
bool UseDefaultSection(const std::string &arg) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
Returns true if settings values from the default section should be used, depending on the current net...
bool GetBoolArg(const std::string &strArg, bool fDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return boolean argument or default value.
void SelectConfigNetwork(const std::string &network) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Select the network in use.
std::string GetHelpMessage() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get the help string.
const std::string & get_str() const
const std::string & getValStr() const
static path absolute(const path &p)
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to a byte string.
static path PathFromString(const std::string &string)
Convert byte string to path object.
bool RenameOver(fs::path src, fs::path dest)
Rename src to dest.
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
SettingsValue GetSetting(const Settings &settings, const std::string §ion, const std::string &name, bool ignore_default_section_config, bool ignore_nonpersistent, bool get_chain_type)
Get settings value from combined sources: forced settings, command line arguments,...
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
std::vector< SettingsValue > GetSettingsList(const Settings &settings, const std::string §ion, const std::string &name, bool ignore_default_section_config)
Get combined setting value similar to GetSetting(), except if setting was specified multiple times,...
bool OnlyHasDefaultSectionSetting(const Settings &settings, const std::string §ion, const std::string &name)
Return true if a setting is set in the default config file section, and not overridden by a higher pr...
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Accessor for list of settings that skips negated values when iterated over.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
consteval auto _(util::TranslatedLiteral str)
static struct ArgMap arg_map[]
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.