diff options
author | steve donovan <steve.j.donovan@gmail.com> | 2013-05-13 14:35:32 +0400 |
---|---|---|
committer | steve donovan <steve.j.donovan@gmail.com> | 2013-05-13 14:35:32 +0400 |
commit | da8cb526be551d59c434a68fc341abccf0da5370 (patch) | |
tree | d0af63fda018e701ba2fe3e0e0260648baebf5a6 | |
parent | 82d0e4e666959311ce3e533f22f79867ad9e5d88 (diff) |
issue #70: boolean flags may have true default1.1.1
-rw-r--r-- | lua/pl/lapp.lua | 4 | ||||
-rw-r--r-- | tests/test-lapp.lua | 12 |
2 files changed, 15 insertions, 1 deletions
diff --git a/lua/pl/lapp.lua b/lua/pl/lapp.lua index 4b3d781..6039e46 100644 --- a/lua/pl/lapp.lua +++ b/lua/pl/lapp.lua @@ -143,6 +143,7 @@ local function force_short(short) lapp.assert(#short==1,short..": short parameters should be one character") end +-- deducing type of variable from default value; local function process_default (sval,vtype) local val if not vtype or vtype == 'number' then @@ -154,6 +155,9 @@ local function process_default (sval,vtype) local ft = filetypes[sval] return ft[1],ft[2] else + if sval == 'true' and not vtype then + return true, 'boolean' + end if sval:match '^["\']' then sval = sval:sub(2,-2) end return sval,vtype or 'string' end diff --git a/tests/test-lapp.lua b/tests/test-lapp.lua index 13f93a7..cf8cc75 100644 --- a/tests/test-lapp.lua +++ b/tests/test-lapp.lua @@ -79,7 +79,6 @@ local extended = [[ ]]
-
check(extended,{},{foo='1',speed='medium',n=1,p=false,v=false})
check(extended,{'-pv'},{foo='1',speed='medium',n=1,p=true,v=true})
check(extended,{'--foo','2','-s','fast'},{foo='2',speed='fast',n=1,p=false,v=false})
@@ -98,6 +97,7 @@ local with_dashes = [[ check(with_dashes,{'--first-dash'},{first_dash=true,second_dash=false})
+-- optional parameters don't have to be set
local optional = [[
-p (optional string)
]]
@@ -105,10 +105,19 @@ local optional = [[ check(optional,{'-p', 'test'},{p='test'})
check(optional,{},{})
+-- boolean flags may have a true default...
+local false_flag = [[
+ -g group results
+ -f (default true) force result
+]]
+
+check (false_flag,{},{f=true,g=false})
+
local addtype = [[
-l (intlist) List of items
]]
+-- defining a custom type
lapp.add_type('intlist',
function(x)
return tablex.imap(tonumber, utils.split(x, '%s*,%s*'))
@@ -122,3 +131,4 @@ lapp.add_type('intlist', check(addtype,{'-l', '1,2,3'},{l={1,2,3}})
check_error(addtype,{'-l', '1.5,2,3'},"not an integer!")
+
|