Skip to content

Commit

Permalink
imp - bkp - Improved argument parsing for paramete...
Browse files Browse the repository at this point in the history
...rs

---

The argument parser can now differentiate the actual arguments from
parameters without you having to write double quotes. Please note that
this isn't perfect, but should be good enough for general use.

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: True
Part: 1/1
  • Loading branch information
AptiviCEO committed Jan 5, 2025
1 parent e3ce9ab commit b4bb45d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
39 changes: 35 additions & 4 deletions public/Nitrocid/Arguments/ArgumentParse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Nitrocid.Shell.ShellBase.Arguments;
using System;
using System.Collections.Generic;
using System.Text;
using Textify.General;

namespace Nitrocid.Arguments
Expand Down Expand Up @@ -171,10 +172,13 @@ internal static void ParseArguments(string[]? ArgumentsInput, bool earlyStage)
ArgumentsInput ??= [];
var Arguments = earlyStage ? outArgs : AvailableCMDLineArgs;

// Parse the arguments. Assume that unknown arguments are parameters of arguments
string[] finalArguments = FinalizeArguments(ArgumentsInput, Arguments);

// Parse them now
for (int i = 0; i <= ArgumentsInput.Length - 1; i++)
for (int i = 0; i <= finalArguments.Length - 1; i++)
{
string Argument = ArgumentsInput[i];
string Argument = finalArguments[i];
string ArgumentName = Argument.SplitEncloseDoubleQuotes()[0];
if (Arguments.TryGetValue(ArgumentName, out ArgumentInfo? argInfoVal))
{
Expand Down Expand Up @@ -245,11 +249,14 @@ internal static bool IsArgumentPassed(string[]? ArgumentsInput, string argumentN
ArgumentsInput ??= [];
var Arguments = earlyStage ? outArgs : AvailableCMDLineArgs;

// Parse the arguments. Assume that unknown arguments are parameters of arguments
string[] finalArguments = FinalizeArguments(ArgumentsInput, Arguments);

// Parse them now
bool found = false;
for (int i = 0; i <= ArgumentsInput.Length - 1; i++)
for (int i = 0; i <= finalArguments.Length - 1; i++)
{
string Argument = ArgumentsInput[i];
string Argument = finalArguments[i];
string ArgumentName = Argument.SplitEncloseDoubleQuotes()[0];
found = ArgumentName == argumentName && Arguments.ContainsKey(ArgumentName);
if (found)
Expand All @@ -265,5 +272,29 @@ internal static bool IsArgumentPassed(string[]? ArgumentsInput, string argumentN
}
}

private static string[] FinalizeArguments(string[] argumentsInput, Dictionary<string, ArgumentInfo> arguments)
{
// Parse the arguments. Assume that unknown arguments are parameters of arguments
List<string> finalArguments = [];
StringBuilder builder = new();
foreach (var argInput in argumentsInput)
{
if (arguments.TryGetValue(argInput, out ArgumentInfo? argInfoVal))
{
// If we came across a valid argument, add the result and clear the builder
if (builder.Length > 0)
{
finalArguments.Add(builder.ToString().Trim());
builder.Clear();
}
}

// Add the argument name
builder.Append(argInput + " ");
}
if (builder.Length > 0)
finalArguments.Add(builder.ToString().Trim());
return [.. finalArguments];
}
}
}
4 changes: 2 additions & 2 deletions public/Nitrocid/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"Nitrocid KS - Pre-boot in Spanish": {
"commandName": "Project",
"commandLineArgs": "\"lang spa\" verbosepreboot"
"commandLineArgs": "lang spa verbosepreboot"
},
"Nitrocid KS - Enable pre-boot messages": {
"commandName": "Project",
Expand All @@ -60,4 +60,4 @@
"commandLineArgs": "apiversion"
}
}
}
}

0 comments on commit b4bb45d

Please sign in to comment.