58 if (strValue.empty()) {
61 return (
atoi(strValue) != 0);
65 return arg.size() > 0 && arg[0] ==
'-' ? arg.substr(1) : arg;
88 const std::string &value) {
91 size_t option_index = key.find(
'.');
92 if (option_index != std::string::npos) {
93 section = key.substr(0, option_index);
94 key.erase(0, option_index + 1);
96 if (key.substr(0, 2) ==
"no") {
100 LogPrintf(
"Warning: parsed potentially confusing double-negative "
121 "Negating of -%s is meaningless and therefore forbidden", key);
134 std::set<std::string> unsuitables;
139 if (m_network.empty()) {
140 return std::set<std::string>{};
145 return std::set<std::string>{};
148 for (
const auto &arg : m_network_only_args) {
151 unsuitables.insert(arg);
159 static const std::set<std::string> available_sections{
164 std::list<SectionInfo> unrecognized = m_config_sections;
165 unrecognized.remove_if([](
const SectionInfo &appeared) {
166 return available_sections.find(appeared.
m_name) !=
167 available_sections.end();
178 size_t is_index = key.find(
'=');
179 if (is_index != std::string::npos) {
180 val = key.substr(is_index + 1);
195 if (key.length() > 1 && key[1] ==
'-') {
202 std::string &
error) {
204 m_settings.command_line_options.clear();
206 for (
int i = 1; i < argc; i++) {
207 std::string key(argv[i]);
214 if (key.substr(0, 5) ==
"-psn_") {
237 if (!
flags || !section.empty()) {
246 m_settings.command_line_options[key].push_back(value);
252 util::FindKey(m_settings.command_line_options,
"includeconf")) {
255 "-includeconf cannot be used from commandline; -includeconf=" +
256 include.get_str() +
"\n";
263std::optional<unsigned int>
266 for (
const auto &arg_map : m_available_args) {
267 const auto search = arg_map.second.find(
name);
268 if (search != arg_map.second.end()) {
269 return search->second.m_flags;
276 const fs::path &default_value)
const {
280 std::string path_str =
GetArg(arg,
"");
281 if (path_str.empty()) {
282 return default_value;
286 return result.has_filename() ? result : result.parent_path();
291 fs::path &path = m_cached_blocks_path;
301 if (!fs::is_directory(path)) {
318 net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
326 if (!datadir.empty()) {
328 if (!fs::is_directory(path)) {
336 if (net_specific && !
BaseParams().DataDir().empty()) {
367 m_cached_network_datadir_path =
fs::path();
372 std::vector<std::string> result;
374 result.push_back(value.isFalse() ?
"0"
375 : value.isTrue() ?
"1"
391 std::vector<std::string> errors;
394 Join(errors,
"\n- "));
399 Join(errors,
"\n- "));
408 if (settings.empty()) {
416 temp ? settings +
".tmp" : settings);
422 std::vector<std::string> *error_out) {
423 for (
const auto &
error : errors) {
425 error_out->emplace_back(
error);
439 m_settings.rw_settings.clear();
440 std::vector<std::string> read_errors;
445 for (
const auto &setting : m_settings.rw_settings) {
447 std::string key = setting.first;
451 LogPrintf(
"Ignoring unknown rw_settings value %s\n", setting.first);
462 throw std::logic_error(
"Attempt to write settings file when dynamic "
463 "settings are disabled.");
467 std::vector<std::string> write_errors;
474 {
strprintf(
"Failed renaming settings file %s to %s\n",
495 const std::string &strDefault)
const {
496 return GetArg(strArg).value_or(strDefault);
499std::optional<std::string>
522 const std::string &strDefault) {
527 int64_t nDefault)
const {
528 return GetIntArg(strArg).value_or(nDefault);
547 return value.
getInt<int64_t>();
580 const std::string &strValue) {
598 const std::string &strValue) {
600 m_settings.forced_settings[
SettingName(strArg)] = strValue;
609 const std::vector<std::string> &values) {
613 for (
const std::string &s : values) {
617 m_settings.forced_settings[
SettingName(strArg)] = value;
623 size_t eq_index =
name.find(
'=');
624 if (eq_index == std::string::npos) {
625 eq_index =
name.size();
627 std::string arg_name =
name.substr(0, eq_index);
630 std::map<std::string, Arg> &arg_map = m_available_args[cat];
631 auto ret = arg_map.emplace(
638 m_network_only_args.emplace(arg_name);
643 for (
const std::string &
name : names) {
650 m_settings.forced_settings.erase(
SettingName(strArg));
654 const bool show_debug =
GetBoolArg(
"-help-debug",
false);
656 std::string usage =
"";
658 for (
const auto &arg_map : m_available_args) {
659 switch (arg_map.first) {
717 for (
const auto &arg : arg_map.second) {
720 if (arg.second.m_help_param.empty()) {
723 name = arg.first + arg.second.m_help_param;
738 args.
AddArg(
"-?",
"Print this help message and exit",
false,
748 return std::string(message) + std::string(
"\n\n");
752 const std::string &message) {
753 return std::string(
optIndent,
' ') + std::string(option) +
754 std::string(
"\n") + std::string(
msgIndent,
' ') +
765 return GetSpecialFolderPath(CSIDL_APPDATA) /
"Bitcoin";
768 char *pszHome = getenv(
"HOME");
769 if (pszHome ==
nullptr || strlen(pszHome) == 0) {
776 return pathRet /
"Library/Application Support/Bitcoin";
779 return pathRet /
".bitcoin";
786 return datadir.empty() || fs::is_directory(
fs::absolute(datadir));
794 auto get_net = [&](
const std::string &arg) {
801 return value.
isNull() ? false
806 const bool fRegTest = get_net(
"-regtest");
807 const bool fTestNet = get_net(
"-testnet");
808 const bool is_chain_arg_set =
IsArgSet(
"-chain");
810 if (
int(is_chain_arg_set) +
int(fRegTest) +
int(fTestNet) > 1) {
811 throw std::runtime_error(
"Invalid combination of -regtest, -testnet "
812 "and -chain. Can use at most one.");
825 m_network_only_args.count(arg) == 0;
836std::vector<util::SettingsValue>
844 const std::string &
prefix,
const std::string §ion,
845 const std::map<std::string, std::vector<util::SettingsValue>> &args)
const {
846 std::string section_str = section.empty() ?
"" :
"[" + section +
"] ";
847 for (
const auto &arg : args) {
848 for (
const auto &value : arg.second) {
851 std::string value_str =
862 for (
const auto §ion : m_settings.ro_config) {
863 logArgsPrefix(
"Config file arg:", section.first, section.second);
865 for (
const auto &setting : m_settings.rw_settings) {
866 LogPrintf(
"Setting file arg: %s = %s\n", setting.first,
867 setting.second.write());
869 logArgsPrefix(
"Command-line arg:",
"", m_settings.command_line_options);
874WinCmdLineArgs::WinCmdLineArgs() {
875 wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
876 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t> utf8_cvt;
877 argv =
new char *[argc];
879 for (
int i = 0; i < argc; i++) {
880 args[i] = utf8_cvt.to_bytes(wargv[i]);
881 argv[i] = &*args[i].begin();
886WinCmdLineArgs::~WinCmdLineArgs() {
890std::pair<int, char **> WinCmdLineArgs::get() {
891 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
bool CheckValid(const std::string &key, const util::SettingsValue &val, unsigned int flags, std::string &error)
Check settings value validity according to flags.
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
bool ParseKeyValue(std::string &key, std::string &val)
std::optional< bool > SettingToBool(const util::SettingsValue &value)
bool CheckDataDirOption(const ArgsManager &args)
static const int screenWidth
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.
const char *const BITCOIN_CONF_FILENAME
util::SettingsValue InterpretOption(std::string §ion, std::string &key, const std::string &value)
Interpret -nofoo as if the user supplied -foo=0.
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.
const 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.
bool ReadSettingsFile(std::vector< std::string > *errors=nullptr)
Read settings file.
void ForceSetArg(const std::string &strArg, const std::string &strValue)
bool InitSettings(std::string &error)
Read and update settings file with saved settings.
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.
void EnsureDataDir() const
If datadir does not exist, create it along with wallets/ subdirectory(s).
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.
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.
const std::list< SectionInfo > GetUnrecognizedSections() const
Log warnings for unrecognized section names in the config file.
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
static const std::string REGTEST
static const std::string TESTNET
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
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)
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 bool exists(const path &p)
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,...
SettingsValue GetSetting(const Settings &settings, const std::string §ion, const std::string &name, bool ignore_default_section_config, bool ignore_nonpersistent, bool get_chain_name)
Get settings value from combined sources: forced settings, command line arguments,...
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.
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Accessor for list of settings that skips negated values when iterated over.
int atoi(const std::string &str)
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.
int64_t atoi64(const std::string &str)
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.