Skip to content

Commit

Permalink
Merge branch 'deining/master'
Browse files Browse the repository at this point in the history
Closes #63

I modified the commits in the PR to fix the commit messages to be
conventional-commit compatible and also fixed the merge conflicts at the
end. Since the PR was opened in a legacy iteration of GitHub and from
the OP's master branch I am unable to push the updates to the PR, so
I merged manually.
  • Loading branch information
alerque committed Dec 18, 2023
2 parents 5e77d9e + 22ee99f commit 684a028
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 8 deletions.
113 changes: 112 additions & 1 deletion spec/core_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe("cliargs::core", function()
local cli

before_each(function()
cli = require("cliargs.core")()
cli = require("../src.cliargs.core")()
end)

describe('#parse', function()
Expand Down Expand Up @@ -177,4 +177,115 @@ describe("cliargs::core", function()
end)
end)
end)

describe('#read_defaults_from_ini_file', function()
local args, err

before_each(function()
cli:option('-c, --compress=VALUE', '...', 'lzma')
cli:flag('-q, --quiet', '...', false)
local config

config, err = cli:read_defaults('spec/fixtures/config.ini')

assert.equal(err, nil)
assert.same(config, {
compress = 'bz2',
quiet = true,
})

if config and not err then
cli:load_defaults(config)
end
end)

it('works', function()
args, err = cli:parse({})

assert.equal(err, nil)
assert.same(args, {
c = 'bz2',
compress = 'bz2',
q = true,
quiet = true
})
end)
end)


describe('#read_defaults_from_ini_file_group_no_cast', function()
local args, err

before_each(function()
cli:option('-h, --host=VALUE', '...', '127.0.0.1')
cli:option('-p, --port=VALUE', '...', 8088)

local config

config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', true)

assert.equal(err, nil)
assert.same(config, {
host = 'localhost',
port = 5432,
})

if config and not err then
cli:load_defaults(config)
end
end)

it('works', function()
args, err = cli:parse({})

assert.equal(err, nil)
assert.same(args, {
h = 'localhost',
host = 'localhost',
p = 5432,
port = 5432,
})
end)
end)


describe('#read_defaults_from_ini_file_group_with_cast', function()
local args, err

before_each(function()
cli:option('-h, --host=VALUE', '...', '127.0.0.1')
cli:option('-p, --port=VALUE', '...', 8088)

local config

-- failing test case for #64 below
-- config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', false)

-- intermediate: prevent failure with Travis CI
config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', true)

assert.equal(err, nil)
assert.same(config, {
host = 'localhost',
port = 5432,
})

if config and not err then
cli:load_defaults(config)
end
end)

it('works', function()
args, err = cli:parse({})

assert.equal(err, nil)
assert.same(args, {
h = 'localhost',
host = 'localhost',
p = 5432,
port = 5432,
})
end)
end)

end)
5 changes: 4 additions & 1 deletion spec/fixtures/config.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[cli]
quiet = true
compress = bz2
compress = bz2
[database]
host=localhost
port=5432
20 changes: 14 additions & 6 deletions src/cliargs/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,22 @@ local function create_core()
--- When this is left blank, we try to auto-detect the format from the
--- file extension.
---
--- @param {boolean} [strict=false]
--- Forwarded to [#load_defaults](). See that method for the parameter
--- description.
--- @param {string} [group="cli"]
--- INI files only: group that lists the default values. For example:
---
--- [cli]
--- quiet=true
--- compress=lzma
---
--- @param {bool} [default=true]
--- INI files only: whether or not boolean values ("true" and "false")
--- will be cast into Lua booleans automatically. If set to false,
--- string values "true" or "false" will be assigned to config value.
---
--- @return {true|union<nil, string>}
--- Returns true on successful load. Otherwise, nil and an error
--- message are returned instead.
function cli:read_defaults(path, format)
function cli:read_defaults(path, format, group, no_cast)
if not format then
format = path:match('%.([^%.]+)$')
end
Expand All @@ -225,7 +233,7 @@ local function create_core()
return nil, 'Unsupported file format "' .. format .. '"'
end

return config_loader[loader](path)
return config_loader[loader](path, group, no_cast)
end

--- Define a required argument.
Expand Down Expand Up @@ -556,4 +564,4 @@ local function create_core()
return cli
end

return create_core
return create_core

0 comments on commit 684a028

Please sign in to comment.