Files

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"}
  • Mycli.commands = {}
  • // options that are passed will be set to null, also if they are passed all the keys will be set to the options value
  • // args ending with * are required
  • // options ending with = require a value to be passed after the option
  • Mycli.commands.hello_sig = {}
  • Mycli.commands.hello_sig["description"] = "it says hello to you"
  • Mycli.commands.hello_sig["args"] = [{"name*": "first name"}, {"second_name": "second name (optional)"}]
  • Mycli.commands.hello_sig["options"] = [{["-c", "--capitalize"]: "will capitalize your name"}, {["-f=", "--from="]: "name from the person saying hello"}]
  • // options that are not 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 = {})
  • Mycli.commands.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
  • Mycli.main_sig = {}
  • Mycli.main_sig["description"] = "shit cli script"
  • Mycli.main_sig["args"] = [{"email": "your email why not"}, {["commands*", Mycli.commands]: "cli commands"}]
  • // the main fuction will is the root function of the cli script
  • // this function will allways be called if the the required args are passed(and they are valid)
  • // so in this case bc we have another class as args this function will be called first
  • // then the matching command will be called after
  • Mycli.main = function(email = "")
  • globals["email"] = email
  • end function
  • // functions starting with __ wont trigger the main function
  • Mycli.__version = function()
  • print("0.0.1")
  • 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")
  • Thor.init(Mycli)
libs/listLib.src
  • Lst = {}
  • Lst.to_list = function(map)
  • Lst.to_list = function(map, shallow = false)
  • list = []
  • for i in map.indexes
  • if typeof(map[i]) == "map" then
  • list.push([i, Lst.to_list(map[i])])
  • if shallow == true then
  • list.push([i, map[i]])
  • else
  • list.push([i, Lst.to_list(map[i])])
  • end if
  • 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)
  • list = Lst.to_list(obj, true)
  • 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.commands = {}
  • Thor.commands_doc_header = "list of all commands supported: " + char(10)
  • Thor.init = function(class)
  • self.class = globals[class]
  • self.description = globals[class + "_description"]
  • self.class = class
  • self.description = self.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"]])
  • if typeof(obj["value"]) == "function" then
  • command = {}
  • command["name"] = obj["key"]
  • command["func"] = obj["value"]
  • sig = self.class[command["name"] + "_sig"]
  • command["description"] = sig["description"]
  • f = function(obj)
  • param = {}
  • param["name"] = obj.indexes[0]
  • param["description"] = obj[param.name]
  • return param
  • end function
  • sig["args"] = Lst.map(sig["args"], @f)
  • sig["options"] = Lst.map(sig["options"], @f)
  • filter = function(obj)
  • return obj.name[-1:] == "*"
  • end function
  • command["required_args"] = Lst.select(sig["args"], @filter)
  • command["optional_args"] = Lst.reject(sig["args"], @filter)
  • command["options"] = sig["options"]
  • self.commands[command["name"]] = command
  • end if
  • end for
  • self.exec
  • end function
  • Thor.command_doc = function(doc)
  • return "<b>" + doc[0] + "</b> : " + doc[1]["description"]
  • Thor.has_command = function(command_name)
  • return self.commands.hasIndex(command_name) != 0
  • 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)
  • Thor.commands_list = function()
  • out = self.commands_doc_header
  • f = function(t)
  • return [t[0], t[1]["description"]]
  • end function
  • commands_descs = Lst.map(Lst.to_list(self.commands, true), @f)
  • for desc in commands_descs
  • out = out + "<b>" + desc[0] + "</b> : " + desc[1]
  • end for
  • return out
  • end function
  • Thor.command_help = function()
  • end function
  • Thor.cli_doc = function()
  • out = self.description + char(10) + char(10)
  • out = out + self.commands_list
  • return out
  • end function
  • Thor.exec_command = function(command, params)
  • if (command["required_args"].len != 0 and params.len == 0) or params[0] == "-h" or params[0] == "--help" then
  • print("kek")
  • end if
  • print(params)
  • end function
  • Thor.exec = function()
  • if params.len == 0 then
  • print(Thor.full_doc)
  • if params.len == 0 or params[0] == "-h" or params[0] == "--help" or not self.has_command(params[0]) then
  • print(Thor.cli_doc)
  • else
  • self.exec_command(command, params[1:])
  • end if
  • end function