8000 Add environment variable directive parser · jonsequitur/command-line-api@48816c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 48816c4

Browse files
fredrikhrjonsequitur
authored andcommitted
Add environment variable directive parser
1 parent af42e2b commit 48816c4

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.CommandLine.Builder;
4+
using System.CommandLine.Invocation;
5+
using System.CommandLine.Parsing;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
using Xunit;
10+
11+
namespace System.CommandLine.Tests
12+
{
13+
public class EnvironmentVariableDirectiveTests
14+
{
15+
private static readonly Random randomizer = new Random(Seed: 456476756);
16+
17+
[Fact]
18+
public static async Task Sets_environment_variable_to_value()
19+
{
20+
bool asserted = false;
21+
string variable = $"TEST_ENVIRONMENT_VARIABLE{randomizer.Next()}";
22+
const string value = "This is a test";
23+
var rootCommand = new RootCommand
24+
{
25+
Handler = CommandHandler.Create(() =>
26+
{
27+
asserted = true;
28+
Assert.Equal(value, Environment.GetEnvironmentVariable(variable));
29+
})
30+
};
31+
32+
var parser = new CommandLineBuilder(rootCommand)
33+
.UseEnvironmentVariableDirective()
34+
.Build();
35+
36+
await parser.InvokeAsync(new[] { $"[env:{variable}={value}]" });
37+
38+
Assert.True(asserted);
39+
}
40+
41+
[Fact]
42+
public static async Task Trims_environment_variable_name()
43+
{
44+
bool asserted = false;
45+
string variable = $"TEST_ENVIRONMENT_VARIABLE{randomizer.Next()}";
46+
const string value = "This is a test";
47+
var rootCommand = new RootCommand
48+
{
49+
Handler = CommandHandler.Create(() =>
50+
{
51+
asserted = true;
52+
Assert.Equal(value, Environment.GetEnvironmentVariable(variable));
53+
})
54+
};
55+
56+
var parser = new CommandLineBuilder(rootCommand)
57+
.UseEnvironmentVariableDirective()
58+
.Build();
59+
60+
await parser.InvokeAsync(new[] { $"[env: {variable} ={value}]" });
61+
62+
Assert.True(asserted);
63+
}
64+
65+
[Fact]
66+
public static async Task Trims_environment_variable_value()
67+
{
68+
bool asserted = false;
69+
string variable = $"TEST_ENVIRONMENT_VARIABLE{randomizer.Next()}";
70+
const string value = "This is a test";
71+
var rootCommand = new RootCommand
72+
{
73+
Handler = CommandHandler.Create(() =>
74+
{
75+
asserted = true;
76+
Assert.Equal(value, Environment.GetEnvironmentVariable(variable));
77+
})
78+
};
79+
80+
var parser = new CommandLineBuilder(rootCommand)
81+
.UseEnvironmentVariableDirective()
82+
.Build();
83+
84+
await parser.InvokeAsync(new[] { $"[env:{variable}= {value} ]" });
85+
86+
Assert.True(asserted);
87+
}
88+
}
89+
}

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,36 @@ public static CommandLineBuilder UseDebugDirective(
228228
return builder;
229229
}
230230

231+
public static CommandLineBuilder UseEnvironmentVariableDirective(
232+
this CommandLineBuilder builder)
233+
{
234+
builder.AddMiddleware((context, next) =>
235+
{
236+
if (context.ParseResult.Directives.TryGetValues("env", out var directives))
237+
{
238+
foreach (var envDirective in directives)
239+
{
240+
var components = envDirective.Split(new[] { '=' }, count: 2);
241+
var variable = components.Length > 0 ? components[0].Trim() : string.Empty;
242+
if (string.IsNullOrEmpty(variable) || components.Length < 2)
243+
continue;
244+
var value = components[1].Trim();
245+
SetEnvironmentVariable(variable, value);
246+
}
247+
}
248+
249+
return next(context);
250+
}, MiddlewareOrderInternal.EnvironmentVariableDirective);
251+
252+
return builder;
253+
}
254+
231255
public static CommandLineBuilder UseDefaults(this CommandLineBuilder builder)
232256
{
233257
return builder
234258
.UseVersionOption()
235259
.UseHelp()
260+
.UseEnvironmentVariableDirective()
236261
.UseParseDirective()
237262
.UseDebugDirective()
238263
.UseSuggestDirective()

src/System.CommandLine/Invocation/MiddlewareOrder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal enum MiddlewareOrderInternal
1515
{
1616
Startup = -4000,
1717
ExceptionHandler = -3000,
18+
EnvironmentVariableDirective = -2600,
1819
ConfigureConsole = -2500,
1920
RegisterWithDotnetSuggest = -2400,
2021
DebugDirective = -2300,

0 commit comments

Comments
 (0)
0