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

Add helper stocks for getting numerical command arguments #1194

Merged
merged 1 commit into from
Mar 4, 2020

Conversation

asherkin
Copy link
Member

This is quite a common operation, it is all over our base plugins and examples on the wiki. I wrote a plugin for the first time in a very long time the other day that needed some commands and it was really weird not having this available by default.

Tested with the following test plugin:

public void OnPluginStart()
{
	RegConsoleCmd("sm_test", Cmd_Test);
}

Action Cmd_Test(int client, int args)
{
	for (int i = 1; i <= args; ++i) {
		char strVal[32];
		GetCmdArg(i, strVal, sizeof(strVal));
		
		int intVal = GetCmdArgInt(i);
		
		int checkedIntVal;
		bool checked = GetCmdArgIntEx(i, checkedIntVal);
		
		ReplyToCommand(client, "'%s' %d %d (%s)",
			strVal, intVal, checkedIntVal,
			checked ? "valid" : "invalid");
	}
	
	return Plugin_Handled;
}

Input:

sm_test hello 9 99 999 9999 99999 999999 9999999 99999999 999999999 9999999999 99999999999 -9 -99 -999 -9999 -99999 -999999 -9999999 -99999999 -999999999 -9999999999 -99999999999 42hello hello42 0 0hello "" 9999999999nope 99999999999nope

Output:

'hello' 0 0 (invalid)
'9' 9 9 (valid)
'99' 99 99 (valid)
'999' 999 999 (valid)
'9999' 9999 9999 (valid)
'99999' 99999 99999 (valid)
'999999' 999999 999999 (valid)
'9999999' 9999999 9999999 (valid)
'99999999' 99999999 99999999 (valid)
'999999999' 999999999 999999999 (valid)
'9999999999' -1 -1 (valid)
'99999999999' -1 -1 (valid)
'-9' -9 -9 (valid)
'-99' -99 -99 (valid)
'-999' -999 -999 (valid)
'-9999' -9999 -9999 (valid)
'-99999' -99999 -99999 (valid)
'-999999' -999999 -999999 (valid)
'-9999999' -9999999 -9999999 (valid)
'-99999999' -99999999 -99999999 (valid)
'-999999999' -999999999 -999999999 (valid)
'-9999999999' -1 -1 (valid)
'-99999999999' -1 -1 (valid)
'42hello' 42 42 (invalid)
'hello42' 0 0 (invalid)
'0' 0 0 (valid)
'0hello' 0 0 (invalid)
'' 0 0 (invalid)
'9999999999nope' -1 -1 (invalid)
'99999999999nope' -1 -1 (valid)

There are some edge-cases where the Ex variant doesn't detect invalid input, but they're inherited from StringToIntEx sadly and it seems to do a fairly respectable job. I think it is worth adding both variants as 99% of current code doesn't check for failure (and it complicates code a bit), but it is good to give people the option.

This is quite a common operation, it is all over our base plugins
and examples on the wiki.
Copy link
Member

@Headline Headline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me; great addition!

@Headline
Copy link
Member

Headline commented Mar 1, 2020

Before this gets pulled in, we might want to consider updating plugins with this change so it ships all in one go.

@Headline
Copy link
Member

Headline commented Mar 4, 2020

Lets leave that for another time, it's on my list

@Headline Headline merged commit 604942f into master Mar 4, 2020
@Headline Headline deleted the get-cmd-arg-int branch March 4, 2020 21:17
@asherkin
Copy link
Member Author

asherkin commented Mar 4, 2020

I was just working on it 😅

Wrote this amazing one-liner and everything!

vim $(diff -u <(rg -l 'GetCmdArg\(' plugins/) <(rg -l 'StringToInt(Ex)?\(' plugins/) | grep '^ ' | cut -c 2- | xargs)

rumblefrog added a commit to rumblefrog/sp-gid that referenced this pull request Mar 4, 2020
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.

None yet

2 participants