ArgumentParser
public static class ArgumentParser
Inherits: None
Namespace: FractalPike.PikeConsole.Core.RuntimeExecution
Description
Takes arguments and performs parsing actions on them.
This is a suplement to regular parsing methods, not a replacement.
Regular parsing, such as for a singular int, should still be handled
with the native .NET parser (Example: int.TryParse).
Properties
No public properties to showcase for this class.
Methods
| Scope | Return | Name |
|---|---|---|
public |
bool |
ValidateCount |
public |
bool |
TryParseBool |
public |
bool |
TryParseEnum |
public |
bool |
TryParseManyInt |
public |
bool |
TryParseManyFloat |
public |
bool |
TryParseManyDouble |
public |
bool |
TryParseManyBool |
public |
bool |
TryParseManyEnum |
Method Descriptions
ValidateCount
Signature: public static bool ValidateCount(ReadOnlySpan<string> args, int count, out string error)
Parameter details (Click to expand)
ReadOnlySpan<string>:args- The argument string array. Passed as
ReadOnlySpan<string>for performance and mutability safety.
Example:["Hello", "world!"] int:count- The exact amount of arguments allowed.
string: outerror- An error message that is filled if the arguments are not valid.
string.Emptyif the validation passes. - Example:
"Too many arguments. Argument count must be exactly 1."
Description:
Takes an arguments array and a count, then returns if the array length is within
Example(s):
Usage within a CVar.
// If there is not EXACTLY 1 argument, return an error.
if (!ArgumentParser.ValidateCount(args, 1, out string error))
return new(CvarSetResponseStatus.InvalidArgs, error);
Signature: public static bool ValidateCount(ReadOnlySpan<string> args, int count, out string error)
Parameter details (Click to expand)
ReadOnlySpan<string>:args- The argument string array. Passed as
ReadOnlySpan<string>for performance and mutability safety.
Example:["Hello", "world!"] ReadOnlySpan<int>:counts- An array of ints where each element is an allowed exact count of arguments.
int: outcount- The amount of arguments if the check passed. Can be easily used with a switch statement.
-1if the validation fails. - Example:
-1or1or3... string: outerror- An error message that is filled if the arguments are not valid.
string.Emptyif the validation passes. - Example:
"Invalid argument count. Expected one of: 1, 3"
Description:
Takes an arguments array and a counts array, then compares the length of the arguments array to the values in the counts array.
If the length matches any of the values exactly, the method returns true and assigns the length to the out int count parameter.
Example(s):
Using the method with a switch statement.
// If there is not EXACTLY 1 or 3 arguments, return an error.
if (!ArgumentParser.ValidateCount(args, [1, 3], out int count, out string error))
return new(CvarSetResponseStatus.InvalidArgs, error);
// Count is guaranteed to be 1 or 3 here.
switch(count)
{
case 1:
ExecuteWithOne();
break;
case 3:
ExecuteWithThree();
break;
// The default state is not needed, but can stil be included for good hygiene.
// It catches cases where the developer messes up the cases.
default:
return new(CvarSetResponseStatus.InvalidArgs, error);
}
Signature: public static bool ValidateCount(ReadOnlySpan<string> args, int count, out string error)
Parameter details (Click to expand)
ReadOnlySpan<string>:args- The argument string array. Passed as
ReadOnlySpan<string>for performance and mutability safety.
Example:["Hello", "world!"] int:min- The minimum amount of arguments allowed.
int:max- The maximum amount of arguments allowed.
string: outerror- An error message that is filled if the arguments are not valid.
string.Emptyif the validation passes. - Example:
"Too many arguments."or"Not enough arguments."
Description:
Takes an arguments array and a min / max inclusive value , then returns if the array length is within bounds.
Example(s):
Usage within a CVar.
// If there arent BETWEEN 1 and 3 arguments, return an error.
if (!ArgumentParser.ValidateCount(args, 1, 3, out string error))
return new(CvarSetResponseStatus.InvalidArgs, error);
TryParseBool
Signature: public static bool TryParseBool(ReadOnlySpan<char> input, out bool value)
Parameter details (Click to expand)
ReadOnlySpan<char>:input- Raw input that should be parsed into a boolean value.
Accepts1,0,trueandfalse. Case insensitive. - Example:
TRUEorfAlSeor1or0 bool: outvalue- The input value parsed and converted to a strictly typed boolean.
Description:
Takes an input and returns a strictly typed boolean from it.
Example(s):
Excerpt from CVarBool.cs.
if (!ArgumentParser.TryParseBool(args[0], out bool value))
return new(CvarSetResponseStatus.Failed, $"Could not parse {args[0]} into type bool.");
// . . .
Value = value;
TryParseEnum
Signature: public static bool TryParseEnum(ReadOnlySpan<char> input, ReadOnlySpan<string> options, out int index, out string error)
Parameter details (Click to expand)
ReadOnlySpan<char>:input- Raw input that should be parsed into the enum value.
Accepts the index as anintor the named enum value as astring. Case insensitive. - Example:
1ormediumormeDiUm ReadOnlySpan<string>:options- An array of strings that represent the options for the enum.
- Example:
["easy", "medium", "hard", "extreme"] int: outindex- A strictly typed
intthat represents the index of the option. - Example (using above context):
mediumwill yield1.
extremewill yield3. string: outerror- An error message that is filled if the input is not valid.
string.Emptyif the enum parses correctly.
Description:
Takes an input and an array of options, then locates the input
value within the options array and returns the index.
This is not a strong enum
This is used mainly for the CVarEnum resource.
It is tailored around limitations in the Godot editor and
strong typing.
We are basically creating a weak reference using
a string array instead of a strict enum.
Example(s):
Excerpt from CVarEnum.cs.
if (!ArgumentParser.TryParseEnum(args[0], _options, out int index, out error))
return new(CvarSetResponseStatus.Failed, error);
// . . .
Value = index;
TryParseManyInt
Signature: public static bool TryParseManyInt(ReadOnlySpan<string> args, out int[] values, out string error)
Parameter details (Click to expand)
ReadOnlySpan<string>:args- Raw input arguments that should be parsed into a
int. - Example:
["1", "1337", "2"] int[]: outvalues- A strictly typed array of all inputs parsed to
ints. - Example (using above context):
[1, 1337, 2] string: outerror- An error message that is filled if ANY of the input are not valid.
string.Emptyif the input is parsed correctly.
Description:
Takes an array of raw input arguments,
then parses them into a strictly typed array of ints.
All arguments must pass for the method to return successfull.
Example(s):
Hypothetical usage within a command that sets the players position in a 2d grid.
// . . .
if (!ArgumentParser.TryParseManyInt(args.AsSpan(), out int[] coords, out error))
return new Response<ExecutionResponseStatus>(ExecutionResponseStatus.InvalidArgs, $"Invalid grid input: {error}");
// . . .
Player.GridPosition = new Vector2(coords[0], coords[1]);
Continuous vs Non-continuous arguments
If the parameters you want to parse are non-continuous you can pass a collection expression instead of an array span.
Below are 3 common examples of how we might want to pass arguments.
// Args index 0, 2 and 4. Skip all others
ArgumentParser.TryParseManyInt([args[0], args[2], args[4]], out int[] values, out error)
// All arguments
ArgumentParser.TryParseManyInt(args.AsSpan(), out int[] values, out error)
// Arguments from index 0 to index 2
ArgumentParser.TryParseManyInt(args.AsSpan(0, 2), out int[] values, out error)
TryParseManyFloat
Signature: public static bool TryParseManyFloat(ReadOnlySpan<string> args, out float[] values, out string error)
Parameter details (Click to expand)
ReadOnlySpan<string>:args- Raw input arguments that should be parsed into a
float.
Internal systems automatically handles culture invariances, forcing all users to use periods to separate decimals. - Example:
["1.1", "77.7", "2"] float[]: outvalues- A strictly typed array of all inputs parsed to
floats. - Example (using above context):
[1.1f, 77.7f, 2f] string: outerror- An error message that is filled if ANY of the input are not valid.
string.Emptyif the input is parsed correctly.
Description:
Takes an array of raw input arguments,
then parses them into a strictly typed array of floatss.
All arguments must pass for the method to return successfull.
Example(s):
Hypothetical usage within a command that sets the players position in 3d world space.
// . . .
if (!ArgumentParser.TryParseManyFloat(args.AsSpan(), out float[] newPos, out error))
return new Response<ExecutionResponseStatus>(ExecutionResponseStatus.InvalidArgs, $"Invalid position input: {error}");
// . . .
Player.Position = new Vector3(newPos[0], newPos[1], newPos[2]);
Continuous vs Non-continuous arguments
If the parameters you want to parse are non-continuous you can pass a collection expression instead of an array span.
Below are 3 common examples of how we might want to pass arguments.
// Args index 0, 2 and 4. Skip all others
ArgumentParser.TryParseManyFloat([args[0], args[2], args[4]], out float[] values, out error)
// All arguments
ArgumentParser.TryParseManyFloat(args.AsSpan(), out float[] values, out error)
// Arguments from index 0 to index 2
ArgumentParser.TryParseManyFloat(args.AsSpan(0, 2), out float[] values, out error)
TryParseManyDouble
Signature: public static bool TryParseManyDouble(ReadOnlySpan<string> args, out double[] values, out string error)
Parameter details (Click to expand)
ReadOnlySpan<string>:args- Raw input arguments that should be parsed into a
double.
Internal systems automatically handles culture invariances, forcing all users to use periods to separate decimals. - Example:
["1.1", "77.7", "2"] double[]: outvalues- A strictly typed array of all inputs parsed to
doubles. - Example (using above context):
[1.1d, 77.7d, 2d] string: outerror- An error message that is filled if ANY of the input are not valid.
string.Emptyif the input is parsed correctly.
Description:
Takes an array of raw input arguments,
then parses them into a strictly typed array of doubles.
All arguments must pass for the method to return successfull.
Example(s):
Hypothetical usage within a command that sets the world origin in 3d large world space.
In reality, this would never be done through commands.
// . . .
if (!ArgumentParser.TryParseManyDouble(args.AsSpan(), out double[] newOrigin, out error))
return new Response<ExecutionResponseStatus>(ExecutionResponseStatus.InvalidArgs, $"Invalid large position input: {error}");
// . . .
WorldManager.SetOrigin(newOrigin[0], newOrigin[1], newOrigin[2]);
Continuous vs Non-continuous arguments
If the parameters you want to parse are non-continuous you can pass a collection expression instead of an array span.
Below are 3 common examples of how we might want to pass arguments.
// Args index 0, 2 and 4. Skip all others
ArgumentParser.TryParseManyDouble([args[0], args[2], args[4]], out double[] values, out error)
// All arguments
ArgumentParser.TryParseManyDouble(args.AsSpan(), out double[] values, out error)
// Arguments from index 0 to index 2
ArgumentParser.TryParseManyDouble(args.AsSpan(0, 2), out double[] values, out error)
TryParseManyBool
Signature: public static bool TryParseManyBool(ReadOnlySpan<string> args, out bool[] values, out string error)
Parameter details (Click to expand)
ReadOnlySpan<string>:args- Raw input arguments that should be parsed into a
bool. - Example:
["true", "1", "false", "0"] bool[]: outvalues- A strictly typed array of all inputs parsed to
bools. - Example (using above context):
[true, true, false, false] string: outerror- An error message that is filled if ANY of the input are not valid.
string.Emptyif the input is parsed correctly.
Description:
Takes an array of raw input arguments,
then parses them into a strictly typed array of bools.
All arguments must pass for the method to return successfull.
Example(s):
Hypothetical usage within a command that has several boolean flags scattered in the argument signature.
In this case, they are implicitly paired with positional and rotational arguments.
// . . .
if (!ArgumentParser.TryParseManyBool([args[1], args[3]], out bool[] flags, out error))
return new Response<ExecutionResponseStatus>(ExecutionResponseStatus.InvalidArgs, $"Invalid boolean input: {error}");
// . . .
useLocalPosition = flags[0];
useLocalRotation = flags[1];
Continuous vs Non-continuous arguments
If the parameters you want to parse are non-continuous you can pass a collection expression instead of an array span.
Below are 3 common examples of how we might want to pass arguments.
// Args index 0, 2 and 4. Skip all others
ArgumentParser.TryParseManyBool([args[0], args[2], args[4]], out bool[] values, out error)
// All arguments
ArgumentParser.TryParseManyBool(args.AsSpan(), out bool[] values, out error)
// Arguments from index 0 to index 2
ArgumentParser.TryParseManyBool(args.AsSpan(0, 2), out bool[] values, out error)
TryParseManyEnum
Signature: public static bool TryParseManyEnum(ReadOnlySpan<string> args, string[] options, out int[] values, out string error)
Parameter details (Click to expand)
ReadOnlySpan<string>:args- Raw input arguments that should be parsed into a
int.
Note that the int will represent an index for a certain collection of options. - Example:
["command", "starts_with"] int[]: outvalues- A strictly typed array of all inputs parsed to
ints (indexes). - Example (using above context):
[0, 1] string: outerror- An error message that is filled if ANY of the input are not valid.
string.Emptyif the input is parsed correctly.
Description:
Takes an array of raw input arguments,
then parses them into a strictly typed array of ints that represent option indexes. These can be mapped using a string array.
All arguments must pass for the method to return successfull.
Example(s):
Hypothetical usage within a command that has several enums scattered in the argument signature.
In this case, they are implicitly paired with a command that searches for other commands.
// . . .
if (!ArgumentParser.TryParseManyEnum([args[0], args[2]], out int[] flags, out error))
return new Response<ExecutionResponseStatus>(ExecutionResponseStatus.InvalidArgs, $"Invalid boolean input: {error}");
// . . .
findOfType = ExecutionTypes[flags[0]];
searchMode = SearchModes[flags[1]];
Continuous vs Non-continuous arguments
If the parameters you want to parse are non-continuous you can pass a collection expression instead of an array span.
Below are 3 common examples of how we might want to pass arguments.
// Args index 0, 2 and 4. Skip all others
ArgumentParser.TryParseManyEnum([args[0], args[2], args[4]], out int[] values, out error)
// All arguments
ArgumentParser.TryParseManyEnum(args.AsSpan(), out int[] values, out error)
// Arguments from index 0 to index 2
ArgumentParser.TryParseManyEnum(args.AsSpan(0, 2), out int[] values, out error)