Bitcoin Core  29.1.0
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 <common/settings.h>
9 #include <compat/compat.h>
10 #include <sync.h>
11 #include <util/chaintype.h>
12 #include <util/fs.h>
13 
14 #include <iosfwd>
15 #include <list>
16 #include <map>
17 #include <optional>
18 #include <set>
19 #include <stdint.h>
20 #include <string>
21 #include <variant>
22 #include <vector>
23 
24 class ArgsManager;
25 
26 extern const char * const BITCOIN_CONF_FILENAME;
27 extern const char * const BITCOIN_SETTINGS_FILENAME;
28 
29 // Return true if -datadir option points to a valid directory or is not specified.
31 
41 fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific = true);
42 
43 inline bool IsSwitchChar(char c)
44 {
45 #ifdef WIN32
46  return c == '-' || c == '/';
47 #else
48  return c == '-';
49 #endif
50 }
51 
52 enum class OptionsCategory {
53  OPTIONS,
54  CONNECTION,
55  WALLET,
57  ZMQ,
58  DEBUG_TEST,
60  NODE_RELAY,
62  RPC,
63  GUI,
64  COMMANDS,
67  IPC,
68 
69  HIDDEN // Always the last option to avoid printing these in the help
70 };
71 
72 struct KeyInfo {
73  std::string name;
74  std::string section;
75  bool negated{false};
76 };
77 
78 KeyInfo InterpretKey(std::string key);
79 
80 std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value,
81  unsigned int flags, std::string& error);
82 
83 struct SectionInfo {
84  std::string m_name;
85  std::string m_file;
86  int m_line;
87 };
88 
89 std::string SettingToString(const common::SettingsValue&, const std::string&);
90 std::optional<std::string> SettingToString(const common::SettingsValue&);
91 
92 int64_t SettingToInt(const common::SettingsValue&, int64_t);
93 std::optional<int64_t> SettingToInt(const common::SettingsValue&);
94 
95 bool SettingToBool(const common::SettingsValue&, bool);
96 std::optional<bool> SettingToBool(const common::SettingsValue&);
97 
99 {
100 public:
105  enum Flags : uint32_t {
106  ALLOW_ANY = 0x01,
107  // ALLOW_BOOL = 0x02, //!< unimplemented, draft implementation in #16545
108  // ALLOW_INT = 0x04, //!< unimplemented, draft implementation in #16545
109  // ALLOW_STRING = 0x08, //!< unimplemented, draft implementation in #16545
110  // ALLOW_LIST = 0x10, //!< unimplemented, draft implementation in #16545
113 
114  DEBUG_ONLY = 0x100,
115  /* Some options would cause cross-contamination if values for
116  * mainnet were used while running on regtest/testnet (or vice-versa).
117  * Setting them as NETWORK_ONLY ensures that sharing a config file
118  * between mainnet and regtest/testnet won't cause problems due to these
119  * parameters by accident. */
120  NETWORK_ONLY = 0x200,
121  // This argument's value is sensitive (such as a password).
122  SENSITIVE = 0x400,
123  COMMAND = 0x800,
124  };
125 
126 protected:
127  struct Arg
128  {
129  std::string m_help_param;
130  std::string m_help_text;
131  unsigned int m_flags;
132  };
133 
135  common::Settings m_settings GUARDED_BY(cs_args);
136  std::vector<std::string> m_command GUARDED_BY(cs_args);
137  std::string m_network GUARDED_BY(cs_args);
138  std::set<std::string> m_network_only_args GUARDED_BY(cs_args);
139  std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args GUARDED_BY(cs_args);
140  bool m_accept_any_command GUARDED_BY(cs_args){true};
141  std::list<SectionInfo> m_config_sections GUARDED_BY(cs_args);
142  std::optional<fs::path> m_config_path GUARDED_BY(cs_args);
143  mutable fs::path m_cached_blocks_path GUARDED_BY(cs_args);
144  mutable fs::path m_cached_datadir_path GUARDED_BY(cs_args);
145  mutable fs::path m_cached_network_datadir_path GUARDED_BY(cs_args);
146 
147  [[nodiscard]] bool ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys = false);
148 
154  bool UseDefaultSection(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(cs_args);
155 
156  public:
164  common::SettingsValue GetSetting(const std::string& arg) const;
165 
169  std::vector<common::SettingsValue> GetSettingsList(const std::string& arg) const;
170 
171  ArgsManager();
172  ~ArgsManager();
173 
177  void SelectConfigNetwork(const std::string& network);
178 
179  [[nodiscard]] bool ParseParameters(int argc, const char* const argv[], std::string& error);
180 
184  fs::path GetConfigFilePath() const;
186  [[nodiscard]] bool ReadConfigFiles(std::string& error, bool ignore_invalid_keys = false);
187 
194  std::set<std::string> GetUnsuitableSectionOnlyArgs() const;
195 
199  std::list<SectionInfo> GetUnrecognizedSections() const;
200 
201  struct Command {
203  std::string command;
208  std::vector<std::string> args;
209  };
213  std::optional<const Command> GetCommand() const;
214 
220  fs::path GetBlocksDirPath() const;
221 
227  fs::path GetDataDirBase() const { return GetDataDir(false); }
228 
234  fs::path GetDataDirNet() const { return GetDataDir(true); }
235 
239  void ClearPathCache();
240 
247  std::vector<std::string> GetArgs(const std::string& strArg) const;
248 
255  bool IsArgSet(const std::string& strArg) const;
256 
264  bool IsArgNegated(const std::string& strArg) const;
265 
273  std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
274  std::optional<std::string> GetArg(const std::string& strArg) const;
275 
286  fs::path GetPathArg(std::string arg, const fs::path& default_value = {}) const;
287 
295  int64_t GetIntArg(const std::string& strArg, int64_t nDefault) const;
296  std::optional<int64_t> GetIntArg(const std::string& strArg) const;
297 
305  bool GetBoolArg(const std::string& strArg, bool fDefault) const;
306  std::optional<bool> GetBoolArg(const std::string& strArg) const;
307 
315  bool SoftSetArg(const std::string& strArg, const std::string& strValue);
316 
324  bool SoftSetBoolArg(const std::string& strArg, bool fValue);
325 
326  // Forces an arg setting. Called by SoftSetArg() if the arg hasn't already
327  // been set. Also called directly in testing.
328  void ForceSetArg(const std::string& strArg, const std::string& strValue);
329 
335  ChainType GetChainType() const;
336 
342  std::string GetChainTypeString() const;
343 
347  void AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat);
348 
352  void AddCommand(const std::string& cmd, const std::string& help);
353 
357  void AddHiddenArgs(const std::vector<std::string>& args);
358 
362  void ClearArgs() {
363  LOCK(cs_args);
364  m_available_args.clear();
365  m_network_only_args.clear();
366  }
367 
373  void CheckMultipleCLIArgs() const;
374 
378  std::string GetHelpMessage() const;
379 
384  std::optional<unsigned int> GetArgFlags(const std::string& name) const;
385 
390  bool GetSettingsPath(fs::path* filepath = nullptr, bool temp = false, bool backup = false) const;
391 
395  bool ReadSettingsFile(std::vector<std::string>* errors = nullptr);
396 
401  bool WriteSettingsFile(std::vector<std::string>* errors = nullptr, bool backup = false) const;
402 
407  common::SettingsValue GetPersistentSetting(const std::string& name) const;
408 
412  template <typename Fn>
413  void LockSettings(Fn&& fn)
414  {
415  LOCK(cs_args);
416  fn(m_settings);
417  }
418 
423  void LogArgs() const;
424 
425 private:
432  fs::path GetDataDir(bool net_specific) const;
433 
440  std::variant<ChainType, std::string> GetChainArg() const;
441 
442  // Helper function for LogArgs().
443  void logArgsPrefix(
444  const std::string& prefix,
445  const std::string& section,
446  const std::map<std::string, std::vector<common::SettingsValue>>& args) const;
447 };
448 
449 extern ArgsManager gArgs;
450 
454 bool HelpRequested(const ArgsManager& args);
455 
458 
459 extern const std::vector<std::string> TEST_OPTIONS_DOC;
460 
462 bool HasTestOption(const ArgsManager& args, const std::string& test_option);
463 
470 std::string HelpMessageGroup(const std::string& message);
471 
479 std::string HelpMessageOpt(const std::string& option, const std::string& message);
480 
481 namespace common {
482 #ifdef WIN32
483 class WinCmdLineArgs
484 {
485 public:
486  WinCmdLineArgs();
487  ~WinCmdLineArgs();
488  std::pair<int, char**> get();
489 
490 private:
491  int argc;
492  char** argv;
493  std::vector<std::string> args;
494 };
495 #endif
496 } // namespace common
497 
498 #endif // BITCOIN_COMMON_ARGS_H
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false)
Definition: config.cpp:122
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:226
std::string section
Definition: args.h:74
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const
Return path argument or default value.
Definition: args.cpp:272
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:371
OptionsCategory
Definition: args.h:52
fs::path GetConfigFilePath() const
Return config file path (read-only)
Definition: args.cpp:761
std::string SettingToString(const common::SettingsValue &, const std::string &)
Definition: args.cpp:477
std::variant< ChainType, std::string > GetChainArg() const
Return -regtest/-signet/-testnet/-testnet4/-chain= setting as a ChainType enum if a recognized chain ...
Definition: args.cpp:788
Stored settings.
Definition: settings.h:32
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn&#39;t already have a value.
Definition: args.cpp:538
unsigned int m_flags
Definition: args.h:131
common::SettingsValue GetSetting(const std::string &arg) const
Get setting value.
Definition: args.cpp:825
const std::vector< std::string > TEST_OPTIONS_DOC
Definition: args.cpp:710
const char * prefix
Definition: rest.cpp:1009
void AddHiddenArgs(const std::vector< std::string > &args)
Add many hidden arguments.
Definition: args.cpp:585
int m_line
Definition: args.h:86
disallow -nofoo syntax
Definition: args.h:111
bool HelpRequested(const ArgsManager &args)
Definition: args.cpp:684
void SelectConfigNetwork(const std::string &network)
Select the network in use.
Definition: args.cpp:173
bool negated
Definition: args.h:75
const auto cmd
bool ReadConfigStream(std::istream &stream, const std::string &filepath, std::string &error, bool ignore_invalid_keys=false)
Definition: config.cpp:93
common::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:445
Flags
Flags controlling how config and command line arguments are validated and interpreted.
Definition: args.h:105
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:179
RecursiveMutex cs_args
Definition: args.h:134
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:507
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: args.cpp:546
ChainType GetChainType() const
Returns the appropriate chain type from the program arguments.
Definition: args.cpp:774
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn&#39;t already have a value.
Definition: args.cpp:530
void SetConfigFilePath(fs::path)
Definition: args.cpp:767
std::string m_name
Definition: args.h:84
disable validation
Definition: args.h:106
Definition: args.h:72
int64_t SettingToInt(const common::SettingsValue &, int64_t)
Definition: args.cpp:502
std::string GetHelpMessage() const
Get the help string.
Definition: args.cpp:609
fs::path GetDataDir(bool net_specific) const
Get data directory path.
Definition: args.cpp:307
fs::path GetDataDirBase() const
Get data directory path.
Definition: args.h:227
CRPCCommand m_command
Definition: interfaces.cpp:539
common::Settings m_settings GUARDED_BY(cs_args)
disallow -foo syntax that doesn&#39;t assign any value
Definition: args.h:112
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
bool ReadSettingsFile(std::vector< std::string > *errors=nullptr)
Read settings file.
Definition: args.cpp:402
std::string GetChainTypeString() const
Returns the appropriate chain type string from the program arguments.
Definition: args.cpp:781
KeyInfo InterpretKey(std::string key)
Parse "name", "section.name", "noname", "section.noname" settings keys.
Definition: args.cpp:79
void AddCommand(const std::string &cmd, const std::string &help)
Add subcommand.
Definition: args.cpp:552
bool IsArgNegated(const std::string &strArg) const
Return true if the argument was originally passed as a negated option, i.e.
Definition: args.cpp:452
ChainType
Definition: chaintype.h:11
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:234
ArgsManager & args
Definition: bitcoind.cpp:277
std::vector< common::SettingsValue > GetSettingsList(const std::string &arg) const
Get list of setting values.
Definition: args.cpp:833
#define LOCK(cs)
Definition: sync.h:257
const char * name
Definition: rest.cpp:49
fs::path GetBlocksDirPath() const
Get blocks directory path.
Definition: args.cpp:282
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: args.cpp:564
bool SettingToBool(const common::SettingsValue &, bool)
Definition: args.cpp:525
std::list< SectionInfo > GetUnrecognizedSections() const
Log warnings for unrecognized section names in the config file.
Definition: args.cpp:156
std::string m_file
Definition: args.h:85
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition: args.cpp:699
void CheckMultipleCLIArgs() const
Check CLI command args.
Definition: args.cpp:592
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: args.cpp:689
void ClearArgs()
Clear available arguments.
Definition: args.h:362
std::string m_help_text
Definition: args.h:130
std::optional< const Command > GetCommand() const
Get the command and command args (returns std::nullopt if no command provided)
Definition: args.cpp:342
Definition: args.cpp:868
int flags
Definition: bitcoin-tx.cpp:536
std::optional< unsigned int > GetArgFlags(const std::string &name) const
Return Flags for known arg.
Definition: args.cpp:260
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const
Write settings file or backup settings file.
Definition: args.cpp:425
const char *const BITCOIN_CONF_FILENAME
Definition: args.cpp:39
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:715
std::string name
Definition: args.h:73
bool m_accept_any_command GUARDED_BY(cs_args)
Definition: args.h:140
std::string command
The command (if one has been registered with AddCommand), or empty.
Definition: args.h:203
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:482
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:457
ArgsManager gArgs
Definition: args.cpp:42
void LogArgs() const
Log the config file options and the command line arguments, useful for troubleshooting.
Definition: args.cpp:856
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:703
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:208
const char *const BITCOIN_SETTINGS_FILENAME
Definition: args.cpp:40
void logArgsPrefix(const std::string &prefix, const std::string &section, const std::map< std::string, std::vector< common::SettingsValue >> &args) const
Definition: args.cpp:839
void LockSettings(Fn &&fn)
Access settings with lock held.
Definition: args.h:413
std::string m_help_param
Definition: args.h:129
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: args.cpp:362
void ClearPathCache()
Clear cached directory paths.
Definition: args.cpp:333
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:32
static RPCHelpMan help()
Definition: server.cpp:127
bool CheckDataDirOption(const ArgsManager &args)
Definition: args.cpp:755
bool IsSwitchChar(char c)
Definition: args.h:43
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:107
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:820
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:136