Permalink
Browse files

Merge pull request #35 from geoffleyland/master

Handle dashes in long options in lapp
  • Loading branch information...
2 parents 96dd568 + fd86760 commit 1a21d37a029e1efc523576ab082884c2a19c9905 @stevedonovan committed Oct 12, 2012
Showing with 11 additions and 3 deletions.
  1. +3 −2 lua/pl/lapp.lua
  2. +3 −1 lua/pl/sip.lua
  3. +5 −0 tests/test-lapp.lua
View
@@ -181,6 +181,7 @@ function lapp.process_options_string(str,args)
end
local function set_result(ps,parm,val)
+ parm = type(parm) == "string" and parm:gsub("%W", "_") or parm -- so foo-bar becomes foo_bar in Lua
if not ps.varargs then
results[parm] = val
else
@@ -203,9 +204,9 @@ function lapp.process_options_string(str,args)
end
-- flags: either '-<short>', '-<short>,--<long>' or '--<long>'
- if check '-$v{short}, --$v{long} $' or check '-$v{short} $' or check '--$X{long} $' then
+ if check '-$v{short}, --$o{long} $' or check '-$v{short} $' or check '--$o{long} $' then
if res.long then
- optparm = res.long:gsub('%W','_') -- so foo-bar becomes foo_bar in Lua
+ optparm = res.long:gsub('[^%w%-]','_') -- I'm not sure the $o pattern will let anything else through?
if res.short then aliases[res.short] = optparm end
else
optparm = res.short
View
@@ -34,7 +34,8 @@ local patterns = {
FLOAT = '[%+%-%d]%d*%.?%d*[eE]?[%+%-]?%d*',
INTEGER = '[+%-%d]%d*',
IDEN = '[%a_][%w_]*',
- FILE = '[%a%.\\][:%][%w%._%-\\]*'
+ FILE = '[%a%.\\][:%][%w%._%-\\]*',
+ OPTION = '[%a_][%w_%-]*',
}
local function assert_arg(idx,val,tp)
@@ -92,6 +93,7 @@ local pattern_map = {
v = group(patterns.IDEN),
i = group(patterns.INTEGER),
f = group(patterns.FLOAT),
+ o = group(patterns.OPTION),
r = '(%S.*)',
p = '([%a]?[:]?[\\/%.%w_]+)'
}
View
@@ -89,5 +89,10 @@ check_error(extended,{'-n','x'},"unable to convert to number: x")
check_error(extended,{'-n','12'},"n out of range")
+local with_dashes = [[
+ --first-dash dash
+ --second-dash dash also
+]]
+check(with_dashes,{'--first-dash'},{first_dash=true,second_dash=false})

0 comments on commit 1a21d37

Please sign in to comment.