Skip to content

Commit a676f38

Browse files
committed
Move the ColorScheme object higher in the hierarchy.
1 parent 4853d35 commit a676f38

File tree

6 files changed

+100
-103
lines changed

6 files changed

+100
-103
lines changed

src/Parser.Configuration.cs

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,76 +14,73 @@ namespace CommandLine
1414
{
1515
public static partial class Parser
1616
{
17-
public static class Configuration
17+
public static class ColorScheme
1818
{
19-
public static class ColorScheme
20-
{
21-
private static IColors s_helpColors;
22-
private static object s_lockObj = new object();
19+
private static IColors s_helpColors;
20+
private static object s_lockObj = new object();
2321

24-
public static IColors Get(ConsoleColor backgroundColor = (ConsoleColor)(-1))
22+
public static IColors Get(ConsoleColor backgroundColor = (ConsoleColor)(-1))
23+
{
24+
if (s_helpColors != null)
2525
{
26-
if (s_helpColors != null)
27-
{
28-
return s_helpColors;
29-
}
26+
return s_helpColors;
27+
}
3028

31-
// if the color is not specified, default to the current console's background color.
32-
if (backgroundColor == (ConsoleColor)(-1))
33-
{
34-
backgroundColor = Console.BackgroundColor;
35-
}
29+
// if the color is not specified, default to the current console's background color.
30+
if (backgroundColor == (ConsoleColor)(-1))
31+
{
32+
backgroundColor = Console.BackgroundColor;
33+
}
3634

37-
// Get the color scheme.
38-
lock (s_lockObj)
35+
// Get the color scheme.
36+
lock (s_lockObj)
37+
{
38+
if (s_helpColors == null)
3939
{
40-
if (s_helpColors == null)
41-
{
42-
s_helpColors = GetDefault(backgroundColor);
43-
}
40+
s_helpColors = GetDefault(backgroundColor);
4441
}
45-
46-
return s_helpColors;
4742
}
4843

49-
private static IColors GetDefault(ConsoleColor backgroundColor)
44+
return s_helpColors;
45+
}
46+
47+
private static IColors GetDefault(ConsoleColor backgroundColor)
48+
{
49+
switch (backgroundColor)
5050
{
51-
switch (backgroundColor)
52-
{
53-
case ConsoleColor.Black:
54-
case ConsoleColor.Blue:
55-
case ConsoleColor.DarkBlue:
56-
case ConsoleColor.DarkCyan:
57-
case ConsoleColor.DarkGray:
58-
case ConsoleColor.DarkGreen:
59-
case ConsoleColor.DarkMagenta:
60-
case ConsoleColor.DarkRed:
61-
case ConsoleColor.Magenta:
62-
return new DarkBackground();
63-
case ConsoleColor.DarkYellow:
64-
return new DarkYellowBackground();
65-
case ConsoleColor.Gray:
66-
return new GrayBackground();
67-
case ConsoleColor.Green:
68-
return new GreenBackground();
69-
case ConsoleColor.Red:
70-
return new RedBackground();
71-
case ConsoleColor.Cyan:
72-
return new CyanBackground();
73-
case ConsoleColor.White:
74-
case ConsoleColor.Yellow:
75-
return new LightBackground();
76-
default:
77-
return new DarkBackground();
78-
}
51+
case ConsoleColor.Black:
52+
case ConsoleColor.Blue:
53+
case ConsoleColor.DarkBlue:
54+
case ConsoleColor.DarkCyan:
55+
case ConsoleColor.DarkGray:
56+
case ConsoleColor.DarkGreen:
57+
case ConsoleColor.DarkMagenta:
58+
case ConsoleColor.DarkRed:
59+
case ConsoleColor.Magenta:
60+
return new DarkBackground();
61+
case ConsoleColor.DarkYellow:
62+
return new DarkYellowBackground();
63+
case ConsoleColor.Gray:
64+
return new GrayBackground();
65+
case ConsoleColor.Green:
66+
return new GreenBackground();
67+
case ConsoleColor.Red:
68+
return new RedBackground();
69+
case ConsoleColor.Cyan:
70+
return new CyanBackground();
71+
case ConsoleColor.White:
72+
case ConsoleColor.Yellow:
73+
return new LightBackground();
74+
default:
75+
return new DarkBackground();
7976
}
77+
}
8078

81-
public static void Set(IColors colors)
79+
public static void Set(IColors colors)
80+
{
81+
lock (s_lockObj)
8282
{
83-
lock (s_lockObj)
84-
{
85-
s_helpColors = colors;
86-
}
83+
s_helpColors = colors;
8784
}
8885
}
8986
}

src/Parser.Internal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private static IEnumerable<string> SplitCommandLineIntoSegments(string line)
147147
// short circuit the request for help!
148148
if (args.Length == 2 && (args[1] == "/?" || args[1] == "-?"))
149149
{
150-
HelpGenerator.DisplayHelpForCommmand(args[0], arguments.ArgumentGroups[args[0]], Configuration.ColorScheme.Get());
150+
HelpGenerator.DisplayHelpForCommmand(args[0], arguments.ArgumentGroups[args[0]], ColorScheme.Get());
151151

152152
// if we wanted the help, then we successfully parsed it!
153153
return true;

src/Parser.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ public static void DisplayHelp<TOptions>(HelpFormat helpFormat = HelpFormat.Shor
128128
TypeHelpers.ScanTypeForProperties<TOptions>(out TypeArgumentInfo arguments);
129129

130130
// If we get here, the options type is well defined, so let's display the help.
131-
HelpGenerator.DisplayHelp(helpFormat, arguments, Configuration.ColorScheme.Get());
131+
HelpGenerator.DisplayHelp(helpFormat, arguments, ColorScheme.Get());
132132
}
133133
catch (Exception ex)
134134
{
135-
string errorFormat = $"[{Configuration.ColorScheme.Get().ErrorColor}!Error]: {{0}} {{1}}";
135+
string errorFormat = $"[{ColorScheme.Get().ErrorColor}!Error]: {{0}} {{1}}";
136136
// If we were asked to display the help and something went wrong, display the error that went wrong.
137137
Colorizer.WriteLine(errorFormat, ex.Message, Environment.NewLine);
138138
}
@@ -159,12 +159,12 @@ private static bool InternalTryParse<TOptions>(string[] args, ParserOptions pars
159159
{
160160
if (args[0] == HelpGenerator.RequestShortHelpParameter || args[0] == "/?")
161161
{
162-
HelpGenerator.DisplayHelp(HelpFormat.Short, arguments, Configuration.ColorScheme.Get());
162+
HelpGenerator.DisplayHelp(HelpFormat.Short, arguments, ColorScheme.Get());
163163
return true;
164164
}
165165
else if (args[0] == HelpGenerator.RequestLongHelpParameter)
166166
{
167-
HelpGenerator.DisplayHelp(HelpFormat.Full, arguments, Configuration.ColorScheme.Get());
167+
HelpGenerator.DisplayHelp(HelpFormat.Full, arguments, ColorScheme.Get());
168168
return true;
169169
}
170170
}
@@ -184,10 +184,10 @@ private static bool InternalTryParse<TOptions>(string[] args, ParserOptions pars
184184
ex = new ParserException(innerParserException.Message, innerParserException);
185185
if (parserOptions.LogParseErrorToConsole)
186186
{
187-
string errorFormat = $"[{Configuration.ColorScheme.Get().ErrorColor}!Error]: {{0}} {{1}}";
187+
string errorFormat = $"[{ColorScheme.Get().ErrorColor}!Error]: {{0}} {{1}}";
188188
Colorizer.WriteLine(errorFormat, ex.Message, Environment.NewLine);
189189

190-
HelpGenerator.DisplayHelp(HelpFormat.Short, arguments, Configuration.ColorScheme.Get());
190+
HelpGenerator.DisplayHelp(HelpFormat.Short, arguments, ColorScheme.Get());
191191
}
192192
return false;
193193
}

test/ColorizerWriterObject/TestWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public string ToTestCode()
5555

5656
private string GetColorPropertyNameForColor()
5757
{
58-
IColors currentColorScheme = Parser.Configuration.ColorScheme.Get();
58+
IColors currentColorScheme = Parser.ColorScheme.Get();
5959
string propertyName = "";
6060
// check which color matches the current ForegroundColor.
6161
if (currentColorScheme.ArgumentGroupColor == ForegroundColor) propertyName = nameof(currentColorScheme.ArgumentGroupColor);

test/CommandLineTests.Help.cs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,8 @@ public void GetColorScheme()
780780
try
781781
{
782782
Console.BackgroundColor = ConsoleColor.Black;
783-
Parser.Configuration.ColorScheme.Set(null);
784-
Assert.IsType<DarkBackground>(Parser.Configuration.ColorScheme.Get());
783+
Parser.ColorScheme.Set(null);
784+
Assert.IsType<DarkBackground>(Parser.ColorScheme.Get());
785785
}
786786
finally
787787
{
@@ -797,50 +797,50 @@ public void ValidateBackgroundColorScheme()
797797
try
798798
{
799799
#region Dark backgound
800-
Parser.Configuration.ColorScheme.Set(null);
801-
Assert.IsType<DarkBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.Black));
802-
Parser.Configuration.ColorScheme.Set(null);
803-
Assert.IsType<DarkBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.DarkGray));
804-
Parser.Configuration.ColorScheme.Set(null);
805-
Assert.IsType<DarkBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.DarkCyan));
806-
Parser.Configuration.ColorScheme.Set(null);
807-
Assert.IsType<DarkBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.DarkGreen));
808-
Parser.Configuration.ColorScheme.Set(null);
809-
Assert.IsType<DarkBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.DarkMagenta));
810-
Parser.Configuration.ColorScheme.Set(null);
811-
Assert.IsType<DarkBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.DarkRed));
812-
Parser.Configuration.ColorScheme.Set(null);
813-
Assert.IsType<DarkBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.Blue));
814-
Parser.Configuration.ColorScheme.Set(null);
815-
Assert.IsType<DarkBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.Magenta));
816-
Parser.Configuration.ColorScheme.Set(null);
817-
Assert.IsType<DarkBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.DarkBlue));
800+
Parser.ColorScheme.Set(null);
801+
Assert.IsType<DarkBackground>(Parser.ColorScheme.Get(ConsoleColor.Black));
802+
Parser.ColorScheme.Set(null);
803+
Assert.IsType<DarkBackground>(Parser.ColorScheme.Get(ConsoleColor.DarkGray));
804+
Parser.ColorScheme.Set(null);
805+
Assert.IsType<DarkBackground>(Parser.ColorScheme.Get(ConsoleColor.DarkCyan));
806+
Parser.ColorScheme.Set(null);
807+
Assert.IsType<DarkBackground>(Parser.ColorScheme.Get(ConsoleColor.DarkGreen));
808+
Parser.ColorScheme.Set(null);
809+
Assert.IsType<DarkBackground>(Parser.ColorScheme.Get(ConsoleColor.DarkMagenta));
810+
Parser.ColorScheme.Set(null);
811+
Assert.IsType<DarkBackground>(Parser.ColorScheme.Get(ConsoleColor.DarkRed));
812+
Parser.ColorScheme.Set(null);
813+
Assert.IsType<DarkBackground>(Parser.ColorScheme.Get(ConsoleColor.Blue));
814+
Parser.ColorScheme.Set(null);
815+
Assert.IsType<DarkBackground>(Parser.ColorScheme.Get(ConsoleColor.Magenta));
816+
Parser.ColorScheme.Set(null);
817+
Assert.IsType<DarkBackground>(Parser.ColorScheme.Get(ConsoleColor.DarkBlue));
818818
#endregion
819819

