-
-
Notifications
You must be signed in to change notification settings - Fork 159
Added GetArgs #663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Added GetArgs #663
Conversation
schwarper
commented
Nov 7, 2024
I improved this a bit. |
I'd also like the enumerator version maybe? from #504 |
I thought that getting arg once and split them is better than using GetArg multiple times. However, it might be better. |
Perhaps a solution like this would be the way forward, as I can see calling getarg multiple times might not be the best way forward (my solution), but nor do I think creating a list and a regex expression is (your solution). // Emulation of argstring
private static string ArgString = "my test string";
// Emulation of ArgCount
private static int ArgCount = ArgString.Split().Length;
static void Main(string[] args)
{
Console.WriteLine(string.Join(" ", TestMethods(2))); // Output: string
Console.WriteLine(string.Join(" ", TestMethods(1))); // output: test string
Console.WriteLine(string.Join(" ", TestMethods(1, 2))); // Output: test
Console.WriteLine(string.Join(" ", TestMethods(0, 1))); // Output: my
Console.WriteLine(string.Join(" ", TestMethods())); // Output: test string
}
private static IEnumerable<string> TestMethods(int startIndex = 1, int endIndex = -1)
{
endIndex = (endIndex > ArgCount || endIndex < 0) ? ArgCount : endIndex;
startIndex = (startIndex > ArgCount || startIndex < 0 || startIndex > endIndex) ? ArgCount : startIndex;
Console.WriteLine($"Start: {startIndex} | end: {endIndex}");
return ArgString.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries).AsEnumerable().Skip(startIndex).Take(endIndex - startIndex);
} Splitting with Glad to see that a PR I made back in June and forgot all about is something that other people (you at least) would have the benefit of 😅 |
I think perhaps we are better off just having an I'm open to also starting this index at 1 so that we skip the "command" arg. i.e. public static IEnumerable<string> GetValue {
get
{
for (int i = 0; i < ArgCount; i++)
{
yield return GetArg(i);
}
}
} |
I don't think it's a good idea to use GetArg in loop. However, I'm (unfortunately) okay with both solutions |
GetValue doesn't quite follow the naming convention since GetArg is a thing, hence GetArgs were the name for the method. |
Have we actually determined that the performance of get arg would actually be worse than the string split? The con command on the native side would already know each arg so it's fairly cheap for it to make a copy of the arg and return it back to c#. I'd be interested in a benchmark comparing the two. |
I've contacted you over dm's on discord regarding how to setup a benchmark to test it 😄 |
public static IEnumerable<string> GetArgs(CommandInfo info, int startIndex = 0, int endIndex = -1)
{
if (string.IsNullOrEmpty(info.ArgString))
return [];
string[] args = info.ArgString.Split(' ', StringSplitOptions.RemoveEmptyEntries);
int lastIndex = args.Length - 1;
startIndex = Math.Clamp(startIndex, 0, lastIndex);
endIndex = Math.Clamp(endIndex < 0 ? lastIndex : endIndex, startIndex, lastIndex);
string[] selectedArgs = startIndex == endIndex
? [args[startIndex]]
: args[startIndex..(endIndex + 1)];
return selectedArgs.Select(arg =>
arg.Length >= 2 && arg[0] == '"' && arg[^1] == '"'
? arg[1..^1]
: arg
);
}
public static IEnumerable<string> GetArgsYield(CommandInfo info)
{
if (info.ArgCount == 1)
yield break;
for (int i = 1; i < info.ArgCount; i++)
yield return info.GetArg(i);
} |