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

[Snippet] Argument Parser

Started by LDDestroier, 24 February 2019 - 08:03 PM
LDDestroier #1
Posted 24 February 2019 - 09:03 PM
This code snippet will allow your programs to interpret arguments much like a Linux command would!

https://raw.githubus...r/argparser.lua

Using this, your program can have arguments like:

program.lua -s -c 2 -b Spaghet mamamia ravioli

Example (in progdor2):
SpoilerThis declares which arguments are accepted as flags, and also what type they accept.
False means that it's boolean, otherwise it's "string" or "number".

local argData = {
  ["-pb"] = "string", -- pastebin get
  ["-dd"] = "string", -- direct URL download
  ["-m"] = "string",  -- specify main file
  ["-PB"] = false,	-- pastebin upload
  ["-e"] = false,	 -- automatic self-extractor
  ["-s"] = false,	 -- silent
  ["-a"] = false,	 -- use as API with require, also makes silent
  ["-c"] = false,	 -- use CCA compression
  ["-h"] = false,	 -- show help
  ["-i"] = false,	 -- inspect mode
}

(…) is then ran through the parsing function, and any errors are logged in argErrors.

local argList, argErrors = interpretArgs({...}, argData)
if #argErrors > 0 then
  local errList = ""
  for k,v in pairs(argErrors) do
	if k ~= 1 then
	 errList = errList .. "\"" .. k .. "\": " .. v .. "; "
	end
	error(errList:sub(1, -2))
  end
end


local pastebinGet	= argList["-pb"] -- string, pastebin code
local directDownload = argList["-dd"] -- string, download URL
local mainFile	   = argList["-m"]  -- string, main executable file
local pastebinUpload = argList["-PB"] -- boolean
local selfExtractor  = argList["-e"]  -- boolean
local silent		 = argList["-s"]  -- boolean
local useCompression = argList["-c"]  -- boolean
local justOverwrite  = argList["-o"]  -- boolean
Edited on 24 February 2019 - 08:03 PM
EveryOS #2
Posted 25 February 2019 - 03:33 PM
Looks like my getopts program (which needs redone), but inline and easier to use.
LDDestroier #3
Posted 25 February 2019 - 04:13 PM
Looks like my getopts program (which needs redone), but inline and easier to use.

Oh hey, I was actually looking for that before I made this. But I forgot the name, and figured it'd be good practice to make a parser myself.
EveryOS #4
Posted 26 February 2019 - 03:41 PM
Yeah, when I get to it I plan to make one more update to it - on a separate paste, of course - but I would like to make it easier to use and more flexible.

Oh, and thanks
Edited on 26 February 2019 - 02:42 PM