820820
#region Multicolor
821-
Parser.Configuration.ColorScheme.Set(null);
822-
Assert.IsType<GrayBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.Gray));
823-
Parser.Configuration.ColorScheme.Set(null);
824-
Assert.IsType<DarkYellowBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.DarkYellow));
825-
Parser.Configuration.ColorScheme.Set(null);
826-
Assert.IsType<GreenBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.Green));
827-
Parser.Configuration.ColorScheme.Set(null);
828-
Assert.IsType<CyanBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.Cyan));
829-
Parser.Configuration.ColorScheme.Set(null);
830-
Assert.IsType<RedBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.Red));
821+
Parser.ColorScheme.Set(null);
822+
Assert.IsType<GrayBackground>(Parser.ColorScheme.Get(ConsoleColor.Gray));
823+
Parser.ColorScheme.Set(null);
824+
Assert.IsType<DarkYellowBackground>(Parser.ColorScheme.Get(ConsoleColor.DarkYellow));
825+
Parser.ColorScheme.Set(null);
826+
Assert.IsType<GreenBackground>(Parser.ColorScheme.Get(ConsoleColor.Green));
827+
Parser.ColorScheme.Set(null);
828+
Assert.IsType<CyanBackground>(Parser.ColorScheme.Get(ConsoleColor.Cyan));
829+
Parser.ColorScheme.Set(null);
830+
Assert.IsType<RedBackground>(Parser.ColorScheme.Get(ConsoleColor.Red));
831831

