diff --git a/moonscript/cmd/args.lua b/moonscript/cmd/args.lua new file mode 100644 index 0000000..812b1ae --- /dev/null +++ b/moonscript/cmd/args.lua @@ -0,0 +1,43 @@ +local parse_arguments +parse_arguments = function(spec, args) + local out = { } + local remaining = { } + local last_flag = nil + for _index_0 = 1, #args do + local _continue_0 = false + repeat + local arg = args[_index_0] + if last_flag then + out[last_flag] = arg + _continue_0 = true + break + end + do + local flag = arg:match("-(%w+)") + if flag then + do + local short_name = spec[flag] + if short_name then + out[short_name] = true + else + for char in flag:gmatch(".") do + out[char] = true + end + end + end + _continue_0 = true + break + end + end + table.insert(remaining, arg) + _continue_0 = true + until true + if not _continue_0 then + break + end + end + return out, remaining +end +return { + parse_arguments = parse_arguments +} diff --git a/moonscript/cmd/args.moon b/moonscript/cmd/args.moon new file mode 100644 index 0000000..8ad0a9c --- /dev/null +++ b/moonscript/cmd/args.moon @@ -0,0 +1,27 @@ + +parse_arguments = (spec, args) -> + out = {} + + remaining = {} + last_flag = nil + + for arg in *args + if last_flag + out[last_flag] = arg + continue + + if flag = arg\match "-(%w+)" + if short_name = spec[flag] + out[short_name] = true + else + for char in flag\gmatch "." + out[char] = true + continue + + table.insert remaining, arg + + out, remaining + + + +{:parse_arguments} diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon index 63e88e3..6a11758 100644 --- a/spec/cmd_spec.moon +++ b/spec/cmd_spec.moon @@ -76,6 +76,19 @@ describe "moonc", -> "cool/" }, watcher\get_dirs! + describe "parse args", -> + import parse_arguments from require "moonscript.cmd.args" + + it "parses arguments", -> + out, res = parse_arguments { + print: "p" + }, {"hello", "word", "-gap"} + + assert.same { + g: true + a: true + p: true + }, out describe "stubbed lfs", -> local dirs