63 return (LocaleIndependentAtoi<int>(strValue) != 0);
68 return arg.size() > 0 && arg[0] ==
'-' ? arg.substr(1) : arg;
83 size_t option_index = key.find(
'.');
84 if (option_index != std::string::npos) {
85 result.
section = key.substr(0, option_index);
86 key.erase(0, option_index + 1);
88 if (key.substr(0, 2) ==
"no") {
113 error =
strprintf(
"Negating of -%s is meaningless and therefore forbidden", key.
name);
118 LogPrintf(
"Warning: parsed potentially confusing double-negative -%s=%s\n", key.
name, *value);
124 error =
strprintf(
"Can not set -%s with no value. Please specify value with -%s=value.", key.
name, key.
name);
127 return value ? *value :
"";
138 std::set<std::string> unsuitables;
143 if (m_network.empty())
return std::set<std::string> {};
148 for (
const auto& arg : m_network_only_args) {
150 unsuitables.insert(arg);
159 static const std::set<std::string> available_sections{
167 std::list<SectionInfo> unrecognized = m_config_sections;
168 unrecognized.remove_if([](
const SectionInfo& appeared){
return available_sections.find(appeared.
m_name) != available_sections.end(); });
181 m_settings.command_line_options.clear();
183 for (
int i = 1; i < argc; i++) {
184 std::string key(argv[i]);
191 if (key.substr(0, 5) ==
"-psn_")
continue;
194 if (key ==
"-")
break;
195 std::optional<std::string> val;
196 size_t is_index = key.find(
'=');
197 if (is_index != std::string::npos) {
198 val = key.substr(is_index + 1);
208 if (!m_accept_any_command &&
m_command.empty()) {
225 if (key.length() > 1 && key[1] ==
'-')
242 if (!value)
return false;
244 m_settings.command_line_options[keyinfo.
name].push_back(*value);
248 if (
auto* includes =
util::FindKey(m_settings.command_line_options,
"includeconf")) {
252 error =
"-includeconf cannot be used from commandline; -includeconf=" +
values.begin()->write();
262 for (
const auto& arg_map : m_available_args) {
263 const auto search = arg_map.second.find(
name);
264 if (search != arg_map.second.end()) {
265 return search->second.m_flags;
274 std::string path_str =
GetArg(arg,
"");
275 if (path_str.empty())
return default_value;
278 return result.has_filename() ? result : result.parent_path();
284 fs::path& path = m_cached_blocks_path;
288 if (!path.empty())
return path;
292 if (!fs::is_directory(path)) {
309 fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
312 if (!path.empty())
return path;
315 if (!datadir.empty()) {
317 if (!fs::is_directory(path)) {
325 if (net_specific && !
BaseParams().DataDir().empty()) {
337 m_cached_network_datadir_path =
fs::path();
350 if (!m_accept_any_command) {
352 ret.command = *(it++);
356 ret.args.push_back(*(it++));
363 std::vector<std::string> result;
365 result.push_back(value.isFalse() ?
"0" : value.isTrue() ?
"1" : value.get_str());
378 if (settings.empty()) {
390 static void SaveErrors(
const std::vector<std::string> errors, std::vector<std::string>* error_out)
392 for (
const auto&
error : errors) {
394 error_out->emplace_back(
error);
409 m_settings.rw_settings.clear();
410 std::vector<std::string> read_errors;
415 for (
const auto& setting : m_settings.rw_settings) {
418 LogPrintf(
"Ignoring unknown rw_settings value %s\n", setting.first);
428 throw std::logic_error(
"Attempt to write settings file when dynamic settings are disabled.");
432 std::vector<std::string> write_errors;
458 return GetArg(strArg).value_or(strDefault);
469 if (value.
isNull())
return std::nullopt;
470 if (value.
isFalse())
return "0";
471 if (value.
isTrue())
return "1";
483 return GetIntArg(strArg).value_or(nDefault);
494 if (value.
isNull())
return std::nullopt;
496 if (value.
isTrue())
return 1;
498 return LocaleIndependentAtoi<int64_t>(value.
get_str());
519 if (value.
isNull())
return std::nullopt;
548 m_settings.forced_settings[
SettingName(strArg)] = strValue;
553 Assert(
cmd.find(
'=') == std::string::npos);
557 m_accept_any_command =
false;
568 size_t eq_index =
name.find(
'=');
569 if (eq_index == std::string::npos) {
570 eq_index =
name.size();
572 std::string arg_name =
name.substr(0, eq_index);
575 std::map<std::string, Arg>& arg_map = m_available_args[cat];
580 m_network_only_args.emplace(arg_name);
586 for (
const std::string&
name : names) {
593 const bool show_debug =
GetBoolArg(
"-help-debug",
false);
597 for (
const auto& arg_map : m_available_args) {
598 switch(arg_map.first) {
624 if (show_debug) usage +=
HelpMessageGroup(
"Wallet debugging/testing options:");
645 for (
const auto& arg : arg_map.second) {
648 if (arg.second.m_help_param.empty()) {
651 name = arg.first + arg.second.m_help_param;
676 return std::string(message) + std::string(
"\n\n");
679 std::string
HelpMessageOpt(
const std::string &option,
const std::string &message) {
680 return std::string(
optIndent,
' ') + std::string(option) +
681 std::string(
"\n") + std::string(
msgIndent,
' ') +
693 return GetSpecialFolderPath(CSIDL_APPDATA) /
"Bitcoin";
696 char* pszHome = getenv(
"HOME");
697 if (pszHome ==
nullptr || strlen(pszHome) == 0)
703 return pathRet /
"Library/Application Support/Bitcoin";
706 return pathRet /
".bitcoin";
714 return datadir.empty() || fs::is_directory(
fs::absolute(datadir));
720 return *
Assert(m_config_path);
725 std::variant<ChainType, std::string> arg =
GetChainArg();
726 if (
auto* parsed = std::get_if<ChainType>(&arg))
return *parsed;
727 throw std::runtime_error(
strprintf(
"Unknown chain %s.", std::get<std::string>(arg)));
733 if (
auto* parsed = std::get_if<ChainType>(&arg))
return ChainTypeToString(*parsed);
734 return std::get<std::string>(arg);
739 auto get_net = [&](
const std::string& arg) {
748 const bool fRegTest = get_net(
"-regtest");
749 const bool fSigNet = get_net(
"-signet");
750 const bool fTestNet = get_net(
"-testnet");
751 const auto chain_arg =
GetArg(
"-chain");
753 if ((
int)chain_arg.has_value() + (
int)fRegTest + (
int)fSigNet + (
int)fTestNet > 1) {
754 throw std::runtime_error(
"Invalid combination of -regtest, -signet, -testnet and -chain. Can use at most one.");
787 const std::string&
prefix,
788 const std::string& section,
789 const std::map<std::string, std::vector<util::SettingsValue>>&
args)
const
791 std::string section_str = section.empty() ?
"" :
"[" + section +
"] ";
792 for (
const auto& arg :
args) {
793 for (
const auto& value : arg.second) {
796 std::string value_str = (*
flags &
SENSITIVE) ?
"****" : value.write();
806 for (
const auto& section : m_settings.ro_config) {
807 logArgsPrefix(
"Config file arg:", section.first, section.second);
809 for (
const auto& setting : m_settings.rw_settings) {
810 LogPrintf(
"Setting file arg: %s = %s\n", setting.first, setting.second.write());
812 logArgsPrefix(
"Command-line arg:",
"", m_settings.command_line_options);
817 WinCmdLineArgs::WinCmdLineArgs()
819 wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
820 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t> utf8_cvt;
821 argv =
new char*[argc];
823 for (
int i = 0; i < argc; i++) {
824 args[i] = utf8_cvt.to_bytes(wargv[i]);
825 argv[i] = &*
args[i].begin();
830 WinCmdLineArgs::~WinCmdLineArgs()
835 std::pair<int, char**> WinCmdLineArgs::get()
837 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)
const char *const BITCOIN_SETTINGS_FILENAME
std::optional< std::string > SettingToString(const util::SettingsValue &value)
bool CheckDataDirOption(const ArgsManager &args)
static const int screenWidth
std::optional< util::SettingsValue > InterpretValue(const KeyInfo &key, const std::string *value, unsigned int flags, std::string &error)
Interpret settings value based on registered flags.
std::optional< int64_t > SettingToInt(const util::SettingsValue &value)
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
static bool InterpretBool(const std::string &strValue)
Interpret a string argument as a boolean.
static const int optIndent
std::optional< bool > SettingToBool(const util::SettingsValue &value)
std::string HelpMessageOpt(const std::string &option, const std::string &message)
Format a string to be used as option description in help messages.
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.
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...
std::optional< const Command > GetCommand() const
Get the command and command args (returns std::nullopt if no command provided)
const fs::path & GetBlocksDirPath() const
Get blocks directory path.
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
disallow -nofoo syntax
@ 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
Returns the appropriate chain type from the program arguments.
void ForceSetArg(const std::string &strArg, const std::string &strValue)
void logArgsPrefix(const std::string &prefix, const std::string §ion, const std::map< std::string, std::vector< util::SettingsValue >> &args) const
std::string GetChainTypeString() const
Returns the appropriate chain type 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...
std::optional< unsigned int > GetArgFlags(const std::string &name) const
Return Flags for known arg.
const fs::path & GetDataDirBase() const
Get data directory path.
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 ClearPathCache()
Clear cached directory paths.
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
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.
const fs::path & GetDataDir(bool net_specific) const
Get data directory path.
fs::path GetConfigFilePath() const
Return config file path (read-only)
void AddCommand(const std::string &cmd, const std::string &help)
Add subcommand.
std::vector< util::SettingsValue > GetSettingsList(const std::string &arg) const
Get list of setting values.
std::variant< ChainType, std::string > GetChainArg() const
Return -regtest/-signet/-testnet/-chain= setting as a ChainType enum if a recognized chain type was s...
void LogArgs() const
Log the config file options and the command line arguments, useful for troubleshooting.
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.
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)
Rename src to dest.
bool error(const char *fmt, const Args &... args)
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 a 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.
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.
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.