Files

testCLI.src
  • Mycli = {}
  • Mycli.commands = {}
  • // 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.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"}]
  • Mycli.main_sig["args"] = [{"email": "your email why not"}, {["commands*", Mycli.commands]: "Accepted commands: "}]
  • // the main fuction will is the root function of the cli script
  • // the main fuction 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)
libs/thor.src
  • Thor = {}
  • Thor.class = null
  • Thor.commands = {}
  • Thor.commands_doc_header = "list of all commands supported: " + char(10)
  • Thor.init = function(class, root = true)
  • self.class = null
  • self.main = null
  • self.__commands = {}
  • self.commands = {}
  • Thor.init = function(class)
  • self.class = class
  • self.description = self.class.description
  • if self.class.hasIndex("main") then self.main = self.to_command("main")
  • for obj in self.class
  • 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
  • if obj["key"][:2] == "__" then
  • self.__commands[obj["key"]] = self.to_command(obj["key"])
  • else if obj["key"] != "main" then
  • self.commands[obj["key"]] = self.to_command(obj["key"])
  • end if
  • end if
  • end for
  • //print(self.main)
  • //print(self.__commands)
  • //print(self.commands)
  • self.exec
  • //self.exec
  • end function
  • Thor.to_command = function(name)
  • command = {}
  • command["name"] = name
  • command["func"] = self.class[name]
  • if self.class.hasIndex(name + "_sig") then
  • sig = self.class[name + "_sig"]
  • else
  • return command
  • end if
  • if sig.hasIndex("description") then command["description"] = sig["description"]
  • arg_to_map = function(obj)
  • param = {}
  • param["name"] = obj.indexes[0]
  • if param.hasIndex("description") then param["description"] = obj[param.name]
  • return param
  • end function
  • if sig.hasIndex("args") then
  • sig["args"] = Lst.map(sig["args"], @arg_to_map)
  • required = function(obj)
  • return obj.name[-1:] == "*"
  • end function
  • command["required_args"] = Lst.select(sig["args"], @required)
  • command["optional_args"] = Lst.reject(sig["args"], @required)
  • f = function(obj)
  • return typeof(obj.name) == "list" and obj.name.len == 2 and typeof(obj.name[1]) == "map"
  • end function
  • commands = Lst.select(sig["args"], @f)
  • required = function(obj)
  • return obj.name[0][-1:] == "*"
  • end function
  • command["required_commands"] = Lst.select(commands, @required)
  • command["optional_commands"] = Lst.reject(commands, @required)
  • f = function(obj)
  • sub_cli = new Thor
  • sub_cli.init(obj["name"][1])
  • obj["commands"] = sub_cli
  • obj["name"] = obj["name"][0]
  • return obj
  • end function
  • command["required_commands"] = Lst.map(command["required_commands"], @f)
  • command["optional_commands"] = Lst.map(command["optional_commands"], @f)
  • end if
  • if sig.hasIndex("options") then
  • sig["options"] = Lst.map(sig["options"], @arg_to_map)
  • command["options"] = sig["options"]
  • end if
  • return command
  • end function
  • Thor.has_command = function(command_name)
  • return self.commands.hasIndex(command_name) != 0
  • end function
  • 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 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