Skip to content

Commit 25f56f3

Browse files
committed
Added built-in support for null and default parameters.
1 parent ea654c3 commit 25f56f3

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
- Added real-time parameter information when typing out a command.
9+
- Added built-in support for null and default values for parameters.
910
- Added commands for executing C# expressions or statements at runtime.
1011
- Added exception handling for command callbacks.
1112
- Added a reminder in the devconsole command that the console is disabled by default in release builds.

Runtime/DevConsoleMono.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2040,7 +2040,7 @@ private void InitBuiltInParsers()
20402040
s =>
20412041
{
20422042
// Allow null value, representing a toggle
2043-
if (s.ToLower() == "null" || s.ToLower() == "toggle" || s == "~" || s == "!")
2043+
if (s.ToLower() == "null" || s == "~")
20442044
{
20452045
return null;
20462046
}
@@ -2292,6 +2292,18 @@ private string[] ConvertInput(string[] input, int parameterCount)
22922292

22932293
private object ParseParameter(string input, Type type)
22942294
{
2295+
// Special case if the type is nullable or a class
2296+
if ((Nullable.GetUnderlyingType(type) != null || type.IsClass) && (input.ToLower() == "null" || input == "~"))
2297+
{
2298+
return null;
2299+
}
2300+
2301+
// Special case if the type is a struct
2302+
if (type.IsValueType && (input.ToLower() == "default" || input == "~"))
2303+
{
2304+
return Activator.CreateInstance(type);
2305+
}
2306+
22952307
// Check if a parse function exists for the type
22962308
if (_parameterParseFuncs.TryGetValue(type, out Func<string, object> parseFunc))
22972309
{

0 commit comments

Comments
 (0)