Skip to content

Commit 983641e

Browse files
committed
Fixed issue involving oddball Windows pre-processing of command lines. Minor cleanup.
1 parent 8005665 commit 983641e

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

Binder/J4JCommandLineExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ namespace J4JSoftware.Configuration.CommandLine
44
{
55
public static class J4JCommandLineExtensions
66
{
7+
public static IConfigurationBuilder AddJ4JCommandLine(
8+
this IConfigurationBuilder builder,
9+
IOptionCollection options)
10+
{
11+
var rawCmdLine = new RawCommandLine();
12+
13+
builder.Add(new J4JCommandLineSource(options, rawCmdLine.GetRawCommandLine()));
14+
15+
return builder;
16+
}
17+
718
public static IConfigurationBuilder AddJ4JCommandLine(
819
this IConfigurationBuilder builder,
920
string cmdLine,

Binder/J4JCommandLineSource.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using J4JSoftware.Logging;
3-
using Microsoft.Extensions.Configuration;
1+
using Microsoft.Extensions.Configuration;
42

53
namespace J4JSoftware.Configuration.CommandLine
64
{

Binder/RawCommandLine.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace J4JSoftware.Configuration.CommandLine
5+
{
6+
public class RawCommandLine
7+
{
8+
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
9+
private static extern IntPtr GetCommandLine();
10+
11+
private readonly bool _useKernel32;
12+
13+
public RawCommandLine()
14+
{
15+
_useKernel32 = Environment.OSVersion.Platform switch
16+
{
17+
PlatformID.Win32NT => true,
18+
PlatformID.Win32S => true,
19+
PlatformID.Win32Windows => true,
20+
PlatformID.WinCE => true,
21+
_ => false
22+
};
23+
}
24+
25+
public string GetRawCommandLine()
26+
{
27+
if( !_useKernel32 )
28+
return Environment.CommandLine;
29+
30+
IntPtr ptr = GetCommandLine();
31+
32+
return Marshal.PtrToStringAuto( ptr )!;
33+
}
34+
}
35+
}

Binder/master-text/MasterTextCollection.static.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.ComponentModel;
32
using J4JSoftware.Logging;
43

54
namespace J4JSoftware.Configuration.CommandLine

0 commit comments

Comments
 (0)