Skip to content

Commit 8c51464

Browse files
committed
Change parsing arguments
1 parent afd60aa commit 8c51464

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

Source/Monitorian.Core/AppKeeper.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public async Task<bool> StartAsync(StartupEventArgs e, IEnumerable<string> addit
3737

3838
SubscribeExceptions();
3939

40-
var (success, response) = StartupAgent.Start(ProductInfo.Product, ProductInfo.StartupTaskId, OtherArguments);
40+
var (success, response) = StartupAgent.Start(ProductInfo.Product, ProductInfo.StartupTaskId, ForwardingArguments);
4141
if (!success && (response is not null))
4242
{
4343
ConsoleService.WriteLine(response);
@@ -66,6 +66,9 @@ public void Write(string content)
6666
public static IReadOnlyList<string> OtherArguments => _otherArguments?.ToArray() ?? [];
6767
private static string[] _otherArguments;
6868

69+
public static IReadOnlyList<string> ForwardingArguments => _forwardingArguments?.ToArray() ?? [];
70+
private static string[] _forwardingArguments;
71+
6972
public static IEnumerable<string> EnumerateStandardOptions() =>
7073
new[]
7174
{
@@ -78,26 +81,32 @@ public static IEnumerable<string> EnumerateStandardOptions() =>
7881

7982
private async Task ParseArgumentsAsync(StartupEventArgs e, string[] standardOptions)
8083
{
81-
// Load persistent arguments.
82-
var args = (await LoadArgumentsAsync())?.Split() ?? [];
83-
84-
// Concatenate current and persistent arguments.
84+
// Divide current arguments.
8585
// The first element of StartupEventArgs.Args is not executing assembly's path unlike
8686
// that of arguments provided by Environment.GetCommandLineArgs method.
87-
args = e.Args.Concat(args.Select(x => x.Trim('"'))).ToArray();
88-
if (args is not { Length: > 0 })
89-
return;
87+
var (currentStandard, currentOther) = Divide(e.Args);
9088

91-
const char optionMark = '/';
92-
var isStandard = false;
89+
// Divide persistent arguments.
90+
var persistentArgs = (await LoadArgumentsAsync())?.Split().Select(x => x.Trim('"'));
91+
var (persistentStandard, persistentOther) = Divide(persistentArgs);
9392

94-
var buffer = args
95-
.Where(x => !string.IsNullOrWhiteSpace(x))
96-
.GroupBy(x => (x[0] == optionMark) ? (isStandard = standardOptions.Contains(x.ToLower())) : isStandard)
97-
.ToArray();
93+
_standardArguments = persistentStandard.Concat(currentStandard).ToArray();
94+
_forwardingArguments = currentOther.ToArray();
95+
_otherArguments = persistentOther.Concat(_forwardingArguments).ToArray();
9896

99-
_standardArguments = buffer.SingleOrDefault(x => x.Key)?.ToArray();
100-
_otherArguments = buffer.SingleOrDefault(x => !x.Key)?.ToArray();
97+
(IEnumerable<string> standard, IEnumerable<string> other) Divide(IEnumerable<string> args)
98+
{
99+
const char optionMark = '/';
100+
var isStandard = false;
101+
102+
var buffer = args
103+
.Where(x => !string.IsNullOrWhiteSpace(x))
104+
.GroupBy(x => (x[0] == optionMark) ? (isStandard = standardOptions.Contains(x.ToLower())) : isStandard)
105+
.ToArray() ?? [];
106+
107+
return (standard: buffer.SingleOrDefault(x => x.Key) ?? Enumerable.Empty<string>(),
108+
other: buffer.SingleOrDefault(x => !x.Key) ?? Enumerable.Empty<string>());
109+
}
101110
}
102111

103112
private const string ArgumentsFileName = "arguments.txt";

Source/StartupAgency/PipeHolder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public PipeHolder(string name, Func<string[], Task<string>> handleRequestAsync)
3737
/// <summary>
3838
/// Creates <see cref="System.Threading.Semaphore"/> to start named pipes.
3939
/// </summary>
40-
/// <param name="args">Arguments to another instance</param>
40+
/// <param name="args">Arguments being forwarded to another instance</param>
4141
/// <returns>
4242
/// <para>success: True if no other instance exists and this instance successfully creates</para>
4343
/// <para>response: Response from another instance if that instance exists and returns an response</para>

Source/StartupAgency/StartupAgent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class StartupAgent : IDisposable
2222
/// </summary>
2323
/// <param name="name">Name</param>
2424
/// <param name="startupTaskId">Startup task ID</param>
25-
/// <param name="args">Arguments to another instance</param>
25+
/// <param name="args">Arguments being forwarded to another instance</param>
2626
/// <returns>
2727
/// <para>success: True if no other instance exists and this instance successfully starts</para>
2828
/// <para>response: Response from another instance if that instance exists and returns an response</para>

0 commit comments

Comments
 (0)