Skip to content

Commit 059531f

Browse files
authored
Merge pull request #52 from AlexGhiondea/Renames
Change API shape
2 parents 990c3fa + eaf7de6 commit 059531f

17 files changed

+392
-194
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,20 @@ The `<name>` represents the optional parameter name as defined in the type decla
203203

204204
You can change the prefix for the Parser:
205205
```csharp
206-
Parser.Configuration.EnvironmentVariablePrefix = "myPrefix";
206+
ParserOptions parserOptions = new ParserOptions() { VariableNamePrefix = "myPrefix" };
207+
208+
if (!Parser.TryParse(args, out Options options, parserOptions))
209+
{
210+
return;
211+
}
207212
```
208213

209214
You can also disable the feature completely:
210215
```csharp
211-
Parser.Configuration.UseEnvironmentVariables = false;
216+
ParserOptions parserOptions = new ParserOptions() { ReadFromEnvironment = false };
217+
218+
if (!Parser.TryParse(args, out Options options, parserOptions))
219+
{
220+
return;
221+
}
212222
```

src/CommandLine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<AssemblyVersion>2.0.0</AssemblyVersion>
1111
<FileVersion>$(AssemblyVersion)</FileVersion>
1212
<VersionPrefix>2.0.0</VersionPrefix>
13-
<VersionSuffix>preview</VersionSuffix>
13+
<VersionSuffix>preview2</VersionSuffix>
1414
</PropertyGroup>
1515

1616
<PropertyGroup>

src/Help/Colors/DarkBackgroundColors.cs renamed to src/Help/ColorScheme/DarkBackground.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22

