Skip to content

Commit 687e655

Browse files
committed
util: Add GetPathArg default path argument
Let GetPathArg method be used more places for path arguments that have default values, like "-settings" and BITCOIN_SETTINGS_FILENAME in the next commit. 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 other, so this should help keep them consistent and make them more discoverable.
1 parent 08bcfa2 commit 687e655

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

src/test/getarg_tests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,24 @@ BOOST_AUTO_TEST_CASE(patharg)
245245

246246
ResetArgs(local_args, "-dir=user/.bitcoin/.//");
247247
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
248+
249+
// Check negated and default argument handling. Specifying an empty argument
250+
// is the same as not specifying the argument. This is convenient for
251+
// scripting so later command line arguments can override earlier command
252+
// line arguments or bitcoin.conf values. Currently the -dir= case cannot be
253+
// distinguished from -dir case with no assignment, but #16545 would add the
254+
// ability to distinguish these in the future (and treat the no-assign case
255+
// like an imperative command or an error).
256+
ResetArgs(local_args, "");
257+
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
258+
ResetArgs(local_args, "-dir=override");
259+
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"override"});
260+
ResetArgs(local_args, "-dir=");
261+
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
262+
ResetArgs(local_args, "-dir");
263+
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
264+
ResetArgs(local_args, "-nodir");
265+
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{""});
248266
}
249267

250268
BOOST_AUTO_TEST_CASE(doubledash)

src/util/system.cpp

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

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

src/util/system.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,6 @@ class ArgsManager
270270
*/
271271
std::optional<const Command> GetCommand() const;
272272

273-
/**
274-
* Get a normalized path from a specified pathlike argument
275-
*
276-
* It is guaranteed that the returned path has no trailing slashes.
277-
*
278-
* @param pathlike_arg Pathlike argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
279-
* @return Normalized path which is get from a specified pathlike argument
280-
*/
281-
fs::path GetPathArg(std::string pathlike_arg) const;
282-
283273
/**
284274
* Get blocks directory path
285275
*
@@ -342,6 +332,18 @@ class ArgsManager
342332
*/
343333
std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
344334

335+
/**
336+
* Return path argument or default value
337+
*
338+
* @param arg Argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
339+
* @param default_value Optional default value to return instead of the empty path.
340+
* @return normalized path if argument is set, with redundant "." and ".."
341+
* path components and trailing separators removed (see patharg unit test
342+
* for examples or implementation for details). If argument is empty or not
343+
* set, default_value is returned unchanged.
344+
*/
345+
fs::path GetPathArg(std::string arg, const fs::path& default_value = {}) const;
346+
345347
/**
346348
* Return integer argument or default value
347349
*

0 commit comments

Comments
 (0)