Coverage Report

Created: 2026-09-01 13:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/common/args.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <common/args.h>
7
8
#include <chainparamsbase.h>
9
#include <common/settings.h>
10
#include <sync.h>
11
#include <tinyformat.h>
12
#include <univalue.h>
13
#include <util/chaintype.h>
14
#include <util/check.h>
15
#include <util/fs.h>
16
#include <util/fs_helpers.h>
17
#include <util/log.h>
18
#include <util/strencodings.h>
19
#include <util/string.h>
20
21
#ifdef WIN32
22
#include <shlobj.h>
23
#endif
24
25
#include <algorithm>
26
#include <cstdlib>
27
#include <cstring>
28
#include <map>
29
#include <optional>
30
#include <stdexcept>
31
#include <string>
32
#include <utility>
33
#include <variant>
34
35
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
36
const char * const BITCOIN_SETTINGS_FILENAME = "settings.json";
37
38
ArgsManager gArgs;
39
40
/**
41
 * Interpret a string argument as a boolean.
42
 *
43
 * The definition of LocaleIndependentAtoi<int>() requires that non-numeric string values
44
 * like "foo", return 0. This means that if a user unintentionally supplies a
45
 * non-integer argument here, the return value is always false. This means that
46
 * -foo=false does what the user probably expects, but -foo=true is well defined
47
 * but does not do what they probably expected.
48
 *
49
 * The return value of LocaleIndependentAtoi<int>(...) is zero when given input not
50
 * representable as an int.
51
 *
52
 * For a more extensive discussion of this topic (and a wide range of opinions
53
 * on the Right Way to change this code), see PR12713.
54
 */
55
static bool InterpretBool(const std::string& strValue)
56
15.6k
{
57
15.6k
    if (strValue.empty())
  Branch (57:9): [True: 4.44k, False: 11.1k]
58
4.44k
        return true;
59
11.1k
    return (LocaleIndependentAtoi<int>(strValue) != 0);
60
15.6k
}
61
62
static std::string SettingName(const std::string& arg)
63
1.28M
{
64
1.28M
    return arg.size() > 0 && arg[0] == '-' ? arg.substr(1) : arg;
  Branch (64:12): [True: 1.27M, False: 14.8k]
  Branch (64:30): [True: 1.22M, False: 45.4k]
65
1.28M
}
66
67
/**
68
 * Parse "name", "section.name", "noname", "section.noname" settings keys.
69
 *
70
 * @note Where an option was negated can be later checked using the
71
 * IsArgNegated() method. One use case for this is to have a way to disable
72
 * options that are not normally boolean (e.g. using -nodebuglogfile to request
73
 * that debug log output is not sent to any file at all).
74
 */
75
KeyInfo InterpretKey(std::string key)
76
53.9k
{
77
53.9k
    KeyInfo result;
78
    // Split section name from key name for keys like "testnet.foo" or "regtest.bar"
79
53.9k
    size_t option_index = key.find('.');
80
53.9k
    if (option_index != std::string::npos) {
  Branch (80:9): [True: 1.74k, False: 52.2k]
81
1.74k
        result.section = key.substr(0, option_index);
82
1.74k
        key.erase(0, option_index + 1);
83
1.74k
    }
84
53.9k
    if (key.starts_with("no")) {
  Branch (84:9): [True: 13.7k, False: 40.2k]
85
13.7k
        key.erase(0, 2);
86
13.7k
        result.negated = true;
87
13.7k
    }
88
53.9k
    result.name = key;
89
53.9k
    return result;
90
53.9k
}
91
92
/**
93
 * Interpret settings value based on registered flags.
94
 *
95
 * @param[in]   key      key information to know if key was negated
96
 * @param[in]   value    string value of setting to be parsed
97
 * @param[in]   flags    ArgsManager registered argument flags
98
 * @param[out]  error    Error description if settings value is not valid
99
 *
100
 * @return parsed settings value if it is valid, otherwise nullopt accompanied
101
 * by a descriptive error string
102
 */
103
std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value,
104
                                                  unsigned int flags, std::string& error)
