lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

>>>>> "Sean" == Sean Conner <> writes:

 Sean> I'd like the output to be:

 Sean> foo -t application/x-foo /tmp/   false
 Sean> bar -t application/x-bar        true

My solution:

local lpeg = require "lpeg"

local P,R = lpeg.P, lpeg.R
local Carg,Cc,C,Cg,Ct,Cs = lpeg.Carg, lpeg.Cc, lpeg.C, lpeg.Cg, lpeg.Ct, lpeg.Cs

local char = P"%s" * Carg(1) * Cg(Cc(true),'noredirect')
           + P"%t" * Carg(2)
           + C(P"%")  -- I'm guessing a P"%%" * Cc"%" is missing here
           + C((R" ~" - P"%")^1)

local cmd  = Ct( char^1 )

t = cmd:match("foo -t %t %s",1,"/tmp/","application/x-foo")
print(table.concat(t), not t.noredirect)
t = cmd:match("bar -t %t",   1,"/tmp/","application/x-bar")
print(table.concat(t), not t.noredirect)