Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
args.h
Go to the documentation of this file.
1// Copyright (c) 2023-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_COMMON_ARGS_H
6#define BITCOIN_COMMON_ARGS_H
7
8#include <common/settings.h>
9#include <sync.h>
10#include <util/chaintype.h>
11#include <util/fs.h>
12
13#include <concepts>
14#include <cstdint>
15#include <iosfwd>
16#include <list>
17#include <map>
18#include <optional>
19#include <set>
20#include <string>
21#include <string_view>
22#include <variant>
23#include <vector>
24
25class ArgsManager;
26
27extern const char * const BITCOIN_CONF_FILENAME;
28extern const char * const BITCOIN_SETTINGS_FILENAME;
29
30// Return true if -datadir option points to a valid directory or is not specified.
32
42fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific = true);
43
44inline bool IsSwitchChar(char c)
45{
46#ifdef WIN32
47 return c == '-' || c == '/';
48#else
49 return c == '-';
50#endif
51}
52
53enum class OptionsCategory {
54 OPTIONS,
56 WALLET,
58 ZMQ,
63 RPC,
64 GUI,
68 IPC,
69
70 // Specific to one or more commands (OptionsCategory::COMMANDS)
71 // These are only included in help with their associated commands.
73
74 HIDDEN // Always the last option to avoid printing these in the help
75};
76
77struct KeyInfo {
78 std::string name;
79 std::string section;
80 bool negated{false};
81};
82
83KeyInfo InterpretKey(std::string key);
84
85std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value,
86 unsigned int flags, std::string& error);
87
89 std::string m_name;
90 std::string m_file;
91 int m_line;
92};
93
94std::optional<std::string> SettingToString(const common::SettingsValue&);
95
96template <std::integral Int>
97std::optional<Int> SettingTo(const common::SettingsValue&);
98
99std::optional<bool> SettingToBool(const common::SettingsValue&);
100
102{
103public:
109 ALLOW_ANY = 0x01,
110 // ALLOW_BOOL = 0x02, //!< unimplemented, draft implementation in #16545
111 // ALLOW_INT = 0x04, //!< unimplemented, draft implementation in #16545
112 // ALLOW_STRING = 0x08, //!< unimplemented, draft implementation in #16545
113 // ALLOW_LIST = 0x10, //!< unimplemented, draft implementation in #16545
116
117 DEBUG_ONLY = 0x100,
118 /* Some options would cause cross-contamination if values for
119 * mainnet were used while running on regtest/testnet (or vice-versa).
120 * Setting them as NETWORK_ONLY ensures that sharing a config file
121 * between mainnet and regtest/testnet won't cause problems due to these
122 * parameters by accident. */
124 // This argument's value is sensitive (such as a password).
125 SENSITIVE = 0x400,
126 COMMAND = 0x800,
127 };
128
129private:
130 struct Arg
131 {
132 std::string m_help_param;
133 std::string m_help_text;
134 unsigned int m_flags;
135 };
136
137 mutable Mutex cs_args;
139 std::vector<std::string> m_command GUARDED_BY(cs_args);
140 std::string m_network GUARDED_BY(cs_args);
141 std::set<std::string> m_network_only_args GUARDED_BY(cs_args);
142 std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args GUARDED_BY(cs_args);
143 std::optional<unsigned int> m_default_flags GUARDED_BY(cs_args){};
144 std::map<std::string, std::set<std::string>> m_command_args GUARDED_BY(cs_args);
146 std::list<SectionInfo> m_config_sections GUARDED_BY(cs_args);
147 std::optional<fs::path> m_config_path GUARDED_BY(cs_args);
151
157 bool UseDefaultSection(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(cs_args);
158
159protected:
160 [[nodiscard]] bool ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
162
163public:
171 common::SettingsValue GetSetting(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
172
176 std::vector<common::SettingsValue> GetSettingsList(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
177
180
184 void SelectConfigNetwork(const std::string& network) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
185
186 [[nodiscard]] bool ParseParameters(int argc, const char* const argv[], std::string& error) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
187
194
202
207
210 std::string command;
215 std::vector<std::string> args;
216 };
220 std::optional<const Command> GetCommand() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
221
230 bool CheckCommandOptions(const std::string& command, std::vector<std::string>* errors = nullptr) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
231
238
245
252
257
264 std::vector<std::string> GetArgs(const std::string& strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
265
272 bool IsArgSet(const std::string& strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
273
281 bool IsArgNegated(const std::string& strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
282
290 std::string GetArg(const std::string& strArg, const std::string& strDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
291 std::optional<std::string> GetArg(const std::string& strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
292
303 fs::path GetPathArg(std::string arg, const fs::path& default_value = {}) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
304
312 template <std::integral Int>
313 Int GetArg(const std::string& strArg, Int nDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
314
315 template <std::integral Int>
316 std::optional<Int> GetArg(const std::string& strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
317
319 std::optional<int64_t> GetIntArg(const std::string& strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args) { return GetArg<int64_t>(strArg); }
320
328 bool GetBoolArg(const std::string& strArg, bool fDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
329 std::optional<bool> GetBoolArg(const std::string& strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
330
338 bool SoftSetArg(const std::string& strArg, const std::string& strValue) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
339
347 bool SoftSetBoolArg(const std::string& strArg, bool fValue) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
348
349 // Forces an arg setting. Called by SoftSetArg() if the arg hasn't already
350 // been set. Also called directly in testing.
351 void ForceSetArg(const std::string& strArg, const std::string& strValue) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
352
359
366
370 void AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
371
375 void AddCommand(const std::string& cmd, const std::string& help, std::set<std::string> options = {}) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
376
380 void AddHiddenArgs(const std::vector<std::string>& args) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
381
386
393
398
403 std::optional<unsigned int> GetArgFlags(const std::string& name) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
404
408 void SetDefaultFlags(std::optional<unsigned int>) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
409
414 bool GetSettingsPath(fs::path* filepath = nullptr, bool temp = false, bool backup = false) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
415
420
425 bool WriteSettingsFile(std::vector<std::string>* errors = nullptr, bool backup = false) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
426
431 common::SettingsValue GetPersistentSetting(const std::string& name) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
432
436 template <typename Fn>
438 {
439 LOCK(cs_args);
440 fn(m_settings);
441 }
442
448
449private:
450 // Internal helpers, for use by callers that already hold `cs_args`.
451 common::SettingsValue GetSetting_(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(cs_args);
452 std::optional<unsigned int> GetArgFlags_(const std::string& name) const EXCLUSIVE_LOCKS_REQUIRED(cs_args);
453 fs::path GetPathArg_(std::string arg, const fs::path& default_value = {}) const EXCLUSIVE_LOCKS_REQUIRED(cs_args);
454
462
469 std::variant<ChainType, std::string> GetChainArg() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
470
471 // Helper function for LogArgs().
472 void logArgsPrefix(
473 const std::string& prefix,
474 const std::string& section,
475 const std::map<std::string, std::vector<common::SettingsValue>>& args) const EXCLUSIVE_LOCKS_REQUIRED(cs_args);
476};
477
479
483bool HelpRequested(const ArgsManager& args);
484
487
488extern const std::vector<std::string> TEST_OPTIONS_DOC;
489
491bool HasTestOption(const ArgsManager& args, const std::string& test_option);
492
499std::string HelpMessageGroup(const std::string& message);
500
511
512#endif // BITCOIN_COMMON_ARGS_H
const std::vector< std::string > TEST_OPTIONS_DOC
Definition args.cpp:828
bool HelpRequested(const ArgsManager &args)
Definition args.cpp:795
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition args.cpp:800
std::optional< bool > SettingToBool(const common::SettingsValue &)
Definition args.cpp:571
std::string HelpMessageOpt(std::string_view option, std::string_view help_param, std::string_view message, bool subopt=false)
Format a string to be used as option description in help messages.
Definition args.cpp:810
std::optional< std::string > SettingToString(const common::SettingsValue &)
Definition args.cpp:528
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.
Definition args.cpp:103
OptionsCategory
Definition args.h:53
const char *const BITCOIN_SETTINGS_FILENAME
Definition args.cpp:36
bool CheckDataDirOption(const ArgsManager &args)
Definition args.cpp:874
fs::path AbsPathForConfigVal(const ArgsManager &args, const fs::path &path, bool net_specific=true)
Most paths passed as configuration arguments are treated as relative to the datadir if they are not a...
Definition config.cpp:237
ArgsManager gArgs
Definition args.cpp:38
bool HasTestOption(const ArgsManager &args, const std::string &test_option)
Checks if a particular test option is present in -test command-line arg options.
Definition args.cpp:834
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition args.cpp:806
KeyInfo InterpretKey(std::string key)
Parse "name", "section.name", "noname", "section.noname" settings keys.
Definition args.cpp:75
const char *const BITCOIN_CONF_FILENAME
Definition args.cpp:35
bool IsSwitchChar(char c)
Definition args.h:44
std::optional< Int > SettingTo(const common::SettingsValue &)
Definition args.cpp:551
int flags
const auto cmd
const auto command
ArgsManager & args
Definition bitcoind.cpp:280
ChainType
Definition chaintype.h:12
bool ParseParameters(int argc, const char *const argv[], std::string &error) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Definition args.cpp:175
std::vector< common::SettingsValue > GetSettingsList(const std::string &arg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get list of setting values.
Definition args.cpp:959
std::vector< std::string > GetArgs(const std::string &strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return a vector of strings of the given argument.
Definition args.cpp:422
std::optional< unsigned int > m_default_flags GUARDED_BY(cs_args)
Definition args.h:143
ChainType GetChainType() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Returns the appropriate chain type from the program arguments.
Definition args.cpp:893
std::map< OptionsCategory, std::map< std::string, Arg > > m_available_args GUARDED_BY(cs_args)
void CheckMultipleCLIArgs() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Check CLI command args.
Definition args.cpp:686
Flags
Flags controlling how config and command line arguments are validated and interpreted.
Definition args.h:108
@ NETWORK_ONLY
Definition args.h:123
@ ALLOW_ANY
disable validation
Definition args.h:109
@ DISALLOW_NEGATION
disallow -nofoo syntax
Definition args.h:114
@ DISALLOW_ELISION
disallow -foo syntax that doesn't assign any value
Definition args.h:115
@ DEBUG_ONLY
Definition args.h:117
@ COMMAND
Definition args.h:126
@ SENSITIVE
Definition args.h:125
std::list< SectionInfo > GetUnrecognizedSections() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Log warnings for unrecognized section names in the config file.
Definition args.cpp:152
common::SettingsValue GetSetting(const std::string &arg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get setting value.
Definition args.cpp:953
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.
Definition args.cpp:436
bool ReadConfigString(const std::string &str_config) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Definition config.cpp:120
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.
Definition args.cpp:388
fs::path GetBlocksDirPath() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get blocks directory path.
Definition args.cpp:298
fs::path m_cached_datadir_path GUARDED_BY(cs_args)
fs::path GetDataDirBase() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path.
Definition args.cpp:323
fs::path GetConfigFilePath() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return config file path (read-only)
Definition args.cpp:880
void SetDefaultFlags(std::optional< unsigned int >) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Set default flags to return for an unknown arg.
Definition args.cpp:274
void AddCommand(const std::string &cmd, const std::string &help, std::set< std::string > options={}) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Add command.
Definition args.cpp:616
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.
Definition args.cpp:594
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...
Definition args.cpp:132
std::optional< fs::path > m_config_path GUARDED_BY(cs_args)
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Add argument.
Definition args.cpp:640
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Write settings file or backup settings file.
Definition args.cpp:485
fs::path GetPathArg_(std::string arg, const fs::path &default_value={}) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
Definition args.cpp:280
std::optional< int64_t > GetIntArg(const std::string &strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Definition args.h:319
void LockSettings(Fn &&fn) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Access settings with lock held.
Definition args.h:437
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Definition config.cpp:132
std::list< SectionInfo > m_config_sections GUARDED_BY(cs_args)
std::string m_network GUARDED_BY(cs_args)
void ClearPathCache() EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Clear cached directory paths.
Definition args.cpp:359
fs::path GetDataDir(bool net_specific) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
Get data directory path.
Definition args.cpp:333
fs::path m_cached_blocks_path GUARDED_BY(cs_args)
void SetConfigFilePath(fs::path) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Definition args.cpp:886
std::set< std::string > m_network_only_args GUARDED_BY(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...
Definition args.cpp:505
std::map< std::string, std::set< std::string > > m_command_args GUARDED_BY(cs_args)
void ForceSetArg(const std::string &strArg, const std::string &strValue) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Definition args.cpp:610
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return path argument or default value.
Definition args.cpp:292
std::vector< std::string > m_command GUARDED_BY(cs_args)
void LogArgs() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Log the config file options and the command line arguments, useful for troubleshooting.
Definition args.cpp:983
std::string GetArg(const std::string &strArg, const std::string &strDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return string argument or default value.
Definition args.cpp:517
void AddHiddenArgs(const std::vector< std::string > &args) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Add many hidden arguments.
Definition args.cpp:669
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 ...
Definition args.cpp:907
common::Settings m_settings GUARDED_BY(cs_args)
void logArgsPrefix(const std::string &prefix, const std::string &section, const std::map< std::string, std::vector< common::SettingsValue > > &args) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
Definition args.cpp:965
std::string GetChainTypeString() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Returns the appropriate chain type string from the program arguments.
Definition args.cpp:900
bool ReadSettingsFile(std::vector< std::string > *errors=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Read settings file.
Definition args.cpp:462
void ClearArgs() EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Clear available arguments.
Definition args.cpp:676
bool IsArgSet(const std::string &strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return true if the given argument has been manually set.
Definition args.cpp:431
Mutex cs_args
Definition args.h:137
std::optional< const Command > GetCommand() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get the command and command args (returns std::nullopt if no command provided)
Definition args.cpp:368
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.
Definition args.cpp:602
fs::path GetDataDirNet() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path with appended network identifier.
Definition args.cpp:328
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.
Definition args.cpp:512
common::SettingsValue GetSetting_(const std::string &arg) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
Definition args.cpp:945
bool ReadConfigStream(std::istream &stream, const std::string &filepath, std::string &error, bool ignore_invalid_keys=false) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Definition config.cpp:91
std::optional< unsigned int > GetArgFlags(const std::string &name) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return Flags for known arg.
Definition args.cpp:268
std::optional< unsigned int > GetArgFlags_(const std::string &name) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
Definition args.cpp:256
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Definition args.h:318
bool m_accept_any_command GUARDED_BY(cs_args)
Definition args.h:145
fs::path m_cached_network_datadir_path GUARDED_BY(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...
Definition args.cpp:939
bool GetBoolArg(const std::string &strArg, bool fDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return boolean argument or default value.
Definition args.cpp:560
void SelectConfigNetwork(const std::string &network) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Select the network in use.
Definition args.cpp:169
std::string GetHelpMessage() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get the help string.
Definition args.cpp:703
CRPCCommand m_command
const char * prefix
Definition rest.cpp:1197
const char * name
Definition rest.cpp:71
static RPCMethod help()
Definition server.cpp:127
std::string m_help_param
Definition args.h:132
unsigned int m_flags
Definition args.h:134
std::string m_help_text
Definition args.h:133
std::vector< std::string > args
If command is non-empty: Any args that followed it If command is empty: The unregistered command and ...
Definition args.h:215
std::string command
The command (if one has been registered with AddCommand), or empty.
Definition args.h:210
Definition args.h:77
std::string name
Definition args.h:78
bool negated
Definition args.h:80
std::string section
Definition args.h:79
int m_line
Definition args.h:91
std::string m_file
Definition args.h:90
std::string m_name
Definition args.h:89
Stored settings.
Definition settings.h:34
#define LOCK(cs)
Definition sync.h:268
#define EXCLUSIVE_LOCKS_REQUIRED(...)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82