From 687e655ae2970f2f13aca0267c7de86dc69be763 Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Wed, 9 Feb 2022 17:49:42 -0500 Subject: 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. --- src/util/system.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/util/system.cpp') diff --git a/src/util/system.cpp b/src/util/system.cpp index aa9122106b..af5a511a78 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -387,9 +387,12 @@ std::optional ArgsManager::GetArgFlags(const std::string& name) co return std::nullopt; } -fs::path ArgsManager::GetPathArg(std::string pathlike_arg) const +fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const { - auto result = fs::PathFromString(GetArg(pathlike_arg, "")).lexically_normal(); + if (IsArgNegated(arg)) return fs::path{}; + std::string path_str = GetArg(arg, ""); + if (path_str.empty()) return default_value; + fs::path result = fs::PathFromString(path_str).lexically_normal(); // Remove trailing slash, if present. return result.has_filename() ? result : result.parent_path(); } -- cgit v1.2.3