Garry's Mod Wiki

concommand.Add

  concommand.Add( string name, function callback, function autoComplete = nil, string helpText = nil, number flags = 0 )

Description

Creates a console command that runs a function in lua with optional autocompletion function and help text.

This will fail if the concommand was previously removed with concommand.Remove in a different realm (creating a command on the client that was removed from the server and vice-versa).

Issue Tracker: 1183

Arguments

1 string name
The command name to be used in console.

This cannot be a name of existing console command or console variable. It will silently fail if it is.

2 function callback
The function to run when the concommand is executed.
Function argument(s):
1 Player ply - The player that ran the concommand. NULL entity if command was entered with the dedicated server console.
2 string cmd - The concommand string (if one callback is used for several concommands).
3 table args - A table of all string arguments.
4 string argStr - The arguments as a string.
3 function autoComplete = nil
The function to call which should return a table of options for autocompletion. (Autocompletion Tutorial)

This only properly works on the client since it is not networked.

Function argument(s):
1 string cmd - The concommand this autocompletion is for.
2 string argStr - The arguments typed so far.
3 table args - A table of all string arguments.
Function return value(s):
1 table tbl - A table containing the autocomplete options to display.
4 string helpText = nil
The text to display should a user run 'help cmdName'.
5 number or table flags = 0
Console command modifier flags. Either a bitflag, or a table of enums. See FCVAR enum.

Example

Adds a concommand killyourself which will kill the user.

concommand.Add( "killyourself", function( ply, cmd, args ) ply:Kill() print( "You killed yourself!" ) end )

Example

A concommand that prints the SteamID and nickname of every player on the server.

concommand.Add( "retrieveplayers", function() for _, ply in ipairs( player.GetAll() ) do print( ply:Nick() .. ", " .. ply:SteamID() .. "\n" ) end end )

Example

A simple autocomplete example.

local function SimpleAutoComplete( cmd, args, ... ) local possibleArgs = { ... } local autoCompletes = {} --TODO: Handle "test test" "test test" type arguments local arg = string.Split( args:TrimLeft(), " " ) local lastItem = nil for i, str in pairs( arg ) do if ( str == "" && ( lastItem && lastItem == "" ) ) then table.remove( arg, i ) end lastItem = str end -- Remove empty entries. Can this be done better? local numArgs = #arg local lastArg = table.remove( arg, numArgs ) local prevArgs = table.concat( arg, " " ) if ( #prevArgs > 0 ) then prevArgs = " " .. prevArgs end local possibilities = possibleArgs[ numArgs ] or { lastArg } for _, acStr in pairs( possibilities ) do if ( !acStr:StartsWith( lastArg ) ) then continue end table.insert( autoCompletes, cmd .. prevArgs .. " " .. acStr ) end return autoCompletes end concommand.Add( "cmd_with_autocomplete", function( ply, cmd, args ) print( "my concommand got these args:", unpack( args ) ) end, function( cmd, args ) return SimpleAutoComplete( cmd, args, { "test1", "test2", "hell", "yeah" }, { "batman", "ironman", "antman", "superman", "superiorman" } ) end )
Output: