Created
December 22, 2010 05:42
-
-
Save brentp/751142 to your computer and use it in GitHub Desktop.
patch for penlight to support only long options and space in definition.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lua/pl/lapp.lua b/lua/pl/lapp.lua | |
index 61d4f9d..7b46a90 100644 | |
--- a/lua/pl/lapp.lua | |
+++ b/lua/pl/lapp.lua | |
@@ -122,7 +122,9 @@ function lapp.add_type (name,converter,constraint) | |
end | |
local function force_short(short) | |
- lapp.assert(#short==1,short..": short parameters should be one character") | |
+ if short ~= nil then | |
+ lapp.assert(#short==1,short..": short parameters should be one character") | |
+ end | |
end | |
local function process_default (sval) | |
@@ -174,13 +176,15 @@ function lapp.process_options_string(str) | |
for line in lines(str) do | |
local optspec,optparm,i1,i2,defval,vtype,constraint | |
- line = lstrip(line) | |
+ line = lstrip(line):gsub(", ", ",") | |
-- flags: either -<short> or -<short>,--<long> | |
- if match('-$v{short},--$v{long} $',line,res) or match('-$v{short} $',line,res) then | |
+ if match('-$v{short},--$v{long} $',line,res) or | |
+ match('-$v{short} $',line,res) or | |
+ match('--$v{long} $',line,res) then | |
if res.long then | |
optparm = res.long | |
- aliases[res.short] = optparm | |
+ aliases[res.short or res.long] = optparm | |
else | |
optparm = res.short | |
end | |
diff --git a/tests/test-lapp.lua b/tests/test-lapp.lua | |
index 81dcc25..e30c82e 100644 | |
--- a/tests/test-lapp.lua | |
+++ b/tests/test-lapp.lua | |
@@ -20,6 +20,7 @@ Testing 'array' parameter handling | |
check (parmtest,{'-o','one'},{output={'one'},v={false}}) | |
+check (parmtest,{'--output','two'},{output={'two'},v={false}}) | |
check (parmtest,{'-o','one','-v'},{output={'one'},v={true}}) | |
check (parmtest,{'-o','one','-vv'},{output={'one'},v={true,true}}) | |
check (parmtest,{'-o','one','-o','two'},{output={'one','two'},v={false}}) | |
@@ -28,18 +29,18 @@ check (parmtest,{'-o','one','-o','two'},{output={'one','two'},v={false}}) | |
local simple = [[ | |
Various flags and option types | |
-p A simple optional flag, defaults to false | |
- -q,--quiet A simple flag with long name | |
- -o (string) A required option with argument | |
+ --quiet A simple flag with long name and no short name | |
+ -o,--option (string) A required option with argument | |
<input> (default stdin) Optional input file parameter | |
]] | |
check(simple, | |
{'-o','in'}, | |
- {quiet=false,p=false,o='in',input='<file>'}) | |
+ {quiet=false,p=false,option='in',input='<file>'}) | |
check(simple, | |
- {'-o','help','-q','test-lapp.lua'}, | |
- {quiet=true,p=false,o='help',input='<file>',input_name='test-lapp.lua'}) | |
+ {'-o','help','--quiet','test-lapp.lua'}, | |
+ {quiet=true,p=false,option='help',input='<file>',input_name='test-lapp.lua'}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment