Skip to content

Commit 32f0bfe

Browse files
committed
Added help text for parameters with enums.
1 parent 6c73980 commit 32f0bfe

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

Runtime/Parameter.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Created by: DavidFDev
44

55
using System;
6+
using System.Reflection;
67

78
namespace DavidFDev.DevConsole
89
{
@@ -11,6 +12,12 @@ namespace DavidFDev.DevConsole
1112
/// </summary>
1213
public sealed class Parameter
1314
{
15+
#region Constants
16+
17+
private const int MaxEnumNames = 6;
18+
19+
#endregion
20+
1421
#region Static methods
1522

1623
/// <summary>
@@ -64,8 +71,7 @@ private Parameter() { }
6471
/// <returns></returns>
6572
internal Parameter SetType<T>()
6673
{
67-
Type = typeof(T);
68-
return this;
74+
return SetType(typeof(T));
6975
}
7076

7177
/// <summary>
@@ -76,6 +82,35 @@ internal Parameter SetType<T>()
7682
internal Parameter SetType(Type type)
7783
{
7884
Type = type;
85+
86+
// If the type is an enum, add special help text
87+
if (type.IsEnum)
88+
{
89+
string enumHelpText = string.Empty;
90+
if (type.GetEnumNames().Length > MaxEnumNames)
91+
{
92+
// Recommend using the help_enum command
93+
enumHelpText = $"use <b>help_enum {type.Name}</b> to see options";
94+
}
95+
else
96+
{
97+
// Add names to the help text
98+
FieldInfo[] values = type.GetFields();
99+
bool first = true;
100+
for (int i = 0; i < values.Length; i++)
101+
{
102+
if (values[i].Name.Equals("value__"))
103+
{
104+
continue;
105+
}
106+
107+
enumHelpText += $"{(first ? "" : ", ")}{values[i].Name}={values[i].GetRawConstantValue()}";
108+
first = false;
109+
}
110+
}
111+
HelpText += $"{(HelpText.Length == 0 ? "" : " ")}({enumHelpText})";
112+
}
113+
79114
return this;
80115
}
81116

0 commit comments

Comments
 (0)