Log In  


For your scripts and programs!
Copy this script and paste it into your program. Let me know if you find any bugs.

Example of usage:

let's say the user will run
your cart with this:

run photo.p8 20 50 -width 127 -dimentions 100 20 -v
    ➡️----------⬅️ ➡️----------------------------⬅️
        params                  flags

then in your cart you'll do:

flagbundle=paramstr(stat(6))
?flagbundle.params[1]
--photo.p8
?flagbundle.params[2]
--20
?flagbundle.params[3]
--50
?flagbundle.flags.width
--[table]
?flagbundle.flags.width[1]
--127
?flagbundle.flags.width[2]
--[nil]
?flagbundle.flags.height
--[nil]

Notes:

All flags start with a single dash. That means that "-help" will be acceseed as result.flags["help"] (or result.flags.help) , but "--help" (with two dashes) will be now result.flags["-help"]

"-vhl" will be treated as a single "vhl" flag, instead of separate "-v -h -l".

To see if a flag e.g."help" exists, you can just use:

if results.flags.help then
    do_something()
end

Here results.flags.help is an empty table, so it'll get treated as True

Flags' arguments are always stored into a table, even if there's just one! If your flag requires an argument (e.g. -width 32), to get it you should do like this:

width = results.flags.width[1]

The script:

function paramstr_to_flags(str)
	local function string_is_flag(str)
		return type(str)=="string" and str[1]=="-"
	end
	local function string_is_ok_arg(str)
		return type(str)!="string" or #str>0
	end
	local result={params={},flags={}}
	if(#str==0) return result
	local raw=split(str," ")
	local i=1
	while i<=#raw do
		local word=raw[i]
		if string_is_flag(word) then
			i+=1
			local args={}
			while i<=#raw and not string_is_flag(raw[i]) do
				if(string_is_ok_arg(raw[i]))add(args,raw[i])
				i+=1
			end
			result.flags[sub(word,2)]=args
		else
			if(string_is_ok_arg(word))add(result.params,word)
			i+=1
		end
	end
	return result
end

Example cart (doesn't really work on BBS since you can't provide a paramstr here):

Cart #zuwayugade-0 | 2025-09-28 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA




[Please log in to post a comment]