832832
#endregion
833833

834834
#region Light background
835-
Parser.Configuration.ColorScheme.Set(null);
836-
Assert.IsType<LightBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.White));
837-
Parser.Configuration.ColorScheme.Set(null);
838-
Assert.IsType<LightBackground>(Parser.Configuration.ColorScheme.Get(ConsoleColor.Yellow));
835+
Parser.ColorScheme.Set(null);
836+
Assert.IsType<LightBackground>(Parser.ColorScheme.Get(ConsoleColor.White));
837+
Parser.ColorScheme.Set(null);
838+
Assert.IsType<LightBackground>(Parser.ColorScheme.Get(ConsoleColor.Yellow));
839839
#endregion
840840

841841
#region Unknown color
842-
Parser.Configuration.ColorScheme.Set(null);
843-
Assert.IsType<DarkBackground>(Parser.Configuration.ColorScheme.Get((ConsoleColor)25));
842+
Parser.ColorScheme.Set(null);
843+
Assert.IsType<DarkBackground>(Parser.ColorScheme.Get((ConsoleColor)25));
844844
#endregion
845845
}
846846
finally

test/Helpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static T Parse<T>(string argString, IOutputWriter writer = null, IColors
2929
}
3030

3131
Colorizer.SetupWriter(writer);
32-
Parser.Configuration.ColorScheme.Set(colors);
32+
Parser.ColorScheme.Set(colors);
3333
return Parser.Parse<T>(argString, parserOptions);
3434
}
3535

@@ -46,7 +46,7 @@ public static T Parse<T>(string argString, IOutputWriter writer = null, IColors
4646
}
4747

4848
Colorizer.SetupWriter(writer);
49-
Parser.Configuration.ColorScheme.Set(colors);
49+
Parser.ColorScheme.Set(colors);
5050
Parser.DisplayHelp<T>(helpFormat);
5151
}
5252

0 commit comments

Comments
 (0)