This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
cptdeath58's profile picture

Commands

Started by cptdeath58, 09 July 2014 - 09:30 PM
cptdeath58 #1
Posted 09 July 2014 - 11:30 PM
I need help with the part of identifying if args was given or not.
It's at the "Stop" command
Spoiler

--Manage Server Commands and Messages.
function chat()
newLine()
term.write( "[Server] " )
local input = read()
if input == nil then
  line = line - 1
  newLine()
end
if aC == "True" then
  if input:sub(1,1) == "/" then
   local cmd,args = input:match("/(%S+) ?(.*)")
   if cmd == "help" then
	printHelp()
   elseif cmd == "stop" then
	if args == nil or args == " " then
	 stop = true
	 stopServer()
	else
	 timeLeft = args
	 repeat
	  newLine()
	  term.write( "[Server] will close in "..timeLeft )
	  timeLeft = timeLeft - 1
	  sleep(1)
	 until timeLeft <= 0
	 stop = true
	 stopServer()
	end
   elseif cmd == "time" then
	local utime = os.time()
	local time = textutils.formatTime( utime, false )
	newLine()
	term.write( "[Server][Time]["..time.."]" )
   elseif cmd == "version" then
	printVersion()
   elseif cmd == "ban" then
	ban(args)
   elseif cmd == "list_banned" then
	checkBanned()
	newLine()
	term.write( "[Server] "..BIDs )
   elseif cmd == nil then
	newLine()
	term.write( "[Server][Error] No Command Entered" )
   else
	newLine()
	term.write( "[Server][Error] Command /"..cmd.." unknown" )
   end
  end
end
end
if anyone can help, I'll appreciate it.
Edited on 09 July 2014 - 09:31 PM
Emma #2
Posted 10 July 2014 - 12:51 AM
I think this is what you are asking for:

if args~=nil then --Could also be written if args then
--watever
end
LBPHacker #3
Posted 11 July 2014 - 04:25 PM
I think this is what you are asking for:

if args~=nil then --Could also be written if args then
--watever
end
Nope. args will always be a string. If the :match call finds no arguments, it'll be an empty string.
if args == "" then ...

EDIT: I would use " *(.*)" (space and dash) in the pattern instead of " ?(.*)" (space and question mark) so if there's a command like "/heck ", args won't be full of whitespaces.

Actually, this is the way I usually parse command lines (if the command always begins with a forward slash):
local words = {}
for word in input:gmatch("[^/%s]+") do table.insert(words, word) end
if words[1]:lower() == "stop" then ...
Edited on 11 July 2014 - 02:33 PM
cptdeath58 #4
Posted 12 July 2014 - 12:14 AM
Thanks, You helped me alot!