Skip to content

Commit

Permalink
Add helper stocks for getting numerical command arguments (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Headline committed Mar 4, 2020
1 parent 22eeb2f commit 604942f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions plugins/include/console.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 604942f

Please sign in to comment.