Files

greetings.src
  • Greetings = {}
  • Greetings.greetings_sig = {}
  • Greetings.greetings_sig["description"] = "shit cli script"
  • //required args should come first than optional ones
  • Greetings.greetings_sig["args"] = ["name*", "second_name"]
  • Greetings.greetings_sig["options"] = [{["-c", "--capitalize"]: "the names"}, {["-f=", "--from="]: "from name"}]
  • Greetings.greetings_sig["options"] = [{["-c", "--capitalize"]: "capitalize the names"}, {["-f=", "--from="]: "from name"}]
  • capitalize = function(s)
  • s = s.values
  • s[0] = s[0].upper
  • return s.join("")
  • end function
  • Greetings.greetings = function(args = [], options = {})
  • print("hellow")
  • name = args[0]
  • second_name = args[1]
  • if second_name == null then second_name = ""
  • from = options["-f="]
  • if from == null then from = ""
  • if options["-c"] then
  • name = capitalize(name)
  • if second_name != "" then second_name = capitalize(second_name)
  • if from != "" then from = capitalize(from)
  • end if
  • out = ""
  • if from != "" then out = out + "from " + from + ": "
  • out = out + ["hello", name, second_name].join(" ")
  • print(out)
  • end function
  • import_code("/home/me/Thorr/libs/listLib.src") //requires on listlib
  • import_code("/home/me/Thorr/libs/thor.src") //requires on listlib
  • Thor.init(Greetings, "greetings")
libs/thor.src
  • ThorCommand = {}
  • ThorCommand.init = function(func, sig, name)
  • self.name = name
  • self.func = @func
  • if sig.hasIndex("description") then self.description = sig["description"]
  • if sig.hasIndex("args") then
  • self.args = sig["args"]
  • end if
  • if sig.hasIndex("options") then
  • f = function(obj)
  • param = {}
  • param["name"] = obj.indexes[0]
  • param["description"] = obj[obj.indexes[0]]
  • return param
  • end function
  • self.options = Lst.map(sig["options"], @f)
  • end if
  • end function
  • ThorCommand.required_args = function()
  • f = function(obj)
  • return obj[-1:] == "*"
  • end function
  • return Lst.select(self.args, @f)
  • end function
  • ThorCommand.optional_args = function()
  • f = function(obj)
  • return self.required_args.indexOf(obj) == null
  • end function
  • return Lst.select(self.args, @f)
  • end function
  • ThorCommand.options_list = function()
  • f = function(o)
  • return o.name
  • end function
  • return Lst.map(self.options, @f)
  • end function
  • ThorCommand.args_help = function()
  • out = self.name
  • for arg in self.args
  • out = out + " " + arg
  • end for
  • return out
  • end function
  • ThorCommand.options_help = function()
  • out = "options for " + self.name + ":" + char(10)
  • for i in self.options
  • n = i.name
  • if typeof(i.name) == "list" then n = i.name.join(" or ")
  • out = out + n + " : " + i.description
  • if self.options.indexOf(i) < self.options.len - 1 then out = out + char(10)
  • end for
  • return out
  • end function
  • ThorCommand.help = function()
  • out = self.args_help + char(10) + char(10)
  • out = out + self.options_help
  • return out
  • end function
  • ThorCommand.exec = function()
  • self.func(self.passed_args, self.passed_options)
  • end function
  • // class divider ---------------------------------------
  • ThorManager = {}
  • ThorManager.init = function(thor)
  • self.thor = thor
  • end function
  • ThorManager.eval_command = function(command)
  • used_args = self.thor.global_args[:command.args.len]
  • self.thor.global_args = self.thor.global_args[command.args.len:]
  • self.thor.used_options = self.thor.used_options + Lst.flat(command.options_list)
  • for i in range(command.args.len - used_args.len - 1)
  • used_args.push(null)
  • end for
  • command["passed_args"] = used_args
  • command["passed_options"] = self.options_by_key(command)
  • self.thor.global_execution_queue.push(command)
  • end function
  • ThorManager.options_by_key = function(command)
  • r = {}
  • get_value = function(o)
  • if o.indexOf("=") then
  • striped_options = self.thor.striped_global_options
  • if striped_options.indexOf(o) then
  • value = self.thor.global_options[striped_options.indexOf(o)]
  • return value[value.indexOf("=")+1:]
  • else
  • return null
  • end if
  • else
  • if self.thor.global_options.indexOf(i) != null then return true
  • return null
  • end if
  • end function
  • for o in command.options_list
  • if typeof(o) == "list" then
  • v = null
  • for i in o
  • if get_value(i) != null then v = get_value(i)
  • end for
  • for i in o
  • r[i] = v
  • end for
  • else
  • r[o] = get_value(o)
  • end if
  • end for
  • return r
  • end function
  • // class divider ---------------------------------------
  • Thor = {}
  • Thor.init = function(class, main_func_name)
  • self.class = null
  • self.global_args = []
  • self.passed_args_count = 0
  • self.global_options = []
  • self.used_options = []
  • self.global_execution_queue = []
  • self.manager = new ThorManager
  • self.manager.init(self)
  • main_command = new ThorCommand
  • main_command.init(class[main_func_name], class[main_func_name+"_sig"], main_func_name)
  • self.params_to_args_options(params)
  • self.manager.eval_command(main_command)
  • self.catch_errors
  • self.exec_queue
  • end function
  • Thor.striped_global_options = function()
  • f = function(ob)
  • if ob.indexOf("=") then
  • return ob[:ob.indexOf("=")+1]
  • end if
  • end function
  • return Lst.map(self.global_options, @f)
  • end function
  • Thor.params_to_args_options = function(params)
  • // filter for args
  • f = function(p)
  • return p[0] == "-"
  • end function
  • args = Lst.reject(params, @f)
  • options = {}
  • f = function(param)
  • return param[:1] == "-" and param.indexOf("=") == null
  • end function
  • params_options = Lst.select(params, @f)
  • f = function(param)
  • if param[:2] != "--" and param.len > 2 then
  • r = []
  • for i in param[1:].values
  • r.push("-" + i)
  • end for
  • return r
  • else
  • return param
  • end if
  • end function
  • params_options = Lst.uniq(Lst.flat(Lst.map(params_options, @f)))
  • f = function(param)
  • return param[:1] == "-" and param.indexOf("=") != null
  • end function
  • params_value_options = Lst.select(params, @f)
  • options = params_options + params_value_options
  • self.global_args = args
  • self.passed_args_count = args.len
  • self.global_options = options
  • end function
  • Thor.catch_errors = function()
  • f = function(c)
  • return c.required_args.len
  • end function
  • required_args = Lst.map(self.global_execution_queue, @f)
  • if self.passed_args_count < required_args.sum then
  • print("error: required params not passed"+char(10))
  • print("error: required params not passed, check the docs")
  • exit()
  • end if
  • print(self.striped_global_options)
  • print(self.used_options)
  • used_options = self.used_options + ["-h", "--help"] //cant use self inside the block
  • f = function(p)
  • return used_options.indexOf(p) == null
  • end function
  • undefined_options = Lst.select(self.striped_global_options, @f)
  • for i in undefined_options
  • print("error: " + i + " option is not defined, check the docs")
  • exit()
  • end for
  • if self.global_options.indexOf("-h") != null or self.global_options.indexOf("--help") != null then
  • print(self.global_execution_queue[-1].help)
  • exit()
  • end if
  • end function
  • Thor.exec_queue = function()
  • self.global_execution_queue[-1].exec
  • end function