Open main menu
Posts
Gists
Guilds
Users
Decipher
Docs
Open user menu
Log in
Sign up
Create a new gist
Posts
Gists
Guilds
Users
Decipher
Docs
Files
testCLI.src
libs/listLib.src
libs/thor.src
testCLI.src
Mycli = {}
Mycli_description = "my test cli program :)"
Mycli.hello_sig = {}
Mycli.hello_sig["description"] = "it says hello to you"
Mycli.hello_sig["params"] = [{"name*": "first name"}, {"second_name": "second name (optional)"}]
Mycli.hello_sig["options"] = {["-c", "--capitalize"]: "will capitalize your name"}
// options that are passed will be set to null, also if they are passed all the keys will be set to the options value
// if the functions is called with --help or -h then it wont be called and the documentation will be printed istead
Mycli.hello = function(name, second_name = "", options = {})
if options["-c"] then
name = name.values[0]
name[0].upper
name = name.join("")
end if
print(["hello", name, second_name].join(" "))
end function
import_code("/home/me/thor/libs/listLib.src") //requires on listlib
import_code("/home/me/thor/libs/thor.src") //requires on listlib
Thor.init("Mycli")
libs/listLib.src
Lst = {}
Lst.to_list = function(map)
list = []
for i in map.indexes
if typeof(map[i]) == "map" then
list.push([i, Lst.to_list(map[i])])
else
list.push([i, map[i]])
end if
end for
return list
end function
Lst.to_map = function(list)
l = list[0:]
map = {}
for i in indexes(l)
if typeof(l[i][1]) == "list" and l[i][1].len == 2 and typeof(l[i][1][0]) == "string" then
map[l[i][0]] = Lst.to_map(l[i][1])
else
map[l[i][0]] = l[i][1]
end if
end for
return map
end function
Lst.each = function(obj, func)
if typeof(obj) == "map" then
list = Lst.to_list(obj)
else
list = obj
end if
result = list[0:] //list copy.
for i in indexes(list)
if typeof(obj) == "map" then
func(result[i][0], result[i][1])
else
func(result[i])
end if
end for
end function
Lst.map = function(list, func)
result = list[0:] //list copy.
for i in indexes(list)
result[i] = func(result[i])
end for
return result
end function
Lst.reject = function(list, func)
result = list[0:] //list copy.
i = 0
while i < result.len
if func(result[i]) == true then
result.remove(i)
continue
end if
i = i + 1
end while
return result
end function
Lst.select = function(list, func)
result = list[0:] //list copy.
i = 0
while i < result.len
if func(result[i]) == false then
result.remove(i)
continue
end if
i = i + 1
end while
return result
end function
// do not rename funcc to to func it will infinite loop
Lst.lsort = function(list, funcc)
f = function(i)
return Lst.to_map([["sort_key", funcc(i)], ["obj", i]])
end function
result = Lst.map(list, @f)
result = result.sort("sort_key")
f = function(i)
return i["obj"]
end function
return Lst.map(result, @f)
end function
libs/thor.src
Thor = {}
Thor.class = null
Thor.commands = []
Thor.docs = []
Thor.init = function(class)
self.class = globals[class]
self.description = globals[class + "_description"]
for obj in self.class
if obj["key"][-4:] == "_sig" then
self.docs.push([obj["key"][:-4], obj["value"]])
else if typeof(obj["value"]) == "function" then
self.commands.push([obj["key"], obj["value"]])
end if
end for
self.exec
end function
Thor.command_doc = function(doc)
return "<b>" + doc[0] + "</b> : " + doc[1]["description"]
end function
Thor.full_doc = function()
out = self.description + char(10) + char(10)
out = out + "list of all commands supported: " + char(10)
for doc in self.docs
out = out + self.command_doc(doc)
end for
return out
end function
Thor.exec = function()
if params.len == 0 then
print(Thor.full_doc)
end if
end function