I'm writting a command line parser for Lua and I would like to extract options' names and their values using Lua string.match().
A command line option is defined as : --name=value
Here is the code I use (can be found here) :
local expr = '--username=John'
local name, value = expr:match( '^%-%-(.*)=%"?(.*)%"?' )
print( "found values for '" .. expr .. "'" )
print( name .. " = " .. value )
the ouput is OK :
found values for '--username=John'
username = John
but if I want to set a value with spaces I enclose it between double-quotes
local expr = '--username="John Doe"'
local name, value = expr:match( '^%-%-(.*)=%"?(.*)%"?' )
print( "found values for '" .. expr .. "'" )
print( name .. " = " .. **value )
The output is not what I want since the last double-quote has been extracted by string.match()
found values for '--username="John Doe"'
username = John Doe"
Is my pattern wrong or is it a Lua bug ?