Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pl.lapp: Support end-of-argument-list marker "--" #191

Closed
ghost opened this issue Apr 7, 2016 · 2 comments
Closed

pl.lapp: Support end-of-argument-list marker "--" #191

ghost opened this issue Apr 7, 2016 · 2 comments

Comments

@ghost
Copy link

ghost commented Apr 7, 2016

It would be great of pl.lapp could support the end-of-argument-list marker --.

Let's take this example.lua:

#!/usr/bin/lua
local lapp = require "pl.lapp"
local args = lapp[[
Example application
  <input> (file-in default stdin) The input file
  <output> (file-out default stdout) The output file
]]
print(args[1])

I would expect to be able to call example.lua and see following output:

# ./example.lua -- some argument list
some

Instead I get following error:

# ./example.lua -- some argument list
example: unrecognized parameter: -

Example application
  <input> (file-in default stdin) The input file
  <output> (file-out default stdout) The output file

I cannot call example.lua without such -- marker, because that makes pl.lapp open some and argument as input and output files:

# ./example.lua some argument list
example: some: No such file or directory
@ghost
Copy link
Author

ghost commented Apr 7, 2016

Minimally invasive patch against Penlight 1.2.1 that makes this work:

--- /usr/share/lua/5.2/pl/lapp.lua      2013-09-01 01:39:16.000000000 +0200
+++ pl/lapp.lua 2016-04-07 17:18:04.719756774 +0200
@@ -304,6 +304,7 @@
     local iextra = 1
     local i = 1
     local parm,ps,val
+    local end_of_flags = false

     local function check_parm (parm)
         local eqi = parm:find '='
@@ -321,8 +322,20 @@
     while i <= #arg do
         local theArg = arg[i]
         local res = {}
+        if theArg == "--" then
+            end_of_flags = true
+            iparm = #parmlist + 1
+
+            i = i + 1
+            theArg = arg[i]
+
+            if not theArg then
+                break
+            end
+        end
+
         -- look for a flag, -<short flags> or --<long flag>
-        if match('--$S{long}',theArg,res) or match('-$S{short}',theArg,res) then
+        if not end_of_flags and (match('--$S{long}',theArg,res) or match('-$S{short}',theArg,res)) then
             if res.long then -- long option
                 parm = check_parm(res.long)
             elseif #res.short == 1 or is_flag(res.short) then

@ghost
Copy link
Author

ghost commented Apr 7, 2016

The patch misbehaved if -- was the last argument. I fixed that by inserting following block:

--          theArg = arg[i]

            if not theArg then
                break
            end

ghost pushed a commit to devurandom/lua-runtemplate that referenced this issue Apr 7, 2016
pl/lapp.lua, penlight-1.2.1-lapp-issue-191-end-of-args-marker.patch:
- lunarmodules/Penlight#191
ghost pushed a commit to devurandom/lua-runtemplate that referenced this issue Apr 7, 2016
pl/lapp.lua, penlight-1.2.1-lapp-issue-191-end-of-args-marker.patch:
- lunarmodules/Penlight#191
ghost pushed a commit to devurandom/lua-runtemplate that referenced this issue Apr 7, 2016
pl/lapp.lua, penlight-1.2.1-lapp-issue-191-end-of-args-marker.patch:
- lunarmodules/Penlight#191
ghost pushed a commit to devurandom/lua-runtemplate that referenced this issue Apr 7, 2016
pl/lapp.lua, penlight-1.2.1-lapp-issue-191-end-of-args-marker.patch:
- lunarmodules/Penlight#191
ghost pushed a commit to devurandom/lua-runtemplate that referenced this issue Apr 7, 2016
pl/lapp.lua, penlight-1.2.1-lapp-issue-191-end-of-args-marker.patch:
- lunarmodules/Penlight#191
ghost pushed a commit to devurandom/lua-runtemplate that referenced this issue Apr 7, 2016
pl/lapp.lua, penlight-1.2.1-lapp-issue-191-end-of-args-marker.patch:
- lunarmodules/Penlight#191
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant