66 return arg.size() > 0 &&
arg[0] ==
'-' ?
arg.substr(1) :
arg;
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 :
"";
141 if (m_network.empty())
return std::set<std::string> {};
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;
196 if (
is_index != std::string::npos) {
224 if (key.length() > 1 && key[1] ==
'-')
241 if (!value)
return false;
251 error =
"-includeconf cannot be used from commandline; -includeconf=" +
values.
begin()->
write();
264 return search->second.m_flags;
283 return result.has_filename() ? result : result.parent_path();
293 if (!path.empty())
return path;
297 if (!fs::is_directory(path)) {
307 fs::create_directories(path);
317 if (!path.empty())
return path;
319 const fs::path datadir{
GetPathArg(
"-datadir")};
320 if (!datadir.empty()) {
322 if (!fs::is_directory(path)) {
357 ret.command = *(it++);
361 ret.args.push_back(*(it++));
368 std::vector<std::string> result;
370 result.push_back(value.isFalse() ?
"0" : value.isTrue() ?
"1" : value.get_str());
397 for (
const auto& error :
errors) {
420 for (
const auto& setting :
m_settings.rw_settings) {
423 LogWarning(
"Ignoring unknown rw_settings value %s", setting.first);
433 throw std::logic_error(
"Attempt to write settings file when dynamic settings are disabled.");
474 if (value.
isNull())
return std::nullopt;
475 if (value.
isFalse())
return "0";
476 if (value.
isTrue())
return "1";
486template <std::
integral Int>
492template <std::
integral Int>
499template <std::
integral Int>
502 if (value.
isNull())
return std::nullopt;
504 if (value.
isTrue())
return 1;
509template <std::
integral Int>
528 if (value.
isNull())
return std::nullopt;
538#define INSTANTIATE_INT_TYPE(Type) \
539 template Type ArgsManager::GetArg<Type>(const std::string&, Type) const; \
540 template std::optional<Type> ArgsManager::GetArg<Type>(const std::string&) const; \
541 template Type SettingTo<Type>(const common::SettingsValue&, Type); \
542 template std::optional<Type> SettingTo<Type>(const common::SettingsValue&)
553#undef INSTANTIATE_INT_TYPE
579 Assert(
cmd.find(
'=') == std::string::npos);
595 if (
eq_index == std::string::npos) {
628 std::vector<std::string> found{};
633 found.push_back(
cmd);
636 if (found.size() > 1) {
637 throw std::runtime_error(
strprintf(
"Only one of %s may be specified.",
util::Join(found,
", ")));
705 if (
arg.second.m_help_param.empty()) {
733 return std::string(message) + std::string(
"\n\n");
738 std::string(
"\n") + std::string(
msgIndent,
' ') +
744 "addrman (use deterministic addrman)",
745 "reindex_after_failure_noninteractive_yes (When asked for a reindex after failure interactively, simulate as-if answered with 'yes')",
746 "bip94 (enforce BIP94 consensus rules)",
752 return std::any_of(options.begin(), options.end(), [
test_option](
const auto&
option) {
753 return option == test_option;
781 return pathRet /
"Library/Application Support/Bitcoin";
792 return datadir.empty() || fs::is_directory(
fs::absolute(datadir));
812 throw std::runtime_error(
strprintf(
"Unknown chain %s.", std::get<std::string>(
arg)));
819 return std::get<std::string>(
arg);
840 throw std::runtime_error(
"Invalid combination of -regtest, -signet, -testnet, -testnet4 and -chain. Can use at most one.");
874 const std::string&
prefix,
875 const std::string& section,
876 const std::map<std::string, std::vector<common::SettingsValue>>&
args)
const
878 std::string
section_str = section.empty() ?
"" :
"[" + section +
"] ";
880 for (
const auto& value :
arg.second) {
893 for (
const auto& section :
m_settings.ro_config) {
894 logArgsPrefix(
"Config file arg:", section.first, section.second);
896 for (
const auto& setting :
m_settings.rw_settings) {
897 LogInfo(
"Setting file arg: %s = %s\n", setting.first, setting.second.write());
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 const int msgIndent
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)
static const int screenWidth
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
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::string SettingToString(const common::SettingsValue &, const std::string &)
bool SettingToBool(const common::SettingsValue &, bool)
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
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.
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)
bool IsArgNegated(const std::string &strArg) const
Return true if the argument was originally passed as a negated option, i.e.
void logArgsPrefix(const std::string &prefix, const std::string §ion, const std::map< std::string, std::vector< common::SettingsValue > > &args) const
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
common::SettingsValue GetSetting(const std::string &arg) const
Get setting 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)
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.
void CheckMultipleCLIArgs() const
Check CLI command args.
void SetDefaultFlags(std::optional< unsigned int >)
Set default flags to return for an unknown arg.
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 SetConfigFilePath(fs::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.
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const
Write settings file or backup settings file.
void ClearArgs()
Clear available arguments.
fs::path GetDataDirBase() const
Get data directory path.
fs::path GetBlocksDirPath() const
Get blocks directory path.
fs::path GetConfigFilePath() const
Return config file path (read-only)
std::vector< common::SettingsValue > GetSettingsList(const std::string &arg) const
Get list of setting values.
void AddCommand(const std::string &cmd, const std::string &help)
Add subcommand.
std::variant< ChainType, std::string > GetChainArg() const
Return -regtest/-signet/-testnet/-testnet4/-chain= setting as a ChainType enum if a recognized chain ...
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.
common::SettingsValue GetPersistentSetting(const std::string &name) const
Get current setting from config file or read/write settings file, ignoring nonpersistent command line...
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.
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
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) 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,...
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.
const SettingsValue * begin() const
Pointer to first non-negated value.
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.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.