start writing arg parser

This commit is contained in:
leaf corcoran 2016-04-15 23:51:27 -07:00
parent 4733598583
commit 94e5a32369
3 changed files with 83 additions and 0 deletions

43
moonscript/cmd/args.lua Normal file
View File

@ -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
}

27
moonscript/cmd/args.moon Normal file
View File

@ -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}

View File

@ -76,6 +76,19 @@ describe "moonc", ->
"cool/" "cool/"
}, watcher\get_dirs! }, 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", -> describe "stubbed lfs", ->
local dirs local dirs