3-
namespace CommandLine.Colors
3+
namespace CommandLine.ColorScheme
44
{
5-
public class DarkBackgroundColors : IColors
5+
public class DarkBackground : IColors
66
{
77
public ConsoleColor AssemblyNameColor => ConsoleColor.White;
88
public ConsoleColor ArgumentGroupColor => ConsoleColor.Green;
@@ -12,7 +12,7 @@ public class DarkBackgroundColors : IColors
1212
public ConsoleColor ArgumentValueColor => ConsoleColor.Green;
1313
}
1414

15-
public class DarkYellowBackgroundColors : IColors
15+
public class DarkYellowBackground : IColors
1616
{
1717
public ConsoleColor AssemblyNameColor => ConsoleColor.White;
1818
public ConsoleColor ArgumentGroupColor => ConsoleColor.Green;
@@ -22,7 +22,7 @@ public class DarkYellowBackgroundColors : IColors
2222
public ConsoleColor ArgumentValueColor => ConsoleColor.DarkGray;
2323
}
2424

25-
public class GreenBackgroundColors : IColors
25+
public class GreenBackground : IColors
2626
{
2727
public ConsoleColor AssemblyNameColor => ConsoleColor.White;
2828
public ConsoleColor ArgumentGroupColor => ConsoleColor.DarkGreen;
@@ -32,7 +32,7 @@ public class GreenBackgroundColors : IColors
3232
public ConsoleColor ArgumentValueColor => ConsoleColor.DarkGray;
3333
}
3434

35-
public class CyanBackgroundColors : IColors
35+
public class CyanBackground : IColors
3636
{
3737
public ConsoleColor AssemblyNameColor => ConsoleColor.White;
3838
public ConsoleColor ArgumentGroupColor => ConsoleColor.DarkGreen;
@@ -42,7 +42,7 @@ public class CyanBackgroundColors : IColors
4242
public ConsoleColor ArgumentValueColor => ConsoleColor.DarkGray;
4343
}
4444

45-
public class RedBackgroundColors : IColors
45+
public class RedBackground : IColors
4646
{
4747
public ConsoleColor AssemblyNameColor => ConsoleColor.White;
4848
public ConsoleColor ArgumentGroupColor => ConsoleColor.Green;

src/Help/Colors/IColors.cs renamed to src/Help/ColorScheme/IColors.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace CommandLine.Colors
3+
namespace CommandLine.ColorScheme
44
{
55
public interface IColors
66
{

src/Help/Colors/LightBackgroundColors.cs renamed to src/Help/ColorScheme/LightBackground.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22

3-
namespace CommandLine.Colors
3+
namespace CommandLine.ColorScheme
44
{
5-
public class LightBackgroundColors : IColors
5+
public class LightBackground : IColors
66
{
77
public ConsoleColor ErrorColor => ConsoleColor.Red;
88
public ConsoleColor AssemblyNameColor => ConsoleColor.DarkGray;
@@ -12,7 +12,7 @@ public class LightBackgroundColors : IColors
1212
public ConsoleColor ArgumentValueColor => ConsoleColor.DarkGreen;
1313
}
1414

15-
public class GrayBackgroundColors : IColors
15+
public class GrayBackground : IColors
1616
{
1717
public ConsoleColor ErrorColor => ConsoleColor.Red;
1818
public ConsoleColor AssemblyNameColor => ConsoleColor.DarkGray;

src/Help/HelpGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Linq;
66
using System.Reflection;
7-
using CommandLine.Colors;
7+
using CommandLine.ColorScheme;
88

99
namespace CommandLine
1010
{

src/Parser.Configuration.cs

Lines changed: 54 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,88 +8,79 @@
88
using System.IO;
99
using System.Linq;
1010
using System.Reflection;
11-
using CommandLine.Colors;
11+
using CommandLine.ColorScheme;
1212

1313
namespace CommandLine
1414
{
1515
public static partial class Parser
1616
{
17-
public static class Configuration
17+
public static class ColorScheme
1818
{
19-
public static bool DisplayErrorMessageOnError { get; set; } = true;
19+
private static IColors s_helpColors;
20+
private static object s_lockObj = new object();
2021

21-
public static string EnvironmentVariablePrefix { get; set; } = "Commandline_";
22-
23-
public static bool UseEnvironmentVariables { get; set; } = true;
24-
25-
public static class DisplayColors
22+
public static IColors Get(ConsoleColor backgroundColor = (ConsoleColor)(-1))
2623
{
27-
private static IColors s_helpColors;
28-
private static object s_lockObj = new object();
29-
30-
public static IColors Get(ConsoleColor backgroundColor = (ConsoleColor)(-1))
24+
if (s_helpColors != null)
3125
{
32-
if (s_helpColors != null)
33-
{
34-
return s_helpColors;
35-
}
26+
return s_helpColors;
27+
}
3628

37-
// if the color is not specified, default to the current console's background color.
38-
if (backgroundColor == (ConsoleColor)(-1))
39-
{
40-
backgroundColor = Console.BackgroundColor;
41-
}
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+
}
4234

43-
// Get the color scheme.
44-
lock (s_lockObj)
35+
// Get the color scheme.
36+
lock (s_lockObj)
37+
{
38+
if (s_helpColors == null)
4539
{
46-
if (s_helpColors == null)
47-
{
48-
s_helpColors = GetDefault(backgroundColor);
49-
}
40+
s_helpColors = GetDefault(backgroundColor);
5041
}
51-
52-
return s_helpColors;
5342
}
5443

55-
private static IColors GetDefault(ConsoleColor backgroundColor)
44+
return s_helpColors;
45+
}
46+
47+
private static IColors GetDefault(ConsoleColor backgroundColor)
48+
{
49+
switch (backgroundColor)
5650
{
57-
switch (backgroundColor)
58-
{
59-
case ConsoleColor.Black:
60-
case ConsoleColor.Blue:
61-
case ConsoleColor.DarkBlue:
62-
case ConsoleColor.DarkCyan:
63-
case ConsoleColor.DarkGray:
64-
case ConsoleColor.DarkGreen:
65-
case ConsoleColor.DarkMagenta:
66-
case ConsoleColor.DarkRed:
67-
case ConsoleColor.Magenta:
68-
return new DarkBackgroundColors();
69-
case ConsoleColor.DarkYellow:
70-
return new DarkYellowBackgroundColors();
71-
case ConsoleColor.Gray:
72-
return new GrayBackgroundColors();
73-
case ConsoleColor.Green:
74-
return new GreenBackgroundColors();
75-
case ConsoleColor.Red:
76-
return new RedBackgroundColors();
77-
case ConsoleColor.Cyan:
78-
return new CyanBackgroundColors();
79-
case ConsoleColor.White:
80-
case ConsoleColor.Yellow:
81-
return new LightBackgroundColors();
82-
default:
83-
return new DarkBackgroundColors();
84-
}
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();
8576
}
77+
}
8678

87-
public static void Set(IColors colors)
79+
public static void Set(IColors colors)
80+
{
81+
lock (s_lockObj)
8882
{
89-
lock (s_lockObj)
90-
{
91-
s_helpColors = colors;
92-
}
83+
s_helpColors = colors;
9384
}
9485
}
9586
}

src/Parser.Internal.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,19 @@ 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.DisplayColors.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;
154154
}
155155

156-
options = InternalParse<TOptions>(args, 1, arguments.ArgumentGroups[args[0]]);
156+
options = InternalParse<TOptions>(args, 1, arguments.ArgumentGroups[args[0]], s_defaultParseOptions);
157157
arguments.ActionArgument.SetValue(options, PropertyHelpers.GetValueAsType(args[0], arguments.ActionArgument));
158158
return true;
159159
}
160160

161-
private static TOptions InternalParse<TOptions>(string[] args, int offsetInArray, ArgumentGroupInfo arguments) where TOptions : new()
161+
private static TOptions InternalParse<TOptions>(string[] args, int offsetInArray, ArgumentGroupInfo arguments, ParserOptions parseOptions)
162+
where TOptions : new()
162163
{
163164
TOptions options = new TOptions();
164165
int currentLogicalPosition = 0;
@@ -180,9 +181,9 @@ private static IEnumerable<string> SplitCommandLineIntoSegments(string line)
180181
object defaultValue = value.DefaultValue;
181182

182183
// If we want to read values from the environment, try to get the value
183-
if (Configuration.UseEnvironmentVariables && !value.IsCollection)
184+
if (parseOptions.ReadFromEnvironment && !value.IsCollection && parseOptions.VariableNamePrefix != null)
184185
{
185-
var envVar = Environment.GetEnvironmentVariable($"{Configuration.EnvironmentVariablePrefix}{value.Name}");
186+
var envVar = Environment.GetEnvironmentVariable($"{parseOptions.VariableNamePrefix}{value.Name}");
186187

187188
if (!string.IsNullOrEmpty(envVar))
188189
{

0 commit comments

Comments
 (0)