Skip to content

Commit 2917ce6

Browse files
authored
Merge pull request #35 from AlexGhiondea/SupportColors
Support for colors
2 parents a570027 + 2d49ff5 commit 2917ce6

14 files changed

Lines changed: 1328 additions & 870 deletions

src/Analysis/TypeHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void ScanTypeForProperties<TOptions>(out TypeArgumentInfo tInfo)
5959
var actualAttribs = property.GetCustomAttributes<ActualArgumentAttribute>().ToList();
6060
if (actualAttribs.Count > 1)
6161
{
62-
throw new ArgumentException($"Only one of Required/Optional attribute are allowed per property ({property.Name}). [Red!Help information might be incorrect!]");
62+
throw new ArgumentException($"Only one of Required/Optional attribute are allowed per property ({property.Name}). Help information might be incorrect!");
6363
}
6464

6565
// if we have no attributes on that property, move on

src/CommandLine.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<PropertyGroup>
10-
<Version>1.5.3</Version>
11-
<AssemblyVersion>1.5.3.0</AssemblyVersion>
10+
<Version>2.0.0</Version>
11+
<AssemblyVersion>2.0.0</AssemblyVersion>
1212
<FileVersion>$(AssemblyVersion)</FileVersion>
1313
</PropertyGroup>
1414

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
3+
namespace CommandLine.Colors
4+
{
5+
public class DarkBackgroundColors : IColors
6+
{
7+
public ConsoleColor AssemblyNameColor => ConsoleColor.White;
8+
public ConsoleColor ArgumentGroupColor => ConsoleColor.Green;
9+
public ConsoleColor RequiredArgumentColor => ConsoleColor.Cyan;
10+
public ConsoleColor OptionalArgumentColor => ConsoleColor.Yellow;
11+
public ConsoleColor ErrorColor => ConsoleColor.Red;
12+
public ConsoleColor ArgumentValueColor => ConsoleColor.Green;
13+
}
14+
15+
public class DarkYellowBackgroundColors : IColors
16+
{
17+
public ConsoleColor AssemblyNameColor => ConsoleColor.White;
18+
public ConsoleColor ArgumentGroupColor => ConsoleColor.Green;
19+
public ConsoleColor RequiredArgumentColor => ConsoleColor.Cyan;
20+
public ConsoleColor OptionalArgumentColor => ConsoleColor.Yellow;
21+
public ConsoleColor ErrorColor => ConsoleColor.Red;
22+
public ConsoleColor ArgumentValueColor => ConsoleColor.DarkGray;
23+
}
24+
25+
public class GreenBackgroundColors : IColors
26+
{
27+
public ConsoleColor AssemblyNameColor => ConsoleColor.White;
28+
public ConsoleColor ArgumentGroupColor => ConsoleColor.DarkGreen;
29+
public ConsoleColor RequiredArgumentColor => ConsoleColor.Blue;
30+
public ConsoleColor OptionalArgumentColor => ConsoleColor.DarkRed;
31+
public ConsoleColor ErrorColor => ConsoleColor.Red;
32+
public ConsoleColor ArgumentValueColor => ConsoleColor.DarkGray;
33+
}
34+
35+
public class CyanBackgroundColors : IColors
36+
{
37+
public ConsoleColor AssemblyNameColor => ConsoleColor.White;
38+
public ConsoleColor ArgumentGroupColor => ConsoleColor.DarkGreen;
39+
public ConsoleColor RequiredArgumentColor => ConsoleColor.Blue;
40+
public ConsoleColor OptionalArgumentColor => ConsoleColor.DarkRed;
41+
public ConsoleColor ErrorColor => ConsoleColor.Red;
42+
public ConsoleColor ArgumentValueColor => ConsoleColor.DarkGray;
43+
}
44+
45+
public class RedBackgroundColors : IColors
46+
{
47+
public ConsoleColor AssemblyNameColor => ConsoleColor.White;
48+
public ConsoleColor ArgumentGroupColor => ConsoleColor.Green;
49+
public ConsoleColor RequiredArgumentColor => ConsoleColor.Cyan;
50+
public ConsoleColor OptionalArgumentColor => ConsoleColor.Yellow;
51+
public ConsoleColor ErrorColor => ConsoleColor.DarkRed;
52+
public ConsoleColor ArgumentValueColor => ConsoleColor.Green;
53+
}
54+
}

