mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 22:18:54 +01:00
Merge #12713: Track negated options in the option parser
f7683cba7bTrack negated arguments in the argument paser. (Evan Klitzke)4f872b2450Add additional tests for GetBoolArg() (Evan Klitzke) Pull request description: This change explicitly enable tracking negated options in the option parser. A negated option is one passed with a `-no` prefix. For example, `-nofoo` is the negated form of `-foo`. Negated options were originally added in the 0.6 release. The change here allows code to explicitly distinguish between cases like `-nofoo` and `-foo=0`, which was not possible previously. The option parser does not have any changed semantics as a result of this change, and existing code will parse options just as it did before. The motivation for this change is to provide a way to disable options that are otherwise not boolean options. For example, the `-debuglogfile` option is normally interpreted as a string, where the value is the log file name. With this change a user can pass in `-nodebuglogfile` and the code can see that it was explicitly negated, and use that to disable the log file. This change originally split out from #12689. Tree-SHA512: cd5a7354eb03d2d402863c7b69e512cad382781d9b8f18c1ab104fc46d45a712530818d665203082da39572c8a42313c5be09306dc2a7227cdedb20ef7314823
This commit is contained in:
@@ -185,17 +185,11 @@ BOOST_AUTO_TEST_CASE(util_FormatISO8601Time)
|
||||
BOOST_CHECK_EQUAL(FormatISO8601Time(1317425777), "23:36:17Z");
|
||||
}
|
||||
|
||||
class TestArgsManager : public ArgsManager
|
||||
struct TestArgsManager : public ArgsManager
|
||||
{
|
||||
public:
|
||||
std::map<std::string, std::string>& GetMapArgs()
|
||||
{
|
||||
return mapArgs;
|
||||
};
|
||||
const std::map<std::string, std::vector<std::string> >& GetMapMultiArgs()
|
||||
{
|
||||
return mapMultiArgs;
|
||||
};
|
||||
std::map<std::string, std::string>& GetMapArgs() { return mapArgs; }
|
||||
const std::map<std::string, std::vector<std::string> >& GetMapMultiArgs() { return mapMultiArgs; }
|
||||
const std::unordered_set<std::string>& GetNegatedArgs() { return m_negated_args; }
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_CASE(util_ParseParameters)
|
||||
@@ -223,6 +217,54 @@ BOOST_AUTO_TEST_CASE(util_ParseParameters)
|
||||
BOOST_CHECK(testArgs.GetArgs("-ccc").size() == 2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(util_GetBoolArg)
|
||||
{
|
||||
TestArgsManager testArgs;
|
||||
const char *argv_test[] = {
|
||||
"ignored", "-a", "-nob", "-c=0", "-d=1", "-e=false", "-f=true"};
|
||||
testArgs.ParseParameters(7, (char**)argv_test);
|
||||
|
||||
// Each letter should be set.
|
||||
for (char opt : "abcdef")
|
||||
BOOST_CHECK(testArgs.IsArgSet({'-', opt}) || !opt);
|
||||
|
||||
// Nothing else should be in the map
|
||||
BOOST_CHECK(testArgs.GetMapArgs().size() == 6 &&
|
||||
testArgs.GetMapMultiArgs().size() == 6);
|
||||
|
||||
// The -no prefix should get stripped on the way in.
|
||||
BOOST_CHECK(!testArgs.IsArgSet("-nob"));
|
||||
|
||||
// The -b option is flagged as negated, and nothing else is
|
||||
BOOST_CHECK(testArgs.IsArgNegated("-b"));
|
||||
BOOST_CHECK(testArgs.GetNegatedArgs().size() == 1);
|
||||
BOOST_CHECK(!testArgs.IsArgNegated("-a"));
|
||||
|
||||
// Check expected values.
|
||||
BOOST_CHECK(testArgs.GetBoolArg("-a", false) == true);
|
||||
BOOST_CHECK(testArgs.GetBoolArg("-b", true) == false);
|
||||
BOOST_CHECK(testArgs.GetBoolArg("-c", true) == false);
|
||||
BOOST_CHECK(testArgs.GetBoolArg("-d", false) == true);
|
||||
BOOST_CHECK(testArgs.GetBoolArg("-e", true) == false);
|
||||
BOOST_CHECK(testArgs.GetBoolArg("-f", true) == false);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(util_GetBoolArgEdgeCases)
|
||||
{
|
||||
// Test some awful edge cases that hopefully no user will ever exercise.
|
||||
TestArgsManager testArgs;
|
||||
const char *argv_test[] = {"ignored", "-nofoo", "-foo", "-nobar=0"};
|
||||
testArgs.ParseParameters(4, (char**)argv_test);
|
||||
|
||||
// This was passed twice, second one overrides the negative setting.
|
||||
BOOST_CHECK(!testArgs.IsArgNegated("-foo"));
|
||||
BOOST_CHECK(testArgs.GetBoolArg("-foo", false) == true);
|
||||
|
||||
// A double negative is a positive.
|
||||
BOOST_CHECK(testArgs.IsArgNegated("-bar"));
|
||||
BOOST_CHECK(testArgs.GetBoolArg("-bar", false) == true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(util_GetArg)
|
||||
{
|
||||
TestArgsManager testArgs;
|
||||
|
||||
Reference in New Issue
Block a user