From 604942f0e7648b921be9939d6b32648ccfc853fe Mon Sep 17 00:00:00 2001 From: Headline Date: Wed, 4 Mar 2020 13:17:10 -0800 Subject: [PATCH] Add helper stocks for getting numerical command arguments (#1194) --- plugins/include/console.inc | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/plugins/include/console.inc b/plugins/include/console.inc index ccbcbf1a..c08738ca 100644 --- a/plugins/include/console.inc +++ b/plugins/include/console.inc @@ -423,6 +423,37 @@ native int GetCmdArgs(); */ native int GetCmdArg(int argnum, char[] buffer, int maxlength); +/** + * Retrieves a numeric command argument given its index, from the current + * console or server command. Will return 0 if the argument can not be + * parsed as a number. Use GetCmdArgIntEx to handle that explicitly. + * + * @param argnum Argument number to retrieve. + * @return Value of the command argument. + */ +stock int GetCmdArgInt(int argnum) { + char str[12]; + GetCmdArg(argnum, str, sizeof(str)); + + return StringToInt(str); +} + +/** + * Retrieves a numeric command argument given its index, from the current + * console or server command. Returns false if the argument can not be + * completely parsed as an integer. + * + * @param argnum Argument number to retrieve. + * @param value Populated with the value of the command argument. + * @return Whether the argument was entirely a numeric value. + */ +stock bool GetCmdArgIntEx(int argnum, int &value) { + char str[12]; + int len = GetCmdArg(argnum, str, sizeof(str)); + + return StringToIntEx(str, value) == len && len > 0; +} + /** * Retrieves the entire command argument string in one lump from the current * console or server command.