Skip to content

Commit b00c0c2

Browse files
committed
util: Add GetPathArg default path argument
Lets GetPathArg method be used more places for path arguments that have default values, like "-settings" and BITCOIN_SETTINGS_FILENAME in the next comment. Also: - Fix negated argument handling. Return path{} not path{"0"} when path argument is negated. - Add new tests for default and negated cases - Move GetPathArg() method declaration next to GetArg() declarations. The two methods are close substitutes for each, so this should help keep them consistent and make them more discoverable.
1 parent 8c0f02c commit b00c0c2

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/test/getarg_tests.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ BOOST_AUTO_TEST_CASE(patharg)
249249

250250
ResetArgs("-dir=user/.bitcoin/.//");
251251
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
252+
253+
// Check negated and default arguments
254+
ResetArgs("-dir=override");
255+
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir", "default"), fs::path{"override"});
256+
ResetArgs("");
257+
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir", "default"), fs::path{"default"});
258+
ResetArgs("-nodir");
259+
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir", "default"), fs::path{""});
252260
}
253261

254262
BOOST_AUTO_TEST_CASE(doubledash)

src/util/system.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,12 @@ std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) co
386386
return std::nullopt;
387387
}
388388

389-
fs::path ArgsManager::GetPathArg(std::string pathlike_arg) const
389+
fs::path ArgsManager::GetPathArg(const std::string& arg, const fs::path& default_value) const
390390
{
391-
auto result = fs::PathFromString(GetArg(pathlike_arg, "")).lexically_normal();
391+
if (IsArgNegated(arg)) return fs::path{};
392+
std::string path_str = GetArg(arg, "");
393+
if (path_str.empty()) return default_value;
394+
fs::path result = fs::PathFromString(path_str).lexically_normal();
392395
// Remove trailing slash, if present.
393396
return result.has_filename() ? result : result.parent_path();
394397
}

src/util/system.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,6 @@ class ArgsManager
264264
*/
265265
std::optional<const Command> GetCommand() const;
266266

267-
/**
268-
* Get a normalized path from a specified pathlike argument
269-
*
270-
* It is guaranteed that the returned path has no trailing slashes.
271-
*
272-
* @param pathlike_arg Pathlike argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
273-
* @return Normalized path which is get from a specified pathlike argument
274-
*/
275-
fs::path GetPathArg(std::string pathlike_arg) const;
276-
277267
/**
278268
* Get blocks directory path
279269
*
@@ -336,6 +326,16 @@ class ArgsManager
336326
*/
337327
std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
338328

329+
/**
330+
* Return path argument or default value
331+
*
332+
* It is guaranteed that the returned path has no trailing slashes.
333+
*
334+
* @param arg Argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
335+
* @return Lexically normalized path without trailing separators
336+
*/
337+
fs::path GetPathArg(const std::string& arg, const fs::path& default_value={}) const;
338+
339339
/**
340340
* Return integer argument or default value
341341
*

0 commit comments

Comments
 (0)