Skip to content

Commit

Permalink
add file name to arg table when default value
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanrpage97 committed Jun 5, 2022
1 parent a7b9f88 commit b8392b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lua/pl/lapp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ function lapp.process_options_string(str,args)
line = res.rest
res = {}
local optional
local defval_str
-- do we have ([optional] [<type>] [default <val>])?
if match('$({def} $',line,res) or match('$({def}',line,res) then
local typespec = strip(res.def)
Expand Down Expand Up @@ -288,6 +289,7 @@ function lapp.process_options_string(str,args)
-- optional 'default value' clause. Type is inferred as
-- 'string' or 'number' if there's no explicit type
if default or match('default $r{rest}',typespec,res) then
defval_str = res.rest
defval,vtype = process_default(res.rest,vtype)
end
else -- must be a plain flag, no extra parameter required
Expand All @@ -297,6 +299,7 @@ function lapp.process_options_string(str,args)
local ps = {
type = vtype,
defval = defval,
defval_str = defval_str,
required = defval == nil and not optional,
comment = res.rest or optparm,
constraint = constraint,
Expand Down Expand Up @@ -426,6 +429,9 @@ function lapp.process_options_string(str,args)
if not ps.used then
if ps.required then lapp.error("missing required parameter: "..parm) end
set_result(ps,parm,ps.defval)
if builtin_types[ps.type] == "file" then
set_result(ps, parm .. "_name", ps.defval_str)
end
end
end
return results
Expand Down
13 changes: 10 additions & 3 deletions tests/test-lapp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ Various flags and option types

check(simple,
{'-o','in'},
{quiet=false,p=false,o='in',input='<file>'})
{quiet=false,p=false,o='in',input='<file>', input_name="stdin"})

---- value of flag may be separated by '=' or ':'
check(simple,
{'-o=in'},
{quiet=false,p=false,o='in',input='<file>'})
{quiet=false,p=false,o='in',input='<file>', input_name="stdin"})

check(simple,
{'-o:in'},
{quiet=false,p=false,o='in',input='<file>'})
{quiet=false,p=false,o='in',input='<file>', input_name="stdin"})

-- Check lapp.callback.
local calls = {}
Expand Down Expand Up @@ -156,6 +156,13 @@ check (false_flag,{'-g','-f'},{f=false,g=true})
check (false_flag,{'-g','--'},{f=true,g=true})
check (false_flag,{'-g','--','-a','frodo'},{f=true,g=true; '-a','frodo'})


local default_file_flag = [[
-f (file-out default stdout)
]]

check (default_file_flag,{},{f="<file>", f_name = "stdout"})

local addtype = [[
-l (intlist) List of items
]]
Expand Down

0 comments on commit b8392b9

Please sign in to comment.