105
47.1k
{
106
    // Return negated settings as false values.
107
47.1k
    if (key.negated) {
  Branch (107:9): [True: 12.2k, False: 34.8k]
108
12.2k
        if (flags & ArgsManager::DISALLOW_NEGATION) {
  Branch (108:13): [True: 322, False: 11.9k]
109
322
            error = strprintf("Negating of -%s is meaningless and therefore forbidden", key.name);
110
322
            return std::nullopt;
111
322
        }
112
        // Double negatives like -nofoo=0 are supported (but discouraged)
113
11.9k
        if (value && !InterpretBool(*value)) {
  Branch (113:13): [True: 9.34k, False: 2.62k]
  Branch (113:22): [True: 3.75k, False: 5.59k]
114
3.75k
            LogWarning("Parsed potentially confusing double-negative -%s=%s", key.name, *value);
115
3.75k
            return true;
116
3.75k
        }
117
8.21k
        return false;
118
11.9k
    }
119
34.8k
    if (!value && (flags & ArgsManager::DISALLOW_ELISION)) {
  Branch (119:9): [True: 23.6k, False: 11.1k]
  Branch (119:19): [True: 1.04k, False: 22.6k]
120
1.04k
        error = strprintf("Can not set -%s with no value. Please specify value with -%s=value.", key.name, key.name);
121
1.04k
        return std::nullopt;
122
1.04k
    }
123
33.8k
    return value ? *value : "";
  Branch (123:12): [True: 11.1k, False: 22.6k]
124
34.8k
}
125
126
// Define default constructor and destructor that are not inline, so code instantiating this class doesn't need to
127
// #include class definitions for all members.
128
// For example, m_settings has an internal dependency on univalue.
129
2.08k
ArgsManager::ArgsManager() = default;
130
2.18k
ArgsManager::~ArgsManager() = default;
131
132
std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const
133
2.08k
{
134
2.08k
    std::set<std::string> unsuitables;
135
136
2.08k
    LOCK(cs_args);
137
138
    // if there's no section selected, don't worry
139
2.08k
    if (m_network.empty()) return std::set<std::string> {};
  Branch (139:9): [True: 447, False: 1.63k]
140
141
    // if it's okay to use the default section for this network, don't worry
142
1.63k
    if (m_network == ChainTypeToString(ChainType::MAIN)) return std::set<std::string> {};
  Branch (142:9): [True: 4, False: 1.63k]
143
144
13.8k
    for (const auto& arg : m_network_only_args) {
  Branch (144:26): [True: 13.8k, False: 1.63k]
145
13.8k
        if (OnlyHasDefaultSectionSetting(m_settings, m_network, SettingName(arg))) {
  Branch (145:13): [True: 0, False: 13.8k]
146
0
            unsuitables.insert(arg);
147
0
        }
148
13.8k
    }
149
1.63k
    return unsuitables;
150
1.63k
}
151
152
std::list<SectionInfo> ArgsManager::GetUnrecognizedSections() const
153
2.08k
{
154
    // Section names to be recognized in the config file.
155
2.08k
    static const std::set<std::string> available_sections{
156
2.08k
        ChainTypeToString(ChainType::REGTEST),
157
2.08k
        ChainTypeToString(ChainType::SIGNET),
158
2.08k
        ChainTypeToString(ChainType::TESTNET),
159
2.08k
        ChainTypeToString(ChainType::TESTNET4),
160
2.08k
        ChainTypeToString(ChainType::MAIN),
161
2.08k
    };
162
163
2.08k
    LOCK(cs_args);
164
2.08k
    std::list<SectionInfo> unrecognized = m_config_sections;
165
2.08k
    unrecognized.remove_if([](const SectionInfo& appeared){ return available_sections.contains(appeared.m_name); });
166
2.08k
    return unrecognized;
167
2.08k
}
168
169
void ArgsManager::SelectConfigNetwork(const std::string& network)
170
39.6k
{
171
39.6k
    LOCK(cs_args);
172
39.6k
    m_network = network;
173
39.6k
}
174
175
bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::string& error)
176
53.9k
{
177
53.9k
    LOCK(cs_args);
178
53.9k
    m_settings.command_line_options.clear();
179
180
99.7k
    for (int i = 1; i < argc; i++) {
  Branch (180:21): [True: 86.8k, False: 12.9k]
181
86.8k
        std::string key(argv[i]);
182
183
#ifdef __APPLE__
184
        // At the first time when a user gets the "App downloaded from the
185
        // internet" warning, and clicks the Open button, macOS passes
186
        // a unique process serial number (PSN) as -psn_... command-line
187
        // argument, which we filter out.
188
        if (key.starts_with("-psn_")) continue;
189
#endif
190
191
86.8k
        if (key == "-") break; //bitcoin-tx using stdin
  Branch (191:13): [True: 719, False: 86.1k]
192
86.1k
        std::optional<std::string> val;
193
86.1k
        size_t is_index = key.find('=');
194
86.1k
        if (is_index != std::string::npos) {
  Branch (194:13): [True: 24.3k, False: 61.7k]
195
24.3k
            val = key.substr(is_index + 1);
196
24.3k
            key.erase(is_index);
197
24.3k
        }
198
#ifdef WIN32
199
        key = ToLower(key);
200
        if (key[0] == '/')
201
            key[0] = '-';
202
#endif
203
204
86.1k
        if (key[0] != '-') {
  Branch (204:13): [True: 32.1k, False: 53.9k]
205
32.1k
            if (!m_accept_any_command && m_command.empty()) {
  Branch (205:17): [True: 12.0k, False: 20.0k]
  Branch (205:42): [True: 1.87k, False: 10.2k]
206
                // The first non-dash arg is a registered command
207
1.87k
                std::optional<unsigned int> flags = GetArgFlags_(key);
208
1.87k
                if (!flags || !(*flags & ArgsManager::COMMAND)) {
  Branch (208:21): [True: 1.24k, False: 625]
  Branch (208:31): [True: 598, False: 27]
209
1.84k
                    error = strprintf("Invalid command '%s'", argv[i]);
210
1.84k
                    return false;
211
1.84k
                }
212
1.87k
            }
213
30.3k
            m_command.push_back(key);
214
95.7k
            while (++i < argc) {
  Branch (214:20): [True: 65.3k, False: 30.3k]
215
                // The remaining args are command args
216
65.3k
                m_command.emplace_back(argv[i]);
217
65.3k
            }
218
30.3k
            break;
219
32.1k
        }
220
221
        // Transform --foo to -foo
222
53.9k
        if (key.length() > 1 && key[1] == '-')
  Branch (222:13): [True: 49.6k, False: 4.33k]
  Branch (222:33): [True: 4.19k, False: 45.4k]
223
4.19k
            key.erase(0, 1);
224
225
        // Transform -foo to foo
226
53.9k
        key.erase(0, 1);
227
53.9k
        KeyInfo keyinfo = InterpretKey(key);
228
53.9k
        std::optional<unsigned int> flags = GetArgFlags_('-' + keyinfo.name);
229
230
        // Unknown command line options and command line options with dot
231
        // characters (which are returned from InterpretKey with nonempty
232
        // section strings) are not valid.
233
53.9k
        if (!flags || !keyinfo.section.empty()) {
  Branch (233:13): [True: 6.37k, False: 47.6k]
  Branch (233:23): [True: 441, False: 47.1k]
234
6.81k
            error = strprintf("Invalid parameter %s", argv[i]);
235
6.81k
            return false;
236
6.81k
        }
237
238
47.1k
        std::optional<common::SettingsValue> value = InterpretValue(keyinfo, val ? &*val : nullptr, *flags, error);
  Branch (238:78): [True: 20.7k, False: 26.4k]
239
47.1k
        if (!value) return false;
  Branch (239:13): [True: 1.36k, False: 45.7k]
240
241
45.7k
        m_settings.command_line_options[keyinfo.name].push_back(*value);
242
45.7k
    }
243
244
    // we do not allow -includeconf from command line, only -noincludeconf
245
43.9k
    if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) {
  Branch (245:15): [True: 1.98k, False: 41.9k]
246
1.98k
        const common::SettingsSpan values{*includes};
247
        // Range may be empty if -noincludeconf was passed
248
1.98k
        if (!values.empty()) {
  Branch (248:13): [True: 1.89k, False: 92]
249
1.89k
            error = "-includeconf cannot be used from commandline; -includeconf=" + values.begin()->write();
250
1.89k
            return false; // pick first value as example
251
1.89k
        }
252
1.98k
    }
253
42.0k
    return true;
254
43.9k
}
255
256
std::optional<unsigned int> ArgsManager::GetArgFlags_(const std::string& name) const
257
166k
{
258
166k
    AssertLockHeld(cs_args);
259
391k
    for (const auto& arg_map : m_available_args) {
  Branch (259:30): [True: 391k, False: 68.0k]
260
391k
        const auto search = arg_map.second.find(name);
261
391k
        if (search != arg_map.second.end()) {
  Branch (261:13): [True: 98.4k, False: 293k]
262
98.4k
            return search->second.m_flags;
263
98.4k
        }
264
391k
    }
265
68.0k
    return m_default_flags;
266
166k
}
267
268
std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const
269
110k
{
270
110k
    LOCK(cs_args);
271
110k
    return GetArgFlags_(name);
272
110k
}
273
274
void ArgsManager::SetDefaultFlags(std::optional<unsigned int> flags)
275
0
{
276
0
    LOCK(cs_args);
277
0
    m_default_flags = flags;
278
0
}
279
280
fs::path ArgsManager::GetPathArg_(std::string arg, const fs::path& default_value) const
281
6.88k
{
282
6.88k
    AssertLockHeld(cs_args);
283
6.88k
    const auto value = GetSetting_(arg);
284
6.88k
    if (value.isFalse()) return {};
  Branch (284:9): [True: 0, False: 6.88k]
285
6.88k
    std::string path_str = SettingToString(value, "");
286
6.88k
    if (path_str.empty()) return default_value;
  Branch (286:9): [True: 1.37k, False: 5.50k]
287
5.50k
    fs::path result = fs::PathFromString(path_str).lexically_normal();
288
    // Remove trailing slash, if present.
289
5.50k
    return result.has_filename() ? result : result.parent_path();
  Branch (289:12): [True: 5.50k, False: 0]
290
6.88k
}
291
292
fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const
293
1.37k
{
294
1.37k
    LOCK(cs_args);
295
1.37k
    return GetPathArg_(std::move(arg), default_value);
296
1.37k
}
297
298
fs::path ArgsManager::GetBlocksDirPath() const
299
5.89k
{
300
5.89k
    LOCK(cs_args);
301
5.89k
    fs::path& path = m_cached_blocks_path;
302
303
    // Cache the path to avoid calling fs::create_directories on every call of
304
    // this function
305
5.89k
    if (!path.empty()) return path;
  Branch (305:9): [True: 3.14k, False: 2.75k]
306
307
2.75k
    if (!GetSetting_("-blocksdir").isNull()) {
  Branch (307:9): [True: 0, False: 2.75k]
308
0
        path = fs::absolute(GetPathArg_("-blocksdir"));
309
0
        if (!fs::is_directory(path)) {
  Branch (309:13): [True: 0, False: 0]
310
0
            path = "";
311
0
            return path;
312
0
        }
313
2.75k
    } else {
314
2.75k
        path = GetDataDir(/*net_specific=*/false);
315
2.75k
    }
316
317
2.75k
    path /= fs::PathFromString(BaseParams().DataDir());
318
2.75k
    path /= "blocks";
319
2.75k
    fs::create_directories(path);
320
2.75k
    return path;
321
2.75k
}
322
323
0
fs::path ArgsManager::GetDataDirBase() const {
324
0
    LOCK(cs_args);
325
0
    return GetDataDir(/*net_specific=*/false);
326
0
}
327
328
26.2k
fs::path ArgsManager::GetDataDirNet() const {
329
26.2k
    LOCK(cs_args);
330
26.2k
    return GetDataDir(/*net_specific=*/true);
331
26.2k
}
332
333
fs::path ArgsManager::GetDataDir(bool net_specific) const
334
28.9k
{
335
28.9k
    AssertLockHeld(cs_args);
336
28.9k
    fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
  Branch (336:22): [True: 26.2k, False: 2.75k]
337
338
    // Used cached path if available
339
28.9k
    if (!path.empty()) return path;
  Branch (339:9): [True: 23.4k, False: 5.50k]
340
341
5.50k
    const fs::path datadir{GetPathArg_("-datadir")};
342
5.50k
    if (!datadir.empty()) {
  Branch (342:9): [True: 5.50k, False: 0]
343
5.50k
        path = fs::absolute(datadir);
344
5.50k
        if (!fs::is_directory(path)) {
  Branch (344:13): [True: 0, False: 5.50k]
345
0
            path = "";
346
0
            return path;
347
0
        }
348
5.50k
    } else {
349
0
        path = GetDefaultDataDir();
350
0
    }
351
352
5.50k
    if (net_specific && !BaseParams().DataDir().empty()) {
  Branch (352:9): [True: 2.75k, False: 2.75k]
  Branch (352:25): [True: 2.75k, False: 0]
353
2.75k
        path /= fs::PathFromString(BaseParams().DataDir());
354
2.75k
    }
355
356
5.50k
    return path;
357
5.50k
}
358
359
void ArgsManager::ClearPathCache()
360
1.37k
{
361
1.37k
    LOCK(cs_args);
362
363
1.37k
    m_cached_datadir_path = fs::path();
364
1.37k
    m_cached_network_datadir_path = fs::path();
365
1.37k
    m_cached_blocks_path = fs::path();
366
1.37k
}
367
368
std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const
369
710
{
370
710
    Command ret;
371
710
    LOCK(cs_args);
372
710
    auto it = m_command.begin();
373
710
    if (it == m_command.end()) {
  Branch (373:9): [True: 456, False: 254]
374
        // No command was passed
375
456
        return std::nullopt;
376
456
    }
377
254
    if (!m_accept_any_command) {
  Branch (377:9): [True: 83, False: 171]
378
        // The registered command
379
83
        ret.command = *(it++);
380
83
    }
381
95.8k
    while (it != m_command.end()) {
  Branch (381:12): [True: 95.6k, False: 254]
382
        // The unregistered command and args (if any)
383
95.6k
        ret.args.push_back(*(it++));
384
95.6k
    }
385
254
    return ret;
386
710
}
387
388
bool ArgsManager::CheckCommandOptions(const std::string& command, std::vector<std::string>* errors) const
389
254
{
390
254
    LOCK(cs_args);
391
392
254
    auto command_options = m_available_args.find(OptionsCategory::COMMAND_OPTIONS);
393
254
    if (command_options == m_available_args.end()) {
  Branch (393:9): [True: 209, False: 45]
394
        // There are no command-specific options at all, so everything is fine
395
209
        return true;
396
209
    }
397
398
45
    const auto command_args = m_command_args.find(command);
399
1.03k
    auto is_valid_opt = [&](const auto& opt) EXCLUSIVE_LOCKS_REQUIRED(cs_args) -> bool {
400
1.03k
        if (command_args == m_command_args.end()) {
  Branch (400:13): [True: 418, False: 620]
401
            // Caller may not have checked that command actually exists
402
            // before calling this function.  In that case, treat it as
403
            // having no valid command-specific options.
404
418
            return false;
405
620
        } else {
406
620
            return command_args->second.contains(opt);
407
620
        }
408
1.03k
    };
409
410
45
    bool ok = true;
411
2.05k
    for (const auto& [arg, _] : command_options->second) {
  Branch (411:31): [True: 2.05k, False: 45]
412
2.05k
        if (!GetSetting_(arg).isNull() && !is_valid_opt(arg)) {
  Branch (412:13): [True: 1.03k, False: 1.01k]
  Branch (412:13): [True: 736, False: 1.32k]
  Branch (412:43): [True: 736, False: 302]
413
736
            ok = false;
414
736
            if (errors != nullptr) {
  Branch (414:17): [True: 0, False: 736]
415
0
                errors->emplace_back(strprintf("The %s option cannot be used with the '%s' command.", arg, command));
416
0
            }
417
736
        }
418
2.05k
    }
419
45
    return ok;
420
254
}
421
422
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
423
229k
{
424
229k
    std::vector<std::string> result;
425
229k
    for (const common::SettingsValue& value : GetSettingsList(strArg)) {
  Branch (425:45): [True: 5.87k, False: 229k]
426
5.87k
        result.push_back(value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str());
  Branch (426:26): [True: 0, False: 5.87k]
  Branch (426:50): [True: 28, False: 5.84k]
427
5.87k
    }
428
229k
    return result;
429
229k
}
430
431
bool ArgsManager::IsArgSet(const std::string& strArg) const
432
14.9k
{
433
14.9k
    return !GetSetting(strArg).isNull();
434
14.9k
}
435
436
bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const
437
0
{
438
0
    fs::path settings = GetPathArg("-settings", BITCOIN_SETTINGS_FILENAME);
439
0
    if (settings.empty()) {
  Branch (439:9): [True: 0, False: 0]
440
0
        return false;
441
0
    }
442
0
    if (backup) {
  Branch (442:9): [True: 0, False: 0]
443
0
        settings += ".bak";
444
0
    }
445
0
    if (filepath) {
  Branch (445:9): [True: 0, False: 0]
446
0
        *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings);
  Branch (446:60): [True: 0, False: 0]
447
0
    }
448
0
    return true;
