60 if (strValue.empty()) {
63 return (LocaleIndependentAtoi<int>(strValue) != 0);
67 return arg.size() > 0 && arg[0] ==
'-' ? arg.substr(1) : arg;
82 size_t option_index = key.find(
'.');
83 if (option_index != std::string::npos) {
84 result.
section = key.substr(0, option_index);
85 key.erase(0, option_index + 1);
87 if (key.substr(0, 2) ==
"no") {
106std::optional<util::SettingsValue>
108 unsigned int flags, std::string &error) {
113 "Negating of -%s is meaningless and therefore forbidden",
119 LogPrintf(
"Warning: parsed potentially confusing double-negative "
127 error =
strprintf(
"Can not set -%s with no value. Please specify value "
132 return value.value_or(
"");
142 std::set<std::string> unsuitables;
147 if (m_network.empty()) {
148 return std::set<std::string>{};
153 return std::set<std::string>{};
156 for (
const auto &arg : m_network_only_args) {
159 unsuitables.insert(arg);
167 static const std::set<std::string> available_sections{
174 std::list<SectionInfo> unrecognized = m_config_sections;
175 unrecognized.remove_if([](
const SectionInfo &appeared) {
176 return available_sections.find(appeared.
m_name) !=
177 available_sections.end();
188 size_t is_index = key.find(
'=');
189 if (is_index != std::string::npos) {
190 val = key.substr(is_index + 1);
205 if (key.length() > 1 && key[1] ==
'-') {
212 std::string &error) {
214 m_settings.command_line_options.clear();
216 for (
int i = 1; i < argc; i++) {
217 std::string key(argv[i]);
224 if (key.substr(0, 5) ==
"-psn_") {
233 std::optional<std::string> val;
247 error =
strprintf(
"Invalid parameter %s", argv[i]);
251 std::optional<util::SettingsValue> value =
257 m_settings.command_line_options[keyinfo.
name].push_back(*value);
263 util::FindKey(m_settings.command_line_options,
"includeconf")) {
266 "-includeconf cannot be used from commandline; -includeconf=" +
267 include.get_str() +
"\n";
274std::optional<unsigned int>
277 for (
const auto &arg_map : m_available_args) {
278 const auto search = arg_map.second.find(
name);
279 if (search != arg_map.second.end()) {
280 return search->second.m_flags;
287 const fs::path &default_value)
const {
291 std::string path_str =
GetArg(arg,
"");
292 if (path_str.empty()) {
293 return default_value;
297 return result.has_filename() ? result : result.parent_path();
302 fs::path &path = m_cached_blocks_path;
312 if (!fs::is_directory(path)) {
329 net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
337 if (!datadir.empty()) {
339 if (!fs::is_directory(path)) {
347 if (net_specific && !
BaseParams().DataDir().empty()) {
358 m_cached_network_datadir_path =
fs::path();
363 std::vector<std::string> result;
365 result.push_back(value.isFalse() ?
"0"
366 : value.isTrue() ?
"1"
379 if (settings.empty()) {
387 temp ? settings +
".tmp" : settings);
393 std::vector<std::string> *error_out) {
394 for (
const auto &error : errors) {
396 error_out->emplace_back(error);
410 m_settings.rw_settings.clear();
411 std::vector<std::string> read_errors;
416 for (
const auto &setting : m_settings.rw_settings) {
420 LogPrintf(
"Ignoring unknown rw_settings value %s\n", setting.first);
431 throw std::logic_error(
"Attempt to write settings file when dynamic "
432 "settings are disabled.");
436 std::vector<std::string> write_errors;
443 {
strprintf(
"Failed renaming settings file %s to %s\n",
464 const std::string &strDefault)
const {
465 return GetArg(strArg).value_or(strDefault);
468std::optional<std::string>
491 const std::string &strDefault) {
496 int64_t nDefault)
const {
497 return GetIntArg(strArg).value_or(nDefault);
516 return value.
getInt<int64_t>();
518 return LocaleIndependentAtoi<int64_t>(value.
get_str());
549 const std::string &strValue) {
567 const std::string &strValue) {
569 m_settings.forced_settings[
SettingName(strArg)] = strValue;
578 const std::vector<std::string> &values) {
582 for (
const std::string &s : values) {
586 m_settings.forced_settings[
SettingName(strArg)] = value;
592 size_t eq_index =
name.find(
'=');
593 if (eq_index == std::string::npos) {
594 eq_index =
name.size();
596 std::string arg_name =
name.substr(0, eq_index);
599 std::map<std::string, Arg> &arg_map = m_available_args[cat];
600 auto ret = arg_map.emplace(
607 m_network_only_args.emplace(arg_name);
612 for (
const std::string &
name : names) {
619 m_settings.forced_settings.erase(
SettingName(strArg));
623 const bool show_debug =
GetBoolArg(
"-help-debug",
false);
625 std::string usage =
"";
627 for (
const auto &arg_map : m_available_args) {
628 switch (arg_map.first) {
686 for (
const auto &arg : arg_map.second) {
689 if (arg.second.m_help_param.empty()) {
692 name = arg.first + arg.second.m_help_param;
707 args.
AddArg(
"-?",
"Print this help message and exit",
false,
717 return std::string(message) + std::string(
"\n\n");
721 const std::string &message) {
722 return std::string(
optIndent,
' ') + std::string(option) +
723 std::string(
"\n") + std::string(
msgIndent,
' ') +
734 return GetSpecialFolderPath(CSIDL_APPDATA) /
"Bitcoin";
737 char *pszHome = getenv(
"HOME");
738 if (pszHome ==
nullptr || strlen(pszHome) == 0) {
745 return pathRet /
"Library/Application Support/Bitcoin";
748 return pathRet /
".bitcoin";
755 return datadir.empty() || fs::is_directory(
fs::absolute(datadir));
763 std::variant<ChainType, std::string> arg =
GetChainArg();
764 if (
auto *parsed = std::get_if<ChainType>(&arg)) {
767 throw std::runtime_error(
768 strprintf(
"Unknown chain %s.", std::get<std::string>(arg)));
773 if (
auto *parsed = std::get_if<ChainType>(&arg)) {
776 return std::get<std::string>(arg);
780 auto get_net = [&](
const std::string &arg) {
787 return value.
isNull() ? false
792 const bool fRegTest = get_net(
"-regtest");
793 const bool fTestNet = get_net(
"-testnet");
794 const auto chain_arg =
GetArg(
"-chain");
796 if (
int(chain_arg.has_value()) +
int(fRegTest) +
int(fTestNet) > 1) {
797 throw std::runtime_error(
"Invalid combination of -regtest, -testnet "
798 "and -chain. Can use at most one.");
818 m_network_only_args.count(arg) == 0;
829std::vector<util::SettingsValue>
837 const std::string &
prefix,
const std::string §ion,
838 const std::map<std::string, std::vector<util::SettingsValue>> &args)
const {
839 std::string section_str = section.empty() ?
"" :
"[" + section +
"] ";
840 for (
const auto &arg : args) {
841 for (
const auto &value : arg.second) {
844 std::string value_str =
855 for (
const auto §ion : m_settings.ro_config) {
856 logArgsPrefix(
"Config file arg:", section.first, section.second);
858 for (
const auto &setting : m_settings.rw_settings) {
859 LogPrintf(
"Setting file arg: %s = %s\n", setting.first,
860 setting.second.write());
862 logArgsPrefix(
"Command-line arg:",
"", m_settings.command_line_options);
867WinCmdLineArgs::WinCmdLineArgs() {
868 wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
869 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t> utf8_cvt;
870 argv =
new char *[argc];
872 for (
int i = 0; i < argc; i++) {
873 args[i] = utf8_cvt.to_bytes(wargv[i]);
874 argv[i] = &*args[i].begin();
879WinCmdLineArgs::~WinCmdLineArgs() {
883std::pair<int, char **> WinCmdLineArgs::get() {
884 return std::make_pair(argc, argv);
bool HelpRequested(const ArgsManager &args)
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
fs::path GetDefaultDataDir()
static const int msgIndent
static void SaveErrors(const std::vector< std::string > errors, std::vector< std::string > *error_out)
std::optional< std::string > SettingToString(const util::SettingsValue &value)
const char *const BITCOIN_SETTINGS_FILENAME
std::optional< bool > SettingToBool(const util::SettingsValue &value)
bool CheckDataDirOption(const ArgsManager &args)
static const int screenWidth
std::optional< util::SettingsValue > InterpretValue(const KeyInfo &key, const std::optional< std::string > &value, unsigned int flags, std::string &error)
Interpret settings value based on registered flags.
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.
bool ParseKeyValue(std::string &key, std::optional< std::string > &val)
const char *const BITCOIN_CONF_FILENAME
static bool InterpretBool(const std::string &strValue)
Interpret a string argument as a boolean.
static const int optIndent
std::string HelpMessageOpt(const std::string &option, const std::string &message)
Format a string to be used as option description in help messages.
std::optional< int64_t > SettingToInt(const util::SettingsValue &value)
fs::path GetConfigFile(const ArgsManager &args, const fs::path &configuration_file_path)
const CBaseChainParams & BaseParams()
Return the currently selected parameters.
std::optional< ChainType > ChainTypeFromString(std::string_view chain)
std::string ChainTypeToString(ChainType chain)
std::set< std::string > GetUnsuitableSectionOnlyArgs() const
Log warnings for options in m_section_only_args when they are specified in the default section but no...
bool IsArgNegated(const std::string &strArg) const
Return true if the argument was originally passed as a negated option, i.e.
std::list< SectionInfo > GetUnrecognizedSections() const
Log warnings for unrecognized section names in the config file.
@ ALLOW_ANY
disable validation
@ DISALLOW_NEGATION
unimplemented, draft implementation in #16545
@ DISALLOW_ELISION
disallow -foo syntax that doesn't assign any value
bool ReadSettingsFile(std::vector< std::string > *errors=nullptr)
Read settings file.
ChainType GetChainType() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
void ForceSetArg(const std::string &strArg, const std::string &strValue)
std::string GetChainTypeString() const
Returns the appropriate chain name string from the program arguments.
bool ParseParameters(int argc, const char *const argv[], std::string &error)
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
util::SettingsValue GetPersistentSetting(const std::string &name) const
Get current setting from config file or read/write settings file, ignoring nonpersistent command line...
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
std::optional< unsigned int > GetArgFlags(const std::string &name) const
Return Flags for known arg.
bool GetSettingsPath(fs::path *filepath=nullptr, bool temp=false, bool backup=false) const
Get settings file path, or return false if read-write settings were disabled with -nosettings.
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
void SelectConfigNetwork(const std::string &network)
Select the network in use.
std::string GetHelpMessage() const
Get the help string.
void ForceSetMultiArg(const std::string &strArg, const std::vector< std::string > &values)
This function is only used for testing purpose so so we should not worry about element uniqueness and...
void ClearPathCache()
Clear cached directory paths.
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const
Write settings file or backup settings file.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
fs::path GetDataDirBase() const
Get data directory path.
fs::path GetBlocksDirPath() const
Get blocks directory path.
void logArgsPrefix(const std::string &prefix, const std::string §ion, const std::map< std::string, std::vector< util::SettingsValue > > &args) const
fs::path GetConfigFilePath() const
Return config file path (read-only)
void ClearForcedArg(const std::string &strArg)
Remove a forced arg setting, used only in testing.
std::vector< util::SettingsValue > GetSettingsList(const std::string &arg) const
Get list of setting values.
std::variant< ChainType, std::string > GetChainArg() const
Return -regtest/–testnet/-chain= setting as a ChainType enum if a recognized chain name was set,...
void LogArgs() const
Log the config file options and the command line arguments, useful for troubleshooting.
fs::path GetDataDir(bool net_specific) const
Get data directory path.
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...
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
util::SettingsValue GetSetting(const std::string &arg) const
Get setting value.
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
void AddHiddenArgs(const std::vector< std::string > &args)
Add many hidden arguments.
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const
Return path argument or default value.
void push_back(UniValue val)
const std::string & get_str() const
const std::string & getValStr() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool RenameOver(fs::path src, fs::path dest)
static path absolute(const path &p)
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
static std::string PathToString(const path &path)
Convert path object to byte string.
static path PathFromString(const std::string &string)
Convert byte string to path object.
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
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 ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
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...
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write 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.
Accessor for list of settings that skips negated values when iterated over.
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.