Skip to content

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
26 changes: 24 additions & 2 deletions managed/CounterStrikeSharp.API/Modules/Commands/CommandInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,29 @@ internal CommandInfo(IntPtr pointer, CCSPlayerController? player)

public string ArgByIndex(int index) => NativeAPI.CommandGetArgByIndex(Handle, index);
public string GetArg(int index) => NativeAPI.CommandGetArgByIndex(Handle, index);


public IEnumerable<string> GetArgs(int startIndex = 0, int endIndex = -1)
{
if (string.IsNullOrEmpty(ArgString))
return [];

string[] args = 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
);
}

/// <summary>
/// Whether or not the command was sent via Console or Chat.
/// </summary>
Expand Down Expand Up @@ -87,4 +109,4 @@ public void ReplyToCommand(string message)
}
}
}
}
}
Loading