449
0
}
450
451
static void SaveErrors(const std::vector<std::string> errors, std::vector<std::string>* error_out)
452
0
{
453
0
    for (const auto& error : errors) {
  Branch (453:28): [True: 0, False: 0]
454
0
        if (error_out) {
  Branch (454:13): [True: 0, False: 0]
455
0
            error_out->emplace_back(error);
456
0
        } else {
457
0
            LogWarning("%s", error);
458
0
        }
459
0
    }
460
0
}
461
462
bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors)
463
0
{
464
0
    fs::path path;
465
0
    if (!GetSettingsPath(&path, /* temp= */ false)) {
  Branch (465:9): [True: 0, False: 0]
466
0
        return true; // Do nothing if settings file disabled.
467
0
    }
468
469
0
    LOCK(cs_args);
470
0
    m_settings.rw_settings.clear();
471
0
    std::vector<std::string> read_errors;
472
0
    if (!common::ReadSettings(path, m_settings.rw_settings, read_errors)) {
  Branch (472:9): [True: 0, False: 0]
473
0
        SaveErrors(read_errors, errors);
474
0
        return false;
475
0
    }
476
0
    for (const auto& setting : m_settings.rw_settings) {
  Branch (476:30): [True: 0, False: 0]
477
0
        KeyInfo key = InterpretKey(setting.first); // Split setting key into section and argname
478
0
        if (!GetArgFlags_('-' + key.name)) {
  Branch (478:13): [True: 0, False: 0]
479
0
            LogWarning("Ignoring unknown rw_settings value %s", setting.first);
480
0
        }
481
0
    }
482
0
    return true;
483
0
}
484
485
bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors, bool backup) const
486
0
{
487
0
    fs::path path, path_tmp;
488
0
    if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) {
  Branch (488:9): [True: 0, False: 0]
  Branch (488:60): [True: 0, False: 0]
489
0
        throw std::logic_error("Attempt to write settings file when dynamic settings are disabled.");
490
0
    }
491
492
0
    LOCK(cs_args);
493
0
    std::vector<std::string> write_errors;
494
0
    if (!common::WriteSettings(path_tmp, m_settings.rw_settings, write_errors)) {
  Branch (494:9): [True: 0, False: 0]
495
0
        SaveErrors(write_errors, errors);
496
0
        return false;
497
0
    }
498
0
    if (!RenameOver(path_tmp, path)) {
  Branch (498:9): [True: 0, False: 0]
499
0
        SaveErrors({strprintf("Failed renaming settings file %s to %s\n", fs::PathToString(path_tmp), fs::PathToString(path))}, errors);
500
0
        return false;
501
0
    }
502
0
    return true;