src/Help/Colors/IColors.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace CommandLine.Colors
4+
{
5+
public interface IColors
6+
{
7+
ConsoleColor AssemblyNameColor { get; }
8+
ConsoleColor ArgumentGroupColor { get; }
9+
ConsoleColor RequiredArgumentColor { get; }
10+
ConsoleColor OptionalArgumentColor { get; }
11+
ConsoleColor ErrorColor { get; }
12+
ConsoleColor ArgumentValueColor { get; }
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace CommandLine.Colors
4+
{
5+
public class LightBackgroundColors : IColors
6+
{
7+
public ConsoleColor ErrorColor => ConsoleColor.Red;
8+
public ConsoleColor AssemblyNameColor => ConsoleColor.DarkGray;
9+
public ConsoleColor ArgumentGroupColor => ConsoleColor.DarkGreen;
10+
public ConsoleColor RequiredArgumentColor => ConsoleColor.DarkMagenta;
11+
public ConsoleColor OptionalArgumentColor => ConsoleColor.DarkBlue;
12+
public ConsoleColor ArgumentValueColor => ConsoleColor.DarkGreen;
13+
}
14+
15+
public class GrayBackgroundColors : IColors
16+
{
17+
public ConsoleColor ErrorColor => ConsoleColor.Red;
18+
public ConsoleColor AssemblyNameColor => ConsoleColor.DarkGray;
19+
public ConsoleColor ArgumentGroupColor => ConsoleColor.DarkGreen;
20+
public ConsoleColor RequiredArgumentColor => ConsoleColor.Magenta;
21+
public ConsoleColor ArgumentValueColor => ConsoleColor.DarkGreen;
22+
public ConsoleColor OptionalArgumentColor => ConsoleColor.DarkBlue;
23+
}
24+
}

src/Help/HelpGenerator.cs

Lines changed: 78 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using CommandLine.Attributes;
33
using OutputColorizer;
44
using System;
5-
using System.Collections.Generic;
65
using System.Linq;
76
using System.Reflection;
7+
using CommandLine.Colors;
88

99
namespace CommandLine
1010
{
@@ -13,105 +13,114 @@ internal static class HelpGenerator
1313
public const string RequestShortHelpParameter = "-?";
1414
public const string RequestLongHelpParameter = "--help";
1515

16-
private static int DisplayCommandLine(ArgumentGroupInfo arguments)
17-
{
18-
int maxStringSize = 0;
19-
for (int i = 0; i < arguments.RequiredArguments.Count; i++)
20-
{
21-
if (!arguments.RequiredArguments.ContainsKey(i))
22-
{
23-
Colorizer.WriteLine($"{Environment.NewLine}[Red!Error]: Required argument expected at position [Cyan!{i}]. Type declares arguments at position(s) [Cyan!{string.Join(",", arguments.RequiredArguments.Keys.OrderBy(x => x))}]. [Red!Check type definition].");
24-
return -1;
25-
}
26-
27-
var b = arguments.RequiredArguments[i].GetCustomAttribute<ActualArgumentAttribute>();
28-
maxStringSize = Math.Max(maxStringSize, b.Name.Length);
29-
Colorizer.Write("[Cyan!{0}] ", b.Name);
30-
}
31-
32-
foreach (var item in arguments.OptionalArguments.Values)
33-
{
34-
var b = item.GetCustomAttribute<ActualArgumentAttribute>();
35-
maxStringSize = Math.Max(maxStringSize, b.Name.Length);
36-
Colorizer.Write("\\[-[Yellow!{0}] value\\] ", b.Name);
37-
}
38-
39-
Colorizer.WriteLine(string.Empty);
40-
41-
return maxStringSize;
42-
}
43-
44-
internal static void DisplayHelp(HelpFormat helpFormat, TypeArgumentInfo arguments)
16+
internal static void DisplayHelp(HelpFormat helpFormat, TypeArgumentInfo arguments, IColors colors)
4517
{
4618
switch (helpFormat)
4719
{
4820
case HelpFormat.Short:
49-
DisplayShortHelp(arguments);
21+
DisplayShortHelp(arguments, colors);
5022
break;
5123
case HelpFormat.Full:
52-
DisplayDetailedHelp(arguments);
24+
DisplayDetailedHelp(arguments, colors);
5325
break;
54-
default:
55-
throw new ArgumentException("Unrecognized help format", nameof(helpFormat));
5626
}
5727
}
5828

59-
private static void DisplayShortHelp(TypeArgumentInfo type)
29+
private static void DisplayShortHelp(TypeArgumentInfo type, IColors colors)
6030
{
6131
string exeName = Assembly.GetEntryAssembly()?.GetName()?.Name;
6232
Colorizer.WriteLine("Usage: ");
6333

64-
DisplayCommandLine(exeName, type);
34+
DisplayCommandLine(exeName, type, colors);
6535

6636
Colorizer.WriteLine(string.Empty);
67-
Colorizer.WriteLine("For detailed information run '[White!{0} --help]'.", exeName);
37+
string errorFormat = $"For detailed information run '[{colors.AssemblyNameColor}!{{0}} --help]'.";
38+
Colorizer.WriteLine(errorFormat, exeName);
6839
}
6940

70-
private static void DisplayDetailedHelp(TypeArgumentInfo type)
41+
private static void DisplayDetailedHelp(TypeArgumentInfo type, IColors colors)
7142
{
7243
string exeName = Assembly.GetEntryAssembly()?.GetName()?.Name;
7344
Colorizer.WriteLine("Usage: ");
7445

7546
foreach (var item in type.ArgumentGroups.Keys)
7647
{
77-
DisplayDetailedArgumentHelp(exeName, item, type.ArgumentGroups[item]);
48+
DisplayDetailedArgumentHelp(exeName, item, type.ArgumentGroups[item], colors);
7849
}
7950
}
8051

81-
public static void DisplayHelpForCommmand(string command, ArgumentGroupInfo propertyGroup)
52+
public static void DisplayHelpForCommmand(string command, ArgumentGroupInfo propertyGroup, IColors colors)
8253
{
8354
string exeName = Assembly.GetEntryAssembly()?.GetName()?.Name;
8455
Colorizer.WriteLine("Usage: ");
8556

86-
DisplayDetailedArgumentHelp(exeName, command, propertyGroup);
57+
DisplayDetailedArgumentHelp(exeName, command, propertyGroup, colors);
8758
}
8859

89-
private static void DisplayCommandLine(string exeName, TypeArgumentInfo type)
60+
private static void DisplayCommandLine(string exeName, TypeArgumentInfo type, IColors colors)
9061
{
9162
foreach (var group in type.ArgumentGroups)
9263
{
93-
Colorizer.Write(" [White!{0}.exe] ", exeName);
64+
string assemblyNameFormat = $" [{colors.AssemblyNameColor}!{{0}}.exe] "; // " [White!{{0}}.exe] "
65+
Colorizer.Write(assemblyNameFormat, exeName);
9466
if (!string.IsNullOrEmpty(group.Key))
9567
{
96-
Colorizer.Write($"[Green!{group.Key}] ");
68+
string groupFormat = $" [{colors.ArgumentGroupColor}!{{0}}] "; // " [Green!{{0}}] "
69+
Colorizer.Write(string.Format(groupFormat, group.Key));
70+
}
71+
DisplayCommandLine(group.Value, colors);
72+
}
73+
}
74+
75+
private static int DisplayCommandLine(ArgumentGroupInfo arguments, IColors colors)
76+
{
77+
int maxStringSize = 0;
78+
for (int i = 0; i < arguments.RequiredArguments.Count; i++)
79+
{
80+
if (!arguments.RequiredArguments.ContainsKey(i))
81+
{
82+
string requiredArgError = $"{Environment.NewLine}[{colors.ErrorColor}!Error]: Required argument expected at position[{colors.RequiredArgumentColor}!{{0}}]. Type declares arguments at position(s) [{colors.RequiredArgumentColor}!{{1}}]. [{colors.ErrorColor}!Check type definition].";
83+
84+
Colorizer.WriteLine(requiredArgError, i, string.Join(",", arguments.RequiredArguments.Keys.OrderBy(x => x)));
85+
return -1;
9786
}
98-
DisplayCommandLine(group.Value);
87+
88+
var b = arguments.RequiredArguments[i].GetCustomAttribute<ActualArgumentAttribute>();
89+
maxStringSize = Math.Max(maxStringSize, b.Name.Length);
90+
91+
string argumentName = $"[{colors.RequiredArgumentColor}!{{0}}] ";
92+
Colorizer.Write(argumentName, b.Name);
93+
}
94+
95+
foreach (var item in arguments.OptionalArguments.Values)
96+
{
97+
var b = item.GetCustomAttribute<ActualArgumentAttribute>();
98+
maxStringSize = Math.Max(maxStringSize, b.Name.Length);
99+
100+
string optionalArgumentFormat = $"\\[-[{colors.OptionalArgumentColor}!{{0}}] value\\] ";
101+
Colorizer.Write(optionalArgumentFormat, b.Name);
99102
}
103+
104+
Colorizer.WriteLine(string.Empty);
105+
106+
return maxStringSize;
100107
}
101108

102-
private static void DisplayDetailedArgumentHelp(string exeName, string command, ArgumentGroupInfo arguments)
109+
private static void DisplayDetailedArgumentHelp(string exeName, string command, ArgumentGroupInfo arguments, IColors colors)
103110
{
104-
Colorizer.Write(" [White!{0}.exe] ", exeName);
111+
string argumentNameFormat = $" [{colors.AssemblyNameColor}!{{ 0}}.exe] ";
112+
Colorizer.Write(argumentNameFormat, exeName);
105113
if (!string.IsNullOrEmpty(command))
106114
{
107-
Colorizer.Write("[Green!{0}] ", command);
115+
string formatArgs = $"[{colors.ArgumentGroupColor}!{{0}}] ";
116+
Colorizer.Write(formatArgs, command);
108117
}
109-
DisplayDetailedParameterHelp(arguments);
118+
DisplayDetailedParameterHelp(arguments, colors);
110119
}
111120

112-
private static void DisplayDetailedParameterHelp(ArgumentGroupInfo arguments)
121+
private static void DisplayDetailedParameterHelp(ArgumentGroupInfo arguments, IColors colors)
113122
{
114-
int maxStringSize = DisplayCommandLine(arguments);
123+
int maxStringSize = DisplayCommandLine(arguments, colors);
115124
if (maxStringSize < 0)
116125
{
117126
return;
@@ -123,11 +132,13 @@ private static void DisplayDetailedParameterHelp(ArgumentGroupInfo arguments)
123132
var b = arguments.RequiredArguments[i].GetCustomAttribute<ActualArgumentAttribute>();
124133
if (TypeHelpers.IsEnum(arguments.RequiredArguments[i].PropertyType))
125134
{
126-
Colorizer.WriteLine(" - [Cyan!{0}] : {1} (one of [Green!{2}], [Cyan!required])", b.Name.PadRight(maxStringSize), b.Description, GetEnumValuesAsString(arguments.RequiredArguments[i].PropertyType));
135+
string requiredArgFormatDetailed = $" - [{colors.RequiredArgumentColor}!{{0}}] : {{1}} (one of [{colors.ArgumentValueColor}!{{2}}], [{colors.RequiredArgumentColor}!required])";
136+
Colorizer.WriteLine(requiredArgFormatDetailed, b.Name.PadRight(maxStringSize), b.Description, GetEnumValuesAsString(arguments.RequiredArguments[i].PropertyType));
127137
}
128138
else
129139
{
130-
Colorizer.WriteLine(" - [Cyan!{0}] : {1} ([Green!{2}], [Cyan!required])", b.Name.PadRight(maxStringSize), b.Description, GetFriendlyTypeName(arguments.RequiredArguments[i].PropertyType));
140+
string requiredArgFormatDetailed = $" - [{colors.RequiredArgumentColor}!{{0}}] : {{1}} ([{colors.ArgumentValueColor}!{{2}}], [{colors.RequiredArgumentColor}!required])";
141+
Colorizer.WriteLine(requiredArgFormatDetailed, b.Name.PadRight(maxStringSize), b.Description, GetFriendlyTypeName(arguments.RequiredArguments[i].PropertyType));
131142
}
132143
}
133144

@@ -138,11 +149,13 @@ private static void DisplayDetailedParameterHelp(ArgumentGroupInfo arguments)
138149

139150
if (TypeHelpers.IsEnum(item.PropertyType))
140151
{
141-
Colorizer.WriteLine(" - [Yellow!{0}] : {1} (one of [Green!{2}], default=[Yellow!{3}])", b.Name.PadRight(maxStringSize), b.Description, GetEnumValuesAsString(item.PropertyType), b.DefaultValue);
152+
string optionalFormatArg = $" - [{colors.OptionalArgumentColor}!{{0}}] : {{1}} (one of [{colors.ArgumentValueColor}!{{2}}], default=[{colors.OptionalArgumentColor}!{{3}}])";
153+
Colorizer.WriteLine(optionalFormatArg, b.Name.PadRight(maxStringSize), b.Description, GetEnumValuesAsString(item.PropertyType), b.DefaultValue);
142154
}
143155
else
144156
{
145-
Colorizer.WriteLine(" - [Yellow!{0}] : {1} ([Green!{3}], default=[Yellow!{2}])", b.Name.PadRight(maxStringSize), b.Description, b.DefaultValue ?? "", GetFriendlyTypeName(item.PropertyType));
157+
string optionalFormatArg = $" - [{colors.OptionalArgumentColor}!{{0}}] : {{1}} ([{colors.ArgumentValueColor}!{{3}}], default=[{colors.OptionalArgumentColor}!{{2}}])";
158+
Colorizer.WriteLine(optionalFormatArg, b.Name.PadRight(maxStringSize), b.Description, b.DefaultValue ?? "", GetFriendlyTypeName(item.PropertyType));
146159
}
147160
}
148161
Colorizer.WriteLine(string.Empty);
@@ -156,26 +169,33 @@ private static string GetEnumValuesAsString(Type enumType)
156169
private static string GetFriendlyTypeName(Type propertyType)
157170
{
158171
if (propertyType == typeof(string))
172+
{
159173
return "string";
174+
}
160175

161176
if (propertyType == typeof(int) || propertyType == typeof(uint) ||
162177
propertyType == typeof(sbyte) || propertyType == typeof(byte) ||
163178
propertyType == typeof(short) || propertyType == typeof(ushort) ||
164179
propertyType == typeof(double) || propertyType == typeof(float) ||
165180
propertyType == typeof(long) || propertyType == typeof(ulong))
181+
{
166182
return "number";
183+
}
167184

168185
if (propertyType == typeof(bool))
186+
{
169187
return "true or false";
188+
}
170189

171190
if (propertyType == typeof(char))
191+
{
172192
return "char";
173-
174-
if (TypeHelpers.IsEnum(propertyType))
175-
return "enum";
193+
}
176194

177195
if (TypeHelpers.IsList(propertyType))
196+
{
178197
return "list";
198+
}
179199

180200
return "";
181201
}

0 commit comments

Comments
 (0)