Bitcoin ABC 0.31.6
P2P Digital Currency
args.h
Go to the documentation of this file.
1// Copyright (c) 2023 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 <compat.h>
9#include <sync.h>
10#include <util/chaintype.h>
11#include <util/fs.h>
12#include <util/settings.h>
13
14#include <cstdint>
15#include <iosfwd>
16#include <list>
17#include <map>
18#include <optional>
19#include <set>
20#include <string>
21#include <variant>
22#include <vector>
23
24class ArgsManager;
25
26extern const char *const BITCOIN_CONF_FILENAME;
27extern const char *const BITCOIN_SETTINGS_FILENAME;
28
29// Return true if -datadir option points to a valid directory or is not
30// specified.
31bool CheckDataDirOption(const ArgsManager &args);
33 const fs::path &configuration_file_path);
34
35[[nodiscard]] bool ParseKeyValue(std::string &key,
36 std::optional<std::string> &val);
37
47fs::path AbsPathForConfigVal(const ArgsManager &args, const fs::path &path,
48 bool net_specific = true);
49
50inline bool IsSwitchChar(char c) {
51#ifdef WIN32
52 return c == '-' || c == '/';
53#else
54 return c == '-';
55#endif
56}
57
58enum class OptionsCategory {
59 OPTIONS,
61 WALLET,
63 ZMQ,
68 RPC,
69 GUI,
73 CHRONIK,
74
75 // Always the last option to avoid printing these in the help
76 HIDDEN,
77};
78
79struct KeyInfo {
80 std::string name;
81 std::string section;
82 bool negated{false};
83};
84
85KeyInfo InterpretKey(std::string key);
86
87std::optional<util::SettingsValue>
88InterpretValue(const KeyInfo &key, const std::optional<std::string> &value,
89 unsigned int flags, std::string &error);
90
92 std::string m_name;
93 std::string m_file;
94 int m_line;
95};
96
97std::string SettingToString(const util::SettingsValue &, const std::string &);
98std::optional<std::string> SettingToString(const util::SettingsValue &);
99
100int64_t SettingToInt(const util::SettingsValue &, int64_t);
101std::optional<int64_t> SettingToInt(const util::SettingsValue &);
102
103bool SettingToBool(const util::SettingsValue &, bool);
104std::optional<bool> SettingToBool(const util::SettingsValue &);
105
107public:
112 enum Flags : uint32_t {
114 ALLOW_ANY = 0x01,
116 // ALLOW_BOOL = 0x02,
118 // ALLOW_INT = 0x04,
120 // ALLOW_STRING = 0x08,
122 // ALLOW_LIST = 0x10,
127
128 DEBUG_ONLY = 0x100,
129 /* Some options would cause cross-contamination if values for
130 * mainnet were used while running on regtest/testnet (or vice-versa).
131 * Setting them as NETWORK_ONLY ensures that sharing a config file
132 * between mainnet and regtest/testnet won't cause problems due to these
133 * parameters by accident. */
135 // This argument's value is sensitive (such as a password).
136 SENSITIVE = 0x400,
137 };
138
139protected:
140 struct Arg {
141 std::string m_help_param;
142 std::string m_help_text;
143 unsigned int m_flags;
144 };
145
148 std::string m_network GUARDED_BY(cs_args);
149 std::set<std::string> m_network_only_args GUARDED_BY(cs_args);
150 std::map<OptionsCategory, std::map<std::string, Arg>>
151 m_available_args GUARDED_BY(cs_args);
152 std::list<SectionInfo> m_config_sections GUARDED_BY(cs_args);
153 mutable fs::path m_cached_blocks_path GUARDED_BY(cs_args);
154 mutable fs::path m_cached_datadir_path GUARDED_BY(cs_args);
155 mutable fs::path m_cached_network_datadir_path GUARDED_BY(cs_args);
156
157 [[nodiscard]] bool ReadConfigStream(std::istream &stream,
158 const std::string &filepath,
159 std::string &error,
160 bool ignore_invalid_keys = false);
161
167 bool UseDefaultSection(const std::string &arg) const
169
170public:
178 util::SettingsValue GetSetting(const std::string &arg) const;
179
183 std::vector<util::SettingsValue>
184 GetSettingsList(const std::string &arg) const;
185
186 ArgsManager();
187 ~ArgsManager();
188
192 void SelectConfigNetwork(const std::string &network);
193
194 [[nodiscard]] bool ParseParameters(int argc, const char *const argv[],
195 std::string &error);
200
201 [[nodiscard]] bool ReadConfigFiles(std::string &error,
202 bool ignore_invalid_keys = false);
203
209 std::set<std::string> GetUnsuitableSectionOnlyArgs() const;
210
214 std::list<SectionInfo> GetUnrecognizedSections() const;
215
222
230 fs::path GetDataDirBase() const { return GetDataDir(false); }
231
239 fs::path GetDataDirNet() const { return GetDataDir(true); }
240
244 void ClearPathCache();
245
252 std::vector<std::string> GetArgs(const std::string &strArg) const;
253
260 bool IsArgSet(const std::string &strArg) const;
261
269 bool IsArgNegated(const std::string &strArg) const;
270
278 std::string GetArg(const std::string &strArg,
279 const std::string &strDefault) const;
280 std::optional<std::string> GetArg(const std::string &strArg) const;
281
294 fs::path GetPathArg(std::string arg,
295 const fs::path &default_value = {}) const;
296
304 int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const;
305 std::optional<int64_t> GetIntArg(const std::string &strArg) const;
306
314 bool GetBoolArg(const std::string &strArg, bool fDefault) const;
315 std::optional<bool> GetBoolArg(const std::string &strArg) const;
316
324 bool SoftSetArg(const std::string &strArg, const std::string &strValue);
325
333 bool SoftSetBoolArg(const std::string &strArg, bool fValue);
334
335 // Forces an arg setting. Called by SoftSetArg() if the arg hasn't already
336 // been set. Also called directly in testing.
337 void ForceSetArg(const std::string &strArg, const std::string &strValue);
338
339 // Forces a multi arg setting, used only in testing
340 void ForceSetMultiArg(const std::string &strArg,
341 const std::vector<std::string> &values);
342
349 ChainType GetChainType() const;
350
356 std::string GetChainTypeString() const;
357
361 void AddArg(const std::string &name, const std::string &help,
362 unsigned int flags, const OptionsCategory &cat);
363
367 void ClearForcedArg(const std::string &strArg);
368
372 void AddHiddenArgs(const std::vector<std::string> &args);
373
377 void ClearArgs() {
378 LOCK(cs_args);
379 m_available_args.clear();
380 m_network_only_args.clear();
381 }
382
386 std::string GetHelpMessage() const;
387
392 std::optional<unsigned int> GetArgFlags(const std::string &name) const;
393
398 bool GetSettingsPath(fs::path *filepath = nullptr, bool temp = false,
399 bool backup = false) const;
400
404 bool ReadSettingsFile(std::vector<std::string> *errors = nullptr);
405
410 bool WriteSettingsFile(std::vector<std::string> *errors = nullptr,
411 bool backup = false) const;
412
417 util::SettingsValue GetPersistentSetting(const std::string &name) const;
418
422 template <typename Fn> void LockSettings(Fn &&fn) {
423 LOCK(cs_args);
424 fn(m_settings);
425 }
426
431 void LogArgs() const;
432
433private:
441 fs::path GetDataDir(bool net_specific) const;
442
449 std::variant<ChainType, std::string> GetChainArg() const;
450
451 // Helper function for LogArgs().
452 void
453 logArgsPrefix(const std::string &prefix, const std::string &section,
454 const std::map<std::string, std::vector<util::SettingsValue>>
455 &args) const;
456};
457
458extern ArgsManager gArgs;
459
463bool HelpRequested(const ArgsManager &args);
464
466void SetupHelpOptions(ArgsManager &args);
467
474std::string HelpMessageGroup(const std::string &message);
475
483std::string HelpMessageOpt(const std::string &option,
484 const std::string &message);
485
486namespace common {
487#ifdef WIN32
488class WinCmdLineArgs {
489public:
490 WinCmdLineArgs();
491 ~WinCmdLineArgs();
492 std::pair<int, char **> get();
493
494private:
495 int argc;
496 char **argv;
497 std::vector<std::string> args;
498};
499#endif
500} // namespace common
501
502#endif // BITCOIN_COMMON_ARGS_H
bool HelpRequested(const ArgsManager &args)
Definition: args.cpp:701
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: args.cpp:706
OptionsCategory
Definition: args.h:58
const char *const BITCOIN_SETTINGS_FILENAME
Definition: args.cpp:38
bool SettingToBool(const util::SettingsValue &, bool)
Definition: args.cpp:544
bool CheckDataDirOption(const ArgsManager &args)
Definition: args.cpp:753
int64_t SettingToInt(const util::SettingsValue &, int64_t)
Definition: args.cpp:521
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: configfile.cpp:236
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.
Definition: args.cpp:107
ArgsManager gArgs
Definition: args.cpp:40
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition: args.cpp:716
KeyInfo InterpretKey(std::string key)
Parse "name", "section.name", "noname", "section.noname" settings keys.
Definition: args.cpp:78
bool ParseKeyValue(std::string &key, std::optional< std::string > &val)
Definition: args.cpp:187
const char *const BITCOIN_CONF_FILENAME
Definition: args.cpp:37
bool IsSwitchChar(char c)
Definition: args.h:50
fs::path GetConfigFile(const ArgsManager &args, const fs::path &configuration_file_path)
Definition: configfile.cpp:30
std::string HelpMessageOpt(const std::string &option, const std::string &message)
Format a string to be used as option description in help messages.
Definition: args.cpp:720
std::string SettingToString(const util::SettingsValue &, const std::string &)
Definition: args.cpp:490
int flags
Definition: bitcoin-tx.cpp:542
ChainType
Definition: chaintype.h:11
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...
Definition: args.cpp:141
bool IsArgNegated(const std::string &strArg) const
Return true if the argument was originally passed as a negated option, i.e.
Definition: args.cpp:459
std::map< OptionsCategory, std::map< std::string, Arg > > m_available_args GUARDED_BY(cs_args)
std::list< SectionInfo > GetUnrecognizedSections() const
Log warnings for unrecognized section names in the config file.
Definition: args.cpp:165
Flags
Flags controlling how config and command line arguments are validated and interpreted.
Definition: args.h:112
@ NETWORK_ONLY
Definition: args.h:134
@ ALLOW_ANY
disable validation
Definition: args.h:114
@ DISALLOW_NEGATION
unimplemented, draft implementation in #16545
Definition: args.h:124
@ DISALLOW_ELISION
disallow -foo syntax that doesn't assign any value
Definition: args.h:126
@ DEBUG_ONLY
Definition: args.h:128
@ SENSITIVE
Definition: args.h:136
bool ReadSettingsFile(std::vector< std::string > *errors=nullptr)
Read settings file.
Definition: args.cpp:403
ChainType GetChainType() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
Definition: args.cpp:762
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: args.cpp:566
fs::path m_cached_datadir_path GUARDED_BY(cs_args)
std::string GetChainTypeString() const
Returns the appropriate chain name string from the program arguments.
Definition: args.cpp:771
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:211
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: args.cpp:362
util::SettingsValue GetPersistentSetting(const std::string &name) const
Get current setting from config file or read/write settings file, ignoring nonpersistent command line...
Definition: args.cpp:452
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:239
ArgsManager()
Definition: args.cpp:138
std::optional< unsigned int > GetArgFlags(const std::string &name) const
Return Flags for known arg.
Definition: args.cpp:275
std::list< SectionInfo > m_config_sections GUARDED_BY(cs_args)
std::string m_network GUARDED_BY(cs_args)
~ArgsManager()
Definition: args.cpp:139
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.
Definition: args.cpp:376
void LockSettings(Fn &&fn)
Access settings with lock held.
Definition: args.h:422
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
Definition: args.cpp:548
void SelectConfigNetwork(const std::string &network)
Select the network in use.
Definition: args.cpp:182
std::string GetHelpMessage() const
Get the help string.
Definition: args.cpp:622
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...
Definition: args.cpp:577
void ClearPathCache()
Clear cached directory paths.
Definition: args.cpp:354
fs::path m_cached_blocks_path GUARDED_BY(cs_args)
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:372
std::set< std::string > m_network_only_args GUARDED_BY(cs_args)
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const
Write settings file or backup settings file.
Definition: args.cpp:426
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:495
void ClearArgs()
Clear available arguments.
Definition: args.h:377
fs::path GetDataDirBase() const
Get data directory path.
Definition: args.h:230
fs::path GetBlocksDirPath() const
Get blocks directory path.
Definition: args.cpp:300
void logArgsPrefix(const std::string &prefix, const std::string &section, const std::map< std::string, std::vector< util::SettingsValue > > &args) const
Definition: args.cpp:836
fs::path GetConfigFilePath() const
Return config file path (read-only)
Definition: args.cpp:758
util::Settings m_settings GUARDED_BY(cs_args)
void ClearForcedArg(const std::string &strArg)
Remove a forced arg setting, used only in testing.
Definition: args.cpp:617
std::vector< util::SettingsValue > GetSettingsList(const std::string &arg) const
Get list of setting values.
Definition: args.cpp:830
std::variant< ChainType, std::string > GetChainArg() const
Return -regtest/–testnet/-chain= setting as a ChainType enum if a recognized chain name was set,...
Definition: args.cpp:779
void LogArgs() const
Log the config file options and the command line arguments, useful for troubleshooting.
Definition: args.cpp:853
fs::path GetDataDir(bool net_specific) const
Get data directory path.
Definition: args.cpp:326
RecursiveMutex cs_args
Definition: args.h:146
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:816
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:463
util::SettingsValue GetSetting(const std::string &arg) const
Get setting value.
Definition: args.cpp:821
bool ReadConfigStream(std::istream &stream, const std::string &filepath, std::string &error, bool ignore_invalid_keys=false)
Definition: configfile.cpp:98
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
Definition: args.cpp:558
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false)
Definition: configfile.cpp:132
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:525
void AddHiddenArgs(const std::vector< std::string > &args)
Add many hidden arguments.
Definition: args.cpp:611
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: args.cpp:589
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const
Return path argument or default value.
Definition: args.cpp:286
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
bool error(const char *fmt, const Args &...args)
Definition: logging.h:296
Definition: args.cpp:865
const char * prefix
Definition: rest.cpp:816
const char * name
Definition: rest.cpp:47
static RPCHelpMan help()
Definition: server.cpp:183
std::string m_help_param
Definition: args.h:141
unsigned int m_flags
Definition: args.h:143
std::string m_help_text
Definition: args.h:142
Definition: args.h:79
std::string name
Definition: args.h:80
bool negated
Definition: args.h:82
std::string section
Definition: args.h:81
int m_line
Definition: args.h:94
std::string m_file
Definition: args.h:93
std::string m_name
Definition: args.h:92
Stored settings.
Definition: settings.h:31
#define LOCK(cs)
Definition: sync.h:306
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56