503
0
}
504
505
common::SettingsValue ArgsManager::GetPersistentSetting(const std::string& name) const
506
0
{
507
0
    LOCK(cs_args);
508
0
    return common::GetSetting(m_settings, m_network, name, !UseDefaultSection("-" + name),
509
0
        /*ignore_nonpersistent=*/true, /*get_chain_type=*/false);
510
0
}
511
512
bool ArgsManager::IsArgNegated(const std::string& strArg) const
513
2.08k
{
514
2.08k
    return GetSetting(strArg).isFalse();
515
2.08k
}
516
517
std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
518
15.2k
{
519
15.2k
    return GetArg(strArg).value_or(strDefault);
520
15.2k
}
521
522
std::optional<std::string> ArgsManager::GetArg(const std::string& strArg) const
523
82.4k
{
524
82.4k
    const common::SettingsValue value = GetSetting(strArg);
525
82.4k
    return SettingToString(value);
526
82.4k
}
527
528
std::optional<std::string> SettingToString(const common::SettingsValue& value)
529
89.3k
{
530
89.3k
    if (value.isNull()) return std::nullopt;
  Branch (530:9): [True: 83.5k, False: 5.80k]
531
5.80k
    if (value.isFalse()) return "0";
  Branch (531:9): [True: 8, False: 5.79k]
532
5.79k
    if (value.isTrue()) return "1";
  Branch (532:9): [True: 9, False: 5.78k]
533
5.78k
    if (value.isNum()) return value.getValStr();
  Branch (533:9): [True: 0, False: 5.78k]
534
5.78k
    return value.get_str();
535
5.78k
}
536
537
std::string SettingToString(const common::SettingsValue& value, const std::string& strDefault)
538
6.88k
{
539
6.88k
    return SettingToString(value).value_or(strDefault);
540
6.88k
}
541
542
template <std::integral Int>
543
Int ArgsManager::GetArg(const std::string& strArg, Int nDefault) const
544
605k
{
545
605k
    return GetArg<Int>(strArg).value_or(nDefault);
546
605k
}
Unexecuted instantiation: _ZNK11ArgsManager6GetArgITkSt8integralaEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES1_
Unexecuted instantiation: _ZNK11ArgsManager6GetArgITkSt8integralhEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES1_
Unexecuted instantiation: _ZNK11ArgsManager6GetArgITkSt8integralsEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES1_
Unexecuted instantiation: _ZNK11ArgsManager6GetArgITkSt8integraltEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES1_
_ZNK11ArgsManager6GetArgITkSt8integraliEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES1_
Line
Count
Source
544
1.37k
{
545
1.37k
    return GetArg<Int>(strArg).value_or(nDefault);
546
1.37k
}
Unexecuted instantiation: _ZNK11ArgsManager6GetArgITkSt8integraljEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES1_
_ZNK11ArgsManager6GetArgITkSt8integrallEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES1_
Line
Count
Source
544
604k
{
545
604k
    return GetArg<Int>(strArg).value_or(nDefault);
546
604k
}
Unexecuted instantiation: _ZNK11ArgsManager6GetArgITkSt8integralmEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES1_
547
548
template <std::integral Int>
549
std::optional<Int> ArgsManager::GetArg(const std::string& strArg) const
550
680k
{
551
680k
    const common::SettingsValue value = GetSetting(strArg);
552
680k
    return SettingTo<Int>(value);
553
680k
}
Unexecuted instantiation: _ZNK11ArgsManager6GetArgITkSt8integralaEESt8optionalIT_ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZNK11ArgsManager6GetArgITkSt8integralhEESt8optionalIT_ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZNK11ArgsManager6GetArgITkSt8integralsEESt8optionalIT_ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZNK11ArgsManager6GetArgITkSt8integraltEESt8optionalIT_ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
_ZNK11ArgsManager6GetArgITkSt8integraliEESt8optionalIT_ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
550
2.75k
{
551
2.75k
    const common::SettingsValue value = GetSetting(strArg);
552
2.75k
    return SettingTo<Int>(value);
553
2.75k
}
Unexecuted instantiation: _ZNK11ArgsManager6GetArgITkSt8integraljEESt8optionalIT_ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
_ZNK11ArgsManager6GetArgITkSt8integrallEESt8optionalIT_ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
550
675k
{
551
675k
    const common::SettingsValue value = GetSetting(strArg);
552
675k
    return SettingTo<Int>(value);
553
675k
}
_ZNK11ArgsManager6GetArgITkSt8integralmEESt8optionalIT_ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
550
2.75k
{
551
2.75k
    const common::SettingsValue value = GetSetting(strArg);
552
2.75k
    return SettingTo<Int>(value);
553
2.75k
}
554
555
template <std::integral Int>
556
std::optional<Int> SettingTo(const common::SettingsValue& value)
557
680k
{
558
680k
    if (value.isNull()) return std::nullopt;
  Branch (558:9): [True: 0, False: 0]
  Branch (558:9): [True: 0, False: 0]
  Branch (558:9): [True: 0, False: 0]
  Branch (558:9): [True: 0, False: 0]
  Branch (558:9): [True: 2.75k, False: 0]
  Branch (558:9): [True: 0, False: 0]
  Branch (558:9): [True: 647k, False: 27.9k]
  Branch (558:9): [True: 2.75k, False: 0]
559
27.9k
    if (value.isFalse()) return 0;
  Branch (559:9): [True: 0, False: 0]
  Branch (559:9): [True: 0, False: 0]
  Branch (559:9): [True: 0, False: 0]
  Branch (559:9): [True: 0, False: 0]
  Branch (559:9): [True: 0, False: 0]
  Branch (559:9): [True: 0, False: 0]
  Branch (559:9): [True: 8, False: 27.9k]
  Branch (559:9): [True: 0, False: 0]
560
27.9k
    if (value.isTrue()) return 1;
  Branch (560:9): [True: 0, False: 0]
  Branch (560:9): [True: 0, False: 0]
  Branch (560:9): [True: 0, False: 0]
  Branch (560:9): [True: 0, False: 0]
  Branch (560:9): [True: 0, False: 0]
  Branch (560:9): [True: 0, False: 0]
  Branch (560:9): [True: 9, False: 27.9k]
  Branch (560:9): [True: 0, False: 0]
561
27.9k
    if (value.isNum()) return value.getInt<Int>();
  Branch (561:9): [True: 0, False: 0]
  Branch (561:9): [True: 0, False: 0]
  Branch (561:9): [True: 0, False: 0]
  Branch (561:9): [True: 0, False: 0]
  Branch (561:9): [True: 0, False: 0]
  Branch (561:9): [True: 0, False: 0]
  Branch (561:9): [True: 0, False: 27.9k]
  Branch (561:9): [True: 0, False: 0]
562
27.9k
    return LocaleIndependentAtoi<Int>(value.get_str());
563
27.9k
}
Unexecuted instantiation: _Z9SettingToITkSt8integralaESt8optionalIT_ERK8UniValue
Unexecuted instantiation: _Z9SettingToITkSt8integralhESt8optionalIT_ERK8UniValue
Unexecuted instantiation: _Z9SettingToITkSt8integralsESt8optionalIT_ERK8UniValue
Unexecuted instantiation: _Z9SettingToITkSt8integraltESt8optionalIT_ERK8UniValue
_Z9SettingToITkSt8integraliESt8optionalIT_ERK8UniValue
Line
Count
Source
557
2.75k
{
558
2.75k
    if (value.isNull()) return std::nullopt;
  Branch (558:9): [True: 2.75k, False: 0]
559
0
    if (value.isFalse()) return 0;
  Branch (559:9): [True: 0, False: 0]
560
0
    if (value.isTrue()) return 1;
  Branch (560:9): [True: 0, False: 0]
561
0
    if (value.isNum()) return value.getInt<Int>();
  Branch (561:9): [True: 0, False: 0]
562
0
    return LocaleIndependentAtoi<Int>(value.get_str());
563
0
}
Unexecuted instantiation: _Z9SettingToITkSt8integraljESt8optionalIT_ERK8UniValue
_Z9SettingToITkSt8integrallESt8optionalIT_ERK8UniValue
Line
Count
Source
557
675k
{
558
675k
    if (value.isNull()) return std::nullopt;
  Branch (558:9): [True: 647k, False: 27.9k]
559
27.9k
    if (value.isFalse()) return 0;
  Branch (559:9): [True: 8, False: 27.9k]
560
27.9k
    if (value.isTrue()) return 1;
  Branch (560:9): [True: 9, False: 27.9k]
561
27.9k
    if (value.isNum()) return value.getInt<Int>();
  Branch (561:9): [True: 0, False: 27.9k]
562
27.9k
    return LocaleIndependentAtoi<Int>(value.get_str());
563
27.9k
}
_Z9SettingToITkSt8integralmESt8optionalIT_ERK8UniValue
Line
Count
Source
557
2.75k
{
558
2.75k
    if (value.isNull()) return std::nullopt;
  Branch (558:9): [True: 2.75k, False: 0]
559
0
    if (value.isFalse()) return 0;
  Branch (559:9): [True: 0, False: 0]
560
0
    if (value.isTrue()) return 1;
  Branch (560:9): [True: 0, False: 0]
561
0
    if (value.isNum()) return value.getInt<Int>();
  Branch (561:9): [True: 0, False: 0]
562
0
    return LocaleIndependentAtoi<Int>(value.get_str());
563
0
}
564
565
template <std::integral Int>
566
Int SettingTo(const common::SettingsValue& value, Int nDefault)
567
0
{
568
0
    return SettingTo<Int>(value).value_or(nDefault);
569
0
}
Unexecuted instantiation: _Z9SettingToITkSt8integralaET_RK8UniValueS0_
Unexecuted instantiation: _Z9SettingToITkSt8integralhET_RK8UniValueS0_
Unexecuted instantiation: _Z9SettingToITkSt8integralsET_RK8UniValueS0_
Unexecuted instantiation: _Z9SettingToITkSt8integraltET_RK8UniValueS0_
Unexecuted instantiation: _Z9SettingToITkSt8integraliET_RK8UniValueS0_
Unexecuted instantiation: _Z9SettingToITkSt8integraljET_RK8UniValueS0_
Unexecuted instantiation: _Z9SettingToITkSt8integrallET_RK8UniValueS0_
Unexecuted instantiation: _Z9SettingToITkSt8integralmET_RK8UniValueS0_
570
571
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
572
125k
{
573
125k
    return GetBoolArg(strArg).value_or(fDefault);
574
125k
}
575
576
std::optional<bool> ArgsManager::GetBoolArg(const std::string& strArg) const
577
134k
{
578
134k
    const common::SettingsValue value = GetSetting(strArg);
579
134k
    return SettingToBool(value);
580
134k
}
581
582
std::optional<bool> SettingToBool(const common::SettingsValue& value)
583
134k
{
584
134k
    if (value.isNull()) return std::nullopt;
  Branch (584:9): [True: 128k, False: 6.26k]
585
6.26k
    if (value.isBool()) return value.get_bool();
  Branch (585:9): [True: 17, False: 6.24k]
586
6.24k
    return InterpretBool(value.get_str());
587
6.26k
}
588
589
bool SettingToBool(const common::SettingsValue& value, bool fDefault)
590
0
{
591
0
    return SettingToBool(value).value_or(fDefault);
592
0
}
593
594
#define INSTANTIATE_INT_TYPE(Type)                                                    \
595
    template Type ArgsManager::GetArg<Type>(const std::string&, Type) const;          \
596
    template std::optional<Type> ArgsManager::GetArg<Type>(const std::string&) const; \
597
    template Type SettingTo<Type>(const common::SettingsValue&, Type);                \
598
    template std::optional<Type> SettingTo<Type>(const common::SettingsValue&)
599
600
INSTANTIATE_INT_TYPE(int8_t);
601
INSTANTIATE_INT_TYPE(uint8_t);
602
INSTANTIATE_INT_TYPE(int16_t);
603
INSTANTIATE_INT_TYPE(uint16_t);
604
INSTANTIATE_INT_TYPE(int32_t);
605
INSTANTIATE_INT_TYPE(uint32_t);
606
INSTANTIATE_INT_TYPE(int64_t);
607
INSTANTIATE_INT_TYPE(uint64_t);
608
609
#undef INSTANTIATE_INT_TYPE
610
611
bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
612
42.4k
{
613
42.4k
    LOCK(cs_args);
614
42.4k
    if (!GetSetting_(strArg).isNull()) return false;
  Branch (614:9): [True: 22.6k, False: 19.7k]
615
19.7k
    m_settings.forced_settings[SettingName(strArg)] = strValue;
616
19.7k
    return true;
617
42.4k
}
618
619
bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
620
26.0k
{
621
26.0k
    if (fValue)
  Branch (621:9): [True: 23.9k, False: 2.16k]
622
23.9k
        return SoftSetArg(strArg, std::string("1"));
623
2.16k
    else
624
2.16k
        return SoftSetArg(strArg, std::string("0"));
625
26.0k
}
626
627
void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
628
46.4k
{
629
46.4k
    LOCK(cs_args);
630
46.4k
    m_settings.forced_settings[SettingName(strArg)] = strValue;
631
46.4k
}
632
633
void ArgsManager::AddCommand(const std::string& cmd, const std::string& help, std::set<std::string> options)
634
15.7k
{
635
15.7k
    Assert(cmd.find('=') == std::string::npos);
636
15.7k
    Assert(cmd.at(0) != '-');
637
638
15.7k
    LOCK(cs_args);
639
15.7k
    m_accept_any_command = false; // latch to false
640
15.7k
    std::map<std::string, Arg>& arg_map = m_available_args[OptionsCategory::COMMANDS];
641
15.7k
    auto ret = arg_map.emplace(cmd, Arg{"", help, ArgsManager::COMMAND});
642
15.7k
    if (!options.empty()) {
  Branch (642:9): [True: 7.92k, False: 7.83k]
643
7.92k
        auto& cmdopts = m_available_args[OptionsCategory::COMMAND_OPTIONS];
644
7.92k
        bool command_has_all_options_defined = true;
645
32.6k
        for (const auto& opt : options) {
  Branch (645:30): [True: 32.6k, False: 7.92k]
646
32.6k
            if (!cmdopts.contains(opt)) {
  Branch (646:17): [True: 0, False: 32.6k]
647
0
                command_has_all_options_defined = false;
648
0
            }
649
32.6k
        }
650
7.92k
        Assert(command_has_all_options_defined);
651
652
7.92k
        m_command_args.try_emplace(cmd, std::move(options));
653
7.92k
    }
654
15.7k
    Assert(ret.second); // Fail on duplicate commands
655
15.7k
}
656
657
void ArgsManager::AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat)
658
310k
{
659
310k
    Assert((flags & ArgsManager::COMMAND) == 0); // use AddCommand
660
661
    // Split arg name from its help param
662
310k
    size_t eq_index = name.find('=');
663
310k
    if (eq_index == std::string::npos) {
  Branch (663:9): [True: 161k, False: 148k]
664
161k
        eq_index = name.size();
665
161k
    }
666
310k
    std::string arg_name = name.substr(0, eq_index);
667
668
310k
    LOCK(cs_args);
669
670
    // Allow duplicates involving HIDDEN — it is used as a placeholder for args
671
    // unavailable in this binary but tolerated for shared config files (see #13441).
672
1.61M
    for (const auto& arg_map : m_available_args) {
  Branch (672:30): [True: 1.61M, False: 310k]
673
1.61M
        if (arg_map.first == OptionsCategory::HIDDEN || cat == OptionsCategory::HIDDEN) continue;
  Branch (673:13): [True: 294k, False: 1.32M]
  Branch (673:57): [True: 262k, False: 1.06M]
674
1.06M
        Assert(!arg_map.second.contains(arg_name));
675
1.06M
    }
676
677
310k
    std::map<std::string, Arg>& arg_map = m_available_args[cat];
678
310k
    auto ret = arg_map.emplace(arg_name, Arg{name.substr(eq_index, name.size() - eq_index), help, flags});
679
310k
    assert(ret.second); // Make sure an insertion actually happened
  Branch (679:5): [True: 310k, False: 0]
680
681
310k
    if (flags & ArgsManager::NETWORK_ONLY) {
  Branch (681:9): [True: 19.8k, False: 290k]
682
19.8k
        m_network_only_args.emplace(arg_name);
683
19.8k
    }
684
310k
}
685
686
void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names)
687
23.3k
{
688
50.2k
    for (const std::string& name : names) {
  Branch (688:34): [True: 50.2k, False: 23.3k]
689
50.2k
        AddArg(name, "", ArgsManager::ALLOW_ANY, OptionsCategory::HIDDEN);
690
50.2k
    }
691
23.3k
}
692
693
void ArgsManager::ClearArgs()
694
20.7k
{
695
20.7k
    LOCK(cs_args);
696
20.7k
    m_settings = {};
697
20.7k
    m_available_args.clear();
698
20.7k
    m_command_args.clear();
699
20.7k
    m_network_only_args.clear();
700
20.7k
    m_config_sections.clear();
701
20.7k
}
702
703
void ArgsManager::CheckMultipleCLIArgs() const
704
0
{
705
0
    LOCK(cs_args);
706
0
    std::vector<std::string> found{};
707
0
    auto cmds = m_available_args.find(OptionsCategory::CLI_COMMANDS);
708
0
    if (cmds != m_available_args.end()) {
  Branch (708:9): [True: 0, False: 0]
709
0
        for (const auto& [cmd, argspec] : cmds->second) {
  Branch (709:41): [True: 0, False: 0]
710
0
            if (!GetSetting_(cmd).isNull()) {
  Branch (710:17): [True: 0, False: 0]
711
0
                found.push_back(cmd);
712
0
            }
713
0
        }
714
0
        if (found.size() > 1) {
  Branch (714:13): [True: 0, False: 0]
715
0
            throw std::runtime_error(strprintf("Only one of %s may be specified.", util::Join(found, ", ")));
716
0
        }
717
0
    }
718
0
}
719
720
std::string ArgsManager::GetHelpMessage() const
721
710
{
722
710
    const bool show_debug = GetBoolArg("-help-debug", false);
723
724
710
    std::string usage;
725
710
    LOCK(cs_args);
726
727
710
    const auto command_options = m_available_args.find(OptionsCategory::COMMAND_OPTIONS);
728
4.33k
    const auto for_matching_cmd_opts = [&](const std::set<std::string>& select, auto&& fn) EXCLUSIVE_LOCKS_REQUIRED(cs_args) {
729
4.33k
        if (select.empty()) return;
  Branch (729:13): [True: 0, False: 4.33k]
730
4.33k
        if (command_options == m_available_args.end()) return;
  Branch (730:13): [True: 0, False: 4.33k]
731
56.7k
        for (const auto& [name, info] : command_options->second) {
  Branch (731:39): [True: 56.7k, False: 4.33k]
732
56.7k
            if (!show_debug && (info.m_flags & ArgsManager::DEBUG_ONLY)) continue;
  Branch (732:17): [True: 22.0k, False: 34.7k]
  Branch (732:32): [True: 20.1k, False: 1.90k]
733
36.6k
            if (!select.contains(name)) continue;
  Branch (733:17): [True: 20.2k, False: 16.4k]
734
16.4k
            fn(name, info);
735
16.4k
        }
736
4.33k
    };
737
738
1.45k
    for (const auto& [category, category_args] : m_available_args) {
  Branch (738:48): [True: 1.45k, False: 244]
739
1.45k
        switch(category) {
  Branch (739:16): [True: 0, False: 1.45k]
740
400
            case OptionsCategory::OPTIONS:
  Branch (740:13): [True: 400, False: 1.05k]
741
400
                usage += HelpMessageGroup("Options:");
742
400
                break;
743
46
            case OptionsCategory::CONNECTION:
  Branch (743:13): [True: 46, False: 1.40k]
744
46
                usage += HelpMessageGroup("Connection options:");
745
46
                break;
746
12
            case OptionsCategory::ZMQ:
  Branch (746:13): [True: 12, False: 1.44k]
747
12
                usage += HelpMessageGroup("ZeroMQ notification options:");
748
12
                break;
749
32
            case OptionsCategory::DEBUG_TEST:
  Branch (749:13): [True: 32, False: 1.42k]
750
32
                usage += HelpMessageGroup("Debugging/Testing options:");
751
32
                break;
752
59
            case OptionsCategory::NODE_RELAY:
  Branch (752:13): [True: 59, False: 1.39k]
753
59
                usage += HelpMessageGroup("Node relay options:");
754
59
                break;
755
11
            case OptionsCategory::BLOCK_CREATION:
  Branch (755:13): [True: 11, False: 1.44k]
756
11
                usage += HelpMessageGroup("Block creation options:");
757
11
                break;
758
10
            case OptionsCategory::RPC:
  Branch (758:13): [True: 10, False: 1.44k]
759
10
                usage += HelpMessageGroup("RPC server options:");
760
10
                break;
761
17
            case OptionsCategory::IPC:
  Branch (761:13): [True: 17, False: 1.43k]
762
17
                usage += HelpMessageGroup("IPC interprocess connection options:");
763
17
                break;
764
10
            case OptionsCategory::WALLET:
  Branch (764:13): [True: 10, False: 1.44k]
765
10
                usage += HelpMessageGroup("Wallet options:");
766
10
                break;
767
45
            case OptionsCategory::WALLET_DEBUG_TEST:
  Branch (767:13): [True: 45, False: 1.40k]
768
45
                if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:");
  Branch (768:21): [True: 8, False: 37]
769
45
                break;
770
9
            case OptionsCategory::CHAINPARAMS:
  Branch (770:13): [True: 9, False: 1.44k]
771
9
                usage += HelpMessageGroup("Chain selection options:");
772
9
                break;
773
16
            case OptionsCategory::GUI:
  Branch (773:13): [True: 16, False: 1.43k]
774
16
                usage += HelpMessageGroup("UI Options:");
775
16
                break;
776
178
            case OptionsCategory::COMMANDS:
  Branch (776:13): [True: 178, False: 1.27k]
777
178
                usage += HelpMessageGroup("Commands:");
778
178
                break;
779
24
            case OptionsCategory::REGISTER_COMMANDS:
  Branch (779:13): [True: 24, False: 1.43k]
780
24
                usage += HelpMessageGroup("Register Commands:");
781
24
                break;
782
27
            case OptionsCategory::CLI_COMMANDS:
  Branch (782:13): [True: 27, False: 1.42k]
783
27
                usage += HelpMessageGroup("CLI Commands:");
784
27
                break;
785
92
            case OptionsCategory::COMMAND_OPTIONS:
  Branch (785:13): [True: 92, False: 1.36k]
786
558
            case OptionsCategory::HIDDEN:
  Branch (786:13): [True: 466, False: 988]
787
558
                break;
788
1.45k
        } // no default case, so the compiler can warn about missing cases
789
790
1.45k
        if (category == OptionsCategory::COMMAND_OPTIONS) continue;
  Branch (790:13): [True: 92, False: 1.36k]
791
792
        // When we get to the hidden options, stop
793
1.36k
        if (category == OptionsCategory::HIDDEN) break;
  Branch (793:13): [True: 466, False: 896]
794
795
10.0k
        for (const auto& [arg_name, arg_info] : category_args) {
  Branch (795:47): [True: 10.0k, False: 896]
796
10.0k
            if (show_debug || !(arg_info.m_flags & ArgsManager::DEBUG_ONLY)) {
  Branch (796:17): [True: 1.64k, False: 8.40k]
  Branch (796:31): [True: 6.01k, False: 2.39k]
797
7.65k
                usage += HelpMessageOpt(arg_name, arg_info.m_help_param, arg_info.m_help_text);
798
799
7.65k
                if (category == OptionsCategory::COMMANDS) {
  Branch (799:21): [True: 5.95k, False: 1.70k]
800
5.95k
                    const auto cmd_args = m_command_args.find(arg_name);
801
5.95k
                    if (cmd_args == m_command_args.end()) continue;
  Branch (801:25): [True: 1.62k, False: 4.33k]
802
16.4k
                    for_matching_cmd_opts(cmd_args->second, [&](const auto& cmdopt_name, const auto& cmdopt_info) {
803
16.4k
                        usage += HelpMessageOpt(cmdopt_name, cmdopt_info.m_help_param, cmdopt_info.m_help_text, /*subopt=*/true);
804
16.4k
                    });
805
4.33k
                }
806
7.65k
            }
807
10.0k
        }
808
896
    }
809
710
    return usage;
810
710
}
811
812
bool HelpRequested(const ArgsManager& args)
813
710
{
814
710
    return args.IsArgSet("-?") || args.IsArgSet("-h") || args.IsArgSet("-help") || args.IsArgSet("-help-debug");
  Branch (814:12): [True: 95, False: 615]
  Branch (814:35): [True: 28, False: 587]
  Branch (814:58): [True: 2, False: 585]
  Branch (814:84): [True: 17, False: 568]
815
710
}
816
817
void SetupHelpOptions(ArgsManager& args)
818
1.92k
{
819
1.92k
    args.AddArg("-help", "Print this help message and exit (also -h or -?)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
820
1.92k
    args.AddHiddenArgs({"-h", "-?"});
821
1.92k
}
822
823
1.74k
std::string HelpMessageGroup(const std::string &message) {
824
1.74k
    return std::string(message) + std::string("\n\n");
825
1.74k
}
826
827
std::string HelpMessageOpt(std::string_view option, std::string_view help_param, std::string_view message, bool subopt)
828
25.8k
{
829
25.8k
    constexpr int screen_width = 79;
830
25.8k
    int opt_indent = 2;
831
25.8k
    int msg_indent = 7;
832
833
25.8k
    if (subopt) {
  Branch (833:9): [True: 16.4k, False: 9.42k]
834
16.4k
        int bump = msg_indent - opt_indent;
835
16.4k
        opt_indent += bump; // opt_indent now at the old msg_indent level
836
16.4k
        msg_indent += bump; // indent by the same amount
837
16.4k
    }
838
25.8k
    int msg_width = screen_width - msg_indent;
839
840
25.8k
    return strprintf("%*s%s%s\n%*s%s\n\n",
841
25.8k
                     opt_indent, "", option, help_param,
842
25.8k
                     msg_indent, "", FormatParagraph(message, msg_width, msg_indent));
843
25.8k
}
844
845
const std::vector<std::string> TEST_OPTIONS_DOC{
846
    "addrman (use deterministic addrman)",
847
    "reindex_after_failure_noninteractive_yes (When asked for a reindex after failure interactively, simulate as-if answered with 'yes')",
848
    "bip94 (enforce BIP94 consensus rules)",
849
};
850
851
bool HasTestOption(const ArgsManager& args, const std::string& test_option)
852
2.75k
{
853
2.75k
    const auto options = args.GetArgs("-test");
854
2.75k
    return std::any_of(options.begin(), options.end(), [test_option](const auto& option) {
855
0
        return option == test_option;
856
0
    });
857
2.75k
}
858
859
fs::path GetDefaultDataDir()
860
0
{
861
    // Windows:
862
    //   old: C:\Users\Username\AppData\Roaming\Bitcoin
863
    //   new: C:\Users\Username\AppData\Local\Bitcoin
864
    // macOS: ~/Library/Application Support/Bitcoin
865
    // Unix-like: ~/.bitcoin
866
#ifdef WIN32
867
    // Windows
868
    // Check for existence of datadir in old location and keep it there
869
    fs::path legacy_path = GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
870
    if (fs::exists(legacy_path)) return legacy_path;
871
872
    // Otherwise, fresh installs can start in the new, "proper" location
873
    return GetSpecialFolderPath(CSIDL_LOCAL_APPDATA) / "Bitcoin";
874
#else
875
0
    fs::path pathRet;
876
0
    char* pszHome = getenv("HOME");
877
0
    if (pszHome == nullptr || strlen(pszHome) == 0)
  Branch (877:9): [True: 0, False: 0]
  Branch (877:31): [True: 0, False: 0]
878
0
        pathRet = fs::path("/");
879
0
    else
880
0
        pathRet = fs::path(pszHome);
881
#ifdef __APPLE__
882
    // macOS
883
    return pathRet / "Library/Application Support/Bitcoin";
884
#else
885
    // Unix-like
886
0
    return pathRet / ".bitcoin";
887
0
#endif
888
0
#endif
889
0
}
890
891
bool CheckDataDirOption(const ArgsManager& args)
892
0
{
893
0
    const fs::path datadir{args.GetPathArg("-datadir")};
894
0
    return datadir.empty() || fs::is_directory(fs::absolute(datadir));
  Branch (894:12): [True: 0, False: 0]
  Branch (894:31): [True: 0, False: 0]
895
0
}
896
897
fs::path ArgsManager::GetConfigFilePath() const
898
0
{
899
0
    LOCK(cs_args);
900
0
    return *Assert(m_config_path);
901
0
}
902
903
void ArgsManager::SetConfigFilePath(fs::path path)
904
0
{
905
0
    LOCK(cs_args);
906
0
    assert(!m_config_path);
  Branch (906:5): [True: 0, False: 0]
907
0
    m_config_path = path;
908
0
}
909
910
ChainType ArgsManager::GetChainType() const
911
1.37k
{
912
1.37k
    std::variant<ChainType, std::string> arg = GetChainArg();
913
1.37k
    if (auto* parsed = std::get_if<ChainType>(&arg)) return *parsed;
  Branch (913:15): [True: 1.37k, False: 0]
914
0
    throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg)));
915
1.37k
}
916
917
std::string ArgsManager::GetChainTypeString() const
918
710
{
919
710
    auto arg = GetChainArg();
920
710
    if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed);
  Branch (920:15): [True: 690, False: 20]
921
20
    return std::get<std::string>(arg);
922
710
}
923
924
std::variant<ChainType, std::string> ArgsManager::GetChainArg() const
925
2.08k
{
926
8.34k
    auto get_net = [&](const std::string& arg) {
927
8.34k
        LOCK(cs_args);
928
8.34k
        common::SettingsValue value = common::GetSetting(m_settings, /* section= */ "", SettingName(arg),
929
8.34k
            /* ignore_default_section_config= */ false,
930
8.34k
            /*ignore_nonpersistent=*/false,
931
8.34k
            /* get_chain_type= */ true);
932
8.34k
        return value.isNull() ? false : value.isBool() ? value.get_bool() : InterpretBool(value.get_str());
  Branch (932:16): [True: 8.30k, False: 40]
  Branch (932:41): [True: 2, False: 38]
933
8.34k
    };
934
935
2.08k
    const bool fRegTest = get_net("-regtest");
936
2.08k
    const bool fSigNet  = get_net("-signet");
937
2.08k
    const bool fTestNet = get_net("-testnet");
938
2.08k
    const bool fTestNet4 = get_net("-testnet4");
939
2.08k
    const auto chain_arg = GetArg("-chain");
940
941
2.08k
    if ((int)chain_arg.has_value() + (int)fRegTest + (int)fSigNet + (int)fTestNet + (int)fTestNet4 > 1) {
  Branch (941:9): [True: 7, False: 2.07k]
942
7
        throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet, -testnet4 and -chain. Can use at most one.");
943
7
    }
944
2.07k
    if (chain_arg) {
  Branch (944:9): [True: 18, False: 2.06k]
945
18
        if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed;
  Branch (945:18): [True: 5, False: 13]
946
        // Not a known string, so return original string
947
13
        return *chain_arg;
948
18
    }
949
2.06k
    if (fRegTest) return ChainType::REGTEST;
  Branch (949:9): [True: 6, False: 2.05k]
950
2.05k
    if (fSigNet) return ChainType::SIGNET;
  Branch (950:9): [True: 3, False: 2.05k]
951
2.05k
    if (fTestNet) return ChainType::TESTNET;
  Branch (951:9): [True: 1, False: 2.05k]
952
2.05k
    if (fTestNet4) return ChainType::TESTNET4;
  Branch (952:9): [True: 3, False: 2.04k]
953
2.04k
    return ChainType::MAIN;
954
2.05k
}
955
956
bool ArgsManager::UseDefaultSection(const std::string& arg) const
957
1.19M
{
958
1.19M
    AssertLockHeld(cs_args);
959
1.19M
    return m_network == ChainTypeToString(ChainType::MAIN) || !m_network_only_args.contains(arg);
  Branch (959:12): [True: 233k, False: 964k]
  Branch (959:63): [True: 924k, False: 40.0k]
960
1.19M
}
961
962
common::SettingsValue ArgsManager::GetSetting_(const std::string& arg) const
963
969k
{
964
969k
    AssertLockHeld(cs_args);
965
969k
    return common::GetSetting(
966
969k
        m_settings, m_network, SettingName(arg), !UseDefaultSection(arg),
967
969k
        /*ignore_nonpersistent=*/false, /*get_chain_type=*/false);
968
969k
}
969
970
common::SettingsValue ArgsManager::GetSetting(const std::string& arg) const
971
914k
{
972
914k
    LOCK(cs_args);
973
914k
    return GetSetting_(arg);
974
914k
}
975
976
std::vector<common::SettingsValue> ArgsManager::GetSettingsList(const std::string& arg) const
977
229k
{
978
229k
    LOCK(cs_args);
979
229k
    return common::GetSettingsList(m_settings, m_network, SettingName(arg), !UseDefaultSection(arg));
980
229k
}
981
982
void ArgsManager::logArgsPrefix(
983
    const std::string& prefix,
984
    const std::string& section,
985
    const std::map<std::string, std::vector<common::SettingsValue>>& args) const
986
0
{
987
0
    AssertLockHeld(cs_args);
988
0
    std::string section_str = section.empty() ? "" : "[" + section + "] ";
  Branch (988:31): [True: 0, False: 0]
989
0
    for (const auto& arg : args) {
  Branch (989:26): [True: 0, False: 0]
990
0
        for (const auto& value : arg.second) {
  Branch (990:32): [True: 0, False: 0]
991
0
            std::optional<unsigned int> flags = GetArgFlags_('-' + arg.first);
992
0
            if (flags) {
  Branch (992:17): [True: 0, False: 0]
993
0
                std::string value_str = (*flags & SENSITIVE) ? "****" : value.write();
  Branch (993:41): [True: 0, False: 0]
994
0
                LogInfo("%s %s%s=%s\n", prefix, section_str, arg.first, value_str);
995
0
            }
996
0
        }
997
0
    }
998
0
}
999
1000
void ArgsManager::LogArgs() const
1001
0
{
1002
0
    LOCK(cs_args);
1003
0
    for (const auto& section : m_settings.ro_config) {
  Branch (1003:30): [True: 0, False: 0]
1004
0
        logArgsPrefix("Config file arg:", section.first, section.second);
1005
0
    }
1006
0
    for (const auto& setting : m_settings.rw_settings) {
  Branch (1006:30): [True: 0, False: 0]
1007
0
        LogInfo("Setting file arg: %s = %s\n", setting.first, setting.second.write());
1008
0
    }
1009
0
    logArgsPrefix("Command-line arg:", "", m_settings.command_line_options);
1010
0
}