Skip to content
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

Added GetArgs #663

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Added GetArgs #663

wants to merge 7 commits into from

Conversation

schwarper
Copy link
Contributor

[ConsoleCommand("css_testx")]
public void OnTest(CCSPlayerController player, CommandInfo info)
{
    var args = info.GetArgs();

    Console.WriteLine($"TESTX args count => {args.Count}");

    for (int i = 0; i < args.Count; i++)
    {
        Console.WriteLine($"{i}. {args[i]}");
    }
}

[ConsoleCommand("css_testy")]
public void OnTest2(CCSPlayerController player, CommandInfo info)
{
    var args = info.GetArgs(0, 3);

    Console.WriteLine($"TESTX args count => {args.Count}");

    for (int i = 0; i < args.Count; i++)
    {
        Console.WriteLine($"{i}. {args[i]}");
    }
}

image

@schwarper schwarper requested a review from roflmuffin as a code owner November 7, 2024 18:51
@schwarper
Copy link
Contributor Author

#504

I improved this a bit.

@KillStr3aK
Copy link
Contributor

I'd also like the enumerator version maybe? from #504

@schwarper
Copy link
Contributor Author

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.

@WidovV
Copy link
Contributor

WidovV commented Nov 13, 2024

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).
I made a minor test project to showcase what could become the end result. It kinda combines both yours and mine. The if statement or rather ternary don't need to be implemented, I was just too lazy to copy the original.

   // 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 (char[]?)null is reffering to any whitespace as I understand.
The reason for starting startindex at one is, iirc, argstring comes with the command input as first argument, in case of not, my bad. Not sure that "all" of these method calls on enumerable is that performance heavy, but we're avoiding object initialization by using As, which is a thumbs up. Either Take or Skip could be removed by using "TakeWhile" and checking index also.

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 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants