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

fix(lapp) add file name to arg table when using the default value #427

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ see [CONTRIBUTING.md](CONTRIBUTING.md#release-instructions-for-a-new-version) fo
[#413](https://github.com/lunarmodules/Penlight/pull/413)
- feat: `utils.kpairs` new iterator over all non-integer keys
[#413](https://github.com/lunarmodules/Penlight/pull/413)

- fix: `lapp` provides the file name when using the default argument
[#427](https://github.com/lunarmodules/Penlight/pull/427)

## 1.12.0 (2022-Jan-10)
- deprecate: module `pl.text` the contents have moved to `pl.stringx` (removal later)
Expand Down
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"})
alerque marked this conversation as resolved.
Show resolved Hide resolved

---- 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