Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2023-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_COMMON_ARGS_H6#define BITCOIN_COMMON_ARGS_H8#include <common/settings.h>9#include <sync.h>10#include <util/chaintype.h>11#include <util/fs.h>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>25class ArgsManager;27extern const char * const BITCOIN_CONF_FILENAME;28extern const char * const BITCOIN_SETTINGS_FILENAME;30// Return true if -datadir option points to a valid directory or is not specified.31bool CheckDataDirOption(const ArgsManager& args);33/**34* Most paths passed as configuration arguments are treated as relative to35* the datadir if they are not absolute.36*37* @param args Parsed arguments and settings.38* @param path The path to be conditionally prefixed with datadir.39* @param net_specific Use network specific datadir variant40* @return The normalized path.41*/42fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific = true);44inline bool IsSwitchChar(char c)45{46#ifdef WIN3247return c == '-' || c == '/';48#else49return c == '-';50#endif51}53enum class OptionsCategory {54OPTIONS,55CONNECTION,56WALLET,57WALLET_DEBUG_TEST,58ZMQ,59DEBUG_TEST,60CHAINPARAMS,61NODE_RELAY,62BLOCK_CREATION,63RPC,64GUI,65COMMANDS,66REGISTER_COMMANDS,67CLI_COMMANDS,68IPC,70// Specific to one or more commands (OptionsCategory::COMMANDS)71// These are only included in help with their associated commands.72COMMAND_OPTIONS,74HIDDEN // Always the last option to avoid printing these in the help75};77struct KeyInfo {78std::string name;79std::string section;80bool negated{false};81};83KeyInfo InterpretKey(std::string key);85std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value,86unsigned int flags, std::string& error);88struct SectionInfo {89std::string m_name;90std::string m_file;91int m_line;92};94std::string SettingToString(const common::SettingsValue&, const std::string&);95std::optional<std::string> SettingToString(const common::SettingsValue&);97template <std::integral Int>98Int SettingTo(const common::SettingsValue&, Int);100template <std::integral Int>101std::optional<Int> SettingTo(const common::SettingsValue&);103bool SettingToBool(const common::SettingsValue&, bool);104std::optional<bool> SettingToBool(const common::SettingsValue&);106class ArgsManager107{108public:109/**110* Flags controlling how config and command line arguments are validated and111* interpreted.112*/113enum Flags : uint32_t {114ALLOW_ANY = 0x01, //!< disable validation115// ALLOW_BOOL = 0x02, //!< unimplemented, draft implementation in #16545116// ALLOW_INT = 0x04, //!< unimplemented, draft implementation in #16545117// ALLOW_STRING = 0x08, //!< unimplemented, draft implementation in #16545118// ALLOW_LIST = 0x10, //!< unimplemented, draft implementation in #16545119DISALLOW_NEGATION = 0x20, //!< disallow -nofoo syntax120DISALLOW_ELISION = 0x40, //!< disallow -foo syntax that doesn't assign any value122DEBUG_ONLY = 0x100,123/* Some options would cause cross-contamination if values for124* mainnet were used while running on regtest/testnet (or vice-versa).125* Setting them as NETWORK_ONLY ensures that sharing a config file126* between mainnet and regtest/testnet won't cause problems due to these127* parameters by accident. */128NETWORK_ONLY = 0x200,129// This argument's value is sensitive (such as a password).130SENSITIVE = 0x400,131COMMAND = 0x800,132};134private:135struct Arg136{137std::string m_help_param;138std::string m_help_text;139unsigned int m_flags;140};142mutable Mutex cs_args;143common::Settings m_settings GUARDED_BY(cs_args);144std::vector<std::string> m_command GUARDED_BY(cs_args);145std::string m_network GUARDED_BY(cs_args);146std::set<std::string> m_network_only_args GUARDED_BY(cs_args);147std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args GUARDED_BY(cs_args);148std::optional<unsigned int> m_default_flags GUARDED_BY(cs_args){};149std::map<std::string, std::set<std::string>> m_command_args GUARDED_BY(cs_args);150bool m_accept_any_command GUARDED_BY(cs_args){true};151std::list<SectionInfo> m_config_sections GUARDED_BY(cs_args);152std::optional<fs::path> m_config_path GUARDED_BY(cs_args);153mutable fs::path m_cached_blocks_path GUARDED_BY(cs_args);154mutable fs::path m_cached_datadir_path GUARDED_BY(cs_args);155mutable fs::path m_cached_network_datadir_path GUARDED_BY(cs_args);157/**158* Returns true if settings values from the default section should be used,159* depending on the current network and whether the setting is160* network-specific.161*/162bool UseDefaultSection(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(cs_args);164protected:165[[nodiscard]] bool ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);166[[nodiscard]] bool ReadConfigString(const std::string& str_config) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);168public:169/**170* Get setting value.171*172* Result will be null if setting was unset, true if "-setting" argument was passed173* false if "-nosetting" argument was passed, and a string if a "-setting=value"174* argument was passed.175*/176common::SettingsValue GetSetting(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);178/**179* Get list of setting values.180*/181std::vector<common::SettingsValue> GetSettingsList(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);183ArgsManager();184~ArgsManager();186/**187* Select the network in use188*/189void SelectConfigNetwork(const std::string& network) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);191[[nodiscard]] bool ParseParameters(int argc, const char* const argv[], std::string& error) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);193/**194* Return config file path (read-only)195*/196fs::path GetConfigFilePath() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);197void SetConfigFilePath(fs::path) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);198[[nodiscard]] bool ReadConfigFiles(std::string& error, bool ignore_invalid_keys = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_args);200/**201* Log warnings for options in m_section_only_args when202* they are specified in the default section but not overridden203* on the command line or in a network-specific section in the204* config file.205*/206std::set<std::string> GetUnsuitableSectionOnlyArgs() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);208/**209* Log warnings for unrecognized section names in the config file.210*/211std::list<SectionInfo> GetUnrecognizedSections() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);213struct Command {214/** The command (if one has been registered with AddCommand), or empty */215std::string command;216/**217* If command is non-empty: Any args that followed it218* If command is empty: The unregistered command and any args that followed it219*/220std::vector<std::string> args;221};222/**223* Get the command and command args (returns std::nullopt if no command provided)224*/225std::optional<const Command> GetCommand() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);227/**228* Check that any command-specific options the user specified are valid229* for the given command.230*231* @param[in] command The command being run.232* @param[out] errors If non-null, populated with a message for each invalid option.233* @return false if any command-specific options were specified that are not valid for this command234*/235bool CheckCommandOptions(const std::string& command, std::vector<std::string>* errors = nullptr) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);237/**238* Get blocks directory path239*240* @return Blocks path which is network specific241*/242fs::path GetBlocksDirPath() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);244/**245* Get data directory path246*247* @return Absolute path on success, otherwise an empty path when a non-directory path would be returned248*/249fs::path GetDataDirBase() